1 9 10 16 17 package org.lsmp.djepExamples; 18 19 import org.lsmp.djep.vectorJep.VectorJep; 20 import org.nfunk.jep.*; 21 import java.io.*; 22 24 35 class VectorConsole { 36 37 38 private String prompt; 39 40 41 private BufferedReader br; 42 43 44 boolean dumpTree = false; 45 46 boolean dumpSymbols=false; 47 48 49 public VectorConsole() { 50 prompt = "JEP > "; 51 br = new BufferedReader(new InputStreamReader(System.in)); 52 53 } 54 55 56 public static void main(String args[]) throws IOException { 57 VectorConsole c = new VectorConsole(); 58 c.run(args); 59 } 60 61 62 public void run(String args[]) throws IOException { 63 String command=""; 64 JEP j = new VectorJep(); 65 j.addStandardConstants(); 66 j.addStandardFunctions(); 67 j.addComplex(); 68 j.setAllowAssignment(true); 70 j.setAllowUndeclared(true); 71 String temp=""; 72 for(int i=0;i<args.length;++i) 73 { 74 if(args[i].equals("--dumpTree")) 75 dumpTree = true; 76 else if(args[i].equals("--dumpSymbols")) 77 dumpSymbols = true; 78 else 79 temp += " " + args[i]; 80 } 81 if(temp.length()!=0) 82 { 83 j.parseExpression(temp); 84 if (j.hasError()) 85 System.out.println(j.getErrorInfo()); 86 else 87 System.out.println(j.getValueAsObject()); 88 } 89 else 90 { 91 93 System.out.println("JEP - Enter q to quit"); 94 System.out.print(prompt); 95 96 while ((command = getCommand()) != null) 97 { 98 j.parseExpression(command); 99 100 if (j.hasError()) { 101 System.out.println(j.getErrorInfo()); 102 } 103 else 104 { 105 if(dumpTree) 106 ((SimpleNode) j.getTopNode()).dump(""); 107 108 Object value = j.getValueAsObject(); 110 111 if (j.hasError()) { 113 System.out.println(j.getErrorInfo()); 114 } 115 else 116 { 117 if(dumpSymbols) 118 System.out.print(j.getSymbolTable().toString()); 119 System.out.println(value); 120 } 121 122 130 } 131 132 System.out.print(prompt); 133 } 134 } 135 136 } 137 138 143 private String getCommand() throws IOException { 144 String s; 145 146 if (br == null) 147 return null; 148 149 if ( (s = br.readLine()) == null) 150 return null; 151 152 if (s.equals("q") 153 || s.equals("quit") 154 || s.equals("exit")) 155 return null; 156 157 return s; 158 } 159 } 160 | Popular Tags |