Minor warning cleanup

master
Mark 2022-11-09 21:47:55 -08:00
parent 062a4b4d1b
commit 89bc4bd2c0
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 31 additions and 39 deletions

View File

@ -77,7 +77,6 @@ The lines in a file look exactly the same as regular entries in the prompt, but
## Todo (pre-release, in this order): ## Todo (pre-release, in this order):
- Cleanup warnings
- Truncate long expressions in warnings - Truncate long expressions in warnings
- Prevent macro-chaining recursion - Prevent macro-chaining recursion
- Full-reduce option (expand all macros) - Full-reduce option (expand all macros)
@ -89,6 +88,8 @@ The lines in a file look exactly the same as regular entries in the prompt, but
## Todo: ## Todo:
- Cleanup warnings
- Syntax highlight printouts
- History queue + indexing - History queue + indexing
- Show history command - Show history command
- Loop detection - Loop detection
@ -97,6 +98,5 @@ The lines in a file look exactly the same as regular entries in the prompt, but
- Command-line options (load a file) - Command-line options (load a file)
- Unchurch command: make church numerals human-readable - Unchurch command: make church numerals human-readable
- Better Syntax highlighting - Better Syntax highlighting
- Syntax highlight printouts
- Complete file names and commands - Complete file names and commands
- Tests - Tests

View File

@ -95,7 +95,7 @@ def clone(node: lbn.Node):
break break
return out return out
def prepare(root: lbn.Root, *, ban_macro_name = None) -> dict: def prepare(root: lbn.Root, *, ban_macro_name = None) -> list:
""" """
Prepare an expression for expansion. Prepare an expression for expansion.
This will does the following: This will does the following:
@ -109,15 +109,19 @@ def prepare(root: lbn.Root, *, ban_macro_name = None) -> dict:
bound_variables = {} bound_variables = {}
output = { warnings = []
"has_history": False,
"free_variables": set()
}
it = iter(root) it = iter(root)
for s, n in it: for s, n in it:
if isinstance(n, lbn.History): if isinstance(n, lbn.History):
output["has_history"] = True if len(root.runner.history) == 0:
raise lbn.ReductionError("There isn't any history to reference.")
else:
warnings += [
("class:code", "$"),
("class:warn", " will be expanded to "),
("class:code", f"{n.expand()[1]}\n"),
]
# If this expression is part of a macro, # If this expression is part of a macro,
# make sure we don't reference it inside itself. # make sure we don't reference it inside itself.
@ -135,7 +139,11 @@ def prepare(root: lbn.Root, *, ban_macro_name = None) -> dict:
# Turn undefined macros into free variables # Turn undefined macros into free variables
elif n.name not in root.runner.macro_table: elif n.name not in root.runner.macro_table:
output["free_variables"].add(n.name) warnings += [
("class:warn", "Name "),
("class:code", n.name),
("class:warn", " is a free variable\n"),
]
n.parent.set_side( n.parent.set_side(
n.parent_side, n.parent_side,
n.to_freevar() n.to_freevar()
@ -158,7 +166,7 @@ def prepare(root: lbn.Root, *, ban_macro_name = None) -> dict:
elif s == lbn.Direction.LEFT: elif s == lbn.Direction.LEFT:
del bound_variables[n.input.name] del bound_variables[n.input.name]
return output return warnings
# Apply a function. # Apply a function.
# Returns the function's output. # Returns the function's output.

View File

@ -319,8 +319,10 @@ class History(ExpandableEndNode):
return "$" return "$"
def expand(self) -> tuple[lbn.ReductionType, Node]: def expand(self) -> tuple[lbn.ReductionType, Node]:
# We shouldn't ever get here, prepare()
# catches empty history.
if len(self.runner.history) == 0: if len(self.runner.history) == 0:
raise lbn.ReductionError(f"There isn't any history to reference.") raise Exception(f"Tried to expand empty history.")
# .left is VERY important! # .left is VERY important!
# self.runner.history will contain Root nodes, # self.runner.history will contain Root nodes,
# and we don't want those *inside* our tree. # and we don't want those *inside* our tree.

View File

@ -56,25 +56,23 @@ class Runner:
message = self.prompt_message message = self.prompt_message
) )
def parse(self, line) -> tuple[lamb.nodes.Root | MacroDef | Command, dict]: def parse(self, line) -> tuple[lamb.nodes.Root | MacroDef | Command, list]:
e = self.parser.parse_line(line) e = self.parser.parse_line(line)
o = {} w = []
if isinstance(e, MacroDef): if isinstance(e, MacroDef):
e.expr = lamb.nodes.Root(e.expr) e.expr = lamb.nodes.Root(e.expr)
e.set_runner(self) e.set_runner(self)
o = lamb.nodes.prepare(e.expr, ban_macro_name = e.label) w = lamb.nodes.prepare(e.expr, ban_macro_name = e.label)
elif isinstance(e, lamb.nodes.Node): elif isinstance(e, lamb.nodes.Node):
e = lamb.nodes.Root(e) e = lamb.nodes.Root(e)
e.set_runner(self) e.set_runner(self)
o = lamb.nodes.prepare(e) w = lamb.nodes.prepare(e)
return e, o return e, w
def reduce(self, node: lamb.nodes.Root, *, status = {}) -> None: def reduce(self, node: lamb.nodes.Root, *, warnings = []) -> None:
warning_text = []
# Reduction Counter. # Reduction Counter.
# We also count macro (and church) expansions, # We also count macro (and church) expansions,
@ -86,14 +84,6 @@ class Runner:
start_time = time.time() start_time = time.time()
out_text = [] out_text = []
if status["has_history"] and len(self.history) != 0:
warning_text += [
("class:code", "$"),
("class:warn", " will be expanded to "),
("class:code", str(self.history[-1])),
("class:warn", "\n")
]
only_macro = ( only_macro = (
isinstance(node.left, lamb.nodes.Macro) or isinstance(node.left, lamb.nodes.Macro) or
isinstance(node.left, lamb.nodes.Church) isinstance(node.left, lamb.nodes.Church)
@ -103,16 +93,8 @@ class Runner:
m, node = lamb.nodes.expand(node, force_all = only_macro) m, node = lamb.nodes.expand(node, force_all = only_macro)
macro_expansions += m macro_expansions += m
if len(warnings) != 0:
for i in status["free_variables"]: printf(FormattedText(warnings), style = lamb.utils.style)
warning_text += [
("class:warn", "Name "),
("class:code", i),
("class:warn", " is a free variable\n"),
]
if len(warning_text) != 0:
printf(FormattedText(warning_text), style = lamb.utils.style)
while ( while (
@ -210,7 +192,7 @@ class Runner:
*, *,
silent = False silent = False
) -> None: ) -> None:
e, o = self.parse(line) e, w = self.parse(line)
# If this line is a macro definition, save the macro. # If this line is a macro definition, save the macro.
if isinstance(e, MacroDef): if isinstance(e, MacroDef):
@ -230,7 +212,7 @@ class Runner:
# If this line is a plain expression, reduce it. # If this line is a plain expression, reduce it.
elif isinstance(e, lamb.nodes.Node): elif isinstance(e, lamb.nodes.Node):
self.reduce(e, status = o) self.reduce(e, warnings = w)
# We shouldn't ever get here. # We shouldn't ever get here.
else: else: