version 1.2, 1999/11/04 19:36:41 |
version 1.16, 2000/01/20 18:14:32 |
|
|
/** |
/** |
* $OpenXM$ |
* $OpenXM: OpenXM/src/OpenMath/OM2OXM.java,v 1.15 2000/01/20 13:58:48 tam Exp $ |
* |
* |
* $B$3$N%/%i%9$G$O0J2<$N(B BNF $B$GI=$5$l$k9=J82r@O$r<BAu$7$F$$$k(B |
* $B$3$N%/%i%9$G$O0J2<$N(B BNF $B$GI=$5$l$k9=J82r@O$r<BAu$7$F$$$k(B |
* expr -> stag [expr | immediate]* etag |
* expr -> stag [expr | immediate]* etag |
Line 39 final class OM2OXM implements Runnable{ |
|
Line 39 final class OM2OXM implements Runnable{ |
|
private int token = TT_NULL; |
private int token = TT_NULL; |
private boolean lexPushbackFlag = false; |
private boolean lexPushbackFlag = false; |
private OpenXM asir; // for debug |
private OpenXM asir; // for debug |
|
protected boolean debug = false; // for debug message |
|
|
// Token Type for lexical analyzer |
// Token Type for lexical analyzer |
final static int TT_NULL = 0; |
final static int TT_NULL = 0; |
Line 53 final class OM2OXM implements Runnable{ |
|
Line 54 final class OM2OXM implements Runnable{ |
|
while(true){ |
while(true){ |
CMO tmp; |
CMO tmp; |
|
|
Thread.yield(); |
|
|
|
switch(asir.receiveOXtag()){ |
switch(asir.receiveOXtag()){ |
case OpenXM.OX_COMMAND: |
case OpenXM.OX_COMMAND: |
asir.receiveSM(); |
asir.receiveSM(); |
Line 74 final class OM2OXM implements Runnable{ |
|
Line 73 final class OM2OXM implements Runnable{ |
|
return "<OMOBJ>"+ CMO2OM_sub(cmo) +"</OMOBJ>"; |
return "<OMOBJ>"+ CMO2OM_sub(cmo) +"</OMOBJ>"; |
} |
} |
|
|
private static String CMO2OM_sub(CMO cmo){ |
private static String CMO2OM_sub(CMO cmo) throws NumberFormatException{ |
String ret = ""; |
String ret = ""; |
|
|
switch(cmo.getDISCRIMINATOR()){ |
switch(cmo.getDISCRIMINATOR()){ |
Line 89 final class OM2OXM implements Runnable{ |
|
Line 88 final class OM2OXM implements Runnable{ |
|
case CMO.CMO_STRING: |
case CMO.CMO_STRING: |
return "<OMSTR>"+ ((CMO_STRING)cmo).getString() +"</OMSTR>"; |
return "<OMSTR>"+ ((CMO_STRING)cmo).getString() +"</OMSTR>"; |
|
|
// case CMO.CMO_LIST: |
case CMO.CMO_LIST: |
|
ret += "<OMA><OMS name=\"list\" cd=\"basic\"/>"; |
|
{ |
|
CMO ob[] = ((CMO_LIST)cmo).getElements(); |
|
|
|
for(int i=0;i<ob.length;i++){ |
|
ret += CMO2OM_sub(ob[i]); |
|
} |
|
} |
|
ret += "</OMA>"; |
|
return ret; |
|
|
case CMO.CMO_MONOMIAL32: |
case CMO.CMO_MONOMIAL32: |
ret += "<OMA><OMS name=\"Monom\" cd=\"poly\"/>"; |
ret += "<OMA><OMS name=\"Monom\" cd=\"poly\"/>"; |
ret += CMO2OM_sub(((CMO_MONOMIAL32)cmo).getCoefficient()); |
ret += CMO2OM_sub(((CMO_MONOMIAL32)cmo).getCoefficient()); |
Line 116 final class OM2OXM implements Runnable{ |
|
Line 125 final class OM2OXM implements Runnable{ |
|
|
|
case CMO.CMO_DMS_GENERIC: |
case CMO.CMO_DMS_GENERIC: |
ret += "<OMA><OMS name=\"PolyRing\" cd=\"poly\"/>"; |
ret += "<OMA><OMS name=\"PolyRing\" cd=\"poly\"/>"; |
ret += "<OMI>2</OMI>"; |
ret += "<OMI>2</OMI></OMA>"; |
return ret; |
return ret; |
|
|
|
case CMO.CMO_RECURSIVE_POLYNOMIAL: |
|
return CMO2OM_CoefficientOfRecursivePOLYNOMIAL(((CMO_RECURSIVE_POLYNOMIAL)cmo).getPolynomial(),((CMO_RECURSIVE_POLYNOMIAL)cmo).getVariables()); |
|
|
case CMO.CMO_DISTRIBUTED_POLYNOMIAL: |
case CMO.CMO_DISTRIBUTED_POLYNOMIAL: |
ret += "<OMA><OMS name=\"DMP\" cd=\"poly\"/>"; |
ret += "<OMA><OMS name=\"DMP\" cd=\"poly\"/>"; |
ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getRing()); |
ret += CMO2OM_sub(((CMO_DISTRIBUTED_POLYNOMIAL)cmo).getRing()); |
Line 130 final class OM2OXM implements Runnable{ |
|
Line 142 final class OM2OXM implements Runnable{ |
|
ret += "</OMA></OMA>"; |
ret += "</OMA></OMA>"; |
return ret; |
return ret; |
|
|
|
//case CMO.CMO_POLYNOMIAL_IN_ONE_VARIABLE: |
|
|
|
case CMO.CMO_BIGFLOAT: |
|
ret += "<OMA><OMS name=\"times\" cd=\"basic\"/>"; |
|
ret += CMO2OM_sub(((CMO_BIGFLOAT)cmo).getSyosubu()); |
|
ret += "<OMA><OMS name=\"power\" cd=\"basic\"/>"; |
|
ret += "<OMI>2</OMI>"; |
|
ret += CMO2OM_sub(((CMO_BIGFLOAT)cmo).getShisubu()); |
|
ret += "</OMA></OMA>"; |
|
return ret; |
|
|
case CMO.CMO_INDETERMINATE: |
case CMO.CMO_INDETERMINATE: |
return "<OMV name=\""+ ((CMO_INDETERMINATE)cmo).getString() +"\"/>"; |
return "<OMV name=\""+ ((CMO_INDETERMINATE)cmo).getString() +"\"/>"; |
|
|
/* |
case CMO.CMO_TREE: |
case CMO.CMO_TREE: |
ret += "<OMA><OMS name=\""+ ((CMO_TREE)cmo).getName() +"\" cd=\""+ |
return "<OMA><OMS name=\""+ "\"/>"+ |
((CMO_TREE)cmo).getCDName() +"\"/>"; |
"</OMA>"; |
for(int i=0;i<((CMO_TREE)cmo).getLeaves().getElements().length;i++){ |
*/ |
ret += CMO2OM_sub(((CMO_TREE)cmo).getLeaves().getElements()[i]); |
|
} |
|
ret += "</OMA>"; |
|
return ret; |
|
|
default: |
default: |
return "<OMSTR>"+ cmo.toCMOexpression() +"</OMSTR>"; |
//return "<OMSTR>"+ cmo.toCMOexpression() +"</OMSTR>"; |
} |
} |
|
|
|
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<poly.getDegrees().length;i++){ |
|
String mono; |
|
|
|
mono = "<OMA><OMS name=\"times\" cd=\"basic\"/>"+ |
|
"<OMA><OMS name=\"power\" cd=\"basic\"/>"+ |
|
variable +"<OMI>"+ poly.getDegrees()[i] +"</OMI></OMA>"+ |
|
CMO2OM_CoefficientOfRecursivePOLYNOMIAL(poly.getCoefficients()[i], |
|
variables) |
|
+"</OMA>"; |
|
if(i==0){ |
|
ret = mono; |
|
}else{ |
|
ret = "<OMA><OMS name=\"plus\" cd=\"basic\"/>"+ ret + mono +"</OMA>"; |
|
} |
|
} |
|
|
|
return ret; |
|
} |
|
|
private boolean isSpace(int ch){ // use from lex |
private boolean isSpace(int ch){ // use from lex |
return (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r'); |
return (ch==' ' || ch=='\t' || ch=='\n' || ch=='\r'); |
} |
} |
Line 220 final class OM2OXM implements Runnable{ |
|
Line 279 final class OM2OXM implements Runnable{ |
|
}else if(ch != -1){ |
}else if(ch != -1){ |
is.unread(ch); |
is.unread(ch); |
while((ch = is.read()) != '<' && ch != -1){ |
while((ch = is.read()) != '<' && ch != -1){ |
System.out.println("debug: "+ch); |
debug("debug: "+ch); |
if(isSpace(ch)){ |
if(isSpace(ch)){ |
String spaces = String.valueOf(ch); |
String spaces = String.valueOf((char)ch); |
|
|
while(isSpace(ch = is.read())){ |
while(isSpace(ch = is.read())){ |
spaces += (char)ch; |
spaces += (char)ch; |
Line 250 final class OM2OXM implements Runnable{ |
|
Line 309 final class OM2OXM implements Runnable{ |
|
parse_error("We expect type :'"+ type |
parse_error("We expect type :'"+ type |
+"', but we got type :'"+ token +"'("+ attribute +")."); |
+"', but we got type :'"+ token +"'("+ attribute +")."); |
} |
} |
//System.out.println(":"+token+":"+attribute+":"+type); |
//debug(":"+token+":"+attribute+":"+type); |
return true; |
return true; |
} |
} |
|
|
Line 266 final class OM2OXM implements Runnable{ |
|
Line 325 final class OM2OXM implements Runnable{ |
|
} |
} |
|
|
ret = parse_object(); |
ret = parse_object(); |
|
|
exceptTokenTypeInParse(TT_EndTag); |
exceptTokenTypeInParse(TT_EndTag); |
if(!attribute.equals("OMOBJ")){ |
if(!attribute.equals("OMOBJ")){ |
parse_error("We expect '</OMOBJ>'."); |
parse_error("We expect '</OMOBJ>'."); |
Line 278 final class OM2OXM implements Runnable{ |
|
Line 337 final class OM2OXM implements Runnable{ |
|
private CMO parse_object() throws IOException{ |
private CMO parse_object() throws IOException{ |
// object -> variable |
// object -> variable |
// | '<OMI>' S? integer S? '</OMI>' |
// | '<OMI>' S? integer S? '</OMI>' |
|
// | '<OMSTR>' S? utf7 S? '</OMSTR>' |
// | '<OMA>' S? symbol S? objects S? '</OMA>' |
// | '<OMA>' S? symbol S? objects S? '</OMA>' |
CMO ret; |
CMO ret; |
|
|
Line 291 final class OM2OXM implements Runnable{ |
|
Line 351 final class OM2OXM implements Runnable{ |
|
}else if(attribute.equals("OMI")){ |
}else if(attribute.equals("OMI")){ |
pushbackLex(); |
pushbackLex(); |
ret = parse_OMI(); |
ret = parse_OMI(); |
|
}else if(attribute.equals("OMSTR")){ |
|
pushbackLex(); |
|
ret = parse_OMSTR(); |
}else if(attribute.equals("OMA")){ |
}else if(attribute.equals("OMA")){ |
String name,cdname; |
String name,cdname; |
int argnum = 0; |
int argnum = 0; |
Line 299 final class OM2OXM implements Runnable{ |
|
Line 362 final class OM2OXM implements Runnable{ |
|
|
|
if(name.equals("DMP")){ |
if(name.equals("DMP")){ |
ret = parse_symb_DMP(); |
ret = parse_symb_DMP(); |
|
debug("poly: "+ret); |
|
}else if(name.equals("list")){ |
|
ret = parse_objects(); |
}else{ |
}else{ |
ret = new CMO_TREE(name,"Basic",parse_objects()); |
ret = new CMO_TREE(name,"Basic",parse_objects()); |
} |
} |
Line 333 final class OM2OXM implements Runnable{ |
|
Line 399 final class OM2OXM implements Runnable{ |
|
return ret; |
return ret; |
} |
} |
|
|
|
private CMO_STRING parse_OMSTR() throws IOException{ |
|
CMO_STRING ret; |
|
|
|
exceptTokenTypeInParse(TT_StartTag); |
|
if(!attribute.equals("OMSTR")){ |
|
parse_error("We expect '<OMSTR>'."); |
|
} |
|
|
|
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 '</OMSTR>'."); |
|
} |
|
|
|
return ret; |
|
} |
|
|
private CMO parse_symb_DMP() throws IOException{ |
private CMO parse_symb_DMP() throws IOException{ |
parse_object(); |
parse_object(); |
return new CMO_DISTRIBUTED_POLYNOMIAL(new CMO_DMS_GENERIC(), |
return new CMO_DISTRIBUTED_POLYNOMIAL(new CMO_DMS_GENERIC(), |
Line 394 final class OM2OXM implements Runnable{ |
|
Line 484 final class OM2OXM implements Runnable{ |
|
array[i] = ((CMO_ZZ)degree.elementAt(i+1)).intValue(); |
array[i] = ((CMO_ZZ)degree.elementAt(i+1)).intValue(); |
} |
} |
|
|
|
//debug("monom: "+ new CMO_MONOMIAL32(array,(CMO_ZZ)degree.elementAt(0))); |
return new CMO_MONOMIAL32(array,(CMO_ZZ)degree.elementAt(0)); |
return new CMO_MONOMIAL32(array,(CMO_ZZ)degree.elementAt(0)); |
} |
} |
|
|
private CMO parse_objects() throws IOException{ |
private CMO_LIST parse_objects() throws IOException{ |
// $B2r@O$5$l$?(B object $B$r(B LIST $B$GJV$9(B |
// $B2r@O$5$l$?(B object $B$r(B LIST $B$GJV$9(B |
Vector objects = new Vector(); |
Vector objects = new Vector(); |
CMO[] array; |
CMO[] array; |
Line 412 final class OM2OXM implements Runnable{ |
|
Line 503 final class OM2OXM implements Runnable{ |
|
array = new CMO[objects.size()]; |
array = new CMO[objects.size()]; |
objects.copyInto((Object[])array); |
objects.copyInto((Object[])array); |
|
|
System.out.println("debug :"+ new CMO_LIST(array)); |
debug("debug :"+ new CMO_LIST(array)); |
return new CMO_LIST(array); |
return new CMO_LIST(array); |
} |
} |
|
|
Line 528 final class OM2OXM implements Runnable{ |
|
Line 619 final class OM2OXM implements Runnable{ |
|
parse_error("We expect integer."); |
parse_error("We expect integer."); |
} |
} |
|
|
|
debug("ZZ: "+ str); |
return new CMO_ZZ(str); |
return new CMO_ZZ(str); |
} |
} |
|
|
Line 620 final class OM2OXM implements Runnable{ |
|
Line 712 final class OM2OXM implements Runnable{ |
|
return str; |
return str; |
} |
} |
|
|
private void parse_error(String mesg){ |
private void parse_error(String mesg) throws NumberFormatException{ |
System.err.println(mesg); |
String ret; |
System.err.print("error occuered near :"); |
|
|
ret = mesg +"\n"; |
|
ret += "error occuered near :"; |
try{ |
try{ |
for(int i=0;i<10;i++){ |
for(int i=0;i<10;i++){ |
System.err.print((char)is.read()); |
ret += (char)is.read(); |
} |
} |
System.err.println((char)is.read()); |
|
}catch(IOException e){} |
}catch(IOException e){} |
System.exit(1); |
ret += "\n"; |
|
|
|
throw new NumberFormatException(ret); |
} |
} |
|
|
|
private void debug(String str){ |
|
if(debug){ |
|
System.err.println(str); |
|
} |
|
} |
|
|
public static void main(String[] argv) throws IOException{ |
public static void main(String[] argv) throws IOException{ |
OM2OXM P = new OM2OXM(); |
OM2OXM P = new OM2OXM(); |
OpenXM asir; |
OpenXM asir; |
Line 677 final class OM2OXM implements Runnable{ |
|
Line 778 final class OM2OXM implements Runnable{ |
|
|
|
//$B%5!<%PB&$+$iAw?.$5$l$?J8;zNs$r<u?.$7$^$9!#(B |
//$B%5!<%PB&$+$iAw?.$5$l$?J8;zNs$r<u?.$7$^$9!#(B |
while(true){ |
while(true){ |
CMO obj = P.parse(System.in); |
try{ |
asir.send(obj); |
CMO obj = P.parse(System.in); |
asir.sendSM(new SM(SM.SM_popCMO)); |
asir.send(obj); |
|
asir.sendSM(new SM(SM.SM_popCMO)); |
|
}catch(NumberFormatException e){ |
|
System.err.println(e.getMessage()); |
|
}catch(MathcapViolation e){ |
|
System.err.println(e.getMessage()); |
|
} |
} |
} |
|
|
//System.out.println("breaking..."); |
|
|
|
}catch(IOException e){ |
}catch(IOException e){ |
e.printStackTrace(); |
e.printStackTrace(); |
|
}finally{ |
|
System.out.println("breaking..."); |
} |
} |
} |
} |
} |
} |