Compare commits
48 Commits
c317d9950c
...
62b1ead3eb
| Author | SHA1 | Date | |
|---|---|---|---|
| 62b1ead3eb | |||
| 6e150aaaf1 | |||
| f84010eda8 | |||
| 2279c371f2 | |||
| 44101c0c06 | |||
| c97ae8583b | |||
| f87e9faa8f | |||
| 8f152c2593 | |||
| 70aafb70e6 | |||
| dcd93c0d9e | |||
| 5b3f2bd421 | |||
| 1158768446 | |||
| 049d97b5e1 | |||
| 70228366ee | |||
| 541d9cd610 | |||
| 5ccbb5d7e0 | |||
| 449a53abf9 | |||
| e29c6ee5c9 | |||
| 981eb6f755 | |||
| dc15aaadab | |||
| b998d298dd | |||
| 19090414b7 | |||
| 9fd40da8b1 | |||
| 7abb125932 | |||
| b019da3fb3 | |||
| a5520bf4b9 | |||
| b8098f1508 | |||
| 9057cbfcc6 | |||
| 7f0b194d2b | |||
| 7532c72dec | |||
| f3d9c95daf | |||
| cc0f7f91ec | |||
| ab4fdc54f3 | |||
| f0a1b6712f | |||
| ba45069484 | |||
| 8db9246389 | |||
| 6f29a9c203 | |||
| bc5f23881f | |||
| dfdca46e1e | |||
| 8bc27cba26 | |||
| db5507b180 | |||
| 5f130f4f32 | |||
| b5030e3824 | |||
| 8f7b26eafa | |||
| 958978efd3 | |||
| 063770d3bc | |||
| a885c21b9b | |||
| a0b323980f |
4
Makefile
4
Makefile
@@ -109,6 +109,7 @@ SEX_TEST_PROGRAMS = c11 \
|
|||||||
closures \
|
closures \
|
||||||
comments \
|
comments \
|
||||||
compound-literals \
|
compound-literals \
|
||||||
|
encapsulation \
|
||||||
feature-flags \
|
feature-flags \
|
||||||
features \
|
features \
|
||||||
fixpoint \
|
fixpoint \
|
||||||
@@ -127,8 +128,7 @@ SEX_TEST_PROGRAMS = c11 \
|
|||||||
type-shapes \
|
type-shapes \
|
||||||
unicode \
|
unicode \
|
||||||
unnamed-params \
|
unnamed-params \
|
||||||
wildcards \
|
wildcards
|
||||||
worked-example
|
|
||||||
|
|
||||||
# Multi-module linking is checked end to end; see tests/modules/Makefile.
|
# Multi-module linking is checked end to end; see tests/modules/Makefile.
|
||||||
check-modules: sexc
|
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
|
~sexc~ looks for them beside itself, so a work tree needs nothing set
|
||||||
up; installed, ~$(bindir)/sexc~ finds them under ~$(prefix)/share/sex~.
|
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
|
- [[file:lib/threading-macros.org][threading-macros]] --- ~->~, threading a value through a chain of calls
|
||||||
~->~ 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)~.
|
|
||||||
|
|
||||||
** Read-time feature expressions
|
** Read-time feature expressions
|
||||||
Sex is able to use ~#+~ and ~#-~ for conditional compilation: the form that
|
Sex is able to use ~#+~ and ~#-~ for conditional compilation: the form that
|
||||||
|
|||||||
@@ -226,8 +226,7 @@ forms, and what remains."
|
|||||||
;; is the fmt-c directive for the `.' operator (see sex-fmt-c).
|
;; is the fmt-c directive for the `.' operator (see sex-fmt-c).
|
||||||
(('dot-access . rest) (cons '%. (map walk-expr rest)))
|
(('dot-access . rest) (cons '%. (map walk-expr rest)))
|
||||||
;; `%->' is the C arrow. Nothing writes it: it is what a member
|
;; `%->' 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
|
;; path lowers to where the thing it reads from is a pointer
|
||||||
;; name `->' itself being the threading macro
|
|
||||||
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
||||||
(('var . _) (walk-var form))
|
(('var . _) (walk-var form))
|
||||||
;; semen has told a type from an expression; only it knows the names
|
;; semen has told a type from an expression; only it knows the names
|
||||||
|
|||||||
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)~.
|
||||||
24
semen.scm
24
semen.scm
@@ -591,11 +591,11 @@
|
|||||||
(cons walk-embed-result (walk-body expansion env)))))
|
(cons walk-embed-result (walk-body expansion env)))))
|
||||||
(else
|
(else
|
||||||
(case (car form)
|
(case (car form)
|
||||||
((do case case/fallthrough default)
|
((do default)
|
||||||
(parameterize ((operand-position? #f))
|
(parameterize ((operand-position? #f))
|
||||||
(with-scope env (lambda () (walk-parts form env)))))
|
(with-scope env (lambda () (walk-parts form env)))))
|
||||||
|
|
||||||
((for while if switch)
|
((for while if switch case case/fallthrough)
|
||||||
(with-scope env (lambda () (walk-control form env))))
|
(with-scope env (lambda () (walk-control form env))))
|
||||||
|
|
||||||
((lambda)
|
((lambda)
|
||||||
@@ -931,7 +931,7 @@
|
|||||||
|
|
||||||
;;; Whether the Ith of a control form's SLOTS is read as a value: an
|
;;; 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
|
;;; `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)
|
(define (test-slot? head i slots)
|
||||||
(case head
|
(case head
|
||||||
((if) (and (even? i) (< (+ i 1) slots)))
|
((if) (and (even? i) (< (+ i 1) slots)))
|
||||||
@@ -1920,15 +1920,15 @@
|
|||||||
;; from
|
;; from
|
||||||
(core (if (eq? 'extern qualifier)
|
(core (if (eq? 'extern qualifier)
|
||||||
core
|
core
|
||||||
(resolve-wildcard
|
(let ((solved (resolve-wildcard
|
||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
core
|
core
|
||||||
(if (> (length core) 3)
|
(if (> (length core) 3)
|
||||||
(append (take core 3)
|
(append (take core 3)
|
||||||
(map bare-braces
|
(walk-operands core (drop core 3) env))
|
||||||
(walk-operands core (drop core 3) env)))
|
core))
|
||||||
core))
|
env)))
|
||||||
env)))
|
(copy-form-source! solved (map bare-braces solved)))))
|
||||||
(form (if qualifier
|
(form (if qualifier
|
||||||
(copy-form-source! resolved (cons qualifier core))
|
(copy-form-source! resolved (cons qualifier core))
|
||||||
core)))
|
core)))
|
||||||
|
|||||||
@@ -478,6 +478,13 @@ ahead of its pub forms."
|
|||||||
(reports? (sum-src "(for (var i int 0) (match s ((circle (r)) 1) (else 0)) (++ i)"
|
(reports? (sum-src "(for (var i int 0) (match s ((circle (r)) 1) (else 0)) (++ i)"
|
||||||
" (= out 1.0))")
|
" (= out 1.0))")
|
||||||
"match is a statement"))
|
"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"
|
(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)))"
|
(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))))")
|
" (match s ((circle (r)) (= out 2.0)) (else (= out 3.0))))")
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
(compilation "-- -std=c11 -pedantic-errors")
|
(compilation "-- -std=c11 -pedantic-errors")
|
||||||
(input)
|
(input)
|
||||||
(output "c11: 42 43 6 4 6")
|
(output "c11: 42 43 6 4 6 12")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; C11 is the floor, and this is the program that holds it there: the
|
;;; C11 is the floor, and this is the program that holds it there: the
|
||||||
@@ -23,8 +23,9 @@
|
|||||||
|
|
||||||
(sum shape ((square ((side int))) (dot ())))
|
(sum shape ((square ((side int))) (dot ())))
|
||||||
|
|
||||||
(var corner point #(point : 2 3))
|
(var corner _ #(point : 2 3))
|
||||||
(var unit shape (shape/square 1))
|
(var unit _ (shape/square 1))
|
||||||
|
(var pair [_ 2] #(#(point : 1 2) #(point : 3 4)))
|
||||||
|
|
||||||
(fn sides ((s shape)) int
|
(fn sides ((s shape)) int
|
||||||
(var n int 0)
|
(var n int 0)
|
||||||
@@ -38,7 +39,7 @@
|
|||||||
(var k int 1)
|
(var k int 1)
|
||||||
(var add-k (closure ((int)) int)
|
(var add-k (closure ((int)) int)
|
||||||
(closure ((v int)) int (k) (return (+ v k))))
|
(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)
|
(.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))
|
(return 0))
|
||||||
|
|||||||
@@ -3,9 +3,8 @@
|
|||||||
"-5")
|
"-5")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; The worked example from Encapsulation.org, kept here so it stays
|
;;; A recursive sum, an inherent method on it, and a spec realized for
|
||||||
;;; true: a recursive sum, an inherent method on it, and a spec
|
;;; it: every layer of encapsulation in one program, built at once.
|
||||||
;;; realized for it, which is every layer that is built at once.
|
|
||||||
|
|
||||||
(include stdio.h)
|
(include stdio.h)
|
||||||
|
|
||||||
@@ -12,7 +12,8 @@
|
|||||||
|
|
||||||
(record point ((x int) (y int)))
|
(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))))
|
((id int) (first-name (* const char))))
|
||||||
|
|
||||||
(struct raw ((a int) (b int)))
|
(struct raw ((a int) (b int)))
|
||||||
|
|||||||
@@ -6,14 +6,8 @@
|
|||||||
"one step is just the value: 7")
|
"one step is just the value: 7")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; `->' used to be C's arrow. The arrow is what `.' writes now when
|
;;; A step may be a call, a bare name, a member path or a method, and
|
||||||
;;; the thing it reads from is a pointer, which left the name free for
|
;;; the value goes in as its first argument.
|
||||||
;;; 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.
|
|
||||||
|
|
||||||
(include stdio.h)
|
(include stdio.h)
|
||||||
(import threading-macros)
|
(import threading-macros)
|
||||||
|
|||||||
233
types.scm
233
types.scm
@@ -90,122 +90,6 @@
|
|||||||
|
|
||||||
(define (type-of form) ((current-type-of) form))
|
(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)
|
(define (add-name-type! name type)
|
||||||
(hash-table-set! +name-db+ name type))
|
(hash-table-set! +name-db+ name type))
|
||||||
|
|
||||||
@@ -287,6 +171,123 @@
|
|||||||
(and tag (or (get-tag-info tag) (get-type-info tag))))
|
(and tag (or (get-tag-info tag) (get-type-info tag))))
|
||||||
info))))
|
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 matcher macro
|
||||||
;;; (type-match type
|
;;; (type-match type
|
||||||
;;; (int ...)
|
;;; (int ...)
|
||||||
|
|||||||
Reference in New Issue
Block a user