Added basic command handling

master
Mark 2022-10-21 14:44:52 -07:00
parent f8ff0a0c3e
commit 1a4ed032c2
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 12 additions and 8 deletions

View File

@ -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.
#
# <var> => <exp>
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.
#
# <var> = <exp>
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):

View File

@ -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