From 70aafb70e66eab28b68c8634fdc32f4992a413c7 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Wed, 14 Oct 2026 20:08:16 +0300 Subject: [PATCH] read each module once per unit A pub import chain had its interface check read every module below again, so the deepest was parsed once per level above it. --- sex-modules.scm | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/sex-modules.scm b/sex-modules.scm index 1752213d..199a0440 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -78,16 +78,18 @@ (file-readable? module-path) module-path)))) -;;; Every module file read for this unit, first read first: what its -;;; output depends on, besides the unit itself -(define +module-dependencies+ (list)) +;;; Every module file read for this unit, with its forms, last read +;;; first: what its output depends on, besides the unit itself. A module +;;; that an import chain and the interface check both reach is read once +(define +modules-read+ (list)) -(define (module-dependencies) (reverse +module-dependencies+)) +(define (module-dependencies) (reverse (map car +modules-read+))) (define (read-module path) - (unless (member path +module-dependencies+) - (set! +module-dependencies+ (cons path +module-dependencies+))) - (read-from-file path)) + (cond ((assoc path +modules-read+) => cdr) + (else (let ((forms (read-from-file path))) + (set! +modules-read+ (cons (cons path forms) +modules-read+)) + forms)))) (define (read-public-interface module-path) ;; pub fns are reduced to prototypes, other pub forms are just pasted. @@ -134,7 +136,8 @@ (define (check-type-use form use visible hidden acc) (let* ((name (car use)) (seen (assq name visible)) - (kept (assq name hidden))) + (kept (assq name hidden)) + (remedy (if (and kept (fourth kept)) "import its module with pub" "make it pub"))) (cond ((and seen (third seen)) acc) ; pub with its body ((and seen (cdr use)) acc) ; a pub handle, held by one @@ -146,16 +149,11 @@ (sex-error form (string-append "importers cannot see this " (symbol->string (second kept)) ", and C declares one only by defining it; " - (if (fourth kept) - "import its module with pub" - "make it pub")) + remedy) name)) ((not (cdr use)) (sex-error form (string-append "importers cannot see this type, held by value; " - (if (fourth kept) - "import its module with pub" - "make it pub") - ", or hold it through a pointer") + remedy ", or hold it through a pointer") name)) ((any (lambda (f) (eq? name (cadr f))) acc) acc) (else @@ -257,14 +255,11 @@ (define (pub-form? form) (and (pair? form) (eq? (car form) 'pub))) ;;; Pub here or through a pub import. A type declared pub both bodyless -;;; and whole is whole +;;; and whole is whole: its whole entries come first, for `assq' (define (visible-types forms) (let ((all (append (declared-types (filter pub-form? forms)) (append-map exported-types (imported-modules forms #t))))) - (map (lambda (entry) - (list (first entry) (second entry) - (any third (filter (lambda (e) (eq? (first e) (first entry))) all)))) - all))) + (append (filter third all) (remove third all)))) ;;; Declared here without pub, or brought by an import without it, as ;;; (name kind body? imported?) @@ -286,16 +281,16 @@ ;;; The types a module gives its importers, its pub imports' included. ;;; Two modules may import each other, so each is read once (define (exported-types module) - (let read ((modules (list module)) (seen (list)) (acc (list))) + (let next ((modules (list module)) (seen (list)) (acc (list))) (if (null? modules) - acc + (concatenate (reverse acc)) (let ((path (locate-module (symbol->string (car modules))))) (if (or (not path) (member path seen)) - (read (cdr modules) seen acc) + (next (cdr modules) seen acc) (let ((forms (interface-forms (read-module path)))) - (read (append (cdr modules) (imported-modules forms #f)) + (next (append (cdr modules) (imported-modules forms #f)) (cons path seen) - (append acc (declared-types forms))))))))) + (cons (declared-types forms) acc)))))))) (define (public-fn-interface raw-form) ;; (pub fn name args ret . body) -> prototype, keeping a docstring so