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:
Linus Björnstam 2026-11-09 15:02:46 +01:00
parent 78d18c9854
commit 9a365b260b
5 changed files with 474 additions and 44 deletions

View file

@ -19,6 +19,7 @@ type Kind =
| Method = 6
| Field = 8
| EnumMember = 22
| Constant = 14
| Struct = 23
| Module = 2
@ -64,6 +65,72 @@ let private binderName (s: SExpr) : (string * Range) option =
| SList(SAtom { Token = Symbol n; Range = r } :: _, _) -> Some(n, r)
| _ -> None
/// The names a top-level `def` binder binds, as the compiler reads it:
/// - a name, or `(: name T)`;
/// - a list of plain names, `(a b)` or `(Tuple a b)`, a tuple;
/// - else a pattern: its lowercase symbols that are not a list's head. A
/// `(:view step p)` binds what `p` binds, and a comparison nothing.
let private defBinderNames (binder: SExpr) : (string * Range) list =
let rec patternNames (s: SExpr) =
match s with
| SAtom { Token = Symbol n; Range = r } when n <> "_" && not (Char.IsUpper n[0]) -> [ n, r ]
| SList([ SAtom { Token = Keyword "view" }; _; inner ], _) -> patternNames inner
| SList(SAtom { Token = Keyword("at-least" | "at-most" | "greater-than" | "less-than" | "between") } :: _, _) -> []
| SList(SAtom { Token = Symbol _ } :: args, _) -> args |> List.collect patternNames
| SList(items, _) -> items |> List.collect patternNames
| _ -> []
let plain =
function
| SAtom { Token = Symbol _ }
| SAtom { Token = Comma } -> true
| _ -> false
match binder with
| SAtom { Token = Symbol n; Range = r } -> [ n, r ]
| SList(SAtom { Token = Colon } :: SAtom { Token = Symbol n; Range = r } :: _, _) -> [ n, r ]
| SList((SAtom { Token = Symbol head } :: _) as names, _) when
(head = "Tuple" || not (Char.IsUpper head[0])) && List.forall plain names
->
names
|> List.choose (function
| SAtom { Token = Symbol n; Range = r } when n <> "Tuple" && n <> "_" -> Some(n, r)
| _ -> None)
| _ -> patternNames binder
/// The productions of a `def/grammar` after the first, which is the grammar's
/// own type, and the cases of each `(:or (: Case item) ...)`.
let private grammarMembers (grammar: string) (productions: SExpr list) : Symbol list * Symbol list =
let casesOf (production: string) (body: SExpr list) =
body
|> List.collect (function
| SList(SAtom { Token = Keyword "or" } :: alternatives, _) ->
alternatives
|> List.choose (function
| SList(SAtom { Token = Colon } :: SAtom { Token = Symbol n; Range = nr } :: _, cr) when Char.IsUpper n[0] ->
Some(symbol n production Kind.EnumMember cr nr cr [])
| _ -> None)
| _ -> [])
let production =
function
| SList(SAtom { Token = Symbol n; Range = nr } :: (_ :: _ as body), pr) when Char.IsUpper n[0] ->
Some(n, nr, body, pr)
| _ -> None
// As the compiler reads it: if the first form is a production, all are.
// Else the forms are the grammar's one production.
match productions with
| first :: _ when (production first).IsNone -> casesOf grammar productions, []
| _ ->
match List.choose production productions with
| [] -> [], []
| (_, _, firstBody, _) :: rest ->
casesOf grammar firstBody,
rest
|> List.map (fun (n, nr, body, pr) -> symbol n $"a production of `%s{grammar}`" Kind.Class pr nr pr (casesOf n body))
/// One `(: head body)` of a `type` form: the type, and its constructors or
/// fields.
let private typeDef (s: SExpr) : Symbol option =
@ -135,19 +202,37 @@ let rec private ofForm (form: SExpr) : Symbol list =
[ symbol $"impl %s{show head}" "" Kind.Module r (getRange head) (getRange head) (body |> List.collect ofForm) ]
| SList(SAtom { Token = Symbol("def" | "def/mutable" as head) } :: binder :: _, r) ->
match binder with
// `(def (a b) expr)`: every name in the destructuring list.
| SList(items, br) when (match items with
| SAtom { Token = Colon } :: _ -> false
| _ -> true) ->
items
|> List.choose (function
| SAtom { Token = Symbol n; Range = nr } -> Some(symbol n head Kind.Variable r nr br [])
| _ -> None)
| _ ->
binderName binder
|> Option.map (fun (n, nr) -> symbol n head Kind.Variable r nr (getRange binder) [])
|> Option.toList
defBinderNames binder |> List.map (fun (n, nr) -> symbol n head Kind.Variable r nr (getRange binder) [])
// `(def* (binder scrutinee ...) ...)`: each clause binds as a `def`.
| SList(SAtom { Token = Symbol "def*" } :: clauses, _) ->
clauses
|> List.collect (function
| SList(binder :: _ :: _, cr) ->
defBinderNames binder |> List.map (fun (n, nr) -> symbol n "def*" Kind.Variable cr nr (getRange binder) [])
| _ -> [])
// `(:alias new-name existing-name)`.
| SList([ SAtom { Token = Keyword "alias" }; SAtom { Token = Symbol n; Range = nr }; SAtom { Token = Symbol old } ], r) ->
[ symbol n $"an alias of `%s{old}`" Kind.Function r nr r [] ]
// `(def/grammar Name production ...)`: the grammar is a type, and so is
// each production after the first. `#:rules-only` is module metadata.
| SList(SAtom { Token = Symbol "def/grammar" } :: head :: productions, r) when
(match head with
| SAtom { Token = Keyword _ } -> false
| _ -> true)
->
match binderName head with
| Some(name, nr) ->
let cases, others = grammarMembers name productions
symbol name "grammar" Kind.Class r nr (getRange head) cases :: others
| None -> []
// `(def/dimension name ...)`, `(def/unit name value)`,
// `(def/prefix name value)`: names of units, not functions.
| SList(SAtom { Token = Symbol("def/dimension" | "def/unit" | "def/prefix" as head) } :: SAtom { Token = Symbol n; Range = nr } :: _, r) ->
[ symbol n head Kind.Constant r nr r [] ]
// `defun`, `defbjo`, `defbjouble`, `def/macro`, `def/pattern`, and any
// other `def…` form a library adds, which all put the name first.