1
0
forked from alex-eg/sex

index a unit past its first bad form

Record plain declarations, with location and docstring, and keep going
when one toplevel form is wrong. sexc --check and --symbols print
that. Specs, realizations, and closure helpers stay out of the index
for future rework (@alex-eg).
This commit is contained in:
2026-11-11 07:38:33 +03:00
parent 7315694faf
commit fb4b513efc
10 changed files with 892 additions and 59 deletions

101
semen.scm
View File

@@ -4,6 +4,7 @@
scheme
(only (scheme base) make-parameter)
(chicken base)
(chicken condition)
(chicken keyword)
(chicken string)
(chicken module)
@@ -20,7 +21,7 @@
)
(export/rename (process semen-process))
(export closure-env-declaration)
(export closure-env-declaration reset-closures!)
;;; for lambda extraction, docstring processing, macro expansion,
;;; injection of module headers, i.e. all things that rearrange code
@@ -48,21 +49,30 @@
(string-append "called at " (form-file call) ":"
(number->string (form-line call)))))))
(define (process-one forms acc)
(if (macro? (car forms))
(process-rec (macroexpand (car forms) (cdr forms)) acc)
;; `struct ƛint_int' has to be declared before the function whose
;; signature first mentioned it, so the forms that function produced
;; are lifted off and the structs slid underneath them
(let* ((processed (match-sex-form (car forms) acc))
(new (take-until processed acc)))
(process-rec (cdr forms)
(append new (flush-closure-structs!) acc)))))
;;; One bad toplevel form is one diagnostic. The rest of the unit is
;;; still worth indexing. A compile that is not collecting still stops
;;; at the first error, as it always has.
(define (process-rec forms acc)
(cond
((null? forms) (reverse acc))
((macro? (car forms))
(process-rec
(macroexpand (car forms) (cdr forms))
acc))
(else
;; `struct ƛint_int' has to be declared before the function whose
;; signature first mentioned it, so the forms that function produced
;; are lifted off and the structs slid underneath them
(let* ((processed (match-sex-form (car forms) acc))
(new (take-until processed acc)))
(process-rec (cdr forms)
(append new (flush-closure-structs!) acc))))))
((sex-diagnostics)
(handle-exceptions exn
(begin
(record-exception! (car forms) exn)
(process-rec (cdr forms) acc))
(process-one forms acc)))
(else (process-one forms acc))))
(define (take-until forms tail)
(if (eq? forms tail)
@@ -216,6 +226,7 @@
;;; Typdef
(define (process-typedef form new-type target acc)
(record-symbol! 'typedef new-type form target #f #f)
(cons (copy-form-source! form `(typedef ,target ,new-type)) acc))
;;; Fn processing
@@ -306,10 +317,22 @@
"a function's types are written out; `_' is a variable's"
(sex-fn-name expanded)))
(add-name-type! (sex-fn-name expanded) (fn-type-of expanded))
(record-symbol! 'fn (sex-fn-name expanded) sex-fn-raw
`(fn ,(sex-fn-name expanded)
,(sex-fn-arglist expanded)
,(sex-fn-return-type expanded))
doc #f)
(let* ((env (make-fn-env expanded))
;; the parameters are the body's outermost scope
(header (map resolve-closure-types (take expanded boundary)))
(body (parameterize ((operand-position? #f))
(body (parameterize ((sex-scope-range (form-line-range sex-fn-raw))
(operand-position? #f))
(for-each
(lambda (param)
(when (named-arg? param)
(record-symbol! 'var (first param) sex-fn-raw
(named-arg-type param) #f #t)))
(sex-fn-arglist expanded))
(walk-body (drop expanded boundary) env)))
;; A function is header and body stitched together, so the
;; result is fresh structure: without this the definition
@@ -568,10 +591,11 @@
(or (hash-table-ref/default (car scopes) name #f)
(search (cdr scopes))))))
(define (with-scope env body)
(let ((enclosing (hash-table-ref env :scopes)))
(define (with-scope env form body)
(let ((enclosing (hash-table-ref env :scopes))
(range (or (form-line-range form) (sex-scope-range))))
(set! (hash-table-ref env :scopes) (cons (make-hash-table) enclosing))
(let ((walked (body)))
(let ((walked (parameterize ((sex-scope-range range)) (body))))
(set! (hash-table-ref env :scopes) enclosing)
walked)))
@@ -641,10 +665,10 @@
(case (car form)
((do default)
(parameterize ((operand-position? #f))
(with-scope env (lambda () (walk-parts form env)))))
(with-scope env form (lambda () (walk-parts form env)))))
((for while if switch case case/fallthrough)
(with-scope env (lambda () (walk-control form env))))
(with-scope env form (lambda () (walk-control form env))))
((lambda)
(let ((name (aux-name! env make-lambda-name)))
@@ -695,7 +719,8 @@
(drop walked 4))))
walked)))
(when (>= (length bound) 3)
(declare-name! env (second bound) (third bound)))
(declare-name! env (second bound) (third bound))
(record-symbol! 'var (second bound) bound (third bound) #f #t))
bound))
((return)
@@ -1927,6 +1952,14 @@
(set! *pending-closure-structs* (list))
pending))
;;; A second unit must not see the closure structs the previous one minted.
(define (reset-closures!)
(set! +closure-structs+ (make-hash-table))
(set! +closure-forwards+ (make-hash-table))
(set! +closure-conversions+ (make-hash-table))
(set! +closure-calls+ (make-hash-table))
(set! *pending-closure-structs* (list)))
;;; Lowering
(define (make-closure-name enclosing-fn-name counter)
@@ -2092,6 +2125,9 @@
;; the type database as the struct it becomes, or member access
;; through it finds nothing
(let ((form (resolve-closure-types form)))
(let ((core (if (eq? (car form) 'pub) (cdr form) form)))
(when (and (pair? (cdr core)) (symbol? (cadr core)))
(record-symbol! (car core) (cadr core) sex-struct #f doc #f)))
(register-aggregate! form)
(with-docstring doc form acc))))
@@ -2129,11 +2165,20 @@
(for-each (lambda (v)
(add-constructor! (qualify-op name (car v)) name (car v)))
variants)
;; the docstring goes ahead of everything the sum is made of
(fold match-sex-form
(if doc (cons (copy-form-source! form (list 'comment doc)) acc) acc)
(map (lambda (f) (copy-form-source! form f))
(sum-forms name variants)))))))
(record-symbol! 'sum name written `(struct ,name) doc #f)
(for-each (lambda (v)
(record-symbol! 'fn (qualify-op name (car v)) written
`(fn ,(map second (cdr v)) ,name)
#f #f))
variants)
;; the docstring goes ahead of everything the sum is made of.
;; The generated structs and typedefs are the compiler's, and
;; the sum itself is already indexed.
(parameterize ((sex-index-suppressed? #t))
(fold match-sex-form
(if doc (cons (copy-form-source! form (list 'comment doc)) acc) acc)
(map (lambda (f) (copy-form-source! form f))
(sum-forms name variants))))))))
(define (parse-variant form v)
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v)))
@@ -2258,7 +2303,8 @@
(for-each (lambda (value)
(let ((constant (if (pair? value) (car value) value)))
(when (symbol? constant)
(add-name-type! constant `(enum ,name)))))
(add-name-type! constant `(enum ,name))
(record-symbol! 'const constant form `(enum ,name) #f #f))))
(remove comment-form? (caddr f)))))))))
;;; No function around a toplevel form, so no scope chain: what a
@@ -2307,7 +2353,8 @@
(copy-form-source! resolved (cons qualifier core))
core)))
(when (and (pair? (cdr core)) (pair? (cddr core)) (symbol? (second core)))
(add-name-type! (second core) (third core)))
(add-name-type! (second core) (third core))
(record-symbol! 'var (second core) sex-var (third core) #f #f))
(cons form (append (hash-table-ref env :lambda-aux-code) acc))))
;;; A compound literal is not a constant, though cc lets one pass at file