public void Visit(RelationalCondition relCond)
        {
            int operandCntr = 1;

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

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

                case "<=":
                    Console.Write("<=");
                    break;

                case ">":
                    Console.Write(">");
                    break;

                case ">=":
                    Console.Write(">=");
                    break;
                }

                relCond.Operands[operandCntr++].Accept(this);
            }
        }
Example #2
0
        private EqualCondition parseCondition()
        {
            RelationalCondition firstOperand = parseRelationalCondition();

            if (firstOperand != null)
            {
                EqualCondition equalCondition = new EqualCondition();
                equalCondition.Operands.Add(firstOperand);

                while (lexer.Token is EqualToken || lexer.Token is NotEqualToken)
                {
                    switch (lexer.Token)
                    {
                    case EqualToken _:
                        equalCondition.Operators.Add(EqualToken.Text);
                        break;

                    case NotEqualToken _:
                        equalCondition.Operators.Add(NotEqualToken.Text);
                        break;
                    }
                    lexer.NextToken();

                    equalCondition.Operands.Add(parseRelationalCondition());
                }

                return(equalCondition);
            }
            else
            {
                return(null);
            }
        }
Example #3
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);
            }
        }
Example #4
0
        public void Visit(RelationalCondition relCond)
        {
            int operandCntr = 1;

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

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

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

                if (rightOper.GetType() != leftOper.GetType())
                {
                    throw new ExecutorException("Cannot perform relational condition on operands different than double");
                }
                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 double) && !(leftOper is double))
                {
                    throw new ExecutorException("Cannot apply relational condition on types different than num");
                }

                dynamic result = oper switch
                {
                    "<" => leftOper <rightOper?true : false,
                                     "<=" => leftOper <= rightOper?true : false,
                                     ">" => leftOper> rightOper ? true : false,
                    ">=" => leftOper >= rightOper ? true : false,
                    _ => throw new ExecutorException($"Unknown relational operator {oper}")
                };

                Environment.PushToTheStack(result);
            }
        }