Compare commits
No commits in common. "0fbb6e4f88517360524a945d67ef714dc620ccec" and "10b143f2fa51ea2047fd7f54c7d94d379aaa9a98" have entirely different histories.
0fbb6e4f88
...
10b143f2fa
6 changed files with 38 additions and 110 deletions
|
@ -1,7 +1,6 @@
|
|||
py_project(
|
||||
name = "milkshake",
|
||||
lib_deps = [
|
||||
py_requirement("attrs"),
|
||||
py_requirement("lark"),
|
||||
],
|
||||
)
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
from importlib.resources import files
|
||||
|
||||
from attrs import define
|
||||
from lark import Lark, Tree, Token, Transformer, v_args
|
||||
|
||||
|
||||
|
@ -11,19 +10,6 @@ with files(__package__).joinpath("grammar.lark").open("r", encoding="utf-8") as
|
|||
GRAMMAR = fp.read()
|
||||
|
||||
|
||||
@define(frozen=True)
|
||||
class Symbol:
|
||||
name: str
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, str):
|
||||
return self.name == other
|
||||
elif isinstance(other, type(self)):
|
||||
return self.name == other.name
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
@v_args(tree=True)
|
||||
class T(Transformer):
|
||||
"""A prepackaged transformer that cleans up the quoting details."""
|
||||
|
@ -39,9 +25,7 @@ class T(Transformer):
|
|||
qq_map = un_qq
|
||||
qq_set = un_qq
|
||||
qq_atom = un_qq
|
||||
|
||||
def qq_symbol(self, obj):
|
||||
return self.symbol(self.un_qq(obj))
|
||||
qq_symbol = un_qq
|
||||
|
||||
def qq_quote(self, obj):
|
||||
return self.quote(self.un_qq(obj))
|
||||
|
@ -53,9 +37,6 @@ class T(Transformer):
|
|||
unquote = quote
|
||||
unquote_splicing = quote
|
||||
|
||||
def symbol(self, obj):
|
||||
return Symbol(obj.children[0].value)
|
||||
|
||||
|
||||
PARSER = Lark(GRAMMAR, start=["module", "expr"])
|
||||
|
||||
|
|
|
@ -56,11 +56,7 @@ string: /"([^"]|\\")+"/
|
|||
|
||||
pattern: /\/([^\/]|\\\/)+\//
|
||||
|
||||
number: US_FORMAT_NUMBER | EU_FORMAT_NUMBER | UNDERSCORE_NUMBER
|
||||
|
||||
US_FORMAT_NUMBER: /[+-]?(\d+r)?(\d+(,\d{3})*)(\.\d+(,\d{3})*)?(e[+-]?\d+)?/
|
||||
EU_FORMAT_NUMBER: /[+-]?(\d+r)?(\d+(\.\d{3})*)(,\d+(\.\d{3})*)?(e[+-]?\d+)?/
|
||||
UNDERSCORE_NUMBER: /[+-]?(\d+r)?(\d+(_\d{3})*)([,\.]\d+(_\d{3})*)?(e[+-]?\d+)?/
|
||||
number: /[+-]?(\d+r)?(\d[\d,_\.]*)([\.,][\d,_\.]*)?(e[+-]?\d+)?/
|
||||
|
||||
// Note that we're demoting Symbol from the default parse priority of 0 to -1
|
||||
// This because _anything more specific_ should be chosen over symbol
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from milkshake import slurp, Symbol
|
||||
from milkshake import slurp
|
||||
|
||||
from lark import Tree, Token
|
||||
import pytest
|
||||
|
@ -10,26 +10,26 @@ import pytest
|
|||
"input, val",
|
||||
[
|
||||
("()", Tree("list", [])),
|
||||
("nil", nil := Symbol("nil")),
|
||||
("nil", nil := Tree("symbol", ["nil"])),
|
||||
("(nil nil nil)", Tree("list", [nil, nil, nil])),
|
||||
(
|
||||
"(/ + - * % ^ \\ & # @ ! = |)",
|
||||
Tree(
|
||||
"list",
|
||||
[
|
||||
Symbol("/"),
|
||||
Symbol("+"),
|
||||
Symbol("-"),
|
||||
Symbol("*"),
|
||||
Symbol("%"),
|
||||
Symbol("^"),
|
||||
Symbol("\\"),
|
||||
Symbol("&"),
|
||||
Symbol("#"),
|
||||
Symbol("@"),
|
||||
Symbol("!"),
|
||||
Symbol("="),
|
||||
Symbol("|"),
|
||||
Tree("symbol", ["/"]),
|
||||
Tree("symbol", ["+"]),
|
||||
Tree("symbol", ["-"]),
|
||||
Tree("symbol", ["*"]),
|
||||
Tree("symbol", ["%"]),
|
||||
Tree("symbol", ["^"]),
|
||||
Tree("symbol", ["\\"]),
|
||||
Tree("symbol", ["&"]),
|
||||
Tree("symbol", ["#"]),
|
||||
Tree("symbol", ["@"]),
|
||||
Tree("symbol", ["!"]),
|
||||
Tree("symbol", ["="]),
|
||||
Tree("symbol", ["|"]),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -48,12 +48,12 @@ import pytest
|
|||
Tree(
|
||||
"list",
|
||||
[
|
||||
Symbol("+inf"),
|
||||
Symbol("-inf"),
|
||||
Symbol("inf"),
|
||||
Symbol("nan"),
|
||||
Symbol("+nan"),
|
||||
Symbol("-nan"),
|
||||
Tree("symbol", ["+inf"]),
|
||||
Tree("symbol", ["-inf"]),
|
||||
Tree("symbol", ["inf"]),
|
||||
Tree("symbol", ["nan"]),
|
||||
Tree("symbol", ["+nan"]),
|
||||
Tree("symbol", ["-nan"]),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -66,7 +66,7 @@ import pytest
|
|||
Tree(
|
||||
"list",
|
||||
[
|
||||
Symbol("nil"),
|
||||
Tree("symbol", ["nil"]),
|
||||
Tree(
|
||||
"unquote",
|
||||
[
|
||||
|
|
|
@ -8,8 +8,6 @@ to implement a flat, stackless, serializable interpreter.
|
|||
|
||||
import typing as t
|
||||
|
||||
from milkshake import Symbol
|
||||
|
||||
from .models import *
|
||||
from .util import *
|
||||
|
||||
|
@ -20,24 +18,24 @@ class Operator:
|
|||
"""Stack operators."""
|
||||
|
||||
|
||||
class Op:
|
||||
class Task:
|
||||
@attrs.define()
|
||||
class Complete:
|
||||
"""Signals program termination."""
|
||||
|
||||
@attrs.define()
|
||||
class Drop:
|
||||
"""Drop or void the top of the data stack."""
|
||||
pass
|
||||
|
||||
@attrs.define()
|
||||
class Eval:
|
||||
"""Evaluate the current term."""
|
||||
|
||||
@attrs.define()
|
||||
class Invoke:
|
||||
"""Consuming the top N items of the stack of the format [... <fn>, <... args>] invoke fn with args."""
|
||||
pass
|
||||
|
||||
n: int
|
||||
@attrs.define()
|
||||
class Apply:
|
||||
"""Apply an operator to the stack."""
|
||||
|
||||
operator: "Operator" = attrs.field()
|
||||
|
||||
@attrs.define()
|
||||
class Next:
|
||||
|
@ -69,64 +67,19 @@ def step(vm: Vm, cont: Cont, frame: Frame):
|
|||
|
||||
"""
|
||||
|
||||
if frame.op_stack:
|
||||
expr = frame.op_stack.pop()
|
||||
|
||||
else:
|
||||
expr = get_in(vm.log, frame.pc)
|
||||
|
||||
match expr:
|
||||
# AST entries
|
||||
case ["py", native_expr, arg_exprs, vararg_expr]:
|
||||
case ["native", native_fn, arg_exprs, vararg_expr]:
|
||||
pass
|
||||
|
||||
case ["lambda", arg_expr, body_expr]:
|
||||
# "now this one;s hard"
|
||||
case ["invoke", fn_expr, arg_exprs, vararg_expr]:
|
||||
pass
|
||||
|
||||
case ["do", *body_exprs]:
|
||||
case ["var", name]:
|
||||
pass
|
||||
|
||||
case ["if", test_expr, then_expr, else_expr]:
|
||||
case int() | float() | str() | bool():
|
||||
pass
|
||||
|
||||
case ["if", test_expr, then_expr]:
|
||||
pass
|
||||
|
||||
# Generalized "simple" invoke expression
|
||||
case [fn_expr, *arg_exprs]:
|
||||
pass
|
||||
|
||||
# Literals/terminals
|
||||
case Symbol(name):
|
||||
# Resolve a value from the scope(s)
|
||||
for frame in reversed(cont.frames):
|
||||
if name in frame.frame_ns:
|
||||
frame.data_stack.append(frame.frame_ns[name])
|
||||
break
|
||||
else:
|
||||
raise NameError(f"{name!r} is not bound")
|
||||
|
||||
case int() | float() | str() | bool() | dict() | set():
|
||||
# Literals push
|
||||
frame.data_stack.append(expr)
|
||||
|
||||
# Semi-VM instructions
|
||||
case Op.Drop():
|
||||
frame.data_stack.pop()
|
||||
|
||||
case Op.Invoke(n):
|
||||
data = [frame.data_stack.pop() for _ in range(n)]
|
||||
fn = data[-1]
|
||||
args = data[-1::]
|
||||
|
||||
# Need to trampoline the call through the wrapping loop
|
||||
return Op.Frame(fn, args)
|
||||
|
||||
# FIXME: Do we still need this?
|
||||
case Op.Complete():
|
||||
return expr
|
||||
|
||||
|
||||
def run(program, state=None):
|
||||
vm = state or Vm([], {}, [])
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from collections import deque
|
||||
import typing as t
|
||||
|
||||
import attrs
|
||||
|
@ -41,5 +40,5 @@ class Frame:
|
|||
|
||||
pc: list = attrs.field(factory=lambda: [0])
|
||||
frame_ns: dict = attrs.field(factory=dict)
|
||||
op_stack: deque = attrs.field(factory=deque)
|
||||
data_stack: deque = attrs.field(factory=deque)
|
||||
op_stack: list = attrs.field(factory=list)
|
||||
data_stack: list = attrs.field(factory=list)
|
||||
|
|
Loading…
Reference in a new issue