From 5c869516e2edb6d7e0297eb7326813ac5a8b4bd9 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Thu, 9 Mar 2023 22:13:18 -0500 Subject: [PATCH] Upgrade the REPL UI --- silly_thing.py | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/silly_thing.py b/silly_thing.py index 1e3528b..0c57ded 100644 --- a/silly_thing.py +++ b/silly_thing.py @@ -2,8 +2,8 @@ from emis_funky_funktions import * from typing import Collection, Sequence, TypeAlias from ir import Expression, ReplHole, subst_all -from genir import json_to_ir -from types_ import BUILTINS_CONTEXT +from genir import json_to_ir, PatternParseProblem, BranchTypesDiffer, UndefinedVariable +from types_ import BUILTINS_CONTEXT, UnificationError import json from dataclasses import dataclass @@ -42,8 +42,9 @@ def repl_expr(expr: Expression, bindings: ReplHole = ReplHole(BUILTINS_CONTEXT)) expr_subst = subst_all(bindings.val_bindings, expr) result = evaluate(expr_subst) if isinstance(result, ReplHole): - print('Environment updated\n') - print(result.typ_bindings) + new_bindings = [(name, type) for (name, type) in result.typ_bindings.variable_types.items() if name not in bindings.typ_bindings] + print('\n'.join(f'\033[39m{var_name}\033[39m : \033[94m{var_type}' for (var_name, var_type) in new_bindings)) + print('\033[39m') repl(result) else: print(result, end='\n\n') @@ -52,18 +53,46 @@ def repl_expr(expr: Expression, bindings: ReplHole = ReplHole(BUILTINS_CONTEXT)) def repl(bindings: ReplHole = ReplHole(BUILTINS_CONTEXT)): print('Enter a JSON expression:') try: - expr = input('-> ') + expr = input('\033[95m~> \033[96m') except EOFError: - print('') + print('\033[91m\033[39m') return try: ast = json.loads(expr) except json.decoder.JSONDecodeError as e: + print('\n\033[91m -----[ ERROR ]-----\033[39m') print(f'Bad json: ', e.args[0], end='\n\n') + print('\033[39m') return repl(bindings) # TODO handle this - new_expr, new_ty, substs = unwrap_r(json_to_ir(ast, bindings.typ_bindings)) - repl_expr(new_expr, bindings) + match json_to_ir(ast, bindings.typ_bindings): + case Ok((new_expr, new_ty, substs)): + print('\033[39m') + repl_expr(new_expr, bindings) + case Err(err): + print('\n\033[91m -----[ ERROR ]-----\033[39m') + match err: + case PatternParseProblem(pattern): + print(f"Couldn't parse the pattern {repr(pattern)}") + case UnificationError(ty1, ty2): + print(f"Couldn't unify the types {ty1} and {ty2}") + case BranchTypesDiffer(b1_pattern, b2_pattern, b1_type, b2_type): + print(f"Two or more branches had different types!") + print(f"") + print(f"The branch that matches the pattern\033[96m") + print(b1_pattern) + print(f"\033[39mhas the type\033[94m") + print(b1_type) + print(f"\033[39m") + print(f"The branch that matches the pattern\033[96m") + print(b2_pattern) + print(f"\033[39mhas the type\033[94m") + print(b2_type) + print(f"\033[39m") + case UndefinedVariable(var_name): + print(f'The variable {repr(var_name)} was referenced but not defined') + print('\033[39m') + repl(bindings) if __name__ == '__main__': import doctest