t | from re import * | t | from re import * |
| | | |
| | | |
| dh = globals() | | dh = globals() |
| d = {} | | d = {} |
| # ([^0-9A-Za-z_](\d)+[eE][+-]\d) \d[eE][+-]?\d | | # ([^0-9A-Za-z_](\d)+[eE][+-]\d) \d[eE][+-]?\d |
| base_mistake = compile( | | base_mistake = compile( |
| r'/{2,}|\*\*|[a-zA-Z0-9]\w*\(|\.|([^0-9A-Za-z_](\d)+[eE][+-]\d)') | | r'/{2,}|\*\*|[a-zA-Z0-9]\w*\(|\.|([^0-9A-Za-z_](\d)+[eE][+-]\d)') |
| name_for_sub = compile(r"(?<!\w)([a-zA-Z_]\w*)") | | name_for_sub = compile(r"(?<!\w)([a-zA-Z_]\w*)") |
| name_for_match = compile(r"([a-zA-Z_]\w*)\s*=\s*(.*)") | | name_for_match = compile(r"([a-zA-Z_]\w*)\s*=\s*(.*)") |
| splitting_match = compile(r"([a-zA-Z_]\w*)\s*=\s*(.*)") | | splitting_match = compile(r"([a-zA-Z_]\w*)\s*=\s*(.*)") |
| | | |
| | | |
| def analyze(f): | | def analyze(f): |
| if match(r"#", f) is None: | | if match(r"#", f) is None: |
| | | |
| if base_mistake.search(f): | | if base_mistake.search(f): |
| print("Syntax error") | | print("Syntax error") |
| return | | return |
| | | |
| f = sub(r"/", r"//", f) | | f = sub(r"/", r"//", f) |
| # print(f) | | # print(f) |
| f = name_for_sub.sub(r"_c_\1", f) | | f = name_for_sub.sub(r"_c_\1", f) |
| # print(f) | | # print(f) |
| | | |
| if name_for_sub.fullmatch(f): | | if name_for_sub.fullmatch(f): |
| try: | | try: |
| print(eval(f, dh, d)) | | print(eval(f, dh, d)) |
| except NameError: | | except NameError: |
| print("Name error") | | print("Name error") |
| return | | return |
| else: | | else: |
| return | | return |
| | | |
| elif name_for_match.match(f): | | elif name_for_match.match(f): |
| s = splitting_match.match(f) | | s = splitting_match.match(f) |
| left = s.group(1) | | left = s.group(1) |
| right = s.group(2) | | right = s.group(2) |
| #print(left, right) | | #print(left, right) |
| | | |
| try: | | try: |
| right = eval(right, dh, d) | | right = eval(right, dh, d) |
| # print(right) | | # print(right) |
| except NameError: | | except NameError: |
| print("Name error") | | print("Name error") |
| return | | return |
| except SyntaxError: | | except SyntaxError: |
| print("Syntax error") | | print("Syntax error") |
| else: | | else: |
| try: | | try: |
| exec(left + "=" + str(right), dh, d) | | exec(left + "=" + str(right), dh, d) |
| except SyntaxError: | | except SyntaxError: |
| print("Syntax error") | | print("Syntax error") |
| return | | return |
| else: | | else: |
| return | | return |
| | | |
| else: | | else: |
| if search(r"=", f): | | if search(r"=", f): |
| print("Assignment error") | | print("Assignment error") |
| return | | return |
| try: | | try: |
| print(eval(f, dh, d)) | | print(eval(f, dh, d)) |
| except NameError: | | except NameError: |
| print("Name error") | | print("Name error") |
| return | | return |
| except SyntaxError: | | except SyntaxError: |
| print("Syntax error") | | print("Syntax error") |
| return | | return |
| except BaseException: | | except BaseException: |
| print("Runtime error") | | print("Runtime error") |
| return | | return |
| else: | | else: |
| return | | return |
| | | |
| | | |
| a = input() | | a = input() |
| while a: | | while a: |
| analyze(a) | | analyze(a) |
| a = input() | | a = input() |
| | | |