Compare commits

...

10 Commits

Author SHA1 Message Date
f9809c19c4 tell what a member path walked from the fields it has left 2026-10-14 20:09:49 +03:00
31b7046048 share reading a test source and catching its error
encapsulation.scm had copies of both from codegen.scm.
2026-10-14 20:09:17 +03:00
2cfce45935 parenthesise a conditional without a helper to do it for 2026-10-14 20:09:17 +03:00
bb252dc244 tell a grouped for header by its shape, not by a var in it
(for ((= s 0) (< s n) (++ s)) ...) passed the check and reached cc as
a call of (= s 0).
2026-10-14 20:08:39 +03:00
eeb5357fd4 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.
2026-10-14 20:08:16 +03:00
1aed7cee4c compile the name spellings once, and skip a name that has none
Read from a table, the patterns were compiled again for every symbol
written; that tripled what unkebabify cost.
2026-10-14 20:07:20 +03:00
4ada1059a2 name the test for output going to stdout
-o - was taught to two copies of it.
2026-10-14 20:07:03 +03:00
9a6e2dcd83 keep the parameter-type rule in types, where both readers find it
sex-modules spelled out its own copy of named-arg-type, and its own
strip-pub; a parameter shape taught to one would not reach the other.
2026-10-14 20:06:51 +03:00
00e75dee48 declare an ops method ahead through the one prototype accessor
fn-prototype repeated sex-fn-prototype, and names? repeated mentions?;
each pair kept the better half.
2026-10-14 20:06:51 +03:00
2904be4499 drop walk-literal, which the rebase left without callers
walk-initializer took its place, but walked the values as statements;
a match among them is refused again wherever the literal stands.
2026-10-14 20:05:29 +03:00
10 changed files with 127 additions and 147 deletions

View File

@@ -95,10 +95,10 @@ forms, and what remains."
`(%begin ,@(map walk-expr comments) ,form)))
;;; (for init test step body ...). C's parens around the header, carried
;;; over, leave a call of a `var' in init's slot and the body in test's
;;; over, leave all three parts in init's slot and the body in test's
(define (check-for-header form slots)
(cond ((and (pair? slots) (pair? (car slots)) (pair? (caar slots))
(eq? 'var (car (caar slots))))
(cond ((and (pair? slots) (list? (car slots)) (= 3 (length (car slots)))
(every pair? (car slots)))
(sex-error form "a for header is not grouped: write (for init test step body ...)"))
((< (length slots) 3)
(sex-error form "malformed for: write (for init test step body ...)"))))
@@ -133,17 +133,19 @@ forms, and what remains."
;;; exclamation mark, both letters. Not the retroflex click ǃ, which
;;; looks more like `!' -- enough that clang warns at every use.
(define +name-spellings+
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ")))
(map (lambda (spelling) (cons (irregex (car spelling)) (cdr spelling)))
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ"))))
(define (unkebabify sym)
(case sym
((- -- -> -= / /= ! != ?) sym) ; bare operators, not names
(else
(string->symbol
(fold (lambda (spelling name)
(irregex-replace/all (car spelling) name (cdr spelling)))
(symbol->string sym)
+name-spellings+)))))
(let ((name (symbol->string sym)))
(if (or (memq sym '(- -- -> -= / /= ! != ?)) ; bare operators, not names
(not (string-index name (lambda (c) (memv c '(#\- #\/ #\? #\!))))))
sym
(string->symbol
(fold (lambda (spelling name)
(irregex-replace/all (car spelling) name (cdr spelling)))
name
+name-spellings+)))))
(define (atom-to-fmt-c atom)
(case atom

View File

@@ -428,11 +428,9 @@
(bodies (map (lambda (m)
(if (comment-form? m) (list m) (process-fn m (list))))
methods))
(early (filter (lambda (m)
(called-before? (sex-fn-name m) m methods bodies))
fns)))
(early (filter (lambda (m) (called-before? m methods bodies)) fns)))
(append (append-map identity (reverse bodies))
(fold (lambda (m acc) (process-fn (fn-prototype m) acc)) acc early)))))
(fold (lambda (m acc) (process-fn (sex-fn-prototype m) acc)) acc early)))))
;;; The method's fn form, renamed to vec2/length, once it is registered
;;; under `length'; a comment is passed along
@@ -446,28 +444,23 @@
(let ((name (second core)))
(when (get-method type name)
(sex-error form "method is already defined on" type name))
(let ((qualified (qualify-op type name)))
(let* ((qualified (qualify-op type name))
(renamed (rename-fn form qualified)))
(add-method! type name qualified)
;; its signature too, which a call's receiver is fitted to
(process-fn (fn-prototype (rename-fn form qualified)) (list))
(rename-fn form qualified))))))
(process-fn (sex-fn-prototype renamed) (list))
renamed)))))
;;; Does a body written above METHOD call it?
(define (called-before? name method methods bodies)
(let loop ((ms methods) (bs bodies))
(and (not (eq? (car ms) method))
(or (names? (car bs) name)
(loop (cdr ms) (cdr bs))))))
;;; NAME anywhere in FORM, a literal's contents included
(define (names? form name)
(cond ((eq? form name) #t)
((pair? form) (or (names? (car form) name) (names? (cdr form) name)))
((vector? form) (names? (vector->list form) name))
(else #f)))
(define (called-before? method methods bodies)
(let ((name (sex-fn-name method)))
(let loop ((ms methods) (bs bodies))
(and (not (eq? (car ms) method))
(or (mentions? (car bs) name)
(loop (cdr ms) (cdr bs)))))))
;;; (pub fn name args ret ...) -> (pub fn name args ret)
(define (fn-prototype form)
(define (sex-fn-prototype form)
(let ((header (strip-fn-header-comments form)))
(copy-form-source! form (take header (fn-header-length header)))))
@@ -492,25 +485,6 @@
(set! (hash-table-ref env :scopes) (list parameters))
env))
;;; `(p (* struct point))' writes the type in one piece and
;;; `(p * struct point)' spreads it over the rest; both are
;;; `(* struct point)'
(define (named-arg-type param)
(if (null? (cddr param))
(second param)
(cdr param)))
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int), and
;;; (self * const v) is ((* const v)) just as (self (* const v)) is.
;;; Anything that names nothing -- a variadic tail -- goes through
;;; untouched.
(define (arglist-types arglist)
(map (lambda (param)
(if (named-arg? param)
(list (named-arg-type param))
param))
arglist))
(define (fn-type-of fn-form)
`(fn ,(arglist-types (sex-fn-arglist fn-form))
,(sex-fn-return-type fn-form)))
@@ -791,21 +765,12 @@
(unresolve-closure-types (expression-type queried env)))))
(macroexpand form (list))))
;;; `#(v : (.get a))' is `(v){vːget(&a)}': the values are expressions
;;; like any other, and the type before `:' is not one
(define (walk-literal form env)
(let* ((parts (vector->list form))
(elements (or (and (memq ': parts) (cdr (memq ': parts))) parts)))
(list->vector
(append (drop-right parts (length elements))
(map (lambda (element) (walk-operand element env)) elements)))))
;;; #(struct point : (. p x) 2) -- the values are expressions, and the
;;; words before a `:' a type that is left as written
(define (walk-initializer form env)
(let-values (((prefix rest) (break (lambda (e) (eq? e ':))
(vector->list form))))
(let ((walk (lambda (es) (map (lambda (e) (walk-statement e env)) es))))
(let ((walk (lambda (es) (map (lambda (e) (walk-operand e env)) es))))
(list->vector
(if (null? rest)
(walk prefix)
@@ -888,9 +853,11 @@
(define (wildcard-type? type) (mentions? type '_))
(define (mentions? type word)
(cond ((eq? type word) #t)
((list? type) (any (lambda (part) (mentions? part word)) type))
;;; WORD anywhere in FORM, a literal's contents included
(define (mentions? form word)
(cond ((eq? form word) #t)
((pair? form) (or (mentions? (car form) word) (mentions? (cdr form) word)))
((vector? form) (mentions? (vector->list form) word))
(else #f)))
;;; `(each xs compare)' where `each' takes a closure: the argument is
@@ -1122,20 +1089,17 @@
(let ((base (second form)))
(let step ((expr (walk-operand base env))
(type (expression-type base env))
(read (list)) ; the fields walked, reversed
(fields (cddr form))
(told? #f))
(if (null? fields)
(copy-form-source! form expr)
(let ((target (pointer-target type)))
(unless (or type told?)
(sex-warning form "cannot type this; `.' reads it as a value"
(if (null? read)
base
`(|.| ,base ,@(reverse read)))))
(let ((walked (drop-right (cddr form) (length fields))))
(sex-warning form "cannot type this; `.' reads it as a value"
(if (null? walked) base `(|.| ,base ,@walked)))))
(step (list (if target '%-> 'dot-access) expr (car fields))
(field-type (or target type) (car fields))
(cons (car fields) read)
(cdr fields)
(or told? (not type))))))))
@@ -1961,12 +1925,6 @@ Returns #f if the form is not a function, returns the form otherwise"
(fmt #f "Form " fn-form " is not a function"))
(fourth (fn-core fn-form)))
(define (sex-fn-prototype fn-form)
"Returns all except body"
(assert (sex-fn? fn-form)
(fmt #f "Form " fn-form " is not a function"))
(take fn-form (fn-header-length fn-form)))
(define (sex-fn-body fn-form)
(assert (sex-fn? fn-form)
(fmt #f "Form " fn-form " is not a function"))

View File

@@ -909,24 +909,23 @@
;; It groups to the right, so its else arm is written at
;; `conditional-else', just looser: a chain there stays bare,
;; `a ? x : b ? y : z', and an assignment does not.
(define (c-if/expr . args)
(c-maybe-paren '? (apply c-if/expr/aux args)))
(define (c-if/expr/aux c p . rest)
(let lp ((c c) (p p) (ls rest))
(cond
((or (eq? c 'else) (eq? c #t))
(if (not (null? ls))
(error "forms after else clause in IF" c p ls)
(c-expr p)))
((pair? ls)
(cat (c-in-test (c-expr c)) " ? " (c-expr p) " : "
(c-with-op 'conditional-else
(if (pair? (cdr ls))
(lp (car ls) (cadr ls) (cddr ls))
(lp 'else (car ls) '())))))
(else
(c-or (c-in-test (c-expr c)) (c-expr p))))))
(define (c-if/expr c p . rest)
(c-maybe-paren
'?
(let lp ((c c) (p p) (ls rest))
(cond
((or (eq? c 'else) (eq? c #t))
(if (not (null? ls))
(error "forms after else clause in IF" c p ls)
(c-expr p)))
((pair? ls)
(cat (c-in-test (c-expr c)) " ? " (c-expr p) " : "
(c-with-op 'conditional-else
(if (pair? (cdr ls))
(lp (car ls) (cadr ls) (cddr ls))
(lp 'else (car ls) '())))))
(else
(c-or (c-in-test (c-expr c)) (c-expr p)))))))
(define (c-if . args)
(fmt-if fmt-expression?

View File

@@ -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
@@ -198,13 +196,7 @@
(if (list? fields) (map second (normalize-fields fields)) (list)))
(define (signature-types args ret)
(cons ret (map arg-type (remove comment-form? args))))
;;; (f1 float) -> float, (n unsigned int) -> (unsigned int), (int) -> (int)
(define (arg-type arg)
(cond ((not (named-arg? arg)) arg)
((null? (cddr arg)) (second arg))
(else (cdr arg))))
(cons ret (arglist-types (remove comment-form? args))))
;;; Each name TYPE mentions, and whether it is behind a pointer:
;;;
@@ -247,7 +239,7 @@
(filter-map
(lambda (form)
(and (pair? form)
(let* ((core (if (pub-form? form) (cdr form) form))
(let* ((core (strip-pub form))
(name (and (pair? core) (pair? (cdr core))
(if (and (eq? (car core) 'define) (pair? (second core)))
(car (second core))
@@ -263,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?)
@@ -292,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

View File

@@ -142,8 +142,11 @@
'stdin
(car rest-args))))
(define (stdout-output? output)
(or (eq? output 'default) (equal? output "-")))
(define (write-to-file-or-stdout output what)
(if (or (eq? output 'default) (equal? output "-"))
(if (stdout-output? output)
(what)
(with-output-to-file output
(fn (what)))))
@@ -169,7 +172,7 @@
modules)))))
(define (write-dependencies depfile output input)
(when (or (eq? output 'default) (equal? output "-"))
(when (stdout-output? output)
(error "--depfile needs -o, to name what depends on the modules"))
(with-output-to-file depfile
(lambda ()

View File

@@ -15,12 +15,15 @@
semen
utils)
(define (read-source source)
(with-input-from-string source
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port))))))
(define (sex->c source)
"Compile SOURCE, a string of Sex, and return the generated C."
(let ((forms (with-input-from-string source
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port)))))))
(let ((forms (read-source source)))
(with-output-to-string
(lambda ()
(parameterize ((sex-line-directives 'none))
@@ -37,17 +40,20 @@
(define (emits? source fragment)
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
(define (error-message source)
"Compile SOURCE and return the error text as the user sees it --
message plus arguments, the way CHICKEN prints it -- or #f if SOURCE
compiles."
(define (failure-message thunk)
"The error THUNK raises as the user sees it -- message plus arguments,
the way CHICKEN prints it -- or #f if it raises none."
(handle-exceptions e
(with-output-to-string
(lambda ()
(display ((condition-property-accessor 'exn 'message) e))
(for-each (lambda (a) (display " ") (write a))
((condition-property-accessor 'exn 'arguments) e))))
(begin (sex->c source) #f)))
(begin (thunk) #f)))
(define (error-message source)
"Compile SOURCE and return its error text, or #f if it compiles."
(failure-message (lambda () (sex->c source))))
(define (reports? source fragment)
(let ((m (error-message source)))
@@ -338,6 +344,9 @@ whitespace aside, THEN?"
(test-assert "a grouped header is named"
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i))")
"header is not grouped"))
(test-assert "whatever its init is"
(reports? (in-fn "(for ((= a 0) (< a 4) (++ a)) (g a))")
"header is not grouped"))
(test-assert "even when the body fills the slots it left"
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i) (g i) (g i))")
"header is not grouped"))

View File

@@ -5,23 +5,18 @@
;;; is checked here is what the compiler refuses, and that it refuses
;;; it with the name of whatever is wrong.
;;;
;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm,
;;; which run.scm includes first.
;;; `sex->c', `read-source', `failure-message', `emits?', `reports?'
;;; and `in-fn' come from codegen.scm, which run.scm includes first.
(import sex-modules)
(define (interface-of source)
"The declarations SOURCE, compiled as a module, hands its importers
ahead of its pub forms."
(check-public-interface
(with-input-from-string source
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port)))))))
(check-public-interface (read-source source)))
(define (interface-reports? source fragment)
(let ((m (handle-exceptions e ((condition-property-accessor 'exn 'message) e)
(begin (interface-of source) #f))))
(let ((m (failure-message (lambda () (interface-of source)))))
(and m (string-contains m fragment) #t)))
(define (ops-src . body)

View File

@@ -42,6 +42,9 @@
type-head?
named-arg?
named-arg-type
arglist-types
strip-pub
array-bound?
array-element-type)
"../types.scm")

View File

@@ -42,6 +42,9 @@
type-head?
named-arg?
named-arg-type
arglist-types
strip-pub
array-bound?
array-element-type)
"types.scm")

View File

@@ -389,6 +389,25 @@
(pair? (cdr arg)) ; 1 element args are always type
(not (type-head? arg))))
;;; `(p (* struct point))' writes the type in one piece and
;;; `(p * struct point)' spreads it over the rest; both are
;;; `(* struct point)'
(define (named-arg-type param)
(if (null? (cddr param))
(second param)
(cdr param)))
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int), and
;;; (self * const v) is ((* const v)) just as (self (* const v)) is.
;;; Anything that names nothing -- a variadic tail -- goes through
;;; untouched.
(define (arglist-types arglist)
(map (lambda (param)
(if (named-arg? param)
(list (named-arg-type param))
param))
arglist))
;;; The last element is a bound unless what precedes it is still
;;; waiting for a type name -- a `*', a qualifier, a tag's keyword. C
;;; spells a type with one name at most, so a name after a name, or