Esempio n. 1
0
        private Expression ParseTerm()
        {
            Token      firstTermToken = CurrentToken;
            Expression left           = ParseFactor();

            while (Accept(TokenType.OpMultiply, TokenType.OpDivide, TokenType.OpModulus, TokenType.OpAnd))
            {
                BinaryExpr expr = new BinaryExpr(firstTermToken);
                expr.SetOperatorFromToken(AcceptedToken);
                expr.Left  = left;
                expr.Right = ParseFactor();
                left       = expr;
            }
            return(left);
        }
Esempio n. 2
0
        private Expression ParseExpression()
        {
            Token      firstTermToken = CurrentToken;
            Expression left           = ParseSimpleExpression();

            if (Accept(TokenType.OpLess, TokenType.OpLessOrEquals, TokenType.OpMore,
                       TokenType.OpMoreOrEquals, TokenType.OpEquals, TokenType.OpNotEquals))
            {
                BinaryExpr expr = new BinaryExpr(firstTermToken);
                expr.Left = left;
                expr.SetOperatorFromToken(AcceptedToken);
                expr.Right = ParseSimpleExpression();
                return(expr);
            }
            return(left);
        }
Esempio n. 3
0
        private void Visit(BinaryExpr expr)
        {
            var il         = CurrentMethodIL;
            var commonType = typeChecker.FindCommonType(expr.Left.Type, expr.Right.Type);

            Visit((dynamic)expr.Left);
            if (!expr.Left.Type.SameAs(commonType))
            {
                il.Emit(commonType.GetCILConvertOp());
            }
            Visit((dynamic)expr.Right);
            if (!expr.Right.Type.SameAs(commonType))
            {
                il.Emit(commonType.GetCILConvertOp());
            }
            CILHelper.EmitExprOperation(il, expr.Op, expr.Left.Type);
            ApplyExprSign(expr);
        }
Esempio n. 4
0
        private Expression ParseSimpleExpression()
        {
            Token    firstTermToken = CurrentToken;
            ExprSign termSign       = ExprSign.Plus;

            if (Accept(TokenType.Minus, TokenType.Plus))
            {
                termSign = AcceptedToken.Type == TokenType.Minus ? ExprSign.Minus : ExprSign.Plus;
            }
            Expression left = ParseTerm();

            left.Sign = termSign;
            while (Accept(TokenType.Plus, TokenType.Minus, TokenType.OpOr))
            {
                BinaryExpr expr = new BinaryExpr(firstTermToken);
                expr.SetOperatorFromToken(AcceptedToken);
                expr.Left  = left;
                expr.Right = ParseTerm();
                left       = expr;
            }
            return(left);
        }
Esempio n. 5
0
        private void Visit(BinaryExpr binaryExpr)
        {
            Visit((dynamic)binaryExpr.Left);
            Visit((dynamic)binaryExpr.Right);
            TypeInfo type1 = TypeStack.Pop();
            TypeInfo type2 = TypeStack.Pop();

            if (IsNotVoid(type1) && IsNotVoid(type2))
            {
                TypeInfo opType = TypeChecker.FindOpProductionType(type1, type2, binaryExpr.Op);
                if (opType.SameAs(TypeInfo.BasicVoid))
                {
                    AddError(string.Format("Can't apply operator {0} on types {1} and {2}", binaryExpr.Op, type1, type2), binaryExpr);
                }
                TypeStack.Push(opType);
                binaryExpr.Type = opType;
            }
            else
            {
                TypeStack.Push(TypeInfo.BasicVoid);
            }
        }