The slot after a defn's parameters is unconditionally a type. Parse.decl no
longer takes a set of type names, and is_type_form, qualified_type, types_in,
declared_types and prelude_types are gone with the pre-pass that fed them.
What they were for: (Option f64) and (Some 1) are the same s-expression, so the
parser decided which it had by looking the head up in a set of the file's own
type names. Sound -- one top-level namespace means a name cannot be both a type
and a value -- and brittle, because the set had to be complete. It was wrong
twice in one day, the second time parsing (defn f [] (Rune {.code 65}) (bar))
as a function returning a Rune with a one-form body, silently, in every file in
the language.
Two things fall out. A type the parser could not have known -- a struct
declared further down the file, rl/Vector2 behind an unresolved alias, a
prelude type -- never needed recognising, only placing. And a mistyped type is
a mistyped type: (defn f [] f65 0.0) reaches the resolver's near-miss check and
says did you mean f64, where it used to be read as the first form of the body
and reported as an unknown name.
Unit is written (). The old spelling is refused with a message naming the new
one, the rule the colon-to-dot change followed. Internally it is still
Tname "Unit" and Types.Unit, so the resolver, the shim and the emitter did not
change; Cimport still builds Tname "Unit" for C's void without going through
the parser. Types.to_string prints () though -- that printer prints what a
person would write for every other type it knows, [i32], {K V}, (Ptr T), and
Unit was the odd one out once the source spelling moved.
Dropping prelude_types removes one of the two reasons Macro.reduce may only
drop defns: the memoised set a bootstrap build could have poisoned is gone, so
the remaining reason is the plain one.
46 lines
1.7 KiB
Plaintext
46 lines
1.7 KiB
Plaintext
;;;; handler-bind and signal — spec-conditions.md §1 and §2.
|
|
;;;;
|
|
;;;; The accumulation case, which is what makes these two worth having on their
|
|
;;;; own: signal returns (), a handler that returns normally leaves the
|
|
;;;; signalling function to carry on, and with nothing matching signal is a
|
|
;;;; no-op. No control flow is altered, so none of the transfer machinery
|
|
;;;; restart-case needs exists yet.
|
|
(defstruct AssetMissing [id i32])
|
|
(defstruct Corrupt [id i32])
|
|
|
|
(defvar seen i64)
|
|
(defvar other i64)
|
|
|
|
;;; Signals twice and keeps going both times — that is the whole of §1.
|
|
(defn load-all [] ()
|
|
(signal (AssetMissing {.id 1}))
|
|
(signal (AssetMissing {.id 2}))
|
|
(signal (Corrupt {.id 3})))
|
|
|
|
(defn main [] i32
|
|
;; No handler: a no-op, not an abort and not a message (§2).
|
|
(load-all)
|
|
(print seen) (println "") ; 0
|
|
|
|
(handler-bind [(AssetMissing [c] (set seen (+ seen (i64 (.id c)))))]
|
|
(load-all))
|
|
(print seen) (println "") ; 1 + 2 = 3
|
|
|
|
;; Two clauses, and only the matching one runs for each condition.
|
|
(handler-bind [(AssetMissing [c] (set seen (+ seen 10)))
|
|
(Corrupt [c] (set other (+ other (i64 (.id c)))))]
|
|
(load-all))
|
|
(print seen) (println "") ; 3 + 20 = 23
|
|
(print other) (println "") ; 3
|
|
|
|
;; Nesting: the inner frame does not displace the outer one, so both run.
|
|
(handler-bind [(Corrupt [c] (set other (+ other 100)))]
|
|
(handler-bind [(Corrupt [c] (set other (+ other 1000)))]
|
|
(signal (Corrupt {.id 0}))))
|
|
(print other) (println "") ; 3 + 1000 + 100 = 1103
|
|
|
|
;; And the stack is back to what it was: no handler, no effect.
|
|
(load-all)
|
|
(print other) (println "") ; 1103
|
|
0)
|