Added basic command handling
parent
f8ff0a0c3e
commit
1a4ed032c2
14
parser.py
14
parser.py
|
@ -17,21 +17,21 @@ class Parser:
|
||||||
|
|
||||||
# Simple tokens
|
# Simple tokens
|
||||||
pp_expr = pp.Forward()
|
pp_expr = pp.Forward()
|
||||||
pp_name = pp.Word(pp.alphas + "_")
|
pp_macro = pp.Word(pp.alphas + "_")
|
||||||
pp_name.set_parse_action(tokens.macro.from_parse)
|
pp_macro.set_parse_action(tokens.macro.from_parse)
|
||||||
|
|
||||||
# Function definitions.
|
# Function definitions.
|
||||||
# Right associative.
|
# Right associative.
|
||||||
#
|
#
|
||||||
# <var> => <exp>
|
# <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)
|
pp_lambda_fun.set_parse_action(tokens.lambda_func.from_parse)
|
||||||
|
|
||||||
# Assignment.
|
# Assignment.
|
||||||
# Can only be found at the start of a line.
|
# Can only be found at the start of a line.
|
||||||
#
|
#
|
||||||
# <var> = <exp>
|
# <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)
|
pp_macro_def.set_parse_action(tokens.macro_expression.from_parse)
|
||||||
|
|
||||||
# Function calls.
|
# Function calls.
|
||||||
|
@ -45,10 +45,10 @@ class Parser:
|
||||||
pp_call <<= pp_expr[2, ...]
|
pp_call <<= pp_expr[2, ...]
|
||||||
pp_call.set_parse_action(tokens.lambda_apply.from_parse)
|
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_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)
|
pp_command.set_parse_action(tokens.command.from_parse)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -62,7 +62,7 @@ class Parser:
|
||||||
parse_all = True
|
parse_all = True
|
||||||
)[0]
|
)[0]
|
||||||
print(k)
|
print(k)
|
||||||
return(k)
|
return k
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def run_tests(lines):
|
def run_tests(lines):
|
||||||
|
|
|
@ -6,6 +6,10 @@ class Runner:
|
||||||
self.macro_table = {}
|
self.macro_table = {}
|
||||||
self.expr = None
|
self.expr = None
|
||||||
|
|
||||||
|
def exec_command(self, command: str):
|
||||||
|
if command == "help":
|
||||||
|
print("This is a help message.")
|
||||||
|
|
||||||
# Apply a list of definitions
|
# Apply a list of definitions
|
||||||
def run(self, line: str):
|
def run(self, line: str):
|
||||||
e = Parser.parse_line(line)
|
e = Parser.parse_line(line)
|
||||||
|
@ -17,7 +21,7 @@ class Runner:
|
||||||
self.macro_table[e.label] = e.exp
|
self.macro_table[e.label] = e.exp
|
||||||
|
|
||||||
elif isinstance(e, tokens.command):
|
elif isinstance(e, tokens.command):
|
||||||
pass
|
self.exec_command(e.name)
|
||||||
else:
|
else:
|
||||||
e.bind_variables()
|
e.bind_variables()
|
||||||
self.expr = e
|
self.expr = e
|
||||||
|
|
Reference in New Issue