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 5e842c4df6
commit 7bbb8e19ac
10 changed files with 876 additions and 50 deletions

View File

@@ -4,6 +4,7 @@
scheme
(only (scheme base) make-parameter)
(chicken base)
(chicken condition)
(chicken keyword)
(chicken string)
(chicken module)
@@ -47,18 +48,27 @@
(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)
(let* ((processed (match-sex-form (car forms) acc))
(new (take-until processed acc)))
(process-rec (cdr forms)
(append new 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
(let* ((processed (match-sex-form (car forms) acc))
(new (take-until processed acc)))
(process-rec (cdr forms)
(append new 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)
@@ -207,6 +217,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
@@ -297,10 +308,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 (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
@@ -424,10 +447,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)))
@@ -496,10 +520,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)))
@@ -525,7 +549,8 @@
(list))))
env)))
(when (>= (length walked) 3)
(declare-name! env (second walked) (third walked)))
(declare-name! env (second walked) (third walked))
(record-symbol! 'var (second walked) walked (third walked) #f #t))
walked))
((return)
@@ -1436,6 +1461,9 @@
;;; Record the named structs, unions and enums in the type database
(define (process-struct sex-struct acc)
(let-values (((doc form) (extract-aggregate-docstring sex-struct)))
(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)))
@@ -1473,11 +1501,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)))
@@ -1602,7 +1639,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
@@ -1650,7 +1688,8 @@
(copy-form-source! sex-var (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