From 1a4ed032c212e3b655dcc83834805dfabe94f681 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 21 Oct 2022 14:44:52 -0700 Subject: [PATCH] Added basic command handling --- parser.py | 14 +++++++------- runner.py | 6 +++++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/parser.py b/parser.py index 85e9e8b..729c4d1 100755 --- a/parser.py +++ b/parser.py @@ -17,21 +17,21 @@ class Parser: # Simple tokens pp_expr = pp.Forward() - pp_name = pp.Word(pp.alphas + "_") - pp_name.set_parse_action(tokens.macro.from_parse) + pp_macro = pp.Word(pp.alphas + "_") + pp_macro.set_parse_action(tokens.macro.from_parse) # Function definitions. # Right associative. # # => - pp_lambda_fun = (pp.Suppress("λ") | pp.Suppress("\\")) + pp_name + pp.Suppress(".") + pp_expr + pp_lambda_fun = (pp.Suppress("λ") | pp.Suppress("\\")) + pp_macro + pp.Suppress(".") + pp_expr pp_lambda_fun.set_parse_action(tokens.lambda_func.from_parse) # Assignment. # Can only be found at the start of a line. # # = - pp_macro_def = pp.line_start() + pp_name + pp.Suppress("=") + pp_expr + pp_macro_def = pp.line_start() + pp_macro + pp.Suppress("=") + pp_expr pp_macro_def.set_parse_action(tokens.macro_expression.from_parse) # Function calls. @@ -45,10 +45,10 @@ class Parser: pp_call <<= pp_expr[2, ...] pp_call.set_parse_action(tokens.lambda_apply.from_parse) - pp_expr <<= pp_lambda_fun ^ (lp + pp_expr + rp) ^ pp_name ^ (lp + pp_call + rp) + pp_expr <<= pp_lambda_fun ^ (lp + pp_expr + rp) ^ pp_macro ^ (lp + pp_call + rp) pp_all = pp_expr | pp_macro_def - pp_command = ":" + pp_name + pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_") pp_command.set_parse_action(tokens.command.from_parse) @staticmethod @@ -62,7 +62,7 @@ class Parser: parse_all = True )[0] print(k) - return(k) + return k @staticmethod def run_tests(lines): diff --git a/runner.py b/runner.py index dff6efe..c8162ec 100644 --- a/runner.py +++ b/runner.py @@ -6,6 +6,10 @@ class Runner: self.macro_table = {} self.expr = None + def exec_command(self, command: str): + if command == "help": + print("This is a help message.") + # Apply a list of definitions def run(self, line: str): e = Parser.parse_line(line) @@ -17,7 +21,7 @@ class Runner: self.macro_table[e.label] = e.exp elif isinstance(e, tokens.command): - pass + self.exec_command(e.name) else: e.bind_variables() self.expr = e