#!/usr/bin/env python
# coding: Shift_JIS

#from parse import *
import re
import sys

swvars = []
call_depth = -1

def Switch2If(lines):

    global call_depth
    global swvars
    
    call_depth +=1
    KAKKO = 0
    caseval = 0
    casevals = []
    swnest = 0
    casest = []
    casests = []
    swflg = False
    caseflg = False
    st = []
    joken_sts = []
    newjoken_sts= []
    result_lines = []
    st_order = []

    
    for i, line in enumerate(lines):
        if re.match("case[ \t]+[0-9]+[ \t]*:",line):
            line = line.replace(':', ' :')
            while '\t' in line: line = line.replace('\t', ' ')
            while '  ' in line: line = line.replace('  ', ' ')
            line = line.replace('case ','case(')
            line = line.replace(' :',') :')
            while '  ' in line: line = line.replace('  ', ' ')
            lines[i] = line
            continue


    #for i, line in enumerate(lines):
    i = 0
    while i < len(lines):
        line = lines[i]
        if re.match("switch[ \t]*\(",line) and swflg==False:
            #print ("@@@SWITCH_STATEMENT",line)
            swflg = True
            index1 = line.find('(')+1
            index2 = line.find(')')
            swvar = line[index1:index2]
            swvars.append(swvar)
            swnest = KAKKO

        if swflg == False:
            #print ("@@@NORMAL_STATEMENT",line)
            st.append(line)

        if caseflg == True:
            #print ("@@@CASE_ST",line)
            if i != len(lines)-1:
                if swnest + 1 == KAKKO and (re.match("case[ \t]*\(.+:",line) or re.match("default[ \t]*",line)):  
                    casests.append(casest)
                else:
                    casest.append(line)

            
        if re.match("{",line):
            #print ("@@@HIRAKI")
            KAKKO += 1
        if re.match("}",line):
            #print ("@@@TOJI")
            KAKKO -= 1
            if swflg == True and caseflg == False:
                return ["@@@NOCASE@@@"]
            if swflg == True and swnest == KAKKO:
                if caseflg == True:
                    casest = casest[:-1]
                    #print ("CASEST",casest)
                    caseJoken = "@@@SWEND@@@" + caseJoken
                    joken_sts.append((caseJoken,casest))
                    casest = []
                else:
                    #print ("ST",st)
                    joken_sts.append('MJK',st)
                    st = []
                swflg = False
                caseflg = False
                #print ("@@@SWITCH_END")

        if i == len(lines)-1:
            #print ("ST",st)
            joken_sts.append(('MJK',st))
            #print ('@@@EOF')
            
        if re.match("case[ \t]*\(.+:",line) and swnest + 1 == KAKKO:
            if caseflg == True:
                joken_sts.append((caseJoken,casest))
                casest = []
            else:
                joken_sts.append(('MJK',st))
                st = []
            index1 = line.find('(')+1
            index2 = line.find(')')
            caseval =  line[index1:index2]
            casevals.append(caseval)
            caseJoken = swvar + "==" + caseval
            caseflg = True
            #print ("@@@CASE",caseJoken,line)


        if re.match("default[ \t]*",line) and swnest + 1 == KAKKO:
            if caseflg == True:
                joken_sts.append((caseJoken,casest))
                casest = []
            else:
                joken_sts.append(('MJK',st))
                st = []
            #print ("@@@DEFAULT",line)
            caseval = "ELSE"
            casevals.append(caseval)
            caseJoken = caseval
            caseflg = True
            casevals.append("ELSE")
        i += 1

    #print ("DEPTH",call_depth)
    #for js in joken_sts:
    #    print (js)
    
    for js in joken_sts:
        if any(re.match("switch[ \t]*\(",st) for st in js[1]) == True:
            #print ("@@@CALL_SWITCH2IF",call_depth,js)
            recur = Switch2If(js[1])
            js = (js[0],recur)
        newjoken_sts.append(js)

    #for njs in newjoken_sts:
    #    print (njs)
                
    newjoken_sts.reverse()
    for i,njs in enumerate(newjoken_sts):
        joken = njs[0]
        st = njs[1]
        #print ("JOKEN,ST",joken,st)
        if len(st)==0:
            under_st = newjoken_sts[i-1][1]
            newjoken_sts[i][1].extend(under_st)
            continue
        if not(re.match("break;",st[-1])) and joken != "MJK" and joken != "ELSE" and not("@@@SWEND@@@" in joken) and i>0:
            under_st = newjoken_sts[i-1][1]
        else:
            under_st = []
        if re.match("break;",st[-1]):
            newjoken_sts[i] =  (joken,st[:-1])
            #1つ前のブロックの末尾にbreak文がある場合は，取り除く
            #if re.match("break;",under_st[-1]):
            #    under_st = under_st[:-1]
        newjoken_sts[i][1].extend(under_st)
    newjoken_sts.reverse()

    #for njs in newjoken_sts:
    #    print (njs)
    
    ifflg = False
    for njs in newjoken_sts:
        joken = njs[0]
        st = njs[1]
        if joken == "MJK":
            result_line = st
        else:
            tmp_for_ifflg = joken
            if ("@@@SWEND@@@" in tmp_for_ifflg):
                joken = joken.replace("@@@SWEND@@@","")
            if ("==" in joken and ifflg == False):
                result_line = ["if("+ joken + ")\n", "{\n"]
                ifflg=True
            elif ("==" in joken and ifflg == True):
                result_line = ["else if(" + joken + ")\n","{\n"]
            else:
                result_line = ["else\n","{\n"]
            result_line.extend(st)
            result_line.append("}\n")
            if ("@@@SWEND@@@" in tmp_for_ifflg):
                #print ("SWEND",result_line)
                #result_line.append("}\n")
                ifflg = False
        result_lines.extend(result_line)
        
    #print ("@@@RETURN_SWITCH2IF",call_depth)
    call_depth -= 1
    return result_lines

def presw(workFile1,workFile2):

    fi = open(workFile1)
    lines = fi.readlines()
    fi.close()

    fo = open(workFile2,'w')
    newlines = Switch2If(lines)
    i = 0
    for newline in newlines:
        i += 1
        print (newline,end="",file=fo)
        if "@@@NOCASE@@@" in newline:
            return False
    #    print (newline)
    fo.close()
    return True
    
if __name__ == '__main__':
    presw(sys.argv[1],sys.argv[2])

                                 
