KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > xjep > XAssign


1 /* @author rich
2  * Created on 18-Nov-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.xjep;
9
10 import org.nfunk.jep.*;
11 import org.nfunk.jep.function.*;
12
13 /**
14  * An assignment operator so we can do
15  * x=3+4.
16  * This function implements the SpecialEvaluationI interface
17  * so that it handles seting the value of a variable.
18  * @author Rich Morris
19  * Created on 18-Nov-2003
20  */

21 public class XAssign extends Assign implements CommandVisitorI {
22
23     public XAssign() {}
24     {
25         numberOfParameters = 2;
26     }
27
28     /**
29      * In the pre-process stage, set the equation of the lhs variable to the rhs equation.
30      */

31     public Node process(Node node,Node children[],XJep xjep) throws ParseException
32     {
33         if(node.jjtGetNumChildren()!=2)
34             throw new ParseException("Assignment opperator must have 2 operators.");
35
36         // evaluate the value of the righthand side. Left on top of stack
37

38         // Set the value of the variable on the lhs.
39
Node lhsNode = children[0];
40         if(lhsNode instanceof ASTVarNode)
41         {
42             ASTVarNode vn = (ASTVarNode) lhsNode;
43             XVariable var = (XVariable) vn.getVar();
44             var.setEquation(xjep.deepCopy(children[1]));
45             TreeUtils.copyChildrenIfNeeded(node,children);
46             return node;
47         }
48         throw new ParseException("Assignment should have a variable for the lhs.");
49     }
50 }
51
Popular Tags