Example #1
0
        public virtual Stack <Node> setupReturnValueOnStackFrame(Node rValue)
        {
            int size = activationFrameStack.Count - 1;

            ActivationFrame currentFrame = activationFrameStack.get(size);
            ActivationFrame caller       = activationFrameStack.get(size - 1);

            if (caller != null)
            {
                if (rValue is VariableNode && currentFrame.variableSet.ContainsKey(((VariableNode)rValue).variableName))
                {
                    caller.pushReturnNode(currentFrame.variableSet[((VariableNode)rValue).variableName]);
                }
                if (rValue is IntegralTypeNode)
                {
                    caller.pushReturnNode(rValue);
                }

                activationFrameStack.Push(caller);
                activationFrameStack.Push(currentFrame);
            }

            /*
             *
             * if (frame.variableSet.containsKey(node.typeName())) {
             *  Node variableNode = frame.variableSet.get(node.typeName());
             *  returnValueStack.push( variableNode );
             * }
             */

            return(new Stack <>());
        }
Example #2
0
        public static void booleanInstance(BooleanNode booleanNode, ActivationFrame activationFrame, VariableDeclarationNode variableToAssignTo)
        {
            IList <Node> slotList = new List <Node>();

            slotList.Add(booleanNode);
            interpreterCallback.execute(slotList);
            assignReturnValueToVariable(activationFrame, variableToAssignTo);
        }
Example #3
0
        public static void functionCallNode(FunctionCallNode functionCallNode, ActivationFrame activationFrame, VariableDeclarationNode variableToAssignTo)
        {
            IList <Node> functionList = new List <Node>();

            functionList.Add(functionCallNode);
            interpreterCallback.execute(functionList);
            assignReturnValueToVariable(activationFrame, variableToAssignTo);
        }
Example #4
0
 private static void dumpFrameVariables(ActivationFrame frame)
 {
     Dictionary <string, com.juliar.nodes.Node> .KeyCollection keys = frame.variableSet.Keys;
     foreach (string s in keys)
     {
         Console.WriteLine(string.Format("key {0} = value {1}", s, frame.variableSet[s].ToString()));
     }
 }
Example #5
0
        public static bool evalIfPrimitive(Node n, ActivationFrame activationFrame, Interpreter calback)
        {
            string functionName = ((FinalNode)n.Instructions[0]).dataString();

            if (primitiveFunctions.Contains(functionName))
            {
                evalPrimitives(n, activationFrame, calback);
                return(true);
            }

            return(false);
        }
Example #6
0
        private static void assignReturnValueToVariable(ActivationFrame activationFrame, VariableDeclarationNode variableToAssignTo)
        {
            if (activationFrame.peekReturnNode() != null)
            {
                VariableNode variableNode = (VariableNode)variableToAssignTo.Instructions[1];
                if (activationFrame.variableSet.ContainsKey(variableNode.variableName))
                {
                    activationFrame.variableSet.Remove(variableNode.variableName);
                }

                activationFrame.variableSet[variableNode.variableName] = activationFrame.popNode();
            }
        }
Example #7
0
        public static IList <Node> evalPrimitives(Node n, ActivationFrame activationFrame, Interpreter calback)
        {
            string    functionName = ((FinalNode)n.Instructions[0]).dataString();
            FinalNode finalNode    = new FinalNode();

            switch (functionName)
            {
            case "print":
                printLine(activationFrame, functionName, n.Instructions[2]);
                break;

            case "__getByteFromString":
                getByteFromString(activationFrame, n.Instructions[1], n.Instructions[2]);
                break;

            case "printLine":
                printLine(activationFrame, functionName, n.Instructions[1]);
                break;

            case "fileOpen":
                string data = fileOpen(n.Instructions[2]);
                finalNode.DataString = data;
                finalNode.VariableTypeByIntegralType = IntegralType.jstring;
                activationFrame.pushReturnNode(finalNode);
                //activationFrame.returnNode = finalNode;
                break;

            case "sysExec":
                string ex = sysExec(n.Instructions[2]);
                finalNode.DataString = ex;
                finalNode.VariableTypeByIntegralType = IntegralType.jstring;
                activationFrame.pushReturnNode(finalNode);
                //activationFrame.returnNode = finalNode;
                break;

            case "availableMemory":
                long value = availableMemory();
                finalNode.DataString = value;
                finalNode.VariableTypeByIntegralType = IntegralType.jlong;
                activationFrame.pushReturnNode(finalNode);
                //activationFrame.returnNode = finalNode;
                break;

            default:
                JuliarLogger.log("function " + functionName + " does not exist");
                break;
            }

            return(new List <>());
        }
Example #8
0
        public static void commandInstance(CommandNode commandNode, ActivationFrame activationFrame, VariableDeclarationNode variableToAssignTo)
        {
            IList <Node> slotList = new List <Node>();

            slotList.Add(commandNode);
            interpreterCallback.execute(slotList);

            FinalNode variableNameTerminalNode = (FinalNode)variableToAssignTo.Instructions[1].Instructions[0];

            string variableName = variableNameTerminalNode.dataString();

            if (activationFrame.variableSet.ContainsKey(variableName))
            {
                activationFrame.variableSet.Remove(variableName);
            }

            activationFrame.variableSet[variableName] = activationFrame.peekReturnNode();
            //activationFrame.returnNode = null;
        }
 public override void EvaluateNode(ActivationFrame frame, Interpreter interpreter)
 {
     foreach (Node n in this.Instructions)
     {
         if (n is BooleanOperatorNode || n is ParenthesizedExpressionNode)
         {
             n.EvaluateNode(frame, interpreter);
         }
         else
         {
             if (n is FinalNode && (!((FinalNode)n).dataString().Equals("(") && !((FinalNode)n).dataString().Equals(")")))
             {
                 interpreter.pushOperatorStack(n);
             }
             else if (n is VariableNode || n is FunctionCallNode || n is LiteralNode)
             {
                 interpreter.pushOperandStack(n);
             }
         }
     }
 }
Example #10
0
        private static void getByteFromString(ActivationFrame activationFrame, Node argumentNode, Node index)
        {
            string variableName = ((VariableNode)argumentNode).variableName;
            object variable     = activationFrame.variableSet[variableName];

            if (variable is FinalNode)
            {
                char[] array = sysGetByteFromString(((FinalNode)variable).dataString());

                FinalNode finalNode = new FinalNode();

                string argTwoVariableName = ((VariableNode)index).variableName;
                object argTwo             = activationFrame.variableSet[argTwoVariableName];

                FinalNode argumentTwo = null;

                if (argTwo is PrimitiveNode)
                {
                    argumentTwo = (FinalNode)activationFrame.variableSet[argTwoVariableName].Instructions[0];
                }
                else if (argTwo is FinalNode)
                {
                    argumentTwo = (FinalNode)activationFrame.variableSet[argTwoVariableName];
                }

                assert(argumentTwo != null ? argumentTwo.dataString() : null) != null;
                int parsedIndex = Convert.ToInt32(argumentTwo.dataString());

                if (parsedIndex > array.Length)
                {
                    throw new Exception("\r\nJuliar runtime exception - Index out of bounds accessing variable - '" + variableName + "'");
                }
                if (parsedIndex < array.Length)
                {
                    finalNode.DataString = array[parsedIndex];
                    activationFrame.pushReturnNode(finalNode);
                    //activationFrame.returnNode = finalNode;
                }
            }
        }
Example #11
0
        public static IList <Node> evalReassignment(Node n, ActivationFrame activationFrame, Interpreter callback)
        {
            if (n != null)
            {
                VariableReassignmentNode node = (VariableReassignmentNode)n;

                VariableNode lValueVariableNode = (VariableNode)node.Instructions[0];
                string       variableName       = lValueVariableNode.variableName;

                Node rvalueVariableNode = node.Instructions[2];

                NodeType nodeType = rvalueVariableNode.Type;

                activationFrame.variableSet.Remove(variableName);
                switch (nodeType)
                {
                case com.juliar.nodes.NodeType.LiteralType:
                    LiteralNode literalNode = (LiteralNode)rvalueVariableNode;
                    activationFrame.variableSet[variableName] = literalNode;
                    break;

                case com.juliar.nodes.NodeType.VariableType:
                    VariableNode variableNode = (VariableNode)rvalueVariableNode;
                    activationFrame.variableSet[variableName] = variableNode;
                    break;

                case com.juliar.nodes.NodeType.EvaluatableType:
                    if (true)
                    {
                        Console.WriteLine("need to add code to evaluate");
                        //ArrayList<Node> evaluatableNode = new ArrayList<Node>()
                        //evaluatableNode.add( rvalueVariableNode );
                        callback.execute(new List <Node>(rvalueVariableNode));
                    }
                    break;
                }
            }

            return(new List <>());
        }
Example #12
0
        public override void EvaluateNode(ActivationFrame frame, Interpreter interpreter)
        {
            foreach (Node n in this.Instructions)
            {
                if (n is BooleanOperatorNode || n is ParenthesizedExpressionNode)
                {
                    n.EvaluateNode(frame, interpreter);
                }
            }

            /*
             * if (n instanceof FinalNode) {
             *  interpreter.pushOperatorStack(n);
             * } else if (n instanceof VariableNode || n instanceof FunctionCallNode || n instanceof LiteralNode) {
             *  interpreter.pushOperandStack(n);
             * }
             * if (n.getInstructions().size() == 3) {
             *  EvaluateNode(n.getInstructions().get(0));
             *  EvaluateNode(n.getInstructions().get(1));
             *  EvaluateNode(n.getInstructions().get(2));
             * }*/
        }
Example #13
0
        public static IList <Node> evalAssignment(Node n, ActivationFrame activationFrame, Interpreter calback)
        {
            AssignmentNode assignmentNode = (AssignmentNode)n;
            IList <Node>   instructions   = assignmentNode.Instructions;

            const int varDeclIndex   = 0;
            const int equalSignIndex = 1;
            const int primtiveIndex  = 2;
            VariableDeclarationNode variableToAssignTo = (VariableDeclarationNode)instructions[varDeclIndex];

            // | zero             | one       | two
            // | variableDecl     | EqualSign | Primitive
            // | int variableName | =         | 3

            if (instructions[equalSignIndex] is EqualSignNode)
            {
                object rvalue = instructions[primtiveIndex];

                if (rvalue is FunctionCallNode)
                {
                    functionCallNode((FunctionCallNode)rvalue, activationFrame, variableToAssignTo);
                }
                if (rvalue is PrimitiveNode)
                {
                    primitiveInstance((PrimitiveNode)rvalue, activationFrame, variableToAssignTo);
                }
                if (rvalue is BooleanNode)
                {
                    booleanInstance((BooleanNode)rvalue, activationFrame, variableToAssignTo);
                }
                if (rvalue is CommandNode)
                {
                    commandInstance((CommandNode)rvalue, activationFrame, variableToAssignTo);
                }
            }
            return(new List <>());
        }
Example #14
0
        public static void primitiveInstance(PrimitiveNode primitiveNode, ActivationFrame activationFrame, VariableDeclarationNode variableToAssignTo)
        {
            if (canPrimitiveValueBeAssignedToVar(variableToAssignTo, primitiveNode))
            {
                string variableName;

                if (variableToAssignTo.IntegralType == juserDefined)
                {
                    variableName = variableToAssignTo.UserDefinedNode.FullyQualifiedVariableName;
                }
                else
                {
                    FinalNode variableNameTerminalNode = (FinalNode)variableToAssignTo.Instructions[1].Instructions[0];
                    variableName = variableNameTerminalNode.dataString();
                }

                if (activationFrame.variableSet.ContainsKey(variableName))
                {
                    activationFrame.variableSet.Remove(variableName);
                }

                activationFrame.variableSet[variableName] = primitiveNode;
            }
        }
Example #15
0
        private static void printLine(ActivationFrame activationFrame, string functionName, Node argumentNode)
        {
            FinalNode finalNode = null;

            if (argumentNode == null)
            {
                if (activationFrame.peekReturnNode() != null)
                {
                    //argumentNode = activationFrame.returnNode;
                    argumentNode = activationFrame.popNode();
                }
            }

            switch (argumentNode.Type)
            {
            case LiteralType:
                finalNode = (FinalNode)argumentNode.Instructions[0];
                break;

            case VariableType:
                string variableName     = ((VariableNode)argumentNode).variableName;
                Node   tempVariableNode = activationFrame.variableSet[variableName];
                if (tempVariableNode == null)
                {
                    // a variable has been declared and not initazlized
                    finalNode = new FinalNode();
                    break;
                }
                if (tempVariableNode is VariableNode)
                {
                    string name = ((VariableNode)tempVariableNode).variableName;
                    if (activationFrame.variableSet.ContainsKey(name))
                    {
                        finalNode = (FinalNode)activationFrame.variableSet[name].Instructions[0];
                    }
                }
                else
                {
                    printLine(activationFrame, functionName, tempVariableNode);
                    return;
                }
                break;

            case FinalType:
                finalNode = (FinalNode)argumentNode;
                break;
            }

            if (finalNode == null)
            {
                dumpFrameVariables(activationFrame);
                Debug.Assert(finalNode != null, "the final node cannot be null");
            }

            string stringToPrint = finalNode.dataString();

            if (functionName.Equals("printLine"))
            {
                sysPrintLine(stringToPrint);
                return;
            }
            if (functionName.Equals("print"))
            {
                sysPrint(stringToPrint);
                return;
            }
        }
        public static IList <Node> evalFunctionCall(Node node, ActivationFrameStack activationFrame, string mainFunctionName, IDictionary <string, Node> functionNodeMap, Interpreter callback)
        {
            FunctionCallNode functionCallNode = (FunctionCallNode)node;
            string           functionToCall   = functionCallNode.functionName();

            //ActivationFrame evalFrame = activationFrameStack.pop();
            bool isPrimitive = EvaluatePrimitives.evalIfPrimitive(node, activationFrame.peek(), callback);

            //activationFrameStack.push( evalFrame );
            if (isPrimitive)
            {
                return(new List <>());
            }

            // main should only be called from the compliationUnit
            if (functionCallNode.Equals(mainFunctionName))
            {
                return(new List <>());
            }

            FunctionDeclNode functionDeclNode = (FunctionDeclNode)functionNodeMap[functionToCall];

            if (functionDeclNode != null)
            {
                ActivationFrame frame = new ActivationFrame();
                frame.frameName = functionToCall;

                IList <VariableNode>            sourceVariables = new List <VariableNode>();
                IList <VariableDeclarationNode> targetVariables = new List <VariableDeclarationNode>();

                foreach (Node v in node.Instructions)
                {
                    if (v is VariableNode)
                    {
                        sourceVariables.Add((VariableNode)v);
                    }
                }

                foreach (Node v in functionDeclNode.Instructions)
                {
                    if (v is VariableDeclarationNode)
                    {
                        targetVariables.Add((VariableDeclarationNode)v);
                    }
                }

                if (sourceVariables.Count != targetVariables.Count)
                {
                    throw new Exception("Source and target variable count do not match");
                }

                // since the function that is getting called can reference the variable using the
                // formal parameters of the function this code will match the calling functions data
                // with the target calling functions variable name.
                for (int i = 0; i < sourceVariables.Count; i++)
                {
                    VariableNode variableNode = (VariableNode)targetVariables[0].Instructions[1];
                    if (variableNode.integralTypeNode == sourceVariables[i].integralTypeNode)
                    {
                        frame.variableSet[variableNode.variableName] = activationFrame.peek().variableSet[sourceVariables[i].variableName];
                    }
                    else
                    {
                        throw new Exception("data types are not the same");
                    }
                }

                activationFrame.push(frame);


                IList <Node> statements = getFunctionStatements(functionDeclNode.Instructions);
                callback.execute(statements);
                activationFrame.pop();

                //activationFrame.push(frame);
                //execute(functionDeclNode.getInstructions());

                return(new List <Node>());
            }
            else
            {
                FinalNode primitiveArg = new FinalNode();
                primitiveArg.DataString = functionToCall;
                PrimitiveNode primitiveNode = new PrimitiveNode();
                primitiveNode.addInst(primitiveArg);

                foreach (Node primArgs in node.Instructions)
                {
                    if (primArgs is VariableNode || primArgs is IntegralTypeNode)
                    {
                        primitiveNode.addInst(primArgs);
                    }
                }

                return(EvaluatePrimitives.evalPrimitives(primitiveNode, activationFrame.peek(), callback));
            }
        }
Example #17
0
 public virtual void push(ActivationFrame frame)
 {
     // Logger.log ("push [" + frame.frameName + "]");
     activationFrameStack.Push(frame);
 }
Example #18
0
 public virtual void EvaluateNode(ActivationFrame frame, Interpreter interpreter)
 {
 }
Example #19
0
 public virtual object getRealValue(ActivationFrame frame)
 {
     return("");
 }