KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > matrixJep > function > MAssign


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.24
4       December 30 2002
5       (c) Copyright 2002, Nathan Funk
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9 package org.lsmp.djep.matrixJep.function;
10
11 import java.util.*;
12 import org.nfunk.jep.*;
13 import org.nfunk.jep.function.*;
14 import org.lsmp.djep.matrixJep.*;
15 import org.lsmp.djep.matrixJep.nodeTypes.*;
16 import org.lsmp.djep.vectorJep.values.*;
17
18 /**
19  * A matrix enabled asignment function.
20  * The lhs of an assigment must be a variable.
21  *
22  * @author Rich Morris
23  * Created on 23-Feb-2004
24  */

25 public class MAssign extends Assign implements MatrixSpecialEvaluationI
26 {
27     public MAssign()
28     {
29         numberOfParameters = 2;
30     }
31
32     /** The run method should not be called.
33      * Use {@link #evaluate} instead.
34      */

35     public void run(Stack s) throws ParseException
36     {
37         throw new ParseException("Eval should not be called by Evaluator");
38     }
39
40     /**
41      * A special methods for evaluating an assignment.
42      * When an asignment is encountered, first
43      * evaluate the rhs. Then set the value
44      * of the lhs to the result.
45      */

46     public MatrixValueI evaluate(MatrixNodeI node,MatrixEvaluator visitor,MatrixJep j) throws ParseException
47     {
48         if(node.jjtGetNumChildren()!=2)
49             throw new ParseException("Assignment opperator must have 2 operators.");
50
51         // evaluate the value of the righthand side. Left on top of stack
52

53         MatrixValueI rhsVal = (MatrixValueI) node.jjtGetChild(1).jjtAccept(visitor,null);
54
55         // Set the value of the variable on the lhs.
56
Node lhsNode = node.jjtGetChild(0);
57         if(lhsNode instanceof ASTMVarNode)
58         {
59             ASTMVarNode vn = (ASTMVarNode) lhsNode;
60             MatrixVariableI var = (MatrixVariableI) vn.getVar();
61             var.setMValue(rhsVal);
62             return rhsVal;
63         }
64         throw new ParseException("Assignment should have a variable for the lhs.");
65     }
66 }
67
Popular Tags