def mod_exp(node): (MOD, c1, c2) = node assert_match(MOD, '%') (t1, v1, p1) = walk(c1) (t2, v2, p2) = walk(c2) type = promote(t1, t2) if type[0] == 'integer': return (type, v1 % v2, p1 + p2) else: raise ValueError('unsupported type {} in % operator'.format(type[0]))
def or_exp(node): (OR, c1, c2) = node assert_match(OR, '||') (t1, v1, p1) = walk(c1) (t2, v2, p2) = walk(c2) type = promote(t1, t2) if type[0] in ['integer', 'double']: return (('integer', ), 1 if v1 != 0 or v2 != 0 else 0, p1 + p2) else: raise ValueError('unsupported type {} in % operator'.format(type[0]))
def times_exp(node): (TIMES, c1, c2) = node assert_match(TIMES, 'times') (t1, v1, p1) = walk(c1) (t2, v2, p2) = walk(c2) type = promote(t1, t2) if type[0] in ['char', 'integer', 'double']: return (type, v1 * v2, p1 + p2) else: raise ValueError('unsupported type {} in * operator'.format(type[0]))
def ne_exp(node): (NE, c1, c2) = node assert_match(NE, '!=') (t1, v1, p1) = walk(c1) (t2, v2, p2) = walk(c2) type = promote(t1, t2) if type[0] in ['char', 'integer', 'double']: return (('integer', ), 1 if v1 != v2 else 0, p1 + p2) else: raise ValueError('unsupported type {} in / operator'.format(type[0]))
def and_exp(node): (AND, c1, c2) = node assert_match(AND, '&&') (t1, v1) = walk(c1) (t2, v2) = walk(c2) type = promote(t1, t2) if type[0] in ['integer', 'double']: return (('integer', ), 1 if v1 != 0 and v2 != 0 else 0) else: raise ValueError('unsupported type {} in % operator'.format(type[0]))
def gt_exp(node): (GT, c1, c2) = node assert_match(GT, '>') (t1, v1) = walk(c1) (t2, v2) = walk(c2) type = promote(t1, t2) if type[0] in ['integer', 'double']: return (('integer', ), 1 if v1 > v2 else 0) else: raise ValueError('unsupported type {} in / operator'.format(type[0]))
def minus_exp(node): (MINUS, c1, c2) = node assert_match(MINUS, 'minus') (t1, v1) = walk(c1) (t2, v2) = walk(c2) type = promote(t1, t2) if type[0] in ['integer', 'double']: return (type, v1 - v2) else: raise ValueError('unsupported type {} in - operator'.format(type[0]))
def divide_exp(node): (DIVIDE, c1, c2) = node assert_match(DIVIDE, 'divide') (t1, v1, p1) = walk(c1) (t2, v2, p2) = walk(c2) type = promote(t1, t2) if type[0] in ['char', 'integer', 'double']: return (('integer', ), int(v1 / v2), p1) if (v1 / v2).is_integer() else (('double', ), v1 / v2, p1 + p2) else: raise ValueError('unsupported type {} in / operator'.format(type[0]))
def plus_exp(node): (PLUS, c1, c2) = node assert_match(PLUS, 'plus') (t1, v1) = walk(c1) (t2, v2) = walk(c2) type = promote(t1, t2) if type[0] in ['integer', 'double']: return (type, v1 + v2) elif type[0] == 'string': return (('string', ), str(v1) + str(v2)) else: raise ValueError('unsupported type {} in + operator'.format(type[0]))