예제 #1
0
        private AST Term()
        {
            var node = Factor();

            while (currentToken.TokenType == TokenType.MUL ||
                   currentToken.TokenType == TokenType.INTEGER_DIV ||
                   currentToken.TokenType == TokenType.FLOAT_DIV)
            {
                var token = currentToken;
                if (token.TokenType == TokenType.MUL)
                {
                    Eat(TokenType.MUL);
                }
                else if (token.TokenType == TokenType.INTEGER_DIV)
                {
                    Eat(TokenType.INTEGER_DIV);
                }
                else if (token.TokenType == TokenType.FLOAT_DIV)
                {
                    Eat(TokenType.FLOAT_DIV);
                }

                node = new BinOp(left: node, op: token, right: Factor());
            }

            return(node);
        }
예제 #2
0
        public override int VisitBinOp(BinOp node)
        {
            if (node.op.TokenType == TokenType.PLUS)
            {
                return(Visit(node.left) + Visit(node.right));
            }
            if (node.op.TokenType == TokenType.MINUS)
            {
                return(Visit(node.left) - Visit(node.right));
            }
            if (node.op.TokenType == TokenType.MUL)
            {
                return(Visit(node.left) * Visit(node.right));
            }
            if (node.op.TokenType == TokenType.DIV)
            {
                return(Visit(node.left) / Visit(node.right));
            }

            throw new Exception("Unknown node!");
        }
예제 #3
0
        public AST Expr()
        {
            var node = Term();

            while (currentToken.TokenType == TokenType.PLUS || currentToken.TokenType == TokenType.MINUS)
            {
                var token = currentToken;
                if (token.TokenType == TokenType.PLUS)
                {
                    Eat(TokenType.PLUS);
                }
                if (token.TokenType == TokenType.MINUS)
                {
                    Eat(TokenType.MINUS);
                }

                node = new BinOp(left: node, op: token, right: Term());
            }

            return(node);
        }
예제 #4
0
 public override object VisitBinOp(BinOp node)
 {
     if (node.op.TokenType == TokenType.PLUS)
     {
         return((double)Visit(node.left) + (double)Visit(node.right));
     }
     if (node.op.TokenType == TokenType.MINUS)
     {
         return((double)Visit(node.left) - (double)Visit(node.right));
     }
     if (node.op.TokenType == TokenType.MUL)
     {
         return((double)Visit(node.left) * (double)Visit(node.right));
     }
     if (node.op.TokenType == TokenType.INTEGER_DIV)
     {
         return(double.Parse(Visit(node.left).ToString()) / double.Parse(Visit(node.right).ToString()));
     }
     if (node.op.TokenType == TokenType.FLOAT_DIV)
     {
         return((double)Visit(node.left) / (double)Visit(node.right));
     }
     throw new Exception("Unknown node!");
 }
예제 #5
0
 public abstract int VisitBinOp(BinOp op);
예제 #6
0
 public abstract object VisitBinOp(BinOp op);