KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jepexamples > Console


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.nfunk.jepexamples;
18
19 import java.io.*;
20 import org.nfunk.jep.JEP;
21 //import org.nfunk.sovler.*;
22

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

34 class Console {
35     
36     /** The prompt string */
37     private String JavaDoc prompt;
38     
39     /** The input reader */
40     private BufferedReader br;
41     
42     /** Constructor */
43     public Console() {
44         prompt = "JEP > ";
45         br = new BufferedReader(new InputStreamReader(System.in));
46
47     }
48
49     /** Creates a new Console object and calls run() */
50     public static void main(String JavaDoc args[]) throws IOException {
51         Console c = new Console();
52         c.run(args);
53     }
54     
55     /** The input loop */
56     public void run(String JavaDoc args[]) throws IOException {
57         String JavaDoc command="";
58         JEP j = new JEP();
59         j.addStandardConstants();
60         j.addStandardFunctions();
61         j.addComplex();
62         //j.setTraverse(true);
63

64         if (args.length>0) {
65             // evaluate the expression passed as arguments
66
String JavaDoc temp = args[0];
67             for (int i=1; i<args.length; i++) temp += " " + args[i];
68             j.parseExpression(temp);
69             if (j.hasError())
70                 System.out.println(j.getErrorInfo());
71             else
72                 System.out.println(j.getValueAsObject());
73         } else {
74             // no arguments - interactive mode
75

76             System.out.println("JEP - Enter q to quit");
77             System.out.print(prompt);
78
79             while ((command = getCommand()) != null) {
80                 j.parseExpression(command);
81                 
82                 if (j.hasError()) {
83                     System.out.println(j.getErrorInfo());
84                 } else {
85                     // expression is OK, get the value
86
Object JavaDoc value = j.getValueAsObject();
87                     
88                     // did error occur during evaluation?
89
if (j.hasError()) {
90                         System.out.println(j.getErrorInfo());
91                     } else {
92                         System.out.println(value);
93                     }
94
95 /*
96                     System.out.println(
97                         (LinearVisitor.isLinear(j.getTopNode())) ?
98                         "Linear" : "Not Linear");
99                     System.out.println(
100                         (ConstantVisitor.isConstant(j.getTopNode())) ?
101                         "Constant" : "Not Constant");
102 */

103                 }
104                     
105                 System.out.print(prompt);
106             }
107         }
108         
109     }
110     
111     /**
112      * Get a command from the input.
113      * @return null if an error occures, or if the user enters a terminating
114      * command
115      */

116     private String JavaDoc getCommand() throws IOException {
117         String JavaDoc s;
118         
119         if (br == null)
120             return null;
121
122         if ( (s = br.readLine()) == null)
123             return null;
124
125         if (s.equals("q")
126             || s.equals("quit")
127             || s.equals("exit"))
128             return null;
129         
130         return s;
131     }
132 }
133
Popular Tags