TYPES = ('half', 'single')
TEST_TYPES = ("add", "sub", "div", "mul", "fma", "mad", "prn")
ROUND_TYPES = ("to_zero", "to_nearest_even", "to_positive_infinity", "to_negative_infinity")

fds = {
    a: {
        b: {
            c: open(f'out/{a}/{b}/{c}.tsv', 'w')
            for c in ROUND_TYPES
        }
        for b in TEST_TYPES
    }
    for a in TYPES
}

try:
    with open('all_u.tsv') as f:
        for line in f:
            q = line.split('\t')[0].split()
            ft = 'half' if q[0] == 'h' else 'single'
            rt = ''
            op = 'prn' if len(q) == 3 else q[2]
            match q[1]:
                case '0':
                    rt = 'to_zero'
                case '1':
                    rt = 'to_nearest_even'
                case '2':
                    rt = 'to_positive_infinity'
                case '3':
                    rt = 'to_negative_infinity'
            match op:
                case '+': op = 'add'
                case '-': op = 'sub'
                case '*': op = 'mul'
                case '/': op = 'div'
            fds[ft][op][rt].write(line)
finally:
    for a in fds:
        for b in fds[a]:
            for c in fds[a][b]:
                fds[a][b][c].close()
