From a7078f9a778e09f52cd3b5aaff6db1da061561b5 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 22 Oct 2022 07:48:17 -0700 Subject: [PATCH] Added mdel command --- lamb/commands.py | 27 +++++++++++++++++++++++---- lamb/parser.py | 2 +- lamb/runner.py | 7 ++++--- lamb/tokens.py | 4 +++- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lamb/commands.py b/lamb/commands.py index f978418..8886308 100644 --- a/lamb/commands.py +++ b/lamb/commands.py @@ -1,4 +1,5 @@ from prompt_toolkit.formatted_text import FormattedText +from prompt_toolkit.formatted_text import HTML from prompt_toolkit.shortcuts import clear as clear_screen from lamb.runstatus import CommandStatus @@ -16,11 +17,29 @@ def lamb_command(*, help_text: str): return inner def run(command, runner): - return commands[command](runner) + return commands[command.name](command, runner) +@lamb_command(help_text = "Delete a macro") +def mdel(command, runner): + if len(command.args) != 1: + return CommandStatus( + formatted_text = HTML( + "Command :mdel takes exactly one argument." + ) + ) + + target = command.args[0] + if target not in runner.macro_table: + return CommandStatus( + formatted_text = HTML( + f"Macro \"{target}\" is not defined" + ) + ) + + del runner.macro_table[target] @lamb_command(help_text = "Show macros") -def macros(runner): +def macros(command, runner): return CommandStatus( formatted_text = FormattedText([ ("#FF6600 bold", "\nDefined Macros:\n"), @@ -33,13 +52,13 @@ def macros(runner): ) @lamb_command(help_text = "Clear the screen") -def clear(runner): +def clear(command, runner): clear_screen() utils.show_greeting() @lamb_command(help_text = "Print this help") -def help(runner): +def help(command, runner): return CommandStatus( formatted_text = FormattedText([ ("#FF6600 bold", "\nUsage:\n"), diff --git a/lamb/parser.py b/lamb/parser.py index 88ffd07..afeca05 100755 --- a/lamb/parser.py +++ b/lamb/parser.py @@ -56,7 +56,7 @@ class Parser: (lp + pp_call + rp) ) - pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_") + pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_") + pp.Word(pp.alphas + "_")[0, ...] pp_command.set_parse_action(tokens.command.from_parse) diff --git a/lamb/runner.py b/lamb/runner.py index 29957c4..58db6e4 100644 --- a/lamb/runner.py +++ b/lamb/runner.py @@ -1,3 +1,4 @@ +from distutils.cmd import Command from prompt_toolkit.formatted_text import FormattedText import lamb.commands as commands @@ -15,8 +16,8 @@ class Runner: # If None, no maximum is enforced. self.reduction_limit: int | None = 300 - def exec_command(self, command: str) -> rs.CommandStatus: - if command in commands.commands: + def exec_command(self, command: tokens.command) -> rs.CommandStatus: + if command.name in commands.commands: return commands.run(command, self) # Handle unknown commands @@ -80,7 +81,7 @@ class Runner: # If this line is a command, do the command. elif isinstance(e, tokens.command): - return self.exec_command(e.name) + return self.exec_command(e) # If this line is a plain expression, reduce it. elif isinstance(e, tokens.LambdaToken): diff --git a/lamb/tokens.py b/lamb/tokens.py index 51b2f80..05dadcb 100755 --- a/lamb/tokens.py +++ b/lamb/tokens.py @@ -81,10 +81,12 @@ class command: def from_parse(result): return command( result[0], + result[1:] ) - def __init__(self, name): + def __init__(self, name, args): self.name = name + self.args = args class macro(LambdaToken): """