=================================================================== RCS file: /home/cvs/OpenXM/src/OpenMath/OM2OXM.java,v retrieving revision 1.1.1.1 retrieving revision 1.26 diff -u -p -r1.1.1.1 -r1.26 --- OpenXM/src/OpenMath/OM2OXM.java 1999/11/02 11:52:34 1.1.1.1 +++ OpenXM/src/OpenMath/OM2OXM.java 2000/10/11 08:32:13 1.26 @@ -1,6 +1,8 @@ +/* + * $OpenXM: OpenXM/src/OpenMath/OM2OXM.java,v 1.25 2000/09/13 06:44:55 tam Exp $ + */ + /** - * OM2OXM.java - * * このクラスでは以下の BNF で表される構文解析を実装している * expr -> stag [expr | immediate]* etag * immediate -> INTEGER @@ -31,7 +33,7 @@ import java.io.*; import java.net.*; import java.util.Vector; -import JP.ac.kobe_u.math.tam.OpenXM.*; +import ORG.openxm.tam.*; final class OM2OXM implements Runnable{ private PushbackInputStream is; @@ -39,6 +41,7 @@ final class OM2OXM implements Runnable{ private int token = TT_NULL; private boolean lexPushbackFlag = false; private OpenXM asir; // for debug + protected boolean debug = false; // for debug message // Token Type for lexical analyzer final static int TT_NULL = 0; @@ -51,93 +54,150 @@ final class OM2OXM implements Runnable{ //サーバ側から送信された文字列を受信します。 try{ while(true){ - CMO tmp; + OXmessage message = asir.receive(); - Thread.yield(); - - switch(asir.receiveOXtag()){ - case OpenXM.OX_COMMAND: - asir.receiveSM(); + if(message.getTag() == OXmessage.OX_DATA){ + System.err.println("=> "+ CMO2OM((CMO)message.getBody())); break; - - case OpenXM.OX_DATA: - tmp = asir.receiveCMO(); - System.out.println("=> "+ CMO2OM(tmp) +""); - break; } - } }catch(IOException e){} } public static String CMO2OM(CMO cmo){ + return ""+ CMO2OM_sub(cmo) +""; + } + + private static String CMO2OM_sub(CMO cmo) throws NumberFormatException{ String ret = ""; switch(cmo.getDISCRIMINATOR()){ - case CMO.CMO_NULL: + case CMO.NULL: return "0"; - case CMO.CMO_INT32: + case CMO.INT32: return ""+ ((CMO_INT32)cmo).intValue() +""; - // case CMO.CMO_DATUM: + // case CMO.DATUM: - case CMO.CMO_STRING: + case CMO.STRING: return ""+ ((CMO_STRING)cmo).getString() +""; - // case CMO.CMO_LIST: + case CMO.LIST: + ret += ""; + { + CMO ob[] = ((CMO_LIST)cmo).getElements(); - case CMO.CMO_MONOMIAL32: + for(int i=0;i"; - ret += CMO2OM(((CMO_MONOMIAL32)cmo).getCoefficient()); + ret += CMO2OM_sub(((CMO_MONOMIAL32)cmo).getCoefficient()); for(int i=0;i<((CMO_MONOMIAL32)cmo).getDegree().length;i++){ ret += ""+ ((CMO_MONOMIAL32)cmo).getDegree()[i] +""; } ret += ""; return ret; - case CMO.CMO_ZZ: + case CMO.ZZ: return ""+ ((CMO_ZZ)cmo).BigIntValue() +""; - case CMO.CMO_QQ: + case CMO.QQ: return ""+ - CMO2OM(((CMO_QQ)cmo).getBunshi()) + CMO2OM(((CMO_QQ)cmo).getBunbo()) - +""; + CMO2OM_sub(((CMO_QQ)cmo).getBunshi())+ + CMO2OM_sub(((CMO_QQ)cmo).getBunbo())+ + ""; - case CMO.CMO_ZERO: + case CMO.ZERO: return " 0 "; - // case CMO.CMO_DMS: + // case CMO.DMS: - case CMO.CMO_DMS_GENERIC: + case CMO.DMS_GENERIC: ret += ""; - ret += "2"; + ret += "2"; return ret; - case CMO.CMO_DISTRIBUTED_POLYNOMIAL: + case CMO.RECURSIVE_POLYNOMIAL: + return CMO2OM_CoefficientOfRecursivePOLYNOMIAL(((CMO_RECURSIVE_POLYNOMIAL)cmo).getPolynomial(),((CMO_RECURSIVE_POLYNOMIAL)cmo).getVariables()); + + case CMO.DISTRIBUTED_POLYNOMIAL: ret += ""; - ret += CMO2OM(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getRing()); + ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getRing()); ret += ""; - for(int i=0;i<((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials().length;i++){ - ret += CMO2OM(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials()[i]); + for(int i=0;i<((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials().length; + i++){ + ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getMonomials()[i]); } ret += ""; return ret; - case CMO.CMO_INDETERMINATE: + //case CMO.POLYNOMIAL_IN_ONE_VARIABLE: + + case CMO.BIGFLOAT: + ret += ""; + ret += CMO2OM_sub(((CMO_BIGFLOAT)cmo).getSyosubu()); + ret += ""; + ret += "2"; + ret += CMO2OM_sub(((CMO_BIGFLOAT)cmo).getShisubu()); + ret += ""; + return ret; + + case CMO.INDETERMINATE: return ""; - /* - case CMO.CMO_TREE: - return ""+ - ""; - */ + case CMO.TREE: + ret += ""; + for(int i=0;i<((CMO_TREE)cmo).getLeaves().getElements().length;i++){ + ret += CMO2OM_sub(((CMO_TREE)cmo).getLeaves().getElements()[i]); + } + ret += ""; + return ret; default: - return ""+ cmo.toCMOexpression() +""; + //return ""+ cmo.toCMOexpression() +""; } + + throw new NumberFormatException("unknown convert way:"+ + cmo.toCMOexpression()); } + private static String CMO2OM_CoefficientOfRecursivePOLYNOMIAL(CMO cmo,CMO_LIST variables){ + CMO_POLYNOMIAL_IN_ONE_VARIABLE poly; + String ret = "",variable; + + if(!(cmo instanceof CMO_POLYNOMIAL_IN_ONE_VARIABLE)){ + return CMO2OM_sub(cmo); + } + + poly = (CMO_POLYNOMIAL_IN_ONE_VARIABLE)cmo; + variable = CMO2OM_sub(variables.getElements()[poly.getVariable()]); + + for(int i=0;i"+ + ""+ + variable +""+ poly.getDegrees()[i] +""+ + CMO2OM_CoefficientOfRecursivePOLYNOMIAL(poly.getCoefficients()[i], + variables) + +""; + if(i==0){ + ret = mono; + }else{ + ret = ""+ ret + mono +""; + } + } + + return ret; + } + private boolean isSpace(int ch){ // use from lex return (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r'); } @@ -214,9 +274,9 @@ final class OM2OXM implements Runnable{ }else if(ch != -1){ is.unread(ch); while((ch = is.read()) != '<' && ch != -1){ - System.out.println("debug: "+ch); + debug("debug: "+ch); if(isSpace(ch)){ - String spaces = String.valueOf(ch); + String spaces = String.valueOf((char)ch); while(isSpace(ch = is.read())){ spaces += (char)ch; @@ -244,7 +304,7 @@ final class OM2OXM implements Runnable{ parse_error("We expect type :'"+ type +"', but we got type :'"+ token +"'("+ attribute +")."); } - //System.out.println(":"+token+":"+attribute+":"+type); + //debug(":"+token+":"+attribute+":"+type); return true; } @@ -260,7 +320,7 @@ final class OM2OXM implements Runnable{ } ret = parse_object(); - + exceptTokenTypeInParse(TT_EndTag); if(!attribute.equals("OMOBJ")){ parse_error("We expect ''."); @@ -272,6 +332,7 @@ final class OM2OXM implements Runnable{ private CMO parse_object() throws IOException{ // object -> variable // | '' S? integer S? '' + // | '' S? utf7 S? '' // | '' S? symbol S? objects S? '' CMO ret; @@ -285,6 +346,9 @@ final class OM2OXM implements Runnable{ }else if(attribute.equals("OMI")){ pushbackLex(); ret = parse_OMI(); + }else if(attribute.equals("OMSTR")){ + pushbackLex(); + ret = parse_OMSTR(); }else if(attribute.equals("OMA")){ String name,cdname; int argnum = 0; @@ -293,8 +357,19 @@ final class OM2OXM implements Runnable{ if(name.equals("DMP")){ ret = parse_symb_DMP(); + debug("poly: "+ret); }else{ - ret = new CMO_TREE(name,"Basic",parse_objects()); + CMO[] leaves = parse_objects(); + + if(name.equals("list")){ + ret = new CMO_LIST(leaves); + }else if(name.equals("over") && leaves.length==2 + && leaves[0] instanceof CMO_ZZ + && leaves[1] instanceof CMO_ZZ){ + ret = new CMO_QQ((CMO_ZZ)leaves[0],(CMO_ZZ)leaves[1]); + }else{ + ret = new CMO_TREE(name,"basic",new CMO_LIST(leaves)); + } } exceptTokenTypeInParse(TT_EndTag); @@ -302,7 +377,7 @@ final class OM2OXM implements Runnable{ parse_error("We expect ''."); } }else{ - parse_error("???"); + parse_error("We expect ' or '' or ''."); ret = null; } @@ -327,6 +402,30 @@ final class OM2OXM implements Runnable{ return ret; } + private CMO_STRING parse_OMSTR() throws IOException{ + CMO_STRING ret; + + exceptTokenTypeInParse(TT_StartTag); + if(!attribute.equals("OMSTR")){ + parse_error("We expect ''."); + } + + if(readNextToken() == TT_String){ + //ret = (CMO_STRING)parse_utf7(); + ret = new CMO_STRING(attribute); + }else{ + ret = new CMO_STRING(""); + pushbackLex(); + } + + exceptTokenTypeInParse(TT_EndTag); + if(!attribute.equals("OMSTR")){ + parse_error("We expect ''."); + } + + return ret; + } + private CMO parse_symb_DMP() throws IOException{ parse_object(); return new CMO_DISTRIBUTED_POLYNOMIAL(new CMO_DMS_GENERIC(), @@ -363,6 +462,7 @@ final class OM2OXM implements Runnable{ private CMO_MONOMIAL32 parse_symb_Monom() throws IOException{ Vector degree = new Vector(); + CMO coefficient; int[] array; exceptTokenTypeInParse(TT_StartTag); @@ -374,6 +474,14 @@ final class OM2OXM implements Runnable{ parse_error("We expect ''"); } + coefficient = parse_object(); + if(!(coefficient instanceof CMO_ZZ + || coefficient instanceof CMO_QQ + || coefficient instanceof CMO_INT32 + || coefficient instanceof CMO_ZERO)){ + parse_error("the coefficient of Monomials must be integer or rational."); + } + while(readNextToken() != TT_EndTag){ pushbackLex(); degree.addElement(parse_OMI()); @@ -383,16 +491,16 @@ final class OM2OXM implements Runnable{ parse_error("We expect ''."); } - array = new int[degree.size()-1]; + array = new int[degree.size()]; for(int i=0;i