Compare commits

..

8 Commits

Author SHA1 Message Date
db5507b180 give a record's docstring a line of its own
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m43s
Sex CI / build-linux (push) Successful in 5m42s
2026-10-19 15:56:05 +03:00
5f130f4f32 order types.scm by what each part reads
The lookups came after the tables built on them, type-key after the
sums filed under it, and reset-declarations! before +ops-db+.
2026-10-19 15:56:05 +03:00
b5030e3824 say what the threading test checks, not how -> came free 2026-10-19 15:56:05 +03:00
8f7b26eafa name the encapsulation test after what it covers 2026-10-19 15:56:05 +03:00
958978efd3 keep the threading macro out of the writer's arrow comment 2026-10-19 15:56:05 +03:00
063770d3bc document the standard library beside it, not in the readme 2026-10-19 15:56:05 +03:00
a885c21b9b infer a global's type before its braces lose theirs
A global's initializer was stripped to plain braces before `_' was
solved, so #(point : 2 3) left nothing to infer point from.
2026-10-19 15:33:37 +03:00
a0b323980f refuse a match in a case label
case and case/fallthrough walked their label as a statement, so a match
there was taken and wrote `case switch:'.
2026-10-19 15:32:47 +03:00
11 changed files with 171 additions and 165 deletions

View File

@@ -109,6 +109,7 @@ SEX_TEST_PROGRAMS = c11 \
closures \
comments \
compound-literals \
encapsulation \
feature-flags \
features \
fixpoint \
@@ -126,8 +127,7 @@ SEX_TEST_PROGRAMS = c11 \
type-shapes \
unicode \
unnamed-params \
wildcards \
worked-example
wildcards
# Multi-module linking is checked end to end; see tests/modules/Makefile.
check-modules: sexc

View File

@@ -176,24 +176,9 @@ same name wins over one of ours.
~sexc~ looks for them beside itself, so a work tree needs nothing set
up; installed, ~$(bindir)/sexc~ finds them under ~$(prefix)/share/sex~.
There is one so far.
Each module is documented in a file of its own beside it:
*** threading-macros
~->~ passes a value through a chain of forms, each step taking what came
before it as its first argument, so the chain reads in the order it
happens instead of inside out. Clojure spells it this way and this is
the same thing:
#+begin_src scheme
(import threading-macros)
(-> 5 double-it (add 3) double-it) ; (double-it (add (double-it 5) 3))
(-> lp (. a x)) ; lp->a->x
(-> q .length-sq) ; a method is a step like any other
#+end_src
A step that is not a list is a call of one argument: ~(-> x f)~ is
~(f x)~.
- [[file:lib/threading-macros.org][threading-macros]] --- ~->~, threading a value through a chain of calls
** Read-time feature expressions
Sex is able to use ~#+~ and ~#-~ for conditional compilation: the form that

View File

@@ -191,8 +191,7 @@ forms, and what remains."
;; is the fmt-c directive for the `.' operator (see sex-fmt-c).
(('dot-access . rest) (cons '%. (map walk-expr rest)))
;; `%->' is the C arrow. Nothing writes it: it is what a member
;; path lowers to where the thing it reads from is a pointer, the
;; name `->' itself being the threading macro
;; path lowers to where the thing it reads from is a pointer
(('%-> . rest) (cons '%-> (map walk-expr rest)))
(('var . _) (walk-var form))
;; An expression has no room for a statement, so a comment in a

16
lib/threading-macros.org Normal file
View File

@@ -0,0 +1,16 @@
* threading-macros
~->~ passes a value through a chain of forms, each step taking what came
before it as its first argument, so the chain reads in the order it
happens instead of inside out. Clojure spells it this way and this is
the same thing:
#+begin_src scheme
(import threading-macros)
(-> 5 double-it (add 3) double-it) ; (double-it (add (double-it 5) 3))
(-> lp (. a x)) ; lp->a->x
(-> q .length-sq) ; a method is a step like any other
#+end_src
A step that is not a list is a call of one argument: ~(-> x f)~ is
~(f x)~.

View File

@@ -591,7 +591,10 @@
((for while if switch)
(with-scope env (lambda () (walk-control form env))))
((case case/fallthrough default)
((case case/fallthrough)
(walk-control form env))
((default)
(parameterize ((operand-position? #f))
(walk-parts form env)))
@@ -899,7 +902,7 @@
;;; Whether the Ith of a control form's SLOTS is read as a value: an
;;; `if''s tests but not its trailing else, a `for''s header, the
;;; scrutinee of a `while' or a `switch'
;;; scrutinee of a `while' or a `switch', a `case''s label
(define (test-slot? head i slots)
(case head
((if) (and (even? i) (< (+ i 1) slots)))
@@ -1873,15 +1876,15 @@
;; from
(core (if (eq? 'extern qualifier)
core
(resolve-wildcard
(copy-form-source!
core
(if (> (length core) 3)
(append (take core 3)
(map bare-braces
(walk-operands core (drop core 3) env)))
core))
env)))
(let ((solved (resolve-wildcard
(copy-form-source!
core
(if (> (length core) 3)
(append (take core 3)
(walk-operands core (drop core 3) env))
core))
env)))
(copy-form-source! solved (map bare-braces solved)))))
(form (if qualifier
(copy-form-source! resolved (cons qualifier core))
core)))

View File

@@ -452,6 +452,13 @@
(reports? (sum-src "(for (var i int 0) (match s ((circle (r)) 1) (else 0)) (++ i)"
" (= out 1.0))")
"match is a statement"))
(test-assert "nor as a case label"
(reports? (sum-src "(switch 1 (case (match s ((circle (r)) 1) (else 0)) (= out 1.0)))")
"match is a statement"))
(test-assert "nor as a case/fallthrough's"
(reports? (sum-src "(switch 1 (case/fallthrough (match s ((circle (r)) 1) (else 0))"
" (= out 1.0)))")
"match is a statement"))
(test-assert "but an if's branches are statements, the else among them"
(emits? (sum-src "(if (== out 0.0) (match s ((circle (r)) (= out r)) (else (= out 1.0)))"
" (match s ((circle (r)) (= out 2.0)) (else (= out 3.0))))")

View File

@@ -1,6 +1,6 @@
(compilation "-- -std=c11 -pedantic-errors")
(input)
(output "c11: 42 43 6 4 6")
(output "c11: 42 43 6 4 6 12")
(return 0)
;;; C11 is the floor, and this is the program that holds it there: the
@@ -23,8 +23,9 @@
(sum shape ((square ((side int))) (dot ())))
(var corner point #(point : 2 3))
(var unit shape (shape/square 1))
(var corner _ #(point : 2 3))
(var unit _ (shape/square 1))
(var pair [_ 2] #(#(point : 1 2) #(point : 3 4)))
(fn sides ((s shape)) int
(var n int 0)
@@ -38,7 +39,7 @@
(var k int 1)
(var add-k (closure ((int)) int)
(closure ((v int)) int (k) (return (+ v k))))
(printf "c11: %d %d %d %d %d\n"
(printf "c11: %d %d %d %d %d %d\n"
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2)
(sides unit) (.area corner))
(sides unit) (.area corner) (.area [pair 1]))
(return 0))

View File

@@ -3,9 +3,8 @@
"-5")
(return 0)
;;; The worked example from Encapsulation.org, kept here so it stays
;;; true: a recursive sum, an inherent method on it, and a spec
;;; realized for it, which is every layer that is built at once.
;;; A recursive sum, an inherent method on it, and a spec realized for
;;; it: every layer of encapsulation in one program, built at once.
(include stdio.h)

View File

@@ -12,7 +12,8 @@
(record point ((x int) (y int)))
(record named "Fields of more than one kind, and a docstring."
(record named
"Fields of more than one kind, and a docstring."
((id int) (first-name (* const char))))
(struct raw ((a int) (b int)))

View File

@@ -6,14 +6,8 @@
"one step is just the value: 7")
(return 0)
;;; `->' used to be C's arrow. The arrow is what `.' writes now when
;;; the thing it reads from is a pointer, which left the name free for
;;; threading -- the first module of the standard library.
;;;
;;; What this pins is that a step may be a call, a bare name or a
;;; method, that the value is threaded into the *first* argument, and
;;; that the compiler's own generated arrows survived the rename --
;;; `ops' below lifts a method, which is where the captures live.
;;; A step may be a call, a bare name, a member path or a method, and
;;; the value goes in as its first argument.
(include stdio.h)
(import threading-macros)

233
types.scm
View File

@@ -90,122 +90,6 @@
(define (type-of form) ((current-type-of) form))
;;; A sum's variants, in declaration order:
;;;
;;; (sum shape ((circle ((r float))) (empty ())))
;;; -> shape -> ((circle (r float)) (empty))
;;;
;;; Fields are normalized, so a caller counts them and reads their
;;; types without re-parsing the declaration.
(define +sum-db+ (make-hash-table))
(define (add-sum! name variants)
(hash-table-set! +sum-db+ (type-key name) variants))
(define (get-sum name)
(hash-table-ref/default +sum-db+ (type-key name) #f))
;;; shape/circle -> (shape . circle), for each variant of each sum
(define +constructor-db+ (make-hash-table))
(define (add-constructor! constructor sum variant)
(hash-table-set! +constructor-db+ constructor (cons sum variant)))
(define (get-constructor name)
(hash-table-ref/default +constructor-db+ name #f))
(define (variant-fields variants name)
(let ((entry (assq name variants)))
(and entry (cdr entry))))
;;; A spec is a parameter and a list of methods, each of which either
;;; declares a signature or also carries a default body:
;;;
;;; (spec (printable T) (fn show ((self (* const T))) void))
;;; -> printable -> (T (fn show ((self (* const T))) void))
;;;
;;; And a realization is only ever asked whether it exists -- the
;;; methods it provides land in +ops-db+ like any others.
(define +spec-db+ (make-hash-table))
(define +realize-db+ (make-hash-table))
(define (add-spec! name parameter methods)
(hash-table-set! +spec-db+ name (cons parameter methods)))
(define (get-spec name)
(hash-table-ref/default +spec-db+ name #f))
(define (spec-parameter spec) (car spec))
(define (spec-methods spec) (cdr spec))
(define (add-realization! spec type)
(hash-table-set! +realize-db+ (cons spec (type-key type)) #t))
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
(define (realizes? spec type)
(hash-table-exists? +realize-db+ (cons spec (type-key type))))
;;; Both of these refuse a second declaration of the same name, which
;;; is per translation unit rather than per process -- so a process
;;; that compiles two units has to start the second one clean.
(define (reset-declarations!)
(set! +sum-db+ (make-hash-table))
(set! +constructor-db+ (make-hash-table))
(set! +ops-db+ (make-hash-table))
(set! +spec-db+ (make-hash-table))
(set! +realize-db+ (make-hash-table)))
;;; Methods of an `ops' block: (type . method) -> the qualified name
;;; the call is rewritten to.
;;;
;;; (ops vec2 (fn length ((self (* const vec2))) float ...))
;;; -> (vec2 . length) -> vec2/length
;;;
;;; The signature is not kept here; `vec2/length' is an ordinary
;;; function by the time anything asks, so +name-db+ already has it.
(define +ops-db+ (make-hash-table))
(define (add-method! type method qualified)
(hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
(define (get-method type method)
(hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
(define (qualify-op type name)
(string->symbol
(string-append (symbol->string type) "/" (symbol->string name))))
;;; The name an `ops' block was written against: `(struct point)' and a
;;; `const' wrapper both name `point', and anything else names nothing.
(define (ops-key type)
(cond ((symbol? type) (type-key type))
((not (and (pair? type) (symbol? (car type)))) #f)
((memq (car type) '(struct union enum))
(and (pair? (cdr type)) (symbol? (cadr type)) (cadr type)))
((memq (car type) '(const volatile)) (ops-key (cadr type)))
(else #f)))
;;; The name methods, realizations and sums are filed under. A typedef
;;; is followed to the aggregate it names, and one that ends in anything
;;; else stops at the last typedef, so a scalar does not share them:
;;;
;;; (record v ...) (typedef alias v) alias -> v
;;; (typedef meters float) meters -> meters
;;; (typedef km meters) km -> meters
(define (type-key name)
(let follow ((name name) (seen (list)))
(let ((target (and (not (memq name seen)) (typedef-target name))))
(cond ((not target) name)
((symbol? target)
(if (typedef-target target) (follow target (cons name seen)) name))
((target-tag target))
(else name)))))
(define (typedef-target name)
(let ((info (and (symbol? name) (get-type-info name))))
(and info (eq? (car info) 'typedef) (caddr info))))
(define (add-name-type! name type)
(hash-table-set! +name-db+ name type))
@@ -287,6 +171,123 @@
(and tag (or (get-tag-info tag) (get-type-info tag))))
info))))
;;; The name methods, realizations and sums are filed under. A typedef
;;; is followed to the aggregate it names, and one that ends in anything
;;; else stops at the last typedef, so a scalar does not share them:
;;;
;;; (record v ...) (typedef alias v) alias -> v
;;; (typedef meters float) meters -> meters
;;; (typedef km meters) km -> meters
(define (type-key name)
(let follow ((name name) (seen (list)))
(let ((target (and (not (memq name seen)) (typedef-target name))))
(cond ((not target) name)
((symbol? target)
(if (typedef-target target) (follow target (cons name seen)) name))
((target-tag target))
(else name)))))
(define (typedef-target name)
(let ((info (and (symbol? name) (get-type-info name))))
(and info (eq? (car info) 'typedef) (caddr info))))
;;; A sum's variants, in declaration order:
;;;
;;; (sum shape ((circle ((r float))) (empty ())))
;;; -> shape -> ((circle (r float)) (empty))
;;;
;;; Fields are normalized, so a caller counts them and reads their
;;; types without re-parsing the declaration.
(define +sum-db+ (make-hash-table))
(define (add-sum! name variants)
(hash-table-set! +sum-db+ (type-key name) variants))
(define (get-sum name)
(hash-table-ref/default +sum-db+ (type-key name) #f))
;;; shape/circle -> (shape . circle), for each variant of each sum
(define +constructor-db+ (make-hash-table))
(define (add-constructor! constructor sum variant)
(hash-table-set! +constructor-db+ constructor (cons sum variant)))
(define (get-constructor name)
(hash-table-ref/default +constructor-db+ name #f))
(define (variant-fields variants name)
(let ((entry (assq name variants)))
(and entry (cdr entry))))
;;; Methods of an `ops' block: (type . method) -> the qualified name
;;; the call is rewritten to.
;;;
;;; (ops vec2 (fn length ((self (* const vec2))) float ...))
;;; -> (vec2 . length) -> vec2/length
;;;
;;; The signature is not kept here; `vec2/length' is an ordinary
;;; function by the time anything asks, so +name-db+ already has it.
(define +ops-db+ (make-hash-table))
(define (add-method! type method qualified)
(hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
(define (get-method type method)
(hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
(define (qualify-op type name)
(string->symbol
(string-append (symbol->string type) "/" (symbol->string name))))
;;; The name an `ops' block was written against: `(struct point)' and a
;;; `const' wrapper both name `point', and anything else names nothing.
(define (ops-key type)
(cond ((symbol? type) (type-key type))
((not (and (pair? type) (symbol? (car type)))) #f)
((memq (car type) '(struct union enum))
(and (pair? (cdr type)) (symbol? (cadr type)) (cadr type)))
((memq (car type) '(const volatile)) (ops-key (cadr type)))
(else #f)))
;;; A spec is a parameter and a list of methods, each of which either
;;; declares a signature or also carries a default body:
;;;
;;; (spec (printable T) (fn show ((self (* const T))) void))
;;; -> printable -> (T (fn show ((self (* const T))) void))
;;;
;;; And a realization is only ever asked whether it exists -- the
;;; methods it provides land in +ops-db+ like any others.
(define +spec-db+ (make-hash-table))
(define +realize-db+ (make-hash-table))
(define (add-spec! name parameter methods)
(hash-table-set! +spec-db+ name (cons parameter methods)))
(define (get-spec name)
(hash-table-ref/default +spec-db+ name #f))
(define (spec-parameter spec) (car spec))
(define (spec-methods spec) (cdr spec))
(define (add-realization! spec type)
(hash-table-set! +realize-db+ (cons spec (type-key type)) #t))
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
(define (realizes? spec type)
(hash-table-exists? +realize-db+ (cons spec (type-key type))))
;;; Sums, constructors, methods and specs refuse a second declaration
;;; of the same name, which is per translation unit rather than per
;;; process -- so a process that compiles two units has to start the
;;; second one clean.
(define (reset-declarations!)
(set! +sum-db+ (make-hash-table))
(set! +constructor-db+ (make-hash-table))
(set! +ops-db+ (make-hash-table))
(set! +spec-db+ (make-hash-table))
(set! +realize-db+ (make-hash-table)))
;;; Type matcher macro
;;; (type-match type
;;; (int ...)