This repository has been archived on 2024-11-05. You can view files and clone it, but cannot push or open issues/pull-requests.
lamb/runner.py

134 lines
2.9 KiB
Python
Raw Normal View History

2022-10-21 14:40:49 -07:00
import tokens
from parser import Parser
2022-10-21 17:05:25 -07:00
import enum
class RunStatus:
"""
Base class for run status.
These are returned whenever the runner does something.
"""
pass
class MacroStatus(RunStatus):
"""
Returned when a macro is defined.
Values:
`was_rewritten`: If true, an old macro was replaced.
`macro_label`: The name of the macro we just made.
"""
def __init__(
self,
*,
was_rewritten: bool,
macro_label: str
):
self.was_rewritten = was_rewritten
self.macro_label = macro_label
class StopReason(enum.Enum):
BETA_NORMAL = ("#FFFFFF", "β-normal form")
LOOP_DETECTED = ("#FFFF00", "loop detected")
MAX_EXCEEDED = ("#FFFF00", "too many reductions")
INTERRUPT = ("#FF0000", "user interrupt")
class ReduceStatus(RunStatus):
"""
Returned when an expression is reduced.
Values:
`reduction_count`: How many reductions were made.
`stop_reason`: Why we stopped. See `StopReason`.
"""
def __init__(
self,
*,
reduction_count: int,
stop_reason: StopReason,
result: tokens.LambdaToken
):
self.reduction_count = reduction_count
self.stop_reason = stop_reason
self.result = result
2022-10-21 14:40:49 -07:00
class Runner:
def __init__(self):
self.macro_table = {}
2022-10-21 17:05:25 -07:00
# Maximum amount of reductions.
# If None, no maximum is enforced.
self.reduction_limit: int | None = 300
2022-10-21 14:40:49 -07:00
2022-10-21 14:44:52 -07:00
def exec_command(self, command: str):
if command == "help":
print("This is a help message.")
2022-10-21 17:05:25 -07:00
def reduce_expression(self, expr: tokens.LambdaToken) -> ReduceStatus:
# Reduction Counter.
# We also count macro expansions,
# and subtract those from the final count.
i = 0
macro_expansions = 0
while i < self.reduction_limit:
r = expr.reduce(self.macro_table)
expr = r.output
# If we can't reduce this expression anymore,
# it's in beta-normal form.
if not r.was_reduced:
return ReduceStatus(
reduction_count = i - macro_expansions,
stop_reason = StopReason.BETA_NORMAL,
result = r.output
)
# Count reductions
i += 1
if r.reduction_type == tokens.ReductionType.MACRO_EXPAND:
macro_expansions += 1
return ReduceStatus(
reduction_count = i - macro_expansions,
stop_reason = StopReason.MAX_EXCEEDED,
result = r.output
)
2022-10-21 14:40:49 -07:00
# Apply a list of definitions
2022-10-21 17:05:25 -07:00
def run(self, line: str) -> RunStatus:
2022-10-21 14:40:49 -07:00
e = Parser.parse_line(line)
2022-10-21 17:05:25 -07:00
# If this line is a macro definition, save the macro.
2022-10-21 14:40:49 -07:00
if isinstance(e, tokens.macro_expression):
2022-10-21 17:05:25 -07:00
was_rewritten = e.label in self.macro_table
2022-10-21 14:40:49 -07:00
e.exp.bind_variables()
self.macro_table[e.label] = e.exp
2022-10-21 17:05:25 -07:00
return MacroStatus(
was_rewritten = was_rewritten,
macro_label = e.label
)
# If this line is a command, do the command.
2022-10-21 14:40:49 -07:00
elif isinstance(e, tokens.command):
2022-10-21 17:05:25 -07:00
return self.exec_command(e.name)
# If this line is a plain expression, reduce it.
2022-10-21 14:40:49 -07:00
else:
e.bind_variables()
2022-10-21 17:05:25 -07:00
return self.reduce_expression(e)
2022-10-21 14:40:49 -07:00
2022-10-21 17:05:25 -07:00
def run_lines(self, lines: list[str]):
2022-10-21 14:40:49 -07:00
for l in lines:
self.run(l)