go: types.sx — func-decl + stmt-level dispatch + 7 tests [nothing]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
Phase 3 cont. Adds:
* go-check-func-decl — binds the function in the outer ctx (recursive
self-reference will work once call-checking lands), extends the
body's ctx with each :field param group via go-ctx-extend-field
(the binding-group shape's *third* consumer in the type checker;
five total across parser+typer when counted with struct fields,
var-decls, const-decls, func params, method receivers).
* go-check-stmt — dispatches on :return / :assign / :var-decl /
:const-decl / :short-decl / :type-decl / :block; falls back to
go-synth for expression statements.
* go-check-block — threads ctx through stmts so that decls inside
the block extend the ctx for subsequent stmts.
* go-check-return-list — each return expr assignable to the
corresponding declared result type; mismatch counts are typed.
* go-check-assign / go-check-assign-pairs — RHS assignable to LHS
synthesised type, count mismatch typed.
* Helpers: go-decl-params-to-ty-list (flattens :field NAMES TYPE to
a flat list of N types), go-extend-with-params (folds extend-field
over a param-group list), go-repeat-ty.
Coverage tests:
func empty() {} → ok
func add(x, y int) int { return x + y } → ok
func bad() int { return "hi" } → typed error
func sig(x int) int → signature-only binds
func sumsq(x, y int) int { return x*x + y*y } → params visible
func two() int { var x int = 1; var y int = 2; → nested decl
return x + y }
func g() int { var x int; x = 5; return x } → assign verified
types 47/47, total 352/352.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
144
lib/go/types.sx
144
lib/go/types.sx
@@ -415,4 +415,148 @@
|
||||
(and (list? decl) (= (first decl) :type-decl))
|
||||
(let ((name (nth decl 1)) (ty (nth decl 2)))
|
||||
(go-ctx-extend ctx name ty))
|
||||
(and (list? decl) (= (first decl) :func-decl))
|
||||
(go-check-func-decl ctx decl)
|
||||
:else ctx)))
|
||||
|
||||
;; ── function-decl checking ──────────────────────────────────────
|
||||
|
||||
(define
|
||||
go-repeat-ty
|
||||
(fn (n ty acc)
|
||||
(cond
|
||||
(<= n 0) acc
|
||||
:else (go-repeat-ty (- n 1) ty (cons ty acc)))))
|
||||
|
||||
(define
|
||||
go-decl-params-to-ty-list
|
||||
;; Flatten (:field NAMES TYPE) param groups into a list of types,
|
||||
;; one entry per name. For func-type signatures.
|
||||
(fn (params)
|
||||
(cond
|
||||
(or (= params nil) (= (len params) 0)) (list)
|
||||
:else
|
||||
(let ((field (first params)))
|
||||
(let ((names (nth field 1)) (ty (nth field 2)))
|
||||
(let ((rest-tys (go-decl-params-to-ty-list (rest params))))
|
||||
(go-repeat-ty (len names) ty rest-tys)))))))
|
||||
|
||||
(define
|
||||
go-extend-with-params
|
||||
;; Extend CTX with every binding in every (:field NAMES TYPE) param group.
|
||||
(fn (ctx params)
|
||||
(cond
|
||||
(or (= params nil) (= (len params) 0)) ctx
|
||||
:else
|
||||
(go-extend-with-params
|
||||
(go-ctx-extend-field ctx (first params))
|
||||
(rest params)))))
|
||||
|
||||
(define
|
||||
go-check-return-list
|
||||
;; Each EXPR assignable to the corresponding RESULTS type.
|
||||
;; v0: lengths must match; multi-return funcs deferred.
|
||||
(fn (ctx exprs results)
|
||||
(cond
|
||||
(and (= (len exprs) 0) (= (len results) 0)) :ok
|
||||
(not (= (len exprs) (len results)))
|
||||
(list :type-error :return-count-mismatch
|
||||
(len exprs) (len results))
|
||||
:else
|
||||
(let ((r (go-check ctx (first exprs) (first results))))
|
||||
(cond
|
||||
(go-type-error? r) r
|
||||
:else (go-check-return-list ctx (rest exprs) (rest results)))))))
|
||||
|
||||
(define
|
||||
go-check-assign
|
||||
(fn (ctx stmt)
|
||||
(let ((lhs-list (nth stmt 1)) (rhs-list (nth stmt 2)))
|
||||
(cond
|
||||
(not (= (len lhs-list) (len rhs-list)))
|
||||
(list :type-error :assign-count-mismatch
|
||||
(len lhs-list) (len rhs-list))
|
||||
:else (go-check-assign-pairs ctx lhs-list rhs-list)))))
|
||||
|
||||
(define
|
||||
go-check-assign-pairs
|
||||
(fn (ctx lhs-list rhs-list)
|
||||
(cond
|
||||
(= (len lhs-list) 0) :ok
|
||||
:else
|
||||
(let ((lhs-ty (go-synth ctx (first lhs-list))))
|
||||
(cond
|
||||
(go-type-error? lhs-ty) lhs-ty
|
||||
:else
|
||||
(let ((r (go-check ctx (first rhs-list) lhs-ty)))
|
||||
(cond
|
||||
(go-type-error? r) r
|
||||
:else
|
||||
(go-check-assign-pairs ctx (rest lhs-list)
|
||||
(rest rhs-list)))))))))
|
||||
|
||||
(define
|
||||
go-check-stmt
|
||||
;; Returns either an extended CTX (decls), :ok (sealed stmts), or
|
||||
;; :type-error. RESULTS is the enclosing func's declared return types
|
||||
;; (used by :return).
|
||||
(fn (ctx stmt results)
|
||||
(cond
|
||||
(and (list? stmt) (= (first stmt) :var-decl))
|
||||
(go-check-decl ctx stmt)
|
||||
(and (list? stmt) (= (first stmt) :const-decl))
|
||||
(go-check-decl ctx stmt)
|
||||
(and (list? stmt) (= (first stmt) :short-decl))
|
||||
(go-check-decl ctx stmt)
|
||||
(and (list? stmt) (= (first stmt) :type-decl))
|
||||
(go-check-decl ctx stmt)
|
||||
(and (list? stmt) (= (first stmt) :return))
|
||||
(let ((exprs (nth stmt 1)))
|
||||
(let ((err (go-check-return-list ctx exprs results)))
|
||||
(cond (go-type-error? err) err :else ctx)))
|
||||
(and (list? stmt) (= (first stmt) :block))
|
||||
(let ((err (go-check-block ctx (nth stmt 1) results)))
|
||||
(cond (go-type-error? err) err :else ctx))
|
||||
(and (list? stmt) (= (first stmt) :assign))
|
||||
(let ((err (go-check-assign ctx stmt)))
|
||||
(cond (go-type-error? err) err :else ctx))
|
||||
:else
|
||||
(let ((t (go-synth ctx stmt)))
|
||||
(cond (go-type-error? t) t :else ctx)))))
|
||||
|
||||
(define
|
||||
go-check-block
|
||||
;; Thread ctx through stmts; if any stmt is a decl, its extension
|
||||
;; propagates to subsequent stmts. Returns :ok or :type-error.
|
||||
(fn (ctx stmts results)
|
||||
(cond
|
||||
(or (= stmts nil) (= (len stmts) 0)) :ok
|
||||
:else
|
||||
(let ((r (go-check-stmt ctx (first stmts) results)))
|
||||
(cond
|
||||
(go-type-error? r) r
|
||||
:else (go-check-block r (rest stmts) results))))))
|
||||
|
||||
(define
|
||||
go-check-func-decl
|
||||
;; Bind the function in the outer ctx (so recursion works), extend
|
||||
;; ctx with params, check the body. Returns the outer ctx with the
|
||||
;; function bound, or :type-error.
|
||||
(fn (ctx decl)
|
||||
(let ((name (nth decl 1)) (params (nth decl 2))
|
||||
(results (nth decl 3)) (body (nth decl 4)))
|
||||
(let ((fn-ty
|
||||
(list :ty-func
|
||||
(go-decl-params-to-ty-list params) results)))
|
||||
(let ((ctx-with-fn (go-ctx-extend ctx name fn-ty)))
|
||||
(cond
|
||||
(= body nil) ctx-with-fn
|
||||
(and (list? body) (= (first body) :block))
|
||||
(let ((body-ctx
|
||||
(go-extend-with-params ctx-with-fn params)))
|
||||
(let ((err
|
||||
(go-check-block body-ctx (nth body 1) results)))
|
||||
(cond
|
||||
(go-type-error? err) err
|
||||
:else ctx-with-fn)))
|
||||
:else ctx-with-fn))))))
|
||||
|
||||
Reference in New Issue
Block a user