コード例 #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
ファイル: Parser.cs プロジェクト: AntonyBaasan/lsbasi-csharp
        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);