1 8 9 package org.lsmp.djepExamples; 10 import org.nfunk.jep.*; 11 import org.lsmp.djep.xjep.*; 12 13 16 public class XJepExample { 17 18 public static void main(String args[]) 19 { 20 21 XJep j = new XJep(); 22 j.addStandardConstants(); 23 j.addStandardFunctions(); 24 j.addComplex(); 25 j.setAllowUndeclared(true); 26 j.setAllowAssignment(true); 27 j.setImplicitMul(true); 28 29 try 30 { 31 Node node10 = j.parse("x=3"); 32 Node node11 = j.preprocess(node10); 33 System.out.println(j.evaluate(node11)); 34 Node node12 = j.parse("y=x^2"); 35 Node node13 = j.preprocess(node12); 36 System.out.println(j.evaluate(node13)); 37 Node node14 = j.parse("z=y+x"); 38 Node node15 = j.simplify(j.preprocess(node14)); 39 System.out.println(j.evaluate(node15)); 40 41 j.setVarValue("x",new Double (4)); 45 System.out.println(j.evaluate(node13)); 46 System.out.println(j.evaluate(node15)); 47 System.out.println("z: "+j.getVarValue("z").toString()); 48 49 53 j.setVarValue("x",new Double (5)); 54 System.out.println("j.setVarValue(\"x\",new Double(5));"); 55 System.out.println("j.findVarValue(y): "+j.calcVarValue("y").toString()); 56 System.out.println("j.findVarValue(z): "+j.calcVarValue("z").toString()); 57 58 j.getSymbolTable().clearValues(); 65 j.setVarValue("x",new Double (6)); 66 System.out.println("j.setVarValue(\"x\",new Double(6));"); 67 System.out.println("j.findVarValue(z): "+j.calcVarValue("z").toString()); 68 69 j.getSymbolTable().clearValues(); 70 j.setVarValue("x",new Double (7)); 71 System.out.println(j.evaluate(node15)); 72 System.out.println("z: "+j.getVarValue("z").toString()); 73 74 76 j.restartParser("x=1; // semi colon; in comment \ny=2; z=3;"); 77 Node node21; 78 while((node21 = j.continueParsing()) != null) 79 j.println(node21); 80 } 81 catch(ParseException e) 82 { 83 System.out.println("Error with parsing"); 84 } 85 catch(Exception e) 86 { 87 System.out.println("Error with evaluation"); 88 } 89 } 90 } 91 | Popular Tags |