Compare commits
8 Commits
913af0b463
...
db5507b180
| Author | SHA1 | Date | |
|---|---|---|---|
| db5507b180 | |||
| 5f130f4f32 | |||
| b5030e3824 | |||
| 8f7b26eafa | |||
| 958978efd3 | |||
| 063770d3bc | |||
| a885c21b9b | |||
| a0b323980f |
4
Makefile
4
Makefile
@@ -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
|
||||
|
||||
19
Readme.org
19
Readme.org
@@ -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
|
||||
|
||||
@@ -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
16
lib/threading-macros.org
Normal 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)~.
|
||||
13
semen.scm
13
semen.scm
@@ -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
|
||||
(let ((solved (resolve-wildcard
|
||||
(copy-form-source!
|
||||
core
|
||||
(if (> (length core) 3)
|
||||
(append (take core 3)
|
||||
(map bare-braces
|
||||
(walk-operands core (drop core 3) env)))
|
||||
(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)))
|
||||
|
||||
@@ -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))))")
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)))
|
||||
|
||||
@@ -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
233
types.scm
@@ -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 ...)
|
||||
|
||||
Reference in New Issue
Block a user