KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djepExamples > VectorConsole


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 /**
11  * Console - JEP Example Applet
12  * Copyright (c) 2000 Nathan Funk
13  *
14  * @author Nathan Funk
15  */

16
17 package org.lsmp.djepExamples;
18
19 import org.lsmp.djep.vectorJep.VectorJep;
20 import org.nfunk.jep.*;
21 import java.io.*;
22 //import org.nfunk.sovler.*;
23

24 /**
25 * This class implements a simple command line utility for evaluating
26 * mathematical expressions.
27 *
28 * Usage: java org.nfunk.jepexamples.Console [expression]
29 *
30 * If an argument is passed, it is interpreted as an expression
31 * and evaluated. Otherwise, a prompt is printed, and the user can enter
32 * expressions to be evaluated. To exit from the command prompt a 'q' must
33 * be entered.
34 */

35 class VectorConsole {
36     
37     /** The prompt string */
38     private String JavaDoc prompt;
39     
40     /** The input reader */
41     private BufferedReader br;
42     
43     /** where to give a dump of tree after parsing. **/
44     boolean dumpTree = false;
45     /** whether to print symbol table after each line. **/
46     boolean dumpSymbols=false;
47     
48     /** Constructor */
49     public VectorConsole() {
50         prompt = "JEP > ";
51         br = new BufferedReader(new InputStreamReader(System.in));
52
53     }
54
55     /** Creates a new Console object and calls run() */
56     public static void main(String JavaDoc args[]) throws IOException {
57         VectorConsole c = new VectorConsole();
58         c.run(args);
59     }
60     
61     /** The input loop */
62     public void run(String JavaDoc args[]) throws IOException {
63         String JavaDoc command="";
64         JEP j = new VectorJep();
65         j.addStandardConstants();
66         j.addStandardFunctions();
67         j.addComplex();
68         //j.setTraverse(true);
69
j.setAllowAssignment(true);
70         j.setAllowUndeclared(true);
71         String JavaDoc 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             // no arguments - interactive mode
92

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                     // expression is OK, get the value
109
Object JavaDoc value = j.getValueAsObject();
110                     
111                     // did error occur during evaluation?
112
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 /*
123                     System.out.println(
124                         (LinearVisitor.isLinear(j.getTopNode())) ?
125                         "Linear" : "Not Linear");
126                     System.out.println(
127                         (ConstantVisitor.isConstant(j.getTopNode())) ?
128                         "Constant" : "Not Constant");
129 */

130                 }
131                     
132                 System.out.print(prompt);
133             }
134         }
135         
136     }
137     
138     /**
139      * Get a command from the input.
140      * @return null if an error occures, or if the user enters a terminating
141      * command
142      */

143     private String JavaDoc getCommand() throws IOException {
144         String JavaDoc 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