Esempio n. 1
0
        private NumericalNode Term()
        {
            NumericalNode result = Factor();
            Token         token;

            while (CurrentToken.Type != TokenType.EOF)
            {
                token = CurrentToken;
                if (CurrentToken.Type == TokenType.TIMES)
                {
                    Eat(TokenType.TIMES);
                }
                else if (CurrentToken.Type == TokenType.INTEGER_DIV)
                {
                    Eat(TokenType.INTEGER_DIV);
                }
                else if (CurrentToken.Type == TokenType.FLOAT_DIV)
                {
                    Eat(TokenType.FLOAT_DIV);
                }
                else
                {
                    break;
                }
                result = new BinaryOperator(result, token, Factor());
            }
            return(result);
        }
Esempio n. 2
0
        public dynamic VisitBinaryOperator(NumericalNode node)
        {
            dynamic result;
            var     TmpNode = node as BinaryOperator;

            switch (TmpNode.Op.Type)
            {
            case TokenType.ADD:
                result = Visit(TmpNode.Left) + Visit(TmpNode.Right);
                break;

            case TokenType.SUBTRACT:
                result = Visit(TmpNode.Left) - Visit(TmpNode.Right);
                break;

            case TokenType.TIMES:
                result = Visit(TmpNode.Left) * Visit(TmpNode.Right);
                break;

            case TokenType.INTEGER_DIV:
                result = Visit(TmpNode.Left) / Visit(TmpNode.Right);
                break;

            case TokenType.FLOAT_DIV:
                result = Visit(TmpNode.Left) / Visit(TmpNode.Right);
                break;

            default:
                throw new Exception("Unrecognised op");
            }

            return(result);
        }
Esempio n. 3
0
        public dynamic Visit(NumericalNode node)
        {
            var nodeName      = node.GetType().Name;
            var visitorMethod = this.GetType().GetMethod("Visit" + nodeName);

            return(visitorMethod.Invoke(this, new object[] { node }));
        }
Esempio n. 4
0
        public dynamic VisitNum(NumericalNode node)
        {
            var num = node as Num;

            if (num.Value.Value.Contains('.'))
            {
                return(float.Parse(num.Value.Value));
            }
            return(int.Parse((node as Num).Value.Value));
        }
Esempio n. 5
0
        public int VisitUnaryOperator(NumericalNode node)
        {
            var tmpNode = node as UnaryOperator;

            if (tmpNode.Op.Type == TokenType.ADD)
            {
                return(Visit(tmpNode.Node));
            }
            else if (tmpNode.Op.Type == TokenType.SUBTRACT)
            {
                return(-Visit(tmpNode.Node));
            }
            else
            {
                throw new Exception("Runtime Error: Could not interpret unary operator");
            }
        }
Esempio n. 6
0
        private NumericalNode Expr()
        {
            NumericalNode result = Term();
            Token         token;

            while (CurrentToken.Type == TokenType.ADD || CurrentToken.Type == TokenType.SUBTRACT)
            {
                token = CurrentToken;
                if (CurrentToken.Type == TokenType.ADD)
                {
                    Eat(TokenType.ADD);
                }
                else if (CurrentToken.Type == TokenType.SUBTRACT)
                {
                    Eat(TokenType.SUBTRACT);
                }
                result = new BinaryOperator(result, token, Term());
            }
            return(result);
        }
Esempio n. 7
0
        public int VisitVariable(NumericalNode node)
        {
            var varNode = node as Variable;

            return(GlobalScope[varNode.Value.Value]);
        }
Esempio n. 8
0
 public Assignment(ASTNode Left, Token Op, NumericalNode Right)
 {
     this.Left  = Left;
     this.Right = Right;
     this.Op    = Op;
 }
Esempio n. 9
0
 public UnaryOperator(NumericalNode node, Token op)
 {
     Node = node;
     Op   = op;
 }
Esempio n. 10
0
 public BinaryOperator(NumericalNode Left, Token Op, NumericalNode Right)
 {
     this.Left  = Left;
     this.Right = Right;
     this.Op    = Op;
 }