File cleanup

This commit is contained in:
2022-11-07 19:58:11 -08:00
parent ac08c5be59
commit 231c873b1c
7 changed files with 156 additions and 51 deletions

View File

@@ -1,66 +1,29 @@
if __name__ != "__main__":
raise ImportError("lamb.__main__ should never be imported. Run it directly.")
from prompt_toolkit import PromptSession
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit import print_formatted_text as printf
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.formatted_text import to_plain_text
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.lexers import Lexer
from pyparsing import exceptions as ppx
import lamb
# Simple lexer for highlighting.
# Improve this later.
class LambdaLexer(Lexer):
def lex_document(self, document):
def inner(line_no):
return [("class:text", str(document.lines[line_no]))]
return inner
lamb.utils.show_greeting()
# Replace "\" with pretty "λ"s
bindings = KeyBindings()
@bindings.add("\\")
def _(event):
event.current_buffer.insert_text("λ")
r = lamb.Runner(
prompt_session = PromptSession(
style = lamb.utils.style,
lexer = LambdaLexer(),
key_bindings = bindings
lexer = lamb.utils.LambdaLexer(),
key_bindings = lamb.utils.bindings
),
prompt_message = FormattedText([
("class:prompt", "==> ")
])
)
r.run_lines([
"T = λab.a",
"F = λab.b",
"NOT = λa.(a F T)",
"AND = λab.(a b F)",
"OR = λab.(a T b)",
"XOR = λab.(a (NOT b) b)",
"M = λx.(x x)",
"W = M M",
"Y = λf.( (λx.(f (x x))) (λx.(f (x x))) )",
"PAIR = λabi.( i a b )",
"S = λnfa.(f (n f a))",
"Z = λn.n (λa.F) T",
"MULT = λnmf.n (m f)",
"H = λp.((PAIR (p F)) (S (p F)))",
"D = λn.n H (PAIR 0 0) T",
"FAC = λyn.(Z n)(1)(MULT n (y (D n)))"
])
while True:
try:
i = r.prompt()

View File

@@ -101,7 +101,14 @@ def cmd_load(command, runner):
lines = [x.strip() for x in f.readlines()]
for i in range(len(lines)):
l = lines[i]
l = lines[i].strip()
# Skip comments and empty lines
if l.startswith("#"):
continue
if l == "":
continue
try:
x = runner.parse(l)[0]
except ppx.ParseException as e:

View File

@@ -56,7 +56,7 @@ class LambdaParser:
(self.lp + self.pp_history + self.rp)
)
self.pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_") + pp.Word(pp.alphas + pp.nums + "_")[0, ...]
self.pp_command = pp.Suppress(":") + pp.Word(pp.alphas + "_") + pp.Word(pp.alphas + pp.nums + "_.")[0, ...]
self.pp_all = (

View File

@@ -1,5 +1,7 @@
from prompt_toolkit.styles import Style
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.lexers import Lexer
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit import print_formatted_text as printf
from importlib.metadata import version
@@ -14,6 +16,11 @@ style = Style.from_dict({ # type: ignore
"code": "#AAAAAA italic",
"muted": "#AAAAAA",
# Syntax highlighting colors
"syn_cmd": "#FFFFFF italic",
"syn_lambda": "#AAAAAA",
"syn_paren": "#AAAAAA",
# Command formatting
# cmd_h: section titles
# cmd_key: keyboard keys, usually one character
@@ -28,6 +35,46 @@ style = Style.from_dict({ # type: ignore
})
# Replace "\" with pretty "λ"s
bindings = KeyBindings()
@bindings.add("\\")
def _(event):
event.current_buffer.insert_text("λ")
# Simple lexer for highlighting.
# Improve this later.
class LambdaLexer(Lexer):
def lex_document(self, document):
def inner(line_no):
out = []
tmp_str = []
d = str(document.lines[line_no])
if d.startswith(":"):
return [
("class:syn_cmd", d)
]
for c in d:
if c in "\\λ.":
if len(tmp_str) != 0:
out.append(("class:text", "".join(tmp_str)))
out.append(("class:syn_lambda", c))
tmp_str = []
elif c in "()":
if len(tmp_str) != 0:
out.append(("class:text", "".join(tmp_str)))
out.append(("class:syn_paren", c))
tmp_str = []
else:
tmp_str.append(c)
if len(tmp_str) != 0:
out.append(("class:text", "".join(tmp_str)))
return out
return inner
def show_greeting():
# | _.._ _.|_
# |_(_|| | ||_)