diff --git a/README.md b/README.md index b9caba5..15b20e8 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ Lamb comes with a few commands. Prefix them with a `:` `:mdel [macro]` Delete a macro +`:clearmacros` Delete all macros + `:save [filename]`\ `:load [filename]` Save or load the current environment to a file. The lines in a file look exactly the same as regular entries in the prompt, but must only contain macro definitions. @@ -75,7 +77,6 @@ Lamb treats each λ expression as a binary tree. Variable binding and reduction ## Todo (pre-release, in this order): - - Fix :save - Cleanup warnings - Truncate long expressions in warnings - Prevent macro-chaining recursion diff --git a/lamb/commands.py b/lamb/commands.py index 6737a7c..dc4c4a4 100644 --- a/lamb/commands.py +++ b/lamb/commands.py @@ -163,20 +163,49 @@ def mdel(command, runner) -> None: del runner.macro_table[target] +@lamb_command( + help_text = "Delete all macros" +) +def clearmacros(command, runner) -> None: + confirm = runner.prompt_session.prompt( + message = FormattedText([ + ("class:warn", "Are you sure? "), + ("class:text", "[yes/no]: ") + ]) + ).lower() + + if confirm != "yes": + printf( + HTML( + "Cancelled." + ), + style = lamb.utils.style + ) + return + + runner.macro_table = {} + @lamb_command( help_text = "Show macros" ) def macros(command, runner) -> None: - printf(FormattedText([ - ("class:cmd_h", "\nDefined Macros:\n"), - ] + - [ - ("class:text", f"\t{name} \t {exp}\n") - for name, exp in runner.macro_table.items() - ]), - style = lamb.utils.style - ) + if len(runner.macro_table) == 0: + printf(FormattedText([ + ("class:warn", "No macros are defined."), + ]), + style = lamb.utils.style + ) + else: + printf(FormattedText([ + ("class:cmd_h", "\nDefined Macros:\n"), + ] + + [ + ("class:text", f"\t{name} \t {exp}\n") + for name, exp in runner.macro_table.items() + ]), + style = lamb.utils.style + ) @lamb_command( help_text = "Clear the screen" diff --git a/lamb/node.py b/lamb/node.py index dbb4a3f..427d439 100644 --- a/lamb/node.py +++ b/lamb/node.py @@ -494,12 +494,18 @@ def print_node(node: Node, *, export: bool = False) -> str: elif isinstance(n, Func): if s == Direction.UP: + if isinstance(n.parent, Call): + out += "(" + if isinstance(n.parent, Func): out += n.input.name else: out += "λ" + n.input.name if not isinstance(n.left, Func): out += "." + elif s == Direction.LEFT: + if isinstance(n.parent, Call): + out += ")" elif isinstance(n, Call): if s == Direction.UP: