def plus_exp(node): (PLUS,c1,c2) = node t1 = walk(c1) t2 = walk(c2) return promote(t1,t2)
def mul_exp(node): (MUL, c1, c2) = node (t1, v1) = walk(c1) (t2, v2) = walk(c2) t = promote(t1, t2) return (t, coerce(t, t1)(v1) * coerce(t, t2)(v2))
def minus_exp(node): (MINUS, c1, c2) = node (t1, v1) = walk(c1) (t2, v2) = walk(c2) t = promote(t1, t2) return (t, coerce(t, t1)(v1) - coerce(t, t2)(v2))
def plus_exp(node): (PLUS, c1, c2) = node (t1, v1) = walk(c1) (t2, v2) = walk(c2) t = promote(t1, t2) return (t, coerce(t, t1)(v1) + coerce(t, t2)(v2))
def le_exp(node): (LE,c1,c2) = node t1 = walk(c1) t2 = walk(c2) if promote(t1,t2)[0] == 'VOID_TYPE': raise ValueError("Illegal type in relational operator.") return ('INTEGER_TYPE',)
def div_exp(node): (DIV,c1,c2) = node t1 = walk(c1) t2 = walk(c2) tr = promote(t1,t2) if tr[0] not in ['INTEGER_TYPE','FLOAT_TYPE']: raise ValueError("operation on type {} not supported" .format(tr[0])) return tr
def le_exp(node): (LE, c1, c2) = node (t1, v1) = walk(c1) (t2, v2) = walk(c2) t = promote(t1, t2) if coerce(t, t1)(v1) <= coerce(t, t2)(v2): return ('INTEGER_TYPE', 1) else: return ('INTEGER_TYPE', 0)
def div_exp(node): (DIV, c1, c2) = node (t1, v1) = walk(c1) (t2, v2) = walk(c2) t = promote(t1, t2) if t == 'INTEGER_TYPE': return ('INTEGER_TYPE', v1 // v2) else: return (t, coerce(t, t1)(v1) / coerce(t, t2)(v2))