from c_int import Int
from casting import cast
from globals_consts import NAMESPACE
from temps import used_temps, get_temp, get_temp_func


def binary_expression(copy_strings, expression, target, variables_name, vtypes):
    from expression import generate_expression
    c1, t1, tt1 = generate_expression(None, expression.left, vtypes, variables_name, copy_strings, False)
    c2, t2, tt2 = generate_expression(None, expression.right, vtypes, variables_name, copy_strings, False)
    for ttt in tt1: used_temps.remove(ttt)
    for ttt in tt2: used_temps.remove(ttt)
    ot = cast(t1, t2)
    rt = ot
    if expression.op in ['<', '>', '<=', '>=', '==', '!=', '&&']:
        rt = Int()
    if target is None or target == []:
        target = [get_temp() for _ in range(ot.size)]
        used_temps.extend(target)
    code = ''
    if expression.op in ['&&', '||']:
        if expression.op == '&&':
            code += c1
            code += t1.cast(ot, tt1, target)
            f2 = get_temp_func()
            f2h = open(f'{f2}.mcfunction', 'w')
            f2h.write(c2)
            f2h.write(t2.cast(ot, tt2, target))
            f2h.close()
            code += f'execute unless score {target[0]} {NAMESPACE} matches 0 run function {NAMESPACE}:{f2}\n'
        elif expression.op == '||':
            code += c1
            code += t1.cast(ot, tt1, target)
            f2 = get_temp_func()
            f2h = open(f'{f2}.mcfunction', 'w')
            f2h.write(c2)
            f2h.write(t2.cast(ot, tt2, target))
            f2h.close()
            code += f'execute if score {target[0]} {NAMESPACE} matches 0 run function {NAMESPACE}:{f2}\n'
    else:
        if ot == t1:
            code += c1
            code += c2
            code += t2.cast(ot, tt2, target)
            code += ot.binary(expression.op, tt1, target, target)
        else:
            code += c1
            code += t1.cast(ot, tt1, target)
            code += c2
            code += ot.binary(expression.op, target, tt2, target)
    return code, rt, target
[((9, 18, 9, 105), 'expression.generate_expression', 'generate_expression', ({(9, 38, 9, 42): 'None', (9, 44, 9, 59): 'expression.left', (9, 61, 9, 67): 'vtypes', (9, 69, 9, 83): 'variables_name', (9, 85, 9, 97): 'copy_strings', (9, 99, 9, 104): 'False'}, {}), '(None, expression.left, vtypes, variables_name,\n    copy_strings, False)', False, 'from expression import generate_expression\n'), ((10, 18, 10, 106), 'expression.generate_expression', 'generate_expression', ({(10, 38, 10, 42): 'None', (10, 44, 10, 60): 'expression.right', (10, 62, 10, 68): 'vtypes', (10, 70, 10, 84): 'variables_name', (10, 86, 10, 98): 'copy_strings', (10, 100, 10, 105): 'False'}, {}), '(None, expression.right, vtypes, variables_name,\n    copy_strings, False)', False, 'from expression import generate_expression\n'), ((13, 9, 13, 21), 'casting.cast', 'cast', ({(13, 14, 13, 16): 't1', (13, 18, 13, 20): 't2'}, {}), '(t1, t2)', False, 'from casting import cast\n'), ((11, 20, 11, 42), 'temps.used_temps.remove', 'used_temps.remove', ({(11, 38, 11, 41): 'ttt'}, {}), '(ttt)', False, 'from temps import used_temps, get_temp, get_temp_func\n'), ((12, 20, 12, 42), 'temps.used_temps.remove', 'used_temps.remove', ({(12, 38, 12, 41): 'ttt'}, {}), '(ttt)', False, 'from temps import used_temps, get_temp, get_temp_func\n'), ((16, 13, 16, 18), 'c_int.Int', 'Int', ({}, {}), '()', False, 'from c_int import Int\n'), ((19, 8, 19, 33), 'temps.used_temps.extend', 'used_temps.extend', ({(19, 26, 19, 32): 'target'}, {}), '(target)', False, 'from temps import used_temps, get_temp, get_temp_func\n'), ((18, 18, 18, 28), 'temps.get_temp', 'get_temp', ({}, {}), '()', False, 'from temps import used_temps, get_temp, get_temp_func\n'), ((25, 17, 25, 32), 'temps.get_temp_func', 'get_temp_func', ({}, {}), '()', False, 'from temps import used_temps, get_temp, get_temp_func\n'), ((34, 17, 34, 32), 'temps.get_temp_func', 'get_temp_func', ({}, {}), '()', False, 'from temps import used_temps, get_temp, get_temp_func\n')]