Compare commits

..

11 Commits

Author SHA1 Message Date
6f7b28cfa5 drop the default SEXC from the check scripts' usage 2026-10-19 18:23:20 +03:00
76a521b4b9 build the programs the way the modules are built
Each program had its own recipe: a hand-written link list, a cd into
tests/ so its includes would not find the modules' namesakes, and a
renamed output so csc would not overwrite the module sexc.o. Read off
their imports like the modules', and compiled to an object named after
their source, they share one rule; include-relative ends the cd.
2026-10-19 18:23:20 +03:00
20151ece1b take every program in tests/sex-programs as a test
A program left out of the hand-kept list was never run, and nothing said
so; the one module the tests import moves to lib/ instead.
2026-10-19 18:23:20 +03:00
7547deb8dd check modules and exit codes with the test egg
Written as shell inside make, a check said only that it failed and left
the reason to rerunning it by hand; now each says what it expected.
2026-10-19 18:23:20 +03:00
4b5f3ffd6b read the module graph off the import forms
Each module's prerequisite line restated its import form, so adding a
module or an import meant editing the Makefile to match, and a missed
line only showed when an object came out of order.
2026-10-19 18:23:14 +03:00
a6e9b3c948 build the unit tests and sextest from the root
Each subdirectory's Makefile repeated the compiler's module list just to
link one program, and its result was copied back to the root anyway.
2026-10-19 18:23:14 +03:00
b49f72e387 keep the local egg repository in a file of its own
The Makefile was mostly about where the eggs live; what sexc is built
from is easier to find without it.
2026-10-19 18:23:14 +03:00
ed369a8ff7 make each compiler module one file, built once
A module was a body plus a (module ...) wrapper naming its exports, and
the wrapper came in up to three copies: the root's, a test one that
exported everything, and sextest's, each recompiling the same body.

csc -module makes a file a module by itself, with export forms in its
body, so the wrappers are gone. tests/ and tools/sextest/ link what the
root built, finding it on the repository path; the tests see what a
module exports, and the names only they need are exported as such.
2026-10-19 18:23:14 +03:00
540f5c75cd tidy what the fixes wrote around existing helpers
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m55s
Sex CI / build-linux (push) Successful in 5m57s
written-type? matches the shapes instead of measuring them, array-form?
and pub-form? replace their spelled-out copies, both test files share
one fails-with?, and moved test comments are back above their groups.
2026-10-19 18:21:19 +03:00
a4880f1b96 process an ops method's prototype once
Registering a method processed its prototype only for the signature it
records, and a method called from above was processed again to be
declared ahead.
2026-10-19 18:21:10 +03:00
64db505e6b work out what a module exports once per unit
Every module's interface check walks the pub imports below it, so a
chain reduced each module's interface once per level above it.
2026-10-19 18:21:02 +03:00
4 changed files with 43 additions and 33 deletions

View File

@@ -429,12 +429,16 @@
(sex-error form "ops needs a type name" form)) (sex-error form "ops needs a type name" form))
(let* ((methods (map (lambda (m) (register-op type m)) (cddr form))) (let* ((methods (map (lambda (m) (register-op type m)) (cddr form)))
(fns (remove comment-form? methods)) (fns (remove comment-form? methods))
;; the signatures too, which a call's receiver is fitted to
(protos (map (lambda (m) (process-fn (sex-fn-prototype m) (list))) fns))
(bodies (map (lambda (m) (bodies (map (lambda (m)
(if (comment-form? m) (list m) (process-fn m (list)))) (if (comment-form? m) (list m) (process-fn m (list))))
methods)) methods))
(early (filter (lambda (m) (called-before? m methods bodies)) fns))) (early (filter-map (lambda (m proto)
(append (append-map identity (reverse bodies)) (and (called-before? m methods bodies) proto))
(fold (lambda (m acc) (process-fn (sex-fn-prototype m) acc)) acc early))))) fns protos)))
(append (concatenate (reverse bodies))
(fold append acc early)))))
;;; The method's fn form, renamed to vec2/length, once it is registered ;;; The method's fn form, renamed to vec2/length, once it is registered
;;; under `length'; a comment is passed along ;;; under `length'; a comment is passed along
@@ -451,8 +455,6 @@
(let* ((qualified (qualify-op type name)) (let* ((qualified (qualify-op type name))
(renamed (rename-fn form qualified))) (renamed (rename-fn form qualified)))
(add-method! type name qualified) (add-method! type name qualified)
;; its signature too, which a call's receiver is fitted to
(process-fn (sex-fn-prototype renamed) (list))
renamed))))) renamed)))))
;;; Does a body written above METHOD call it? ;;; Does a body written above METHOD call it?
@@ -797,14 +799,12 @@
;;; (* char) (* const char) (* size-t) (unsigned int) types ;;; (* char) (* const char) (* size-t) (unsigned int) types
;;; (* p) (* (g)) (¤ xs 0) expressions ;;; (* p) (* (g)) (¤ xs 0) expressions
(define (written-type? form env) (define (written-type? form env)
(and (pair? form) (match form
;; the length a dereference and a subscript have ((or ('* operand) ('¤ operand _))
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1)))
(let ((operand (second form)))
(if (symbol? operand) (if (symbol? operand)
(not (or (lookup-name env operand) (get-name-type operand))) (not (or (lookup-name env operand) (get-name-type operand)))
(written-type? operand env))) (written-type? operand env)))
(type-head? form)))) (_ (type-head? form))))
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'. ;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
(define (resolve-wildcard form env) (define (resolve-wildcard form env)
@@ -1145,7 +1145,7 @@
;;; A field of a path, indexed or not: xs, [xs 0] -> xs ;;; A field of a path, indexed or not: xs, [xs 0] -> xs
(define (field-name field) (define (field-name field)
(if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field))) (if (and (array-form? field) (pair? (cdr field)))
(second field) (second field)
field)) field))

View File

@@ -196,8 +196,7 @@
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list))) (if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
(remove comment-form? variants))) (remove comment-form? variants)))
(('ops type . methods) (('ops type . methods)
(append-map interface-types (append-map interface-types (filter pub-form? methods)))
(filter (lambda (m) (and (pair? m) (eq? (car m) 'pub))) methods)))
(('pub 'realize spec . methods) (('pub 'realize spec . methods)
(append-map (lambda (m) (interface-types (cons 'pub m))) (append-map (lambda (m) (interface-types (cons 'pub m)))
(remove comment-form? methods))) (remove comment-form? methods)))
@@ -301,10 +300,23 @@
(let ((path (locate-module (symbol->string (car modules))))) (let ((path (locate-module (symbol->string (car modules)))))
(if (or (not path) (member path seen)) (if (or (not path) (member path seen))
(next (cdr modules) seen acc) (next (cdr modules) seen acc)
(let ((forms (interface-forms (read-module path)))) (let ((exports (module-exports path)))
(next (append (cdr modules) (imported-modules forms #f)) (next (append (cdr modules) (cdr exports))
(cons path seen) (cons path seen)
(cons (declared-types forms) acc)))))))) (cons (car exports) acc))))))))
;;; The types the module at PATH declares to its importers, and the
;;; modules its interface imports; worked out once, though the check of
;;; every module above it on a pub import chain walks there
(define +module-exports+ (list))
(define (module-exports path)
(cond ((assoc path +module-exports+) => cdr)
(else (let* ((forms (interface-forms (read-module path)))
(exports (cons (declared-types forms)
(imported-modules forms #f))))
(set! +module-exports+ (cons (cons path exports) +module-exports+))
exports))))
(define (public-fn-interface raw-form) (define (public-fn-interface raw-form)
;; (pub fn name args ret . body) -> prototype, keeping a docstring so ;; (pub fn name args ret . body) -> prototype, keeping a docstring so

View File

@@ -51,13 +51,12 @@ the way CHICKEN prints it -- or #f if it raises none."
((condition-property-accessor 'exn 'arguments) e)))) ((condition-property-accessor 'exn 'arguments) e))))
(begin (thunk) #f))) (begin (thunk) #f)))
(define (error-message source) (define (fails-with? thunk fragment)
"Compile SOURCE and return its error text, or #f if it compiles." (let ((m (failure-message thunk)))
(failure-message (lambda () (sex->c source)))) (and m (string-contains m fragment) #t)))
(define (reports? source fragment) (define (reports? source fragment)
(let ((m (error-message source))) (fails-with? (lambda () (sex->c source)) fragment))
(and m (string-contains m fragment) #t)))
(define (followed-by? source first then) (define (followed-by? source first then)
"Is FIRST in the C compiled from SOURCE, and the next thing past it, "Is FIRST in the C compiled from SOURCE, and the next thing past it,
@@ -90,9 +89,6 @@ whitespace aside, THEN?"
(test-group "codegen" (test-group "codegen"
;; c-switch handed its scrutinee straight to `cat', which only works
;; when it is an atom. Anything else was displayed as a raw
;; s-expression: `switch ((%. e type))'.
;; A case body shared the switch's scope, so two cases could not ;; A case body shared the switch's scope, so two cases could not
;; declare one name, and a label could not be followed by a ;; declare one name, and a label could not be followed by a
;; declaration before C23. Only a body that declares needs a block ;; declaration before C23. Only a body that declares needs a block
@@ -109,6 +105,9 @@ whitespace aside, THEN?"
(not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))") (not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))")
"{" "{")))) "{" "{"))))
;; c-switch handed its scrutinee straight to `cat', which only works
;; when it is an atom. Anything else was displayed as a raw
;; s-expression: `switch ((%. e type))'.
(test-group "switch scrutinee" (test-group "switch scrutinee"
(test-assert "member access" (test-assert "member access"
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))" (emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"
@@ -336,8 +335,6 @@ whitespace aside, THEN?"
(test-assert "pub with nothing to define" (test-assert "pub with nothing to define"
(reports? "(pub 1)" "codegen.sex:1:"))) (reports? "(pub 1)" "codegen.sex:1:")))
;; A nested pointer chain used to silently lose a level:
;; (var p (* (* char))) emitted `char *p'
;; A grouped header, C's parens carried over, died in the writer with ;; A grouped header, C's parens carried over, died in the writer with
;; `(caddr) bad argument type' and no location ;; `(caddr) bad argument type' and no location
(test-group "malformed for" (test-group "malformed for"
@@ -357,6 +354,8 @@ whitespace aside, THEN?"
(reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))" (reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))"
"codegen.sex:2:"))) "codegen.sex:2:")))
;; A nested pointer chain used to silently lose a level:
;; (var p (* (* char))) emitted `char *p'
(test-group "malformed types are rejected" (test-group "malformed types are rejected"
(test-assert "nested pointer chain" (test-assert "nested pointer chain"
(reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat")) (reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat"))

View File

@@ -5,7 +5,7 @@
;;; is checked here is what the compiler refuses, and that it refuses ;;; is checked here is what the compiler refuses, and that it refuses
;;; it with the name of whatever is wrong. ;;; it with the name of whatever is wrong.
;;; ;;;
;;; `sex->c', `read-source', `failure-message', `emits?', `reports?' ;;; `sex->c', `read-source', `fails-with?', `emits?', `reports?'
;;; and `in-fn' come from codegen.scm, which run.scm includes first. ;;; and `in-fn' come from codegen.scm, which run.scm includes first.
(import sex-modules) (import sex-modules)
@@ -16,8 +16,7 @@ ahead of its pub forms."
(check-public-interface (read-source source))) (check-public-interface (read-source source)))
(define (interface-reports? source fragment) (define (interface-reports? source fragment)
(let ((m (failure-message (lambda () (interface-of source))))) (fails-with? (lambda () (interface-of source)) fragment))
(and m (string-contains m fragment) #t)))
(define (ops-src . body) (define (ops-src . body)
(string-append (string-append