KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > evaluation > CommandEvaluator


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 package org.nfunk.jep.evaluation;
11
12 import java.util.*;
13 import org.nfunk.jep.*;
14 import org.nfunk.jep.function.*;
15
16 /**
17  *
18  * @author nathan
19  */

20 public class CommandEvaluator {
21     private int nCommands, nParam;
22     private CommandElement command;
23     private Stack stack;
24     private PostfixMathCommandI pfmc;
25     private int i;
26     
27     public CommandEvaluator() {
28         stack = new Stack();
29     }
30     
31     public Object JavaDoc evaluate(CommandElement[] commands, SymbolTable symTab) throws Exception JavaDoc {
32         
33         int nCommands = commands.length;
34         
35         stack.removeAllElements();
36         
37         // for each command
38
i = 0;
39         while (i<nCommands) {
40             command = commands[i];
41
42             switch (command.getType()) {
43                 case CommandElement.FUNC: {
44                     // Function
45
pfmc = command.getPFMC();
46                 
47                     // set the number of current parameters
48
// (it is no faster to first check getNumberOfParameters()==-1)
49
pfmc.setCurNumberOfParameters(command.getNumParam());
50                 
51                     pfmc.run(stack);
52                     break;
53                 }
54                 case CommandElement.VAR: {
55                     // Variable
56
stack.push(symTab.getValue(command.getVarName()));
57                     break;
58                 }
59                 default: {
60                     // Constant
61
stack.push(command.getValue());
62                 }
63             }
64             /*
65             if (command instanceof ASTVarNode) {
66                 // Variable
67                 stack.push(symTab.get(((ASTVarNode)command).getName()));
68             } else if (command instanceof PostfixMathCommandI) {
69                 // Function
70                 pfmc = (PostfixMathCommandI)command;
71                 
72                 // set the number of current parameters
73                 // (it is no faster to first check getNumberOfParameters()==-1)
74                 nParam = ((Integer)commands.elementAt(++i)).intValue();
75                 pfmc.setCurNumberOfParameters(nParam);
76                 
77                 pfmc.run(stack);
78             } else {
79             }*/

80             
81             i++;
82         }
83         if (stack.size() != 1) {
84             throw new Exception JavaDoc("CommandEvaluator.evaluate(): Stack size is not 1");
85         }
86         return stack.pop();
87     }
88 }
89
Popular Tags