Exemple #1
0
        private int aggregateVariable(IList <VariableNode> variableNodeList, ActivationFrame frame)
        {
            int sum = 0;

            for (int i = 0; i < variableNodeList.Count; i++)
            {
                Node      v         = frame.variableSet[variableNodeList[i].variableName];
                FinalNode finalNode = null;

                if (v is FinalNode && v.Instructions.Count == 0)
                {
                    finalNode = (FinalNode)v;
                }

                if (v is PrimitiveNode)
                {
                    PrimitiveNode primitiveNode = (PrimitiveNode)v;
                    finalNode = (FinalNode)primitiveNode.Instructions[0];
                }

                if (finalNode != null)
                {
                    sum += Convert.ToInt32(finalNode.dataString());
                }
            }

            return(sum);
        }
Exemple #2
0
        private IList <Node> evalActivationFrame(Node node)
        {
            ActivationFrame frame = activationFrameStack.peek();

            frame.variableSet[((VariableNode)node).variableName] = node;
            return(new List <>());
        }
Exemple #3
0
        private IList <Node> evaluateAggregate(Node node, ActivationFrame frame, Interpreter callback)
        {
            SummationType summationType = (SummationType)node.Instructions[0];
            IList <Node>  list          = node.Instructions;

            int size = list.Count;
            int sum  = 0;

            IList <VariableNode> listOfVariableNodes = new List <VariableNode>();

            for (int i = 1; i < size; i++)
            {
                if (node.Instructions[i] is VariableNode)
                {
                    listOfVariableNodes.Add((VariableNode)node.Instructions[i]);
                }
                else
                {
                    string value = ((FinalNode)list[i].Instructions[0]).dataString();
                    sum += Convert.ToInt32(value);
                }
            }

            if (listOfVariableNodes.Count > 0)
            {
                sum += aggregateVariable(listOfVariableNodes, frame);
            }

            FinalNode returnNode = new FinalNode();

            returnNode.DataString = sum;
            frame.pushReturnNode(returnNode);

            return(new List <>());
        }
Exemple #4
0
        public virtual void evaluateExpressionStack()
        {
            Node @operator = null;

            if (operatorStack.Count == 0)
            {
                throw new Exception("not operators on stack");
            }

            @operator = operatorStack.Pop();

            //TODO ensure the type is accurate.
            //assert false : "this needs to fail until i fix it";
            if (@operator.Type.Equals(NodeType.EqualEqualType) && operandStack.Count >= 2)
            {
                NodeImpl r = (NodeImpl)operandStack.Pop();
                NodeImpl l = (NodeImpl)operandStack.Pop();

                ActivationFrame frame = activationFrameStack.peek();

                object rReal = r.getRealValue(frame);
                object lreal = l.getRealValue(frame);

                bool        equalEqualEval = rReal.Equals(lreal);
                BooleanNode booleanNode    = new BooleanNode();
                FinalNode   finalNode      = new FinalNode();
                finalNode.DataString = equalEqualEval;
                booleanNode.addInst(finalNode);

                operandStack.Push(booleanNode);
            }
        }
Exemple #5
0
        private IList <Node> evaluateEvauatable(Node node, ActivationFrame frame, Interpreter interpreter)
        {
            foreach (Node n in node.Instructions)
            {
                if (n is FinalNode)
                {
                    string data = ((FinalNode)n).dataString();
                    switch (data)
                    {
                    case "+":
                    case "-":
                    case "*":
                    case "/":
                    case ">=":
                    case "<=":
                    case "&&":
                    case "==":
                        interpreter.pushOperatorStack(n);
                        break;

                    default:
                        interpreter.pushOperandStack(n);
                        break;
                    }
                }

                if (n is LiteralNode)
                {
                    evaluateEvauatable(n, frame, interpreter);
                }

                if (n is FunctionCallNode)
                {
                }

                if (n is VariableNode)
                {
                    string s = ((VariableNode)n).VariableName;
                    interpreter.pushOperandStack(n);
                }

                if (n is BinaryNode)
                {
                    evaluateEvauatable(n, frame, interpreter);
                }
            }

            /*
             *          }
             *
             *          if ( shouldEvaluate ) {
             *              interpreter.evaluateExpressionStack();
             *          }
             *
             *      }
             */
            return(new List <>());
        }
Exemple #6
0
        private IList <Node> binaryNode(string variableName, Node node)
        {
            evalBinaryNode(node);
            ActivationFrame frame = activationFrameStack.peek();
            VariableNode    v     = (VariableNode)frame.variableSet[variableName];

            v.IntegralTypeNode = (IntegralTypeNode)frame.operandStack.Pop();
            return(new List <>());
        }
Exemple #7
0
        private IList <Node> evalIfStatement(Node node, ActivationFrame frame, Interpreter callback)
        {
            IList <Node> instructionList = node.Instructions;
            int          size            = instructionList.Count;


            IList <Node> trueExpressions = new List <Node>();
            BooleanNode  booleanNode     = getBooleanExpressionNode(instructionList, size, trueExpressions);

            if (booleanNode != null && booleanNode.Instructions.Count == 1)
            {
                FinalNode finalNode = (FinalNode)booleanNode.Instructions[0];
                if (finalNode.IntegralType == IntegralType.jboolean)
                {
                    bool? @bool = Convert.ToBoolean(finalNode.dataString());
                    if (@bool)
                    {
                        return(trueExpressions);
                    }
                }
            }
            else if (booleanNode != null && booleanNode.Instructions.Count > 0)
            {
                Node currentValue = frame.popNode();
                frame.pushReturnNode(null);
                bool?booleanResult = false;
                evalBooleanNode(booleanNode, frame, callback);

                if (frame.peekReturnNode() != null && frame.peekReturnNode() is BooleanNode)
                {
                    Node      booleanEvalReturnNode = frame.popNode();
                    FinalNode result = getFinalNodeFromAnyNode(booleanEvalReturnNode);
                    booleanResult = Convert.ToBoolean(result.dataString());
                }

                // TODO - FINISH THIS
                if (booleanResult && frame.peekReturnNode() != null)
                {
                    Node returnNode = frame.popNode();
                    if (returnNode is BreakExprNode)
                    {
                        IList <Node> returnList = new List <Node>();
                        returnList.Add(returnNode);
                        return(new List <>());
                    }
                }

                frame.pushReturnNode(currentValue);
            }

            return(new List <>());
        }
Exemple #8
0
        private IList <Node> evalVariableDeclration(Node node, ActivationFrame frame, Interpreter callback)
        {
            if (node.Instructions.Count == 2)
            {
                VariableNode variableNode = (VariableNode)node.Instructions[1];
                frame.variableSet[variableNode.variableName] = variableNode;
            }
            else if (node.Instructions.Count > 2)
            {
                IList <Node> returnValue = EvaluateAssignments.evalVariableDeclWithAssignment(node, activationFrameStack, mainFunctionName, functionNodeMap);
                if (returnValue.Count > 0)
                {
                    execute(returnValue);
                }
            }

            return(new List <>());
        }
Exemple #9
0
        private IList <Node> evalCompilationUnit()
        {
            foreach (KeyValuePair <string, Node> entry in functionNodeMap)
            {
                if (entry.Key.Equals(mainFunctionName))
                {
                    ActivationFrame frame = new ActivationFrame();
                    frame.frameName = mainFunctionName;
                    activationFrameStack.push(frame);
                    execute(entry.Value.Instructions);
                    activationFrameStack.pop();

                    break;
                }
            }

            return(new List <>());
        }
Exemple #10
0
        private void integralTypeNode(IntegralTypeNode itn)
        {
            ActivationFrame frame = activationFrameStack.peek();

            frame.operandStack.Push(itn);
        }
Exemple #11
0
 private IList <Node> evalBooleanOperator(Node node, ActivationFrame frame, Interpreter callback)
 {
     return(new List <>());
 }
Exemple #12
0
        private IList <Node> evalBooleanNode(Node node, ActivationFrame frame, Interpreter callback)
        {
            Node variableType = node.Instructions[0];

            if (node is BooleanOperatorNode)
            {
                //      ((BooleanOperatorNode)node).evaluateExpression( frame , this);
            }

            try
            {
                Node lvalue = null;
                if (variableType is VariableNode)
                {
                    string variableName = ((VariableNode)node.Instructions[0]).variableName;
                    lvalue = frame.variableSet[variableName];
                }
                else
                {
                    lvalue = variableType;
                }

                bool isEqualEqual;
                Node rvalue = null;
                // This is ugly code. Need to find a better way to
                // handle these cases.
                // Multiple ifs will only cause confusion.
                FinalNode updatedLvalue = null;

                /*
                 *          if (node.getInstructions().size() == 1) {
                 *              //lvalue must be a single boolean expression
                 *              if (lvalue instanceof FinalNode) {
                 *                  BooleanNode booleanNode = new BooleanNode();
                 *                  booleanNode.addInst(lvalue);
                 *                  frame.pushReturnNode( booleanNode );
                 *                  return new ArrayList<>();
                 *              }
                 *
                 *              if (lvalue instanceof PrimitiveNode) {
                 *                  updatedLvalue = (FinalNode) lvalue.getInstructions().get(0);
                 *              }
                 *
                 *          } else if (node.getInstructions().size() > 1) {
                 *              //if (booleanOperatorNode.getInstructions().get(0) instanceof EqualEqualSignNode) {
                 *                  //isEqualEqual;
                 *              //}
                 *              rvalue = node.getInstructions().get(2);
                 *              FinalNode updatedRvalue = null;
                 *              if (rvalue != null && rvalue instanceof PrimitiveNode) {
                 *                  updatedRvalue = (FinalNode) rvalue.getInstructions().get(0);
                 *              }
                 *
                 *              if (updatedLvalue != null) {
                 *                  lvalue = updatedLvalue;
                 *              }
                 *
                 *              if (updatedRvalue != null) {
                 *                  rvalue = updatedRvalue;
                 *              }
                 *
                 *              isEqualEqual = getFinalNodeFromAnyNode( lvalue) .dataString().equals( getFinalNodeFromAnyNode(rvalue).dataString());
                 *              //else if (booleanOperatorNode.getInstructions().get(0) instanceof  )
                 *              FinalNode finalNode = new FinalNode();
                 *              finalNode.setDataString(isEqualEqual);
                 *
                 *              BooleanNode booleanNode = new BooleanNode();
                 *              booleanNode.addInst(finalNode);
                 *
                 *              frame.pushReturnNode( booleanNode );
                 *              return new ArrayList<>();
                 *          }
                 */
            }
            catch (Exception ex)
            {
                JuliarLogger.log(ex.Message);
            }

            return(new List <>());
        }
Exemple #13
0
        private IList <Node> evalWhileExpression(Node node, ActivationFrame frame, Interpreter callback)
        {
            IList <Node> instructionList = ((NodeImpl)node).ConditionalExpressions;

            Node expressionNode = instructionList[0];

            if (expressionNode is EvaluatableNode)
            {
                expressionNode.EvaluateNode(frame, callback);
            }

            /*
             * BooleanOperatorNode booleanNode = (BooleanOperatorNode) instructionList.get( 0 );
             *
             * evalBooleanNode(booleanNode, frame, callback);
             *
             * Node boolEvalResult = null;
             *
             * if (frame.peekReturnNode() != null) {
             *  boolEvalResult = frame.popNode();
             *
             *  FinalNode finalNode = (FinalNode) boolEvalResult.getInstructions().get(0);
             *  Boolean executeTrue = Boolean.parseBoolean(finalNode.dataString());
             *
             *  if (executeTrue) {
             *      Boolean breakStatement = false;
             *      while(true) {
             *          for (int expressionCount = 0; expressionCount < trueExpressions.size(); expressionCount++) {
             *              List<Node> currentExpressionInWhileBody = new ArrayList<>();
             *              Node currentNode = trueExpressions.get( expressionCount );
             *              currentExpressionInWhileBody.add( currentNode );
             *
             *              if (currentNode instanceof StatementNode && currentNode.getInstructions().get(0).getInstructions().get(0) instanceof BreakExprNode){
             *                  breakStatement = true;
             *                  break;
             *              }
             *
             *              execute(currentExpressionInWhileBody);
             *
             *              if (frame.peekReturnNode() instanceof BreakExprNode){
             *                  breakStatement = true;
             *                  break;
             *              }
             *          }
             *
             *          if (breakStatement) {
             *              frame.pushReturnNode( null );
             *              break;
             *          }
             *
             *          // re-evaluateExpression the loop condtion
             *          evalBooleanNode(booleanNode, frame, callback);
             *          boolEvalResult = frame.popNode();
             *          finalNode = (FinalNode) boolEvalResult.getInstructions().get(0);
             *
             *          //assert finalNode.dataString().equalsIgnoreCase( "true ") || finalNode.dataString().equalsIgnoreCase( "false" ) : "A boolean value was not returned";
             *
             *          if (!(Boolean.parseBoolean( finalNode.dataString() ))) {
             *              break;
             *          }
             *      }
             *  }
             * }*/

            return(new List <>());
        }
Exemple #14
0
 private IList <Node> evalReturn(Node oldnode, ActivationFrame frame, Interpreter callback)
 {
     return(activationFrameStack.setupReturnValueOnStackFrame(oldnode));
 }
Exemple #15
0
 private IList <Node> evaluateBreak(Node node, ActivationFrame frame)
 {
     frame.pushReturnNode(node);
     return(new List <>());
 }