flan/test/programs/pkgs/tree/tree.flan
Joseph Ferano cc2cfa175b Sets are read, and the reader that answers a Value is the package's
The tokenizer refused #{} because "it needs a hash set to even
represent" — which is a claim about a reader, and a tokenizer represents
nothing. #{ now pushes } on the same balance stack { does, there is one
new token kind and no new closer, and err-set is gone rather than kept
with a message it no longer earns. skip-value needed nothing: it is
written against the depth and not against the kinds.

The dynamic reader moves out of test/programs/arena-edn.flan and into
vendor/edn/read.flan as (edn/read bytes), answering an (Option Value)
against whichever allocator the caller bound. Two decisions are written
down where they are made:

  * a set is a Value.Set holding a deduplicated (Vec Value), because
    (Map Value bool) does not typecheck — keyable refuses a key holding
    a Vec or a Map — and restricting elements to keyable Values would
    refuse #{[0 0] [1 0]}, which is the file this was built for. Insert
    is O(n) against a structural value=?, so building the tileset's 54
    pairs is 1458 comparisons, once.
  * a Value copies every string into the allocator where a Token stays
    a view. A view handed back out of the function that owns the buffer
    is a dangling pointer, and free-all would not even take it. Odin's
    json parser clones for the same reason.

An imported defdata was a refusal in load.ml — "not implemented yet
(milestone 4)" — and it had to go first. It is the type's name plus the
Type. half of a constructor symbol, which arrives as a Var node when the
case has no fields and a Struct node when it has; a match pattern needed
nothing, because a case resolves against the scrutinee's type and was
never a top-level name. programs/pkg-data.flan is that on its own.

programs/edn-read.flan reads assets/edn/tileset.edn, which is the
editor's real output: :texture-path and a :selected-cells of 54 integer
pairs, with no type declared for any of it. It also overwrites the
source buffer in place after reading and prints the document back, which
is the copy contract asserted rather than described.
2026-09-18 22:50:54 +07:00

33 lines
1.2 KiB
Plaintext

;;;; A data type a package exports, which is what vendor/edn's Value needed.
;;;;
;;;; A struct imports by renaming one name. A data type has a second half — the
;;;; case table — and the question this package exists to answer is where each
;;;; half ends up. The answer is that a case is not a top-level name: it has no
;;;; existence apart from its type, so the only thing to qualify is the [Node.]
;;;; in front of it, and an importer's [(Leaf n)] pattern stays bare because it
;;;; resolves against the scrutinee's type and never against a name.
(defdata Node
[(Leaf [n i64])
(Branch [kids (Vec Node)])
(Empty [])])
;; Built inside the package, where the constructor is written unqualified and
;; the import has to rewrite it.
(defn leaf [n i64] Node (Node.Leaf {.n n}))
;; A case with no fields is a value and not a call, so this is the [Var] node
;; where the others are [Struct] nodes — the second shape the rename has to
;; catch, and the one it is easy to catch only half of.
(defn nothing [] Node Node.Empty)
(defn total [t Node] i64
(match t
(Leaf n) n
(Branch kids)
(let [s (i64 0)]
(dotimes [i (len kids)]
(set s (+ s (total (at kids i)))))
s)
Empty (i64 0)))