go: types.sx — declaration checking (var/const/type + :=) + 12 tests [nothing]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Phase 3 cont. Adds go-check-decl which dispatches on AST shape and
returns either the extended context or a :type-error:
:var-decl (:field NAMES TYPE-or-nil) EXPRS-or-nil
:const-decl (same shape; same logic in v0 — mutability later)
:short-decl LHS-LIST EXPRS (lhs is a list of :var nodes)
:type-decl NAME TYPE (type alias)
New helpers:
go-default-type — untyped-int → int, untyped-float → float64,
etc. Used when inferring var x = EXPR.
go-check-exprs-against — every expr assignable to the declared type.
go-bind-names-to-synth — pair names with default-typed synth of
corresponding exprs; extends ctx.
The canonical Go pitfall flows through end-to-end now:
(go-check-decl ctx (go-parse "var x float64 = 42 / 7"))
→ ctx + (x → float64)
Because: 42/7 synthesises to ty-untyped-int (binop result of two
untyped operands), then go-check-exprs-against uses go-type-assignable?
to check ty-untyped-int → ty-name "float64" — :ok via the
untyped-int-to-any-numeric assignability rule. The 6 (integer) result
gets float-converted on assignment, never floated mid-computation.
types 40/40, total 345/345.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
105
lib/go/types.sx
105
lib/go/types.sx
@@ -311,3 +311,108 @@
|
||||
(go-type-assignable? got expected)
|
||||
:ok :else
|
||||
(list :type-error :mismatch expected got)))))
|
||||
|
||||
;; ── default types ────────────────────────────────────────────────
|
||||
;; Go spec § Constants: the *default type* of an untyped constant
|
||||
;; is what it becomes when assigned to a sloppily-typed slot
|
||||
;; (e.g., `var x = 42` makes x an int).
|
||||
|
||||
(define
|
||||
go-default-type
|
||||
(fn (t)
|
||||
(cond
|
||||
(not (list? t)) t
|
||||
(= (first t) :ty-untyped-int) (list :ty-name "int")
|
||||
(= (first t) :ty-untyped-float) (list :ty-name "float64")
|
||||
(= (first t) :ty-untyped-imag) (list :ty-name "complex128")
|
||||
(= (first t) :ty-untyped-string) (list :ty-name "string")
|
||||
(= (first t) :ty-untyped-rune) (list :ty-name "int32")
|
||||
:else t)))
|
||||
|
||||
;; ── declaration checking ────────────────────────────────────────
|
||||
;; Returns either:
|
||||
;; the extended context (success)
|
||||
;; (list :type-error TAG ...) (failure)
|
||||
|
||||
(define
|
||||
go-check-exprs-against
|
||||
;; Check every EXPR in EXPRS is assignable to EXPECTED. Returns the
|
||||
;; first :type-error encountered, or :ok.
|
||||
(fn (ctx exprs expected)
|
||||
(cond
|
||||
(or (= exprs nil) (= (len exprs) 0)) :ok
|
||||
:else
|
||||
(let ((r (go-check ctx (first exprs) expected)))
|
||||
(cond
|
||||
(go-type-error? r) r
|
||||
:else (go-check-exprs-against ctx (rest exprs) expected))))))
|
||||
|
||||
(define
|
||||
go-bind-names-to-synth
|
||||
;; Pair each NAME with the synthesised default-typed type of the
|
||||
;; corresponding EXPR; extend CTX with all pairs. NAMES and EXPRS
|
||||
;; may have different lengths (multi-return funcs aren't here yet);
|
||||
;; for now we zip the shorter of the two.
|
||||
(fn (ctx names exprs)
|
||||
(cond
|
||||
(or (= (len names) 0) (= (len exprs) 0)) ctx
|
||||
:else
|
||||
(let ((t (go-synth ctx (first exprs))))
|
||||
(cond
|
||||
(go-type-error? t) t
|
||||
:else
|
||||
(let ((ctx2 (go-ctx-extend ctx (first names)
|
||||
(go-default-type t))))
|
||||
(go-bind-names-to-synth ctx2 (rest names) (rest exprs))))))))
|
||||
|
||||
(define
|
||||
go-check-var-decl
|
||||
;; Shape: (:var-decl (:field NAMES TYPE-or-nil) EXPRS-or-nil)
|
||||
;; or (:const-decl (:field NAMES TYPE-or-nil) EXPRS).
|
||||
;; Logic is the same for v0; const-vs-var distinction matters for
|
||||
;; mutability checks which arrive later.
|
||||
(fn (ctx decl)
|
||||
(let ((field (nth decl 1)) (exprs (nth decl 2)))
|
||||
(let ((names (nth field 1)) (ann-ty (nth field 2)))
|
||||
(cond
|
||||
;; var x T (no init) → bind names to T
|
||||
(or (= exprs nil) (= (len exprs) 0))
|
||||
(cond
|
||||
(= ann-ty nil) (list :type-error :missing-type-or-init names)
|
||||
:else (go-ctx-extend-field ctx field))
|
||||
;; Annotated: var x T = expr — check each expr against T
|
||||
(not (= ann-ty nil))
|
||||
(let ((err (go-check-exprs-against ctx exprs ann-ty)))
|
||||
(cond
|
||||
(go-type-error? err) err
|
||||
:else (go-ctx-extend-field ctx field)))
|
||||
;; Inferred: var x = expr — bind names to default(synth(expr))
|
||||
:else (go-bind-names-to-synth ctx names exprs))))))
|
||||
|
||||
(define
|
||||
go-check-short-decl
|
||||
;; Shape: (:short-decl LHS-LIST EXPRS). LHS is a list of (:var NAME).
|
||||
;; Extracts the names and falls through to bind-names-to-synth.
|
||||
(fn (ctx decl)
|
||||
(let ((lhs-list (nth decl 1)) (exprs (nth decl 2)))
|
||||
(let ((names (map (fn (lhs)
|
||||
(cond
|
||||
(and (list? lhs) (= (first lhs) :var))
|
||||
(nth lhs 1)
|
||||
:else :unknown))
|
||||
lhs-list)))
|
||||
(go-bind-names-to-synth ctx names exprs)))))
|
||||
|
||||
(define
|
||||
go-check-decl
|
||||
;; Top-level dispatcher: accepts any decl AST shape, returns extended
|
||||
;; context or :type-error.
|
||||
(fn (ctx decl)
|
||||
(cond
|
||||
(and (list? decl) (= (first decl) :var-decl)) (go-check-var-decl ctx decl)
|
||||
(and (list? decl) (= (first decl) :const-decl)) (go-check-var-decl ctx decl)
|
||||
(and (list? decl) (= (first decl) :short-decl)) (go-check-short-decl ctx decl)
|
||||
(and (list? decl) (= (first decl) :type-decl))
|
||||
(let ((name (nth decl 1)) (ty (nth decl 2)))
|
||||
(go-ctx-extend ctx name ty))
|
||||
:else ctx)))
|
||||
|
||||
Reference in New Issue
Block a user