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, 'expression.generate_expression', 'generate_expression', False, 'from expression import generate_expression\n'), (10, 'expression.generate_expression', 'generate_expression', False, 'from expression import generate_expression\n'), (13, 'casting.cast', 'cast', False, 'from casting import cast\n'), (11, 'temps.used_temps.remove', 'used_temps.remove', False, 'from temps import used_temps, get_temp, get_temp_func\n'), (12, 'temps.used_temps.remove', 'used_temps.remove', False, 'from temps import used_temps, get_temp, get_temp_func\n'), (16, 'c_int.Int', 'Int', False, 'from c_int import Int\n'), (19, 'temps.used_temps.extend', 'used_temps.extend', False, 'from temps import used_temps, get_temp, get_temp_func\n'), (18, 'temps.get_temp', 'get_temp', False, 'from temps import used_temps, get_temp, get_temp_func\n'), (25, 'temps.get_temp_func', 'get_temp_func', False, 'from temps import used_temps, get_temp, get_temp_func\n'), (34, 'temps.get_temp_func', 'get_temp_func', False, 'from temps import used_temps, get_temp, get_temp_func\n')]