예제 #1
0
        //Operando da soma/subtracao
        //term : factor((MUL|DIV)factor)*
        private IAST term()
        {
            IAST node = this.factor();

            while (new TokenType[] { TokenType.MUL, TokenType.DIV }.Contains(this._currentToken.Type))
            {
                Token token = this._currentToken;
                if (token.Type == TokenType.MUL)
                {
                    this.eat(TokenType.MUL);
                }
                else if (token.Type == TokenType.DIV)
                {
                    this.eat(TokenType.DIV);
                }
                node = new BinOp(node, token, this.factor());
            }
            return(node);
        }
예제 #2
0
        //Expressao Final
        //expr : term((PLUS|MINUS)term)*
        private IAST expr()
        {
            IAST node = this.term();

            while (new TokenType[] { TokenType.PLUS, TokenType.MINUS }.Contains(this._currentToken.Type))
            {
                Token token = this._currentToken;

                if (token.Type == TokenType.PLUS)
                {
                    this.eat(TokenType.PLUS);
                }
                else if (token.Type == TokenType.MINUS)
                {
                    this.eat(TokenType.MINUS);
                }
                node = new BinOp(node, token, this.term());
            }
            return(node);
        }
예제 #3
0
 public void Visit(BinOp binOp)
 {
     if (binOp.Token.Type == TokenType.PLUS)
     {
         reducedTree = new Num(new Token(TokenType.INTEGER, this.ReduceTree(binOp.Left).Token.Value
                                         + this.ReduceTree(binOp.Rigth).Token.Value));
     }
     else if (binOp.Token.Type == TokenType.MINUS)
     {
         reducedTree = new Num(new Token(TokenType.INTEGER, this.ReduceTree(binOp.Left).Token.Value
                                         - this.ReduceTree(binOp.Rigth).Token.Value));
     }
     else if (binOp.Token.Type == TokenType.MUL)
     {
         reducedTree = new Num(new Token(TokenType.INTEGER, this.ReduceTree(binOp.Left).Token.Value
                                         *this.ReduceTree(binOp.Rigth).Token.Value));
     }
     else if (binOp.Token.Type == TokenType.DIV)
     {
         reducedTree = new Num(new Token(TokenType.INTEGER, this.ReduceTree(binOp.Left).Token.Value
                                         / this.ReduceTree(binOp.Rigth).Token.Value));
     }
 }