flan/test/programs/pkg-data.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

42 lines
1.7 KiB
Plaintext

;;;; A data type imported from a package, which was a refusal until vendor/edn
;;;; needed one — "an imported data type is not implemented yet (milestone 4)".
;;;;
;;;; Four things, and each is a different way the rename could be half done:
;;;; the type named in a signature, a constructor written on this side of the
;;;; import, a constructor the *package* wrote and the import had to rewrite,
;;;; and a match whose patterns name the cases bare. The last is the one that
;;;; needs no rename at all — a pattern resolves against the scrutinee's type —
;;;; and it is here so that the day someone qualifies case names too, this says
;;;; what broke.
(import tree "pkgs/tree")
(defvar frame Allocator)
;; The type in a signature, and a bare pattern over a value the package made.
(defn describe [t tree/Node] string
(match t
(Leaf _n) "leaf"
(Branch _k) "branch"
Empty "empty"))
(defn main [] i32
(set frame (arena-new 4096))
(with-allocator frame
(let [kids (vec-new tree/Node)]
;; Written here, qualified, which is how the importer spells it.
(push kids (tree/Node.Leaf {.n (i64 1)}))
;; Written inside the package, unqualified, and rewritten by the import.
(push kids (tree/leaf (i64 2)))
(push kids (tree/nothing))
(let [t (tree/Node.Branch {.kids kids})]
(println (describe t))
(println (describe (tree/leaf (i64 9))))
(println (describe (tree/nothing)))
;; Summed by the package's own match, over a value this file built, so
;; the two sides agree about the layout and not only about the names.
(println (tree/total t)))))
(free-all frame)
(arena-destroy frame)
0)