Fixed :save & added new commands

master
Mark 2022-11-07 19:02:27 -08:00
parent cf2d82acc6
commit ab682ef700
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
3 changed files with 46 additions and 10 deletions

View File

@ -57,6 +57,8 @@ Lamb comes with a few commands. Prefix them with a `:`
`:mdel [macro]` Delete a macro `:mdel [macro]` Delete a macro
`:clearmacros` Delete all macros
`:save [filename]`\ `: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. `: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): ## Todo (pre-release, in this order):
- Fix :save
- Cleanup warnings - Cleanup warnings
- Truncate long expressions in warnings - Truncate long expressions in warnings
- Prevent macro-chaining recursion - Prevent macro-chaining recursion

View File

@ -163,11 +163,40 @@ def mdel(command, runner) -> None:
del runner.macro_table[target] 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(
"<err>Cancelled.</err>"
),
style = lamb.utils.style
)
return
runner.macro_table = {}
@lamb_command( @lamb_command(
help_text = "Show macros" help_text = "Show macros"
) )
def macros(command, runner) -> None: def macros(command, runner) -> None:
if len(runner.macro_table) == 0:
printf(FormattedText([
("class:warn", "No macros are defined."),
]),
style = lamb.utils.style
)
else:
printf(FormattedText([ printf(FormattedText([
("class:cmd_h", "\nDefined Macros:\n"), ("class:cmd_h", "\nDefined Macros:\n"),
] + ] +

View File

@ -494,12 +494,18 @@ def print_node(node: Node, *, export: bool = False) -> str:
elif isinstance(n, Func): elif isinstance(n, Func):
if s == Direction.UP: if s == Direction.UP:
if isinstance(n.parent, Call):
out += "("
if isinstance(n.parent, Func): if isinstance(n.parent, Func):
out += n.input.name out += n.input.name
else: else:
out += "λ" + n.input.name out += "λ" + n.input.name
if not isinstance(n.left, Func): if not isinstance(n.left, Func):
out += "." out += "."
elif s == Direction.LEFT:
if isinstance(n.parent, Call):
out += ")"
elif isinstance(n, Call): elif isinstance(n, Call):
if s == Direction.UP: if s == Direction.UP: