version 1.6, 1999/11/14 22:57:44 |
version 1.24, 2000/04/17 03:18:57 |
|
|
/** |
/** |
* $OpenXM: OpenXM/src/OpenMath/OM2OXM.java,v 1.5 1999/11/12 11:36:09 tam Exp $ |
* $OpenXM: OpenXM/src/OpenMath/OM2OXM.java,v 1.23 2000/03/15 15:02:05 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 |
private boolean debug = true; |
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 52 final class OM2OXM implements Runnable{ |
|
Line 52 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 |
try{ |
try{ |
while(true){ |
while(true){ |
CMO tmp; |
OXmessage message = asir.receive(); |
|
|
Thread.yield(); |
if(message.getTag() == OXmessage.OX_DATA){ |
|
System.out.println("=> "+ CMO2OM((CMO)message.getBody())); |
switch(asir.receiveOXtag()){ |
|
case OpenXM.OX_COMMAND: |
|
asir.receiveSM(); |
|
break; |
break; |
|
|
case OpenXM.OX_DATA: |
|
tmp = asir.receiveCMO(); |
|
System.out.println("=> "+ CMO2OM(tmp)); |
|
break; |
|
} |
} |
|
|
} |
} |
}catch(IOException e){} |
}catch(IOException e){} |
} |
} |
Line 75 final class OM2OXM implements Runnable{ |
|
Line 66 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()){ |
case CMO.CMO_NULL: |
case CMO.NULL: |
return "<OMI>0</OMI>"; |
return "<OMI>0</OMI>"; |
|
|
case CMO.CMO_INT32: |
case CMO.INT32: |
return "<OMI>"+ ((CMO_INT32)cmo).intValue() +"</OMI>"; |
return "<OMI>"+ ((CMO_INT32)cmo).intValue() +"</OMI>"; |
|
|
// case CMO.CMO_DATUM: |
// case CMO.DATUM: |
|
|
case CMO.CMO_STRING: |
case CMO.STRING: |
return "<OMSTR>"+ ((CMO_STRING)cmo).getString() +"</OMSTR>"; |
return "<OMSTR>"+ ((CMO_STRING)cmo).getString() +"</OMSTR>"; |
|
|
// case CMO.CMO_LIST: |
case CMO.LIST: |
|
ret += "<OMA><OMS name=\"list\" cd=\"basic\"/>"; |
|
{ |
|
CMO ob[] = ((CMO_LIST)cmo).getElements(); |
|
|
case CMO.CMO_MONOMIAL32: |
for(int i=0;i<ob.length;i++){ |
|
ret += CMO2OM_sub(ob[i]); |
|
} |
|
} |
|
ret += "</OMA>"; |
|
return ret; |
|
|
|
case 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()); |
for(int i=0;i<((CMO_MONOMIAL32)cmo).getDegree().length;i++){ |
for(int i=0;i<((CMO_MONOMIAL32)cmo).getDegree().length;i++){ |
Line 101 final class OM2OXM implements Runnable{ |
|
Line 102 final class OM2OXM implements Runnable{ |
|
ret += "</OMA>"; |
ret += "</OMA>"; |
return ret; |
return ret; |
|
|
case CMO.CMO_ZZ: |
case CMO.ZZ: |
return "<OMI>"+ ((CMO_ZZ)cmo).BigIntValue() +"</OMI>"; |
return "<OMI>"+ ((CMO_ZZ)cmo).BigIntValue() +"</OMI>"; |
|
|
case CMO.CMO_QQ: |
case CMO.QQ: |
return "<OMA><OMS name=\"over\" cd=\"basic\"/>"+ |
return "<OMA><OMS name=\"over\" cd=\"basic\"/>"+ |
CMO2OM_sub(((CMO_QQ)cmo).getBunshi())+ |
CMO2OM_sub(((CMO_QQ)cmo).getBunshi())+ |
CMO2OM_sub(((CMO_QQ)cmo).getBunbo())+ |
CMO2OM_sub(((CMO_QQ)cmo).getBunbo())+ |
"</OMA>"; |
"</OMA>"; |
|
|
case CMO.CMO_ZERO: |
case CMO.ZERO: |
return "<OMI> 0 </OMI>"; |
return "<OMI> 0 </OMI>"; |
|
|
// case CMO.CMO_DMS: |
// case CMO.DMS: |
|
|
case CMO.CMO_DMS_GENERIC: |
case CMO.DMS_GENERIC: |
ret += "<OMA><OMS name=\"PolyRing\" cd=\"poly\"/>"; |
ret += "<OMA><OMS name=\"PolyRing\" cd=\"poly\"/>"; |
ret += "<OMI>2</OMI></OMA>"; |
ret += "<OMI>2</OMI></OMA>"; |
return ret; |
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 += "<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()); |
ret += "<OMA><OMS name=\"SDMP\" cd=\"poly\"/>"; |
ret += "<OMA><OMS name=\"SDMP\" cd=\"poly\"/>"; |
Line 131 final class OM2OXM implements Runnable{ |
|
Line 135 final class OM2OXM implements Runnable{ |
|
ret += "</OMA></OMA>"; |
ret += "</OMA></OMA>"; |
return ret; |
return ret; |
|
|
case CMO.CMO_INDETERMINATE: |
//case CMO.POLYNOMIAL_IN_ONE_VARIABLE: |
|
|
|
case 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.INDETERMINATE: |
return "<OMV name=\""+ ((CMO_INDETERMINATE)cmo).getString() +"\"/>"; |
return "<OMV name=\""+ ((CMO_INDETERMINATE)cmo).getString() +"\"/>"; |
|
|
/* |
case 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 306 final class OM2OXM implements Runnable{ |
|
Line 357 final class OM2OXM implements Runnable{ |
|
ret = parse_symb_DMP(); |
ret = parse_symb_DMP(); |
debug("poly: "+ret); |
debug("poly: "+ret); |
}else{ |
}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); |
exceptTokenTypeInParse(TT_EndTag); |
Line 314 final class OM2OXM implements Runnable{ |
|
Line 375 final class OM2OXM implements Runnable{ |
|
parse_error("We expect '</OMA>'."); |
parse_error("We expect '</OMA>'."); |
} |
} |
}else{ |
}else{ |
parse_error("???"); |
parse_error("We expect '<OMI> or '<OMA>' or '<OMV>'."); |
ret = null; |
ret = null; |
} |
} |
|
|
Line 399 final class OM2OXM implements Runnable{ |
|
Line 460 final class OM2OXM implements Runnable{ |
|
|
|
private CMO_MONOMIAL32 parse_symb_Monom() throws IOException{ |
private CMO_MONOMIAL32 parse_symb_Monom() throws IOException{ |
Vector degree = new Vector(); |
Vector degree = new Vector(); |
|
CMO coefficient; |
int[] array; |
int[] array; |
|
|
exceptTokenTypeInParse(TT_StartTag); |
exceptTokenTypeInParse(TT_StartTag); |
Line 410 final class OM2OXM implements Runnable{ |
|
Line 472 final class OM2OXM implements Runnable{ |
|
parse_error("We expect '<Monom>'"); |
parse_error("We expect '<Monom>'"); |
} |
} |
|
|
|
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){ |
while(readNextToken() != TT_EndTag){ |
pushbackLex(); |
pushbackLex(); |
degree.addElement(parse_OMI()); |
degree.addElement(parse_OMI()); |
Line 419 final class OM2OXM implements Runnable{ |
|
Line 489 final class OM2OXM implements Runnable{ |
|
parse_error("We expect '<OMA>'."); |
parse_error("We expect '<OMA>'."); |
} |
} |
|
|
array = new int[degree.size()-1]; |
array = new int[degree.size()]; |
for(int i=0;i<array.length;i++){ |
for(int i=0;i<array.length;i++){ |
array[i] = ((CMO_ZZ)degree.elementAt(i+1)).intValue(); |
array[i] = ((CMO_ZZ)degree.elementAt(i)).intValue(); |
} |
} |
|
|
//debug("monom: "+ new CMO_MONOMIAL32(array,(CMO_ZZ)degree.elementAt(0))); |
//debug("monom: "+ new CMO_MONOMIAL32(array,coefficient)); |
return new CMO_MONOMIAL32(array,(CMO_ZZ)degree.elementAt(0)); |
return new CMO_MONOMIAL32(array,coefficient); |
} |
} |
|
|
private CMO parse_objects() throws IOException{ |
private CMO[] parse_objects() throws IOException{ |
// $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 443 final class OM2OXM implements Runnable{ |
|
Line 512 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 array; |
} |
} |
|
|
private String parse_symbol() throws IOException{ |
private String parse_symbol() throws IOException{ |
Line 559 final class OM2OXM implements Runnable{ |
|
Line 628 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 651 final class OM2OXM implements Runnable{ |
|
Line 721 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 before that :"; |
try{ |
try{ |
for(int i=0;i<10;i++){ |
for(int i=0;i<40;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){ |
private void debug(String str){ |
Line 693 final class OM2OXM implements Runnable{ |
|
Line 766 final class OM2OXM implements Runnable{ |
|
|
|
try{ |
try{ |
asir = new OpenXM(host,CtrlPort,StreamPort); |
asir = new OpenXM(host,CtrlPort,StreamPort); |
asir.sendSM(new SM(SM.SM_mathcap)); |
asir.send(new SM(SM.SM_mathcap)); |
}catch(UnknownHostException e){ |
}catch(UnknownHostException e){ |
System.err.println("host unknown."); |
System.err.println("host unknown."); |
System.err.println(e.getMessage()); |
System.err.println(e.getMessage()); |
Line 703 final class OM2OXM implements Runnable{ |
|
Line 776 final class OM2OXM implements Runnable{ |
|
System.err.println("IOException occuer !!"); |
System.err.println("IOException occuer !!"); |
System.err.println(e.getMessage()); |
System.err.println(e.getMessage()); |
return; |
return; |
|
}catch(MathcapViolation e){ |
|
System.err.println("MathcapViolation !!"); |
|
System.err.println(e.getMessage()); |
|
return; |
} |
} |
|
|
P.asir = asir; |
P.asir = asir; |
Line 714 final class OM2OXM implements Runnable{ |
|
Line 791 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.send(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..."); |
} |
} |
} |
} |
} |
} |