Local names and top-level names as the compiler binds them
- Scope: a run of body defs and defuns is one recursive group; pattern parameters; a let of plain names; with-open in sequence; :as; a named loop's name; def*, begin and :leave arms. A :view's step, a loop's => call and a comprehension's head are not binders. - Symbols: a pattern def or def* gives what it binds; :alias; a def/grammar's productions and cases; unit forms as constants. - Completion: the special forms as the compiler has them now. 🤖 Generated with [ECA](https://eca.dev) (anthropic/claude-opus-5-5) Co-Authored-By: eca-agent <git@eca.dev>
This commit is contained in:
parent
78d18c9854
commit
9a365b260b
5 changed files with 474 additions and 44 deletions
174
test/smoke.py
174
test/smoke.py
|
|
@ -552,6 +552,178 @@ def locals_(server, tmp):
|
|||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
|
||||
BINDERS = """(import (std prelude))
|
||||
|
||||
(: pairs (-> (List (Tuple int int)) (List int)))
|
||||
(defun (pairs ps)
|
||||
(list-map (fun ((Tuple a b)) (+ a b)) ps))
|
||||
|
||||
(: swap (-> (Tuple int int) int))
|
||||
(defun (swap (Tuple p q))
|
||||
(- q p))
|
||||
|
||||
(: unpack (-> (Tuple int int) int))
|
||||
(defun (unpack pair)
|
||||
(let (((lo hi) pair))
|
||||
(- hi lo)))
|
||||
|
||||
(: countdown (-> int int))
|
||||
(defun (countdown n)
|
||||
(defun (down k) (if (= k 0) (up k) (down (- k 1))))
|
||||
(defun (up k) k)
|
||||
(down n))
|
||||
|
||||
(: spliced (-> int int))
|
||||
(defun (spliced n)
|
||||
(begin (def twice (* 2 n)))
|
||||
(def* (thrice (* 3 n)) ((Some s) (first (list n)) :leave (other (+ other 1))))
|
||||
(def (Some t) (first (list n)) :leave (missing (- missing 1)))
|
||||
(+ twice thrice s t))
|
||||
|
||||
(: doubled (-> (List int) (List int)))
|
||||
(defun (doubled xs)
|
||||
(loop (:for x xs) (:as y (* 2 x)) (:acc acc (listing y)) => (first acc)))
|
||||
|
||||
(: named (-> (List int) int))
|
||||
(defun (named xs)
|
||||
(loop go (:for x xs) (:with total 0 total) (:do (go #:total (+ total x))) => total))
|
||||
|
||||
(: viewed (-> string int))
|
||||
(defun (viewed s)
|
||||
(match s
|
||||
((:view string-length len) len)))
|
||||
|
||||
(: opened (-> string int))
|
||||
(defun (opened p)
|
||||
(with-open ((r (open p)) (w (wrap r)))
|
||||
0))
|
||||
|
||||
(: averages (-> (List (List int)) (List int)))
|
||||
(defun (averages rows)
|
||||
(loop (:for row rows)
|
||||
(:subloop (:for y row) (:acc s (summing y)) => (avg (* 2 s)))
|
||||
(:acc out (listing avg))))
|
||||
|
||||
(: lengths (-> (List string) (List int)))
|
||||
(defun (lengths ws)
|
||||
{listing (string-length w) (:for w ws)})
|
||||
"""
|
||||
|
||||
|
||||
def binders_(server, tmp):
|
||||
path = os.path.join(tmp, "binders.bjo")
|
||||
uri = "file://" + path
|
||||
with open(path, "w") as f:
|
||||
f.write(BINDERS)
|
||||
server.diagnostics_after("textDocument/didOpen", uri, BINDERS)
|
||||
|
||||
def definition(needle, offset, nth=0):
|
||||
at = position_of(BINDERS, needle, nth, offset)
|
||||
return server.send("textDocument/definition", {"textDocument": {"uri": uri}, "position": at}, request=True)["result"]
|
||||
|
||||
# (case, use, offset into it, binding, offset into it)
|
||||
cases = [
|
||||
("a fun's pattern parameter", "(+ a b)", 3, "((Tuple a b))", 8),
|
||||
("a defun's pattern parameter", "(- q p)", 3, "(Tuple p q)", 9),
|
||||
("a let's tuple of plain names, its first", "(- hi lo)", 6, "((lo hi)", 2),
|
||||
("a local defun calls itself", "(down (- k", 1, "(defun (down k)", 8),
|
||||
("a local defun calls a later one", "(up k) (down", 1, "(defun (up k)", 8),
|
||||
("a def in a begin, after it", "(+ twice", 3, "(def twice", 5),
|
||||
("a def* clause's name", "thrice s t", 0, "(thrice (* 3", 1),
|
||||
("a def* clause's pattern", "s t))", 0, "((Some s)", 7),
|
||||
("a pattern def, after it", "s t))", 2, "(def (Some t)", 11),
|
||||
("a :leave arm of a def*", "(+ other", 3, "(other (+", 1),
|
||||
("a :leave arm of a def", "(- missing", 3, "(missing (-", 1),
|
||||
("an :as variable", "(listing y)", 9, "(:as y", 5),
|
||||
("a named loop's name", "(go #:total", 1, "loop go", 5),
|
||||
("a :view pattern's variable", "len)))", 0, "len) len", 0),
|
||||
("with-open: a later value sees an earlier binding", "(wrap r)", 6, "((r (open", 2),
|
||||
("a :subloop's => name", "(listing avg)", 9, "(avg (* 2 s))", 1),
|
||||
]
|
||||
for name, needle, offset, target, target_offset in cases:
|
||||
d = definition(needle, offset)
|
||||
ok = d and d["uri"] == uri and d["range"]["start"] == position_of(BINDERS, target, 0, target_offset)
|
||||
expect("binder: " + name, ok, d)
|
||||
|
||||
# Not binders: a function called in a loop's =>, in a comprehension's
|
||||
# head, and as a :view's step.
|
||||
for name, needle, offset in [
|
||||
("a function in a loop's =>", "(first acc)", 1),
|
||||
("a function in a comprehension's head", "(string-length w)", 1),
|
||||
("a :view's step", "string-length len", 0),
|
||||
]:
|
||||
d = definition(needle, offset)
|
||||
expect("not a binder: " + name, d and d["uri"] != uri, d)
|
||||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
|
||||
DECLARATIONS = """(import (std prelude))
|
||||
|
||||
(def/grammar Proc
|
||||
(Proc (List Item))
|
||||
(Item (:or (: Sym symbol) (: Sub (Item ...)))))
|
||||
|
||||
(def (Some found) (first (list 1)))
|
||||
(def (lo hi) (Tuple 1 2))
|
||||
(def* (one 1) ((Tuple two three) (Tuple 2 3)))
|
||||
(:alias uno one)
|
||||
(def/unit ft "3048|10000 m")
|
||||
|
||||
(: use (-> int))
|
||||
(defun (use)
|
||||
(match (Sub Nil) ((Sym _) found) (_ (+ lo hi one two three uno))))
|
||||
"""
|
||||
|
||||
|
||||
def declarations(server, tmp):
|
||||
path = os.path.join(tmp, "declarations.bjo")
|
||||
uri = "file://" + path
|
||||
with open(path, "w") as f:
|
||||
f.write(DECLARATIONS)
|
||||
server.diagnostics_after("textDocument/didOpen", uri, DECLARATIONS)
|
||||
|
||||
found = server.send("textDocument/documentSymbol", {"textDocument": {"uri": uri}}, request=True)["result"]
|
||||
expect(
|
||||
"outline: a grammar, pattern defs, def*, an alias and a unit",
|
||||
names(found)
|
||||
== [
|
||||
("Proc", []),
|
||||
("Item", [("Sym", []), ("Sub", [])]),
|
||||
("found", []),
|
||||
("lo", []),
|
||||
("hi", []),
|
||||
("one", []),
|
||||
("two", []),
|
||||
("three", []),
|
||||
("uno", []),
|
||||
("ft", []),
|
||||
("use", []),
|
||||
],
|
||||
names(found),
|
||||
)
|
||||
|
||||
def definition(needle, offset, nth=0):
|
||||
at = position_of(DECLARATIONS, needle, nth, offset)
|
||||
return server.send("textDocument/definition", {"textDocument": {"uri": uri}, "position": at}, request=True)["result"]
|
||||
|
||||
for name, needle, offset, target, target_offset in [
|
||||
("a grammar's case", "(Sub Nil)", 1, "(: Sub", 3),
|
||||
("a grammar's case in a pattern", "(Sym _)", 1, "(: Sym", 3),
|
||||
("a pattern def's variable", "found) (_", 0, "(Some found)", 6),
|
||||
("a def*'s pattern clause", "three uno", 0, "two three)", 4),
|
||||
("an alias", "uno))))", 0, "(:alias uno", 8),
|
||||
]:
|
||||
d = definition(needle, offset)
|
||||
ok = d and d["uri"] == uri and d["range"]["start"] == position_of(DECLARATIONS, target, 0, target_offset)
|
||||
expect("top-level definition: " + name, ok, d)
|
||||
|
||||
at = position_of(DECLARATIONS, "(def/unit", 0, 4)
|
||||
items = server.send("textDocument/completion", {"textDocument": {"uri": uri}, "position": at}, request=True)["result"]["items"]
|
||||
labels = {i["label"] for i in items}
|
||||
expect("completion: def/grammar and def/unit are special forms", {"def/grammar", "def/unit"} <= labels, sorted(labels)[:20])
|
||||
server.diagnostics_after("textDocument/didClose", uri)
|
||||
|
||||
|
||||
INFERRED = """(import (std prelude))
|
||||
|
||||
(: total (-> (List int) int))
|
||||
|
|
@ -1306,6 +1478,8 @@ with tempfile.TemporaryDirectory() as tmp:
|
|||
constrained(server, tmp)
|
||||
completing(server, tmp)
|
||||
locals_(server, tmp)
|
||||
binders_(server, tmp)
|
||||
declarations(server, tmp)
|
||||
inferred(server, tmp)
|
||||
fields(server, tmp)
|
||||
renaming(server, tmp)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue