Ejemplo n.º 1
0
def expression():
    """
    Parses
        expression = simpleExpression
                     {("=" | "<>" | "<" | "<=" | ">" | ">=") simpleExpression}.
    Generates code for the expression if no error is reported
    """
    x = simpleExpression()
    while SC.sym in {EQ, NE, LT, LE, GT, GE}:
        op = SC.sym; getSym(); y = simpleExpression() # x op y
        if x.tp == Int == y.tp:
		#============== ADDED CONSTANT FOLDING =============
			if type(x) == Const == type(y):					#
				if op == EQ and x.val != y.val:				#
					x = genSkip()							#	
				elif op == NE and x.val == y.val:			#
					x = genSkip()							#
				elif op == LT and x.val >= y.val:			#
					x = genSkip()							#
				elif op == LE and x.val > y.val:			#
					x = genSkip()							#
				elif op == GT and x.val <= y.val:			#
					x = genSkip()							#
				elif op == GE and x.val < y.val:			#
					x = genSkip()							#
			else:
            	x = genRelation(op, x, y)
        else: mark('bad type')
Ejemplo n.º 2
0
def expression():
    """
    Parses
        expression = simpleExpression
                     {("=" | "<>" | "<" | "<=" | ">" | ">=") simpleExpression}.
    Generates code for the expression if no error is reported
    """
    x = simpleExpression()
    while SC.sym in {EQ, NE, LT, LE, GT, GE}:
        op = SC.sym
        getSym()
        y = simpleExpression()  # x op y
        if x.tp == Int == y.tp:
            x = genRelation(op, x, y)
        else:
            mark("bad type")
    return x