import process
import keywords
import variable

statement = ""

scope     = {}
def parse(textline):
    text = textline.strip()
    if text == "":
        return

    if text[0] == '(':
        parse_process(text)

    else:
        parse_value(text)




def parse_process(text):
    left = 0;right = 0;
    try:
        parse_bracket(text, text, left, right)
    except Exception:
        global statement
        statement = ""
        print("kuohaobupipei")


    parse_statement(statement)






def parse_value(line):
    Number = process.get_words_number(line)
    Array = get_value_words_array(line)



    for i in range(0,len(Array)):
        print_value(Array[i])




def print_value(key):
    if key[0] == '(':
        parse_process(key)
        return

    if key == "":
        print("error:空")
        return
    try:

        print (str(eval(key)))
    except:
        try:
            print(keywords.keyword_[key])
        except:
            try:
                print(variable.variable_[key])
            except:
                print("error: 未定义标识符")

def parse_bracket(line,text,left,right):
    for i in range(0,len(line)):
        if line[i]== '(':
            left = left + 1
        if line[i] == ')':
            right = right + 1
        if right > left:
            raise Exception("error: kuo hao bu pi pe")

    judge_brackets_number(text, left, right)






def judge_brackets_number(text,left,right):
    if left > right:
        parse_process_nextline(text,left,right)
    elif left == right:
        global statement
        statement = text
    else:
        raise  Exception("error: kuo hao bu pi pe")





def parse_process_nextline(text,left,right):
    nextline = input('...')
    parse_bracket(nextline, text + " " +nextline, left, right)






def parse_statement(statement_):
    if statement_ == "":
        return
    left = 0; right = 0;
    for i in range(0,len(statement_)):
        if statement_[i] == '(':
            left = left + 1
        elif statement_[i] == ')':
            right = right + 1

    if left == 1 and right == 1:
        parse_one_process(statement_)
        if process.is_value:
            print(process.statement_)

    else:
        for i in range(0, len(statement_)):
            if statement_[i] == '(':
                position_left = i
            if statement_[i] == ')':
                position_right= i
                break

        tempstatement_left = statement_[:position_left]
        tempstatement_right= statement_[position_right + 1:]

        parse_one_process(statement_[position_left :position_right  + 1])
        parse_statement(tempstatement_left +" " + process.statement_ + " " + tempstatement_right)






def parse_one_process(text):
    argumentNumber = process.get_words_number(text[1:-1])
    argumentArray = process.get_words_array(text[1:-1])

    process.DoProcess(argumentArray,text[1:-1])






def get_value_words_array(line):
    positon = 0
    arr = []
    for i in range(0,len(line)):
        if line[i] == ',':
            arr.append(line[positon:i].strip())
            positon = i + 1

    arr.append(line[positon:].strip())

    return  arr




def import_python():
    text = ""
    while True:
        temptext = input("...")
        if temptext.strip() == "#end":
            exec (text)
            break
        text += "\n" + temptext





def is_keyword(word):
    try:
        keywords.keyword_[word]
    except:
        return False

    return True

def is_variable(word):
    try:
        variable.variable_[word]
    except:
        return False

    return True

def main():

    while True:
        textline = input('>')
        if textline.strip() == "#import python":
            import_python()
            continue
        parse(textline)



if __name__ == "__main__":
    main()
