def do_or_xor(ctx: Context, prim, args, annots): a, b = ctx.pop2() val_type, res_type = dispatch_type_map(a, b, { (Bool, Bool): (bool, Bool), (Nat, Nat): (int, Nat), }) handlers = {'OR': lambda x: x[0] | x[1], 'XOR': lambda x: x[0] ^ x[1]} res = res_type(handlers[prim]((val_type(a), val_type(b)))) ctx.push(res, annots=annots)
def do_and(ctx: Context, prim, args, annots): a, b = ctx.pop2() val_type, res_type = dispatch_type_map( a, b, { (Bool, Bool): (bool, Bool), (Nat, Nat): (int, Nat), (Int, Nat): (int, Nat), (Nat, Int): (int, Nat) }) res = res_type(val_type(a) & val_type(b)) ctx.push(res, annots=annots)
def do_mul(ctx: Context, prim, args, annots): a, b = ctx.pop2() res_type = dispatch_type_map( a, b, { (Nat, Nat): Nat, (Nat, Int): Int, (Int, Nat): Int, (Int, Int): Int, (Mutez, Nat): Mutez, (Nat, Mutez): Mutez }) res = res_type(int(a) * int(b)) ctx.push(res, annots=annots)
def do_add(ctx: Context, prim, args, annots): a, b = ctx.pop2() res_type = dispatch_type_map( a, b, { (Nat, Nat): Nat, (Nat, Int): Int, (Int, Nat): Int, (Int, Int): Int, (Timestamp, Int): Timestamp, (Int, Timestamp): Timestamp, (Mutez, Mutez): Mutez }) res = res_type(int(a) + int(b)) ctx.push(res, annots=annots)
def do_concat(ctx: Context, prim, args, annots): top = ctx.pop1() assert_stack_type(top, [String, Bytes, List]) if type(top) in [String, Bytes]: second = ctx.pop1() val_type = dispatch_type_map(top, second, { (String, String): str, (Bytes, Bytes): bytes }) res = type(top)(val_type(top) + val_type(second)) elif type(top) == List: res_type = top.val_type() val_type, sep = {String: (str, ''), Bytes: (bytes, b'')}[res_type] res = res_type(sep.join(map(val_type, top))) else: assert False ctx.push(res, annots=annots)
def do_ediv(ctx: Context, prim, args, annots): a, b = ctx.pop2() q_type, r_type = dispatch_type_map( a, b, { (Nat, Nat): (Nat, Nat), (Nat, Int): (Int, Nat), (Int, Nat): (Int, Nat), (Int, Int): (Int, Nat), (Mutez, Nat): (Mutez, Mutez), (Mutez, Mutez): (Nat, Mutez) }) if int(b) == 0: res = Option.none(Pair.new(q_type(), r_type()).type_expr) else: q, r = divmod(int(a), int(b)) if r < 0: r += abs(int(b)) q += 1 res = Option.some(Pair.new(q_type(q), r_type(r))) ctx.push(res, annots=annots)