Exemplo n.º 1
0
        private AddExpression parseExpression()
        {
            MultExpression firstOperand = parseMultExpression();

            if (firstOperand != null)
            {
                AddExpression node = new AddExpression();
                node.Operands.Add(firstOperand);

                while (lexer.Token is PlusToken || lexer.Token is MinusToken)
                {
                    switch (lexer.Token)
                    {
                    case PlusToken _:
                        node.Operators.Add(PlusToken.Text);
                        break;

                    case MinusToken _:
                        node.Operators.Add(MinusToken.Text);
                        break;
                    }
                    lexer.NextToken();

                    node.Operands.Add(parseMultExpression());
                }

                return(node);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        private INode parseParamExpression()
        {
            bool unary = false;

            // Check if unary token
            if (lexer.Token is MinusToken)
            {
                unary = true;
                lexer.NextToken();
            }

            // Expression
            if (lexer.Token is LRoundBracketToken && !unary)
            {
                lexer.NextToken();
                AddExpression expr = parseExpression()
                                     ?? throw new ParserException("Expected expression in " + lexer.Token.Position);
                accept(typeof(RRoundBracketToken));

                return(new ExpressionExprParam(expr));
            }

            // Identifier or function call
            if (lexer.Token is IdentifierToken)
            {
                string identifier = (lexer.Token as IdentifierToken).Value;
                lexer.NextToken();

                // Try to build function call
                FuncCall funcCall = parseFuncCall(identifier);

                if (funcCall != null)
                {
                    return(new FuncCallExprParam(unary, funcCall));
                }
                else
                {
                    return(new IdentifierExprParam(unary, identifier));
                }
            }

            // Num value
            if (lexer.Token is NumValueToken)
            {
                double value = (lexer.Token as NumValueToken).Value;
                lexer.NextToken();
                return(new NumValueExprParam(unary, value));
            }

            // Str value
            if (lexer.Token is StrValueToken && !unary)
            {
                string value = (lexer.Token as StrValueToken).Value;
                lexer.NextToken();
                return(new StrValueExprParam(value));
            }

            return(null);
        }
Exemplo n.º 3
0
        private AssignmentStatement parseAssign(string identifier)
        {
            if (lexer.Token is AssignmentToken)
            {
                lexer.NextToken();

                AddExpression rightSide = parseExpression()
                                          ?? throw new ParserException("Expected expression in " + lexer.Token.Position);

                return(new AssignmentStatement(identifier, rightSide));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 4
0
        private ReturnStatement parseReturn()
        {
            if (lexer.Token is ReturnToken)
            {
                lexer.NextToken();

                AddExpression expression = parseExpression()
                                           ?? throw new ParserException("Expected expression in " + lexer.Token.Position);

                return(new ReturnStatement(expression));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 5
0
        private RelationalCondition parseRelationalCondition()
        {
            AddExpression firstOperand = parseExpression();

            if (firstOperand != null)
            {
                RelationalCondition relCond = new RelationalCondition();
                relCond.Operands.Add(firstOperand);

                while (lexer.Token is LessThanToken || lexer.Token is LessEqualThanToken ||
                       lexer.Token is GreaterThanToken || lexer.Token is GreaterEqualThanToken)
                {
                    switch (lexer.Token)
                    {
                    case LessThanToken _:
                        relCond.Operators.Add(LessThanToken.Text);
                        break;

                    case LessEqualThanToken _:
                        relCond.Operators.Add(LessEqualThanToken.Text);
                        break;

                    case GreaterThanToken _:
                        relCond.Operators.Add(GreaterThanToken.Text);
                        break;

                    case GreaterEqualThanToken _:
                        relCond.Operators.Add(GreaterEqualThanToken.Text);
                        break;
                    }
                    lexer.NextToken();

                    relCond.Operands.Add(parseExpression());
                }

                return(relCond);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 6
0
        public void Visit(AddExpression addExpr)
        {
            int operandCntr = 1;

            addExpr.Operands[0].Accept(this);

            foreach (string oper in addExpr.Operators)
            {
                switch (oper)
                {
                case "+":
                    Console.Write("+");
                    break;

                case "-":
                    Console.Write("-");
                    break;
                }

                addExpr.Operands[operandCntr++].Accept(this);
            }
        }
Exemplo n.º 7
0
        private RepeatStatement parseRepeat()
        {
            if (lexer.Token is RepeatToken)
            {
                lexer.NextToken();

                accept(typeof(LRoundBracketToken));

                AddExpression numOfTimes = parseExpression()
                                           ?? throw new ParserException("Expected expression in " + lexer.Token.Position);

                accept(typeof(RRoundBracketToken));

                BlockStatement body = parseBlockStatement();

                return(new RepeatStatement(numOfTimes, body));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 8
0
        private INode parseMethCall(string identifier)
        {
            if (lexer.Token is DotToken)
            {
                lexer.NextToken();

                Token  methNameToken = accept(typeof(IdentifierToken));
                string methName      = (methNameToken as IdentifierToken).Value;

                accept(typeof(LRoundBracketToken));

                AddExpression argument = parseExpression();

                accept(typeof(RRoundBracketToken));

                return(new MethCall(identifier, methName, argument));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 9
0
        public void Visit(AddExpression addExpr)
        {
            int operandCntr = 1;

            addExpr.Operands[0].Accept(this);

            foreach (string oper in addExpr.Operators)
            {
                addExpr.Operands[operandCntr++].Accept(this);

                dynamic rightOper = Environment.PopFromTheStack();
                dynamic leftOper  = Environment.PopFromTheStack();

                if (rightOper.GetType() != leftOper.GetType())
                {
                    throw new ExecutorException("Cannot perform an arithmetic operation on operands of different types");
                }
                else if (leftOper is TurtleItem || leftOper is TurtleItem)
                {
                    throw new ExecutorException("Cannot perform an arithmetic operation on operands of type Turtle");
                }
                else if (rightOper is string && leftOper is string && oper == "-")
                {
                    throw new ExecutorException("Cannot substract str value from str value");
                }

                dynamic result = oper switch
                {
                    "+" => leftOper + rightOper,
                    "-" => leftOper - rightOper,
                    _ => throw new ExecutorException($"Unknown additive operator {oper}")
                };

                Environment.PushToTheStack(result);
            }
        }