示例#1
0
 internal AssignmentExpr(OperatorExpression operation,
     Expr left,
     Expr right)
     : base(operation, left, right)
 {
 }
示例#2
0
 internal ArithmeticBinaryExpr(OperatorExpression operation,
     Expr left,
     Expr right)
     : base(operation, left, right)
 {
 }
示例#3
0
 internal BinaryOperatorExpression(OperatorExpression operation)
     : base()
 {
     this.operation = operation;
 }
示例#4
0
 internal BinaryOperatorExpression(
     OperatorExpression operation,
     Expr left,
     Expr right)
     : this()
 {
     this.left = left;
     this.right = right;
     this.operation = operation;
 }
示例#5
0
        /// <summary>
        /// Parses the statements in the function body
        /// recursively and calls other statemetents as needed
        /// </summary>
        internal void ParseStatementExpression()
        {
            Token t = _scanner.GetToken();

            var identfiers = new Stack<Expr>();
            var operators = new Stack<Expr>();
            var functionParameters = new Stack<Expr>();

            bool foundLeftParen = false;
            bool functionHasParameters = false;

              //add some comments in the test branch.

            Action<Expr> PopLeftParen = (Expr expr) =>
            {
                if (foundLeftParen)
                {
                    OperatorExpression opOnStack = (OperatorExpression)operators.Peek();
                    if (opOnStack.OperationKind == ByteCodeIdentifiers.TokenCode.LParen)
                    {
                        operators.Pop();
                    }

                    identfiers.Push(expr);
                }
            };

            while (t != null && !t.IdentiferName.Equals(constants.SEMICOLON))
            {
                if (t.isOperator)
                {
                    if (t.IdentiferName.Equals(constants.LPAREN))
                    {
                        foundLeftParen = true;
                    }

                    if (ParserContext.Context == ParserContextEnum.IfDecl && t.IdentiferName.Equals(constants.RPAREN))
                    {
                        break;
                    }

                    if (operators.Count > 0)
                    {
                        OperatorPrecedenceTable.ShiftReduceEval sre = OperatorPrecedenceTable.IdOperation.Validate(operators.Peek().GetToken(), t);

                        switch (sre)
                        {
                            case OperatorPrecedenceTable.ShiftReduceEval.Shift:
                                {
                                    if (t.IdentiferName.Equals(constants.RPAREN))
                                    {
                                        if (ParserContext.Context == ParserContextEnum.FuncExecution)
                                        {
                                            //do function specific stuff
                                        }
                                        foundLeftParen = false;
                                        functionHasParameters = false;

                                        var parameterList = new List<Expr>();

                                        while (functionParameters.Count > 0)
                                        {
                                            parameterList.Add(functionParameters.Pop());
                                        }

                                        if (operators.Peek().GetToken().IdentiferName.Equals(constants.LPAREN))
                                        {
                                            operators.Pop();
                                            if (ParserContext.Context == ParserContextEnum.FuncExecution)
                                            {
                                                ParserContext.ManualReset();
                                            }
                                        }
                                        else
                                        {
                                            throw new NireExecutionException("mismatched parens");
                                        }

                                        var funcCall = new FunctionCallExpression(identfiers.Pop().GetToken(), parameterList);
                                        identfiers.Push(funcCall);

                                        break;
                                    }
                                    operators.Push(new OperatorExpression(t));
                                    break;
                                }
                            case OperatorPrecedenceTable.ShiftReduceEval.Reduce:
                                {
                                    Expr operation = operators.Pop();
                                    var oe = new OperatorExpression(operation.GetToken());
                                    Stack<Expr> currentIdentifier = null;

                                    if (ParserContext.Context == ParserContextEnum.FuncExecution)
                                    {
                                        if (functionParameters.Count < 2)
                                        {
                                            throw new NireException();
                                        }

                                        currentIdentifier = functionParameters;
                                    }
                                    else
                                    {
                                        if (identfiers.Count < 2)
                                        {
                                            throw new NireException();
                                        }

                                        currentIdentifier = identfiers;
                                    }

                                    //Expr rvalue = identfiers.Pop();
                                    //Expr lvalue = identfiers.Pop();

                                    Expr rvalue = currentIdentifier.Pop();
                                    Expr lvalue = currentIdentifier.Pop();

                                    switch (operation.GetToken().Kind)
                                    {
                                        case ByteCodeIdentifiers.TokenCode.Equal:
                                            {

                                                var ae = new AssignmentExpr(oe, lvalue, rvalue);
                                                if (ae.IsValidBinaryExpression())
                                                {
                                                    PopLeftParen(ae);
                                                }
                                                break;
                                            }
                                        case ByteCodeIdentifiers.TokenCode.Multiply:
                                        case ByteCodeIdentifiers.TokenCode.Divide:
                                        case ByteCodeIdentifiers.TokenCode.EqualEqual:
                                        case ByteCodeIdentifiers.TokenCode.Plus:
                                        case ByteCodeIdentifiers.TokenCode.Minus:
                                            {
                                                var boe = new ArithmeticBinaryExpr(oe, lvalue, rvalue);
                                                if (boe.IsValidBinaryExpression())
                                                {
                                                    PopLeftParen(boe);
                                                }
                                                break;
                                            }
                                    }

                                    if (t.Kind != ByteCodeIdentifiers.TokenCode.SemiColon &&
                                        t.Kind != ByteCodeIdentifiers.TokenCode.LParen &&
                                        t.Kind != ByteCodeIdentifiers.TokenCode.RParen)
                                    {
                                        operators.Push(new OperatorExpression(t));
                                    }
                                    break;
                                }
                        }
                    }
                    else
                    {
                        if (t.Kind != ByteCodeIdentifiers.TokenCode.RParen)
                        {
                            operators.Push(new OperatorExpression(t));
                        }
                    }
                }
                else
                {
                    if (foundLeftParen)
                    {
                        functionHasParameters = true;
                        if (functionHasParameters) { }
                    }

                    if (ParserContext.Context == ParserContextEnum.FuncExecution)
                    {
                        functionParameters.Push(new IdentifierExpression(t));
                    }
                    else
                    {
                        identfiers.Push(DetermineNextToken(t));
                    }
                }

                t = _scanner.GetToken();
            }

            ConstructAst(operators, identfiers);
        }