Compare commits

..

26 Commits

Author SHA1 Message Date
a8f8b23b49 check the constants a module's interface hands its importers
An array bound in a pub record, or a name in a pub define's body, is
the importer's to resolve too, and a private define, or one from an
import without pub, failed there as an undeclared identifier. Found in
vs-map-viewer once its units stopped importing everything. A constant
has no declaration short of itself, so it is an error at the pub form.
2026-10-09 18:21:11 +03:00
5b7ed9793b write a double in the digits that read back as it
CHICKEN prints 15 significant digits unless told otherwise, too few to
name every double: 0.9871465422297019 reached the C as
0.987146542229702, a different constant. Found in vs-map-viewer, where
380 literals in a port that has to match .NET bit for bit changed this
way. The shortest spelling that reads back is written, up to 17 digits.
2026-10-09 18:15:54 +03:00
7a15cde637 hand sexc every digit of a test program's doubles
sextest prints the program it read back to sexc, and printed doubles
in CHICKEN's default 15 digits: 0.16666666666666666 arrived as
0.166666666666667, another number.
2026-10-09 18:15:54 +03:00
4d83ae152b parenthesise what a subscript subscripts
The array was written at whatever level surrounded it, so [(* q) i]
came out *q[i] -- *(q[i]) to C, compiling with q an int ** and reading
the wrong int. The index now starts afresh too: inside a cast it had
taken the cast's level and gained parens, and so had a chain in the
else arm of ?:, which groups to the right and needs none.
2026-10-09 18:03:21 +03:00
b0275be368 print the public interface in declaration order
read-public-interface has returned the forms in order since 2512d52,
and --public-interface still reversed them as it had to before.
2026-10-09 17:47:25 +03:00
199834466d write a make rule for the modules a unit reads
An import pastes another file's forms into the output, so a Makefile
had to make every object depend on every .sex. --depfile names the
modules read, including those read only for an import's interface,
with an empty rule for each, as cc -MD -MP does.

Fixes #40.
2026-10-09 17:44:36 +03:00
360470d0e0 check what a module's importers can see of its types
An importer is given the pub forms and nothing else, so a pub form
naming a private type, or one from an import without pub, compiled in
its module and failed in every importer with `unknown type name'.

The pub forms are checked as the module itself is compiled. A record,
struct, union or sum behind a pointer needs only declaring, so its
bodyless declaration goes out ahead of them -- the opaque handle that
(pub record T) already gave by hand. Held by value it needs its body,
and a typedef or an enum has no declaration short of its definition:
those are errors at the pub form, saying what would do instead. A name
Sex has no declaration for is left to C.

An importer may now be handed a declaration after the definition it
declares, so a bodyless aggregate no longer replaces known fields.

Fixes #57, #58.
2026-10-09 17:43:09 +03:00
2d4e5f6b16 let a method call one written below it
Each method was registered just before its own body was walked, so an
earlier one could not find it. All are registered first now, and one
called above its definition is declared ahead of the bodies, which C
needs as well.

Fixes #44.
2026-10-09 17:37:03 +03:00
0d26b1f6a5 resolve a relative SEX_MODULE_PATH entry from the working directory
make-absolute-pathname with no directory of its own put `src' under
the root, so SEX_MODULE_PATH=src searched /src/.

Fixes #48.
2026-10-09 17:35:05 +03:00
ce236d876c take -o - as stdout
getopt-long reads a lone `-' as an option, so -o - died there asking
for a value, and --output=- got through only to create a file named
`-'.

Fixes #43.
2026-10-09 17:34:02 +03:00
79d9860fc1 name what a member path could not type
Past a typed base, it is a field of a struct only C knows that has no
type, and the warning named the base anyway: `font' for what was
(. font handle), with font declared on the line before.

Fixes #41.
2026-10-09 17:28:20 +03:00
b73cf36160 walk what an initializer holds
The body walk passed a #(...) by as an atom, so nothing inside one was
typed, dispatched or lifted: (. self p) read as self.p with self a
pointer, where the same path outside the literal was right.

Fixes #45.
2026-10-09 17:27:02 +03:00
baf2f92fce reject a for whose header is grouped
C's parens carried over, ((var i int 0) (< i 4) (++ i)), left the
writer short of slots, and it died in caddr with no location; with a
long enough body it would have compiled the body into the header.

Fixes #42.
2026-10-09 17:26:03 +03:00
ea0bed6deb anchor an if's arms only where the if is a statement
Every if had its arms anchored, including one inside an argument that
the writer renders as ?:, so a #line landed between its parts and the
C did not compile. -C output, with no directives, hid it.

Fixes #54.
2026-10-09 17:25:09 +03:00
1dc0c1a2fc make a declaring case body a block
A case body shared the switch's scope, so two cases could not each
declare `bits', and a label could not be followed by a declaration
before C23; inference shared the scope too, and typed a later case's
read of an outer name as the earlier case's local.

Fixes #51.
2026-10-09 17:23:42 +03:00
6583ed958c write arrays with brackets in tests
The tests spelled out (¤ ...), what [...] reads into, which nobody
writes. The reader's own tests keep it: there it is the point.
2026-10-09 16:32:36 +03:00
448b908c40 read hex and binary literals
0x80 was not a number to string->number, so it became a symbol the
printer escaped as |0x80|, and #x was rejected outright. Both spellings
now read as the integer, which the C writes in decimal.

Fixes #47.
2026-10-09 16:06:14 +03:00
f9fafd78d2 read -NAME as the negation of NAME
The reader took -INFINITY as one symbol, and the leading dash was then
mangled like any other: _INFINITY. -1 was always a number, so a name
gets the same reading.

Fixes #63.
2026-10-09 16:05:03 +03:00
08044ce4a1 let sizeof take a type that looks like an expression
(* char) is a pointer type and (* p) a dereference, and only the names
in scope tell them apart, so semen decides and marks a type for the
writer. Before, both were walked as expressions: sizeof(*char).

Fixes #52.
2026-10-09 16:04:00 +03:00
d5b003ef91 spell ? and ! in names as letters C takes
A Lisp predicate came out as ident_start?, which no C compiler reads.
ʔ and ꜝ are letters that look the part; ǃ looks closer, but clang flags
it as a homoglyph of `!' at every use.

Fixes #49.
2026-10-09 16:01:32 +03:00
df3aa1789c walk a define's body, and parenthesise it
The body was pasted as read, so `cast' stayed a call and names kept
their dashes; and fmt-c parenthesised only a function-like body, so
an object-like one regrouped with the operator it was pasted under.

Fixes #55.
2026-10-09 16:00:28 +03:00
504f35722a convert enumerators written with a value
(wire-varint 0) is a pair, and both the writer and map-fields only
expected a name: the writer passed it out unconverted, and a macro
mapping over the enum was handed the pair.

Fixes #46.
2026-10-09 15:59:16 +03:00
8774400f1f write a typedef's target as a type
It fell through to the expression walk, which read (* const char) as a
dereference and an array as a subscript, and died on a fn type.

Fixes #53.
2026-10-09 15:58:01 +03:00
9a490a942c spell an array bound as an expression
It went to fmt-c unwalked, so a bound named CHUNK-AREA kept its dash
while the same name in an expression became CHUNK_AREA.

Fixes #50.
2026-10-09 15:56:32 +03:00
97eb123a42 tell an array's bound by the shape of its type
Whether a trailing name was the bound hinged on the word before it
being a typedef Sex had seen, which a header's pthread_t never is, and
a grouped (* T) never is either: [pthread-t N] became a two-word type.

Fixes #56, #60.
2026-10-09 15:55:33 +03:00
db9801331f parenthesise casts and conditionals by precedence
Neither took part in it: a cast parenthesised its operand only from a
list of binary heads that missed `|', and never itself, so `->' took
it apart; `?:' was written bare under any operator, swallowing it into
the test. Three of the four still compiled, to wrong values.

Fixes #59, #61, #62.
2026-10-09 15:41:43 +03:00
11 changed files with 104 additions and 549 deletions

View File

@@ -150,8 +150,7 @@ may be both:
#+end_src
A receiver whose type cannot be worked out --- something out of a header
nothing has parsed --- is read as a value, with a warning saying so. For a
pointer there, ~(%-> p x)~ writes ~p->x~ whatever ~p~ is.
nothing has parsed --- is read as a value, with a warning saying so.
A method is ~(.name receiver args ...)~ and a field is ~(. receiver
name)~, so the shape of a form says which of the two it is without

353
semen.scm
View File

@@ -2,7 +2,6 @@
(import
scheme
(only (scheme base) make-parameter)
(chicken base)
(chicken keyword)
(chicken string)
@@ -277,8 +276,7 @@
(let* ((env (make-fn-env expanded))
;; the parameters are the body's outermost scope
(header (map resolve-closure-types (take expanded boundary)))
(body (parameterize ((operand-position? #f))
(walk-body (drop expanded boundary) env)))
(body (walk-body (drop expanded boundary) env))
;; A function is header and body stitched together, so the
;; result is fresh structure: without this the definition
;; has no location, and nothing downstream can say where
@@ -348,21 +346,11 @@
(append (map (lambda (g) (cons g #t)) given)
(filter-map
(lambda (w)
(and (spec-default? w)
(and (> (length w) 4) ; carries a body, so a default
(not (find (lambda (g) (eq? (second g) (second w))) given))
(cons (substitute-parameter parameter type w) #f)))
wanted)))
;;; A docstring or a comment is not a body, so
;;;
;;; (fn s ((self (* const T))) void "what s is for")
;;;
;;; is still a method every realization has to write.
(define (spec-default? method)
(let-values (((doc fn) (extract-fn-docstring (strip-fn-header-comments method))))
(any (lambda (form) (not (comment-form? form)))
(drop fn (fn-header-length fn)))))
(define (check-realization form name type parameter wanted given)
(for-each
(lambda (g)
@@ -381,7 +369,7 @@
given)
(let ((missing (filter-map
(lambda (w)
(and (not (spec-default? w))
(and (= (length w) 4) ; no body, so required
(not (find (lambda (g) (eq? (second g) (second w))) given))
(second w)))
wanted)))
@@ -479,11 +467,17 @@
(define (make-fn-env fn-form)
(let ((env (make-hash-table))
(parameters (make-hash-table)))
;; `(p (* struct point))' writes the type in one piece and
;; `(p * struct point)' spreads it over the rest, so the type is
;; the whole tail whenever there is more of it than one element
(for-each (lambda (param)
(when (named-arg? param)
(when (and (pair? param) (pair? (cdr param))
(named-arg? param))
(hash-table-set! parameters
(first param)
(named-arg-type param))))
(if (null? (cddr param))
(second param)
(cdr param)))))
(sex-fn-arglist fn-form))
(set! (hash-table-ref env :fn-name) (sex-fn-name fn-form))
(set! (hash-table-ref env :lambda-counter) 0)
@@ -492,22 +486,13 @@
(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.
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int).
;;; Anything that is not a plain (name type) -- a variadic tail -- goes
;;; through untouched.
(define (arglist-types arglist)
(map (lambda (param)
(if (named-arg? param)
(list (named-arg-type param))
(if (and (pair? param) (= 2 (length param)) (named-arg? param))
(list (second param))
param))
arglist))
@@ -573,35 +558,6 @@
(list walked))))
forms))
;;; #t while the walk is inside a value -- an initializer, an argument,
;;; a returned expression -- where a statement-only form has no C
;;; spelling:
;;;
;;; (var n int (match s ...)) -> int n = switch (s.tag) { ...
;;;
;;; A condition is not tracked: `walk-parts' takes every slot of an `if'
;;; alike.
(define operand-position? (make-parameter #f))
(define (walk-operand form env)
(parameterize ((operand-position? #t))
(walk-statement form env)))
;;; FORMS are values written inside PARENT, which is what a diagnostic
;;; about one of them points at -- a bare name has no location of its
;;; own:
;;;
;;; (var f (fn ((r float)) shape) shape/circle)
(define (walk-operands parent forms env)
(for-each (lambda (form)
(when (and (symbol? form) (get-constructor form))
(sex-error parent
"a constructor is not a function; call it from a lambda"
form)))
forms)
(parameterize ((operand-position? #t))
(walk-body forms env)))
(define (walk-statement form env)
(cond
((vector? form) (walk-initializer form env))
@@ -610,15 +566,19 @@
;; expanded here rather than before the walk, so the macro body can
;; ask `(type-of x)' about a local the walk has already passed
((macro? form)
(let ((expansion (expand-call form env)))
(let ((expansion (parameterize
((current-type-of
(lambda (queried)
(unresolve-closure-types
(expression-type queried env)))))
(macroexpand form (list)))))
(if (and (pair? expansion) (null? (cdr expansion)))
(walk-statement (car expansion) env)
(cons walk-embed-result (walk-body expansion env)))))
(else
(case (car form)
((do for while if switch case case/fallthrough default)
(parameterize ((operand-position? #f))
(with-scope env (lambda () (walk-parts form env)))))
(with-scope env (lambda () (walk-parts form env))))
((lambda)
(let ((name (aux-name! env make-lambda-name)))
@@ -635,7 +595,7 @@
(copy-form-source!
form
`(,construct ,@(map (lambda (capture)
(walk-operand (capture-argument capture)
(walk-statement (capture-argument capture)
env))
(fourth form))))))))
@@ -656,7 +616,7 @@
form
(append prefix
(if (> (length form) 3)
(walk-operands form (drop form 3) env)
(walk-body (drop form 3) env)
(list))))
env))
(bound (if (>= (length walked) 4)
@@ -673,7 +633,7 @@
bound))
((return)
(let ((walked (copy-form-source! form (walk-operands form form env))))
(let ((walked (walk-parts form env)))
(if (>= (length walked) 2)
(copy-form-source!
walked
@@ -683,10 +643,7 @@
(cddr walked))))
walked)))
((match)
(when (operand-position?)
(sex-error form "match is a statement, and has no value"))
(walk-match form env))
((match) (walk-match form env))
((dot-access) (walk-member-path form env))
@@ -699,106 +656,21 @@
(walk-parts form env)))
(else
(cond
((method-head? (car form)) (walk-method-call form env))
((get-constructor (car form))
=> (lambda (constructor) (walk-construction form constructor env)))
((receiver-closure-type (car form) env)
=> (lambda (closure)
(if (method-head? (car form))
(walk-method-call form env)
(let ((closure (receiver-closure-type (car form) env)))
(if closure
;; the receiver is walked first: `((closure ((x int))
;; int () ...) 5)' registers `struct ƛint_int' on the
;; way, and the call helper's signature names it
(let ((receiver (walk-operand (car form) env)))
(let ((receiver (walk-statement (car form) env)))
(copy-form-source!
form
`(,(register-closure-call! closure form)
,receiver
,@(walk-operands form (cdr form) env))))))
(else
(convert-arguments (copy-form-source! form (walk-operands form form env))
env))))))))
;;; (shape/circle 1.0) is the value itself, written where it is used:
;;;
;;; (shape){.tag = shapeǂcircle, .u = {.circle = (struct shapeːcircle){1.0}}}
;;;
;;; so it is a constant where one is wanted, an lvalue a method can take
;;; the address of, and a pub sum pastes nothing into the importer but
;;; its declarations. A lambda argument is converted against its field
;;; the way a call converts one against its parameter.
(define (walk-construction form constructor env)
(let* ((name (car constructor))
(tag (cdr constructor))
(fields (variant-fields (get-sum name) tag))
(args (walk-operands form (cdr form) env)))
(unless (= (length fields) (length args))
(sex-error form "variant takes this many fields" tag (length fields)))
(sum-literal name tag
(map (lambda (field arg) (field-argument field arg env form))
fields args))))
;;; An array field cannot be handed an array, C copying neither, so it
;;; is handed the array's elements:
;;;
;;; (shape/row #(1 2 3 4)) -> ...(struct shapeːrow){{1, 2, 3, 4}}
;;; (shape/row a) -> ...(struct shapeːrow){{a[0], a[1], a[2], a[3]}}
(define (field-argument field arg env form)
(let ((type (second field)))
(if (not (array-form? type))
(convert-to-closure type arg env form)
(let ((given (expression-type arg env)))
(cond
((brace-initializer? arg) arg)
((not (array-form? given))
(sex-error form "an array field takes an array" (first field)))
((not (eqv? (array-length given) (array-length type)))
(sex-error form "array is not as long as its field" (first field)))
;; `#([int 4] : 1 2 3 4)' is its own elements
((vector? arg) (list->vector (cdr (memq ': (vector->list arg)))))
((not (simple-path? arg))
(sex-error form "an array is read once per element; bind it to a var first"
(first field)))
(else (spell-out-array type arg)))))))
(define (array-form? type)
(and (pair? type) (eq? '¤ (car type))))
;;; [int 4] -> 4, and [int N] -> 4 after (define N 4); #f for a length
;;; nothing here can count
(define (array-length type)
(and (array-bound? type)
(let ((bound (last type)))
(cond ((and (integer? bound) (exact? bound)) bound)
((symbol? bound)
(let ((info (get-type-info bound)))
(and info (eq? 'define (car info))
(let ((value (caddr info)))
(and (= 1 (length value))
(integer? (car value))
(exact? (car value))
(car value))))))
(else #f)))))
;;; xs : [int 2] -> #((¤ xs 0) (¤ xs 1))
(define (spell-out-array type expr)
(list->vector (map (lambda (i) `(¤ ,expr ,i)) (iota (array-length type)))))
;;; The forms a macro call stands for, with `(type-of x)' answered from
;;; ENV
(define (expand-call form env)
(parameterize ((current-type-of
(lambda (queried)
(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)))))
,@(map (lambda (a) (walk-statement a env))
(cdr form)))))
(convert-arguments (walk-parts form env) env)))))))))
;;; #(struct point : (. p x) 2) -- the values are expressions, and the
;;; words before a `:' a type that is left as written
@@ -977,31 +849,20 @@
(else (case (car expr)
((dot-access %->) (simple-path? (second expr)))
((*) (and (= 2 (length expr)) (simple-path? (second expr))))
((¤) (and (= 3 (length expr))
(simple-path? (second expr))
(or (symbol? (third expr)) (number? (third expr)))))
(else #f)))))
(define (match-arm-variant arm) (and (pair? (car arm)) (car (car arm))))
(define (check-match form name variants arms)
(for-each
(lambda (arm)
(unless (and (pair? arm)
(or (eq? (car arm) 'else)
(and (list? (car arm)) (= 2 (length (car arm)))
(list? (second (car arm))))))
(sex-error form "an arm is ((variant (binders ...)) body ...)" arm)))
arms)
(let* ((else? (lambda (arm) (eq? (car arm) 'else)))
(named (remove else? arms))
(covered (map match-arm-variant named)))
(when (> (count else? arms) 1)
(sex-error form "match has more than one else"))
(for-each
(lambda (arm)
(let* ((variant (match-arm-variant arm))
(fields (variant-fields variants variant)))
(unless (and (list? (car arm)) (= 2 (length (car arm))))
(sex-error form "an arm is ((variant (binders ...)) body ...)" arm))
(unless fields
(sex-error form "not a variant of" name variant))
(unless (= (length fields) (length (second (car arm))))
@@ -1029,18 +890,13 @@
,@(cdr arm))))))
arms)))
;;; A binder is a copy of its field, an array one included:
;;;
;;; (var xs [int 2] #((¤ s.u.row.xs 0) (¤ s.u.row.xs 1)))
(define (match-bindings scrutinee variant binders fields)
(filter-map
(lambda (binder field)
(and (not (eq? binder '_))
(let ((type (second field))
(path `(dot-access (dot-access (dot-access ,scrutinee u) ,variant)
,(first field))))
`(var ,binder ,type
,(if (array-form? type) (spell-out-array type path) path)))))
`(var ,binder ,(second field)
(dot-access (dot-access (dot-access ,scrutinee u) ,variant)
,(first field)))))
binders fields))
;;; (.length v) is a call to vec2ːlength; (. v length) is the field
@@ -1080,24 +936,17 @@
(copy-form-source!
form
`(,method
,(method-receiver method (or target written) (and target #t)
(walk-operand receiver env) form)
,@(walk-operands form (cddr form) env)))))))
,(method-receiver method (and target #t)
(walk-statement receiver env) form)
,@(map (lambda (argument) (walk-statement argument env))
(cddr form))))))))
;;; A method taking (* Self) wants an address and one taking Self wants
;;; a value; the receiver is one or the other, so one of the two gets
;;; `&' or `*' put in front of it.
;;;
;;; OBJECT is what the receiver holds or points at: a `(const v)' handed
;;; to a method taking (* v) would be quietly stripped of its const.
(define (method-receiver method object pointer? receiver form)
(let* ((self (method-self-type method))
(wants-pointer? (and (pair? self) (eq? '* (car self)))))
(when (and wants-pointer?
(const-type? object)
(not (const-type? (pointer-target self))))
(sex-error form "method may change its receiver, and this one is const"
method))
;;; `&' or `*' put in front of it
(define (method-receiver method pointer? receiver form)
(let ((wants-pointer? (let ((self (method-self-type method)))
(and (pair? self) (eq? '* (car self))))))
(cond ((eq? wants-pointer? pointer?) receiver)
((not wants-pointer?) `(* ,receiver))
;; `&(f())' is not C, and the method cannot be given a
@@ -1120,7 +969,7 @@
;;; only C knows -- (. font handle), not font.
(define (walk-member-path form env)
(let ((base (second form)))
(let step ((expr (walk-operand base env))
(let step ((expr (walk-statement base env))
(type (expression-type base env))
(read (list)) ; the fields walked, reversed
(fields (cddr form))
@@ -1154,10 +1003,8 @@
(eq? 'fn (car signature))
(pair? (cdr signature))
(pair? (second signature))
(unwrap-type (car (second signature))))))
(define (const-type? type)
(and (pair? type) (eq? 'const (car type))))
(let ((self (car (second signature))))
(if (pair? self) (car self) self)))))
(define (as-closure-type type)
@@ -1199,17 +1046,7 @@
(let ((written (car parts)))
(if (= 1 (length written)) (car written) written)))))
((symbol? expr) (or (lookup-name env expr) (get-name-type expr)))
;; the walk expands `(the a)' before it gets there, so its type is
;; that of what it expands to:
;;
;; (.len (the a)) (match (the s) ...) (. (the a) x)
((macro? expr)
(let ((expansion (expand-call expr env)))
(and (pair? expansion) (null? (cdr expansion))
(expression-type (car expansion) env))))
((not (and (list? expr) (pair? expr))) #f)
;; (shape/circle 1.0) -> shape
((and (symbol? (car expr)) (get-constructor (car expr))) => car)
;; a call of no arguments is still a call
((< (length expr) 2)
(and (symbol? (car expr)) (get-return-type (car expr))))
@@ -1772,19 +1609,9 @@
(unless (and (symbol? name) (list? declared) (pair? declared))
(sex-error form "sum needs a name and a list of variants" form))
(let ((variants (map (lambda (v) (parse-variant form v)) declared)))
(for-each (lambda (v)
(for-each (lambda (field) (check-variant-field form name field))
(cdr v)))
variants)
(let* ((names (map car variants))
(twice (find (lambda (v) (> (count (cut eq? <> v) names) 1)) names)))
(when twice
(sex-error form "variant is declared twice" twice)))
(when (get-sum name)
(sex-error form "sum is already defined" name))
(add-sum! name variants)
(for-each (lambda (v) (add-constructor! (qualify-op name (car v)) name (car v)))
variants)
(fold match-sex-form acc
(map (lambda (f) (copy-form-source! form f))
(sum-forms name variants))))))
@@ -1794,25 +1621,6 @@
(sex-error form "a variant is (name (fields ...))" v))
(cons (car v) (normalize-fields (cadr v))))
;;; (sum expr ((neg ((e expr))))) has no size; (neg ((e (* expr)))) does.
;;; An array field is spelled out element by element wherever it is
;;; copied, so its length has to be one sexc can count.
(define (check-variant-field form name field)
(let ((type (second field)))
(when (holds-by-value? type name)
(sex-error form "a sum holds itself through a pointer" (first field)))
(when (and (array-form? type) (not (array-length type)))
(sex-error form "array field needs a length written as a number"
(first field)))))
(define (holds-by-value? type name)
(cond ((eq? type name) #t)
((not (pair? type)) #f)
((memq (car type) '(struct const volatile))
(and (pair? (cdr type)) (holds-by-value? (cadr type) name)))
((array-form? type) (holds-by-value? (array-element-type type) name))
(else #f)))
(define (sum-forms name variants)
(let ((carried (filter (lambda (v) (pair? (cdr v))) variants)))
`(;; forward, so a variant may hold a pointer back to the sum
@@ -1828,32 +1636,39 @@
`((u (union ,(map (lambda (v)
`(,(car v)
(struct ,(qualify-op name (car v)))))
carried))))))))))
carried)))))))
,@(map (lambda (v) (sum-constructor name v)) variants))))
;;; #(shape : .tag shapeǂcircle .u #(.circle #((struct shapeːcircle) : 1.0)))
(define (sum-literal name tag values)
;;; (shape/circle 1.0) builds the value in one compound literal, so a
;;; constructor is an expression and not three statements
;;; Static even when the sum is pub: a pub sum is pasted whole into
;;; the importer, and two units defining shapeːcircle would not link.
(define (sum-constructor name variant)
(let* ((tag (car variant))
(fields (cdr variant))
(payload (qualify-op name tag)))
`(fn ,payload ,fields ,name
(return ,(sum-literal name tag payload fields)))))
;;; #(shape : .tag shapeːtagːcircle .u #(.circle #((struct shapeːcircle) : r)))
(define (sum-literal name tag payload fields)
(list->vector
(append (list name ': (dotted 'tag) (sum-tag-constant name tag))
(if (null? values)
(if (null? fields)
(list)
(list (dotted 'u)
(vector (dotted tag)
(list->vector
(append (list (list 'struct (qualify-op name tag)) ':)
values))))))))
(append (list (list 'struct payload) ':)
(map car fields)))))))))
(define (dotted name)
(string->symbol (string-append "." (symbol->string name))))
;;; shape -> enum shapeǂ, circle -> shapeǂcircle. Not shapeːtag: that
;;; is where a variant named `tag' keeps its payload struct, and an enum
;;; and a struct share C's tag namespace.
(define (sum-tag name)
(string->symbol (string-append (symbol->string name) "ǂ")))
(define (sum-tag name) (qualify-op name 'tag))
(define (sum-tag-constant name variant)
(string->symbol (string-append (symbol->string (sum-tag name))
(symbol->string variant))))
(qualify-op (sum-tag name) variant))
;;; (record point ((x int) (y int))) is a struct that also names
;;; itself, so `point' alone stands where `(struct point)' had to:
@@ -1890,44 +1705,28 @@
;;; No function around a toplevel form, so no scope chain: what a
;;; global's initializer names comes from `get-name-type' alone.
;;; What a global's initializer is walked in: no names but its own,
;;; and anything it lifts is named after the global
(define (make-toplevel-env name)
(define (make-toplevel-env)
(let ((env (make-hash-table)))
(set! (hash-table-ref env :fn-name) name)
(set! (hash-table-ref env :lambda-counter) 0)
(set! (hash-table-ref env :lambda-aux-code) (list))
(set! (hash-table-ref env :scopes) (list))
env))
;;; The writer spells neither `closure' nor `_', and an initializer is
;;; an expression like any other:
;;;
;;; (var n _ 1) -> int n = 1
;;; (var o v #(v : (.get a))) -> v o = (v){vːget(&a)}
(define (process-global-var sex-var acc)
;; A global is not walked for lambdas, but the writer spells neither
;; `closure' nor `_': `(var n _ 1)' has to reach it as `int n = 1'
(let* ((resolved (resolve-closure-types sex-var))
(qualifier (and (memq (car resolved) '(pub extern)) (car resolved)))
(core (if qualifier (cdr resolved) resolved))
(env (make-toplevel-env (and (pair? (cdr core)) (second core))))
;; `(extern var n int)' has no initializer to work a `_' out
;; from
(core (if (eq? 'extern qualifier)
core
(resolve-wildcard
(copy-form-source!
core
(if (> (length core) 3)
(append (take core 3)
(walk-operands core (drop core 3) env))
core))
env)))
(resolve-wildcard core (make-toplevel-env))))
(form (if qualifier
(copy-form-source! resolved (cons qualifier core))
core)))
(when (and (pair? (cdr core)) (pair? (cddr core)) (symbol? (second core)))
(add-name-type! (second core) (third core)))
(cons form (append (hash-table-ref env :lambda-aux-code) acc))))
(cons form acc)))
;;; Utils
(define (non-empty-list? form)

View File

@@ -38,7 +38,6 @@ All commands in `lisp-mode-shared-map' are inherited by this map.")
"import"
"include"
"fn"
"ops"
"pub"
"record"
"realize"
@@ -63,7 +62,6 @@ All commands in `lisp-mode-shared-map' are inherited by this map.")
"if"
"for"
"goto"
"match"
"return"
"switch"
"var"

View File

@@ -881,22 +881,6 @@ whitespace aside, THEN?"
(test-assert "a bare -> is not the arrow"
(emits? (in-line "(= (-> pp x) 1)") "->(pp, x) = 1")))
;; A global's initializer is an expression like a local's, and walked
;; the same way
(test-group "global initializers"
(test-assert "a path is dispatched on its type"
(emits? (string-append "(struct pt ((x int)))"
"(var a (struct pt) #((struct pt) : 1))"
"(var q (* int) (& (. a x)))")
"int * q = &a.x"))
(test-assert "a lambda is lifted, and named after the global"
(emits? "(var f (fn ((x int)) int) (lambda ((x int)) int (return x)))"
"int (*f)(int) = λ0_f"))
(test-assert "ahead of it"
(let ((c (sex->c "(var f (fn ((x int)) int) (lambda ((x int)) int (return x)))")))
(< (string-contains c "static int λ0_f (int x)")
(string-contains c "(*f)(int) = λ0_f")))))
;; Toplevel forms used to be separated by a blank line unconditionally,
;; which spread a block of adjacent typedefs over twice the lines its
;; author wrote

View File

@@ -29,8 +29,7 @@ ahead of its pub forms."
"(record vec2 ((x float) (y float)))"
"(ops vec2"
" (pub fn length ((self (* const vec2))) float (return 1.0))"
" (fn sum-of ((self vec2)) float (return (. self x)))"
" (fn scale ((self (* vec2))) void (= (. self x) 2.0)))"
" (fn sum-of ((self vec2)) float (return (. self x))))"
"(fn mk () vec2 (return #(vec2 : 1.0 2.0)))"
"(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) "
(apply string-append body) ")"))
@@ -143,60 +142,6 @@ ahead of its pub forms."
(reports? (ops-src "(.nope v)") "no such method on"))
(test-assert "and so is a receiver with no type to dispatch on"
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
(test-assert "a self spread over the parameter is a pointer self too"
(emits? (string-append
"(record w ((x int)))"
"(ops w (fn get ((self * const w)) int (return (. self x))))"
"(fn f () int (var a w #(w : 1)) (var p (* const w) (& a))"
" (return (+ (.get a) (.get p))))")
"wːget(&a) + wːget(p)"))
(test-assert "a const receiver is not handed to a method that may change it"
(reports? (ops-src "(var c (* const vec2) p) (.scale c)")
"this one is const"))
(test-assert "one that cannot change it takes it"
(emits? (ops-src "(var c (* const vec2) p) (var n float (.length c))")
"vec2ːlength(c)"))
(test-assert "a macro receiver is typed by what it expands to"
(emits? (string-append "(defmacro (the a) a)"
(ops-src "(var n float (.length (the v)))"))
"vec2ːlength(&v)"))
(test-assert "and so is the start of a path"
(emits? (string-append "(defmacro (the a) a)"
(ops-src "(var q float (. (the p) x))"))
"p->x"))
(test-assert "a typedef of the type has its methods"
(emits? (string-append
"(record w ((x int)))"
"(typedef alias w)"
"(ops w (fn get ((self (* const w))) int (return (. self x))))"
"(fn f () int (var a alias #(alias : 1)) (return (.get a)))")
"wːget(&a)"))
(test-assert "and a method written on the typedef is the type's"
(emits? (string-append
"(record w ((x int)))"
"(typedef alias w)"
"(ops alias (fn get ((self (* const alias))) int (return 1)))"
"(fn f () int (var a w #(w : 1)) (return (.get a)))")
"aliasːget(&a)"))
(test-assert "a typedef of a scalar keeps its own"
(reports? (string-append
"(typedef meters float)"
"(ops meters (fn half ((self meters)) meters (return self)))"
"(fn f () void (var x float 1.0) (.half x))")
"no such method on"))
(test-assert "which a typedef of it shares"
(emits? (string-append
"(typedef meters float)"
"(typedef km meters)"
"(ops meters (fn half ((self meters)) meters (return self)))"
"(fn f () void (var x km 1.0) (var y km (.half x)))")
"metersːhalf(x)"))
(test-assert "a method call inside a literal is still a call"
(emits? (ops-src "(var w vec2 #(vec2 : (.length v) 1.0))")
"(vec2){vec2ːlength(&v), 1.0}"))
(test-assert "however deep"
(emits? (ops-src "(var ws [vec2 1] #(#(vec2 : (.length p) 1.0)))")
"{(vec2){vec2ːlength(p), 1.0}}"))
(test-assert "an address cannot be taken of a call"
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
;; the inner call has to be typed to find the outer one's receiver
@@ -212,11 +157,7 @@ ahead of its pub forms."
;; fields, the record holding both, and a constructor each.
(test-group "sum"
(test-assert "the tag enum is named after the sum"
(emits? (sum-src "") "enum shapeǂ {"))
(test-assert "and its constants after the sum and the variant"
(emits? (sum-src "") "shapeǂcircle"))
(test-assert "a variant may be called tag"
(emits? "(sum t ((tag ((x int))) (none ())))" "struct tːtag {"))
(emits? (sum-src "") "enum shapeːtag"))
(test-assert "a variant with fields gets a payload struct"
(emits? (sum-src "") "struct shapeːcircle {"))
(test-assert "the sum is typedef'd"
@@ -227,61 +168,8 @@ ahead of its pub forms."
(test-assert "the typedef comes before the payloads, for a recursive sum"
(emits? "(sum expr ((lit ((v float))) (neg ((e (* const expr))))))"
"typedef struct expr expr;"))
(test-assert "a constructor is written in place"
(emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))"
"(struct tːa){1}"))
(test-assert "and is no function"
(not (emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))"
"t tːa (")))
(test-assert "so it is a constant at file scope"
(emits? "(sum t ((a ((x int)))))(var g t (t/a 1))" "static t g = (t){"))
(test-assert "and an lvalue a method can take the address of"
(emits? (string-append
"(sum t ((a ((x int)))))"
"(ops t (fn get ((self (* const t))) int (return 1)))"
"(fn f () int (return (.get (t/a 1))))")
"tːget(&(t){"))
(test-assert "a constructor takes as many arguments as its variant has fields"
(reports? "(sum t ((a ((x int)))))(fn f () t (return (t/a 1 2)))"
"variant takes this many fields"))
(test-assert "and is not a function value"
(reports? "(sum t ((a ((x int)))))(fn f () void (var g (fn ((x int)) t) t/a))"
"a constructor is not a function"))
(test-assert "an array field takes a brace literal as it is"
(emits? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1 2))))"
"(struct tːa){{1, 2}}"))
(test-assert "and an array by its elements"
(emits? "(sum t ((a ((xs [int 2])))))(fn f () t (var v [int 2] #(1 2)) (return (t/a v)))"
"(struct tːa){{v[0], v[1]}}"))
(test-assert "which have to be countable"
(reports? "(fn n () int (return 2))(sum t ((a ((xs [int (n)])))))"
"array field needs a length"))
(test-assert "and as many as the field holds"
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var v [int 3] #(1 2 3)) (return (t/a v)))"
"array is not as long as its field"))
(test-assert "an array field takes an array"
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var p (* int) 0) (return (t/a p)))"
"an array field takes an array"))
(test-assert "read from a path, not from a call"
(reports? (string-append "(sum t ((a ((xs [int 2])))))(fn g () int (return 0))"
"(fn f () t (var m [t 2]) (return (t/a (. [m (g)] u a xs))))")
"bind it to a var first"))
(test-assert "an array binder is a copy of each element"
(emits? (string-append "(sum t ((a ((xs [int 2])))))"
"(fn f ((s t)) int (match s ((a (xs)) (return [xs 0]))))")
"int xs[2] = {s.u.a.xs[0], s.u.a.xs[1]}"))
(test-assert "a sum cannot hold itself"
(reports? "(sum box ((inner ((x box)))))" "a sum holds itself through a pointer"))
(test-assert "nor an array of itself"
(reports? "(sum box ((inner ((x [box 2])))))" "a sum holds itself through a pointer"))
(test-assert "though it can point at itself"
(emits? "(sum box ((inner ((x (* box))))))" "box * x;"))
(test-assert "a function argument becomes the closure its field holds"
(emits? (string-append
"(fn g ((n int)) int (return n))"
"(sum t ((a ((k (closure ((int)) int))))))"
"(fn f () t (return (t/a g)))")
"ƛint_int_fromfn(g)"))
(test-assert "constructors are static even when the sum is pub"
(emits? "(pub sum t ((a ((x int)))))" "static t tːa"))
(test-assert "a tag-only sum has no union at all"
(not (emits? "(sum light ((red ()) (green ())))" "union")))
(test-assert "a sum is declared once"
@@ -289,9 +177,7 @@ ahead of its pub forms."
(test-assert "and needs variants"
(reports? "(sum t)" "sum needs a name and a list of variants"))
(test-assert "a variant is a name and a field list"
(reports? "(sum t (a))" "a variant is"))
(test-assert "and is declared once"
(reports? "(sum t ((a ((x int))) (a ())))" "variant is declared twice")))
(reports? "(sum t (a))" "a variant is")))
;; A spec is a contract that emits nothing; a realization is its
;; methods for one type. The refusals are what make it a contract.
@@ -325,9 +211,6 @@ ahead of its pub forms."
(test-assert "a spec is declared once"
(reports? "(spec (p T) (fn s ((a T)) void))(spec (p T) (fn q ((a T)) void))"
"spec is already declared"))
(test-assert "a typedef realizes what its type does"
(reports? (string-append (spec-src "") "(typedef alias v)(realize (p alias))")
"spec is already realized for"))
(test-assert "a type realizes a spec once"
(reports? (string-append (spec-src "") "(realize (p v)"
" (fn s ((a (* const v))) void (return)))")
@@ -346,23 +229,6 @@ ahead of its pub forms."
" (fn q ((a (* const T))) void))"
"(realize (p v) (fn s ((a (* const v))) void (return)))")
"q"))
(test-assert "a docstring is not a body"
(reports? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void \"doc\"))"
"(realize (p v))")
"realization is missing a method of"))
(test-assert "nor is a comment"
(reports? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) int\n"
" ;; must be implemented\n"
" ))"
"(realize (p v))")
"realization is missing a method of"))
(test-assert "a documented default is still a default"
(emits? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) int \"doc\" (return 1)))"
"(realize (p v))")
"/* doc */"))
(test-assert "a method the spec has not got is refused"
(reports? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a (* const T))) void))"
@@ -379,11 +245,6 @@ ahead of its pub forms."
"(spec (p T) (fn s ((a (* const T))) void))"
"(realize (p v) (fn s ((a v)) void (return)))")
"arguments do not match"))
(test-assert "but not on how a pointer is spelled"
(emits? (string-append "(record v ((x int)))"
"(spec (p T) (fn s ((a * const T)) void))"
"(realize (p v) (fn s ((a (* const v))) void (return)))")
"void vːpːs (const v * a)"))
(test-assert "a method name already taken on the type is refused"
(reports? (string-append "(record v ((x int)))"
"(ops v (fn s ((a (* const v))) void (return)))"
@@ -413,18 +274,6 @@ ahead of its pub forms."
(test-assert "a path scrutinee is not copied"
(not (emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
"ƛm0")))
(test-assert "a macro scrutinee is typed by what it expands to"
(emits? (string-append "(defmacro (the a) a)"
(sum-src "(match (the s) ((circle (r)) (= out r))"
" (else (= out 0.0)))"))
"shape ƛm0 = s"))
(test-assert "a typedef of a sum is matched as the sum"
(emits? (string-append
"(sum shape ((circle ((r int))) (empty ())))"
"(typedef alias shape)"
"(fn f () int (var s alias (shape/circle 1))"
" (match s ((circle (r)) (return r)) ((empty ()) (return 0))))")
"case shapeǂcircle"))
(test-assert "else becomes default"
(emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
"default:"))
@@ -451,26 +300,6 @@ ahead of its pub forms."
(reports? (sum-src "(match s ((circle (r)) (= out r)) ((circle (q)) (= out q))"
" (else (= out 0.0)))")
"arm is written twice"))
(test-assert "so is a second else"
(reports? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0))"
" (else (= out 1.0)))")
"more than one else"))
(test-assert "so is an arm that is not one"
(reports? (sum-src "(match s foo)") "an arm is"))
(test-assert "match has no value"
(reports? (sum-src "(var n float (match s ((circle (r)) r) (else 0.0)))")
"match is a statement"))
(test-assert "not even as an argument"
(reports? (sum-src "(= out (match s ((circle (r)) r) (else 0.0)))")
"match is a statement"))
(test-assert "but a case body is a statement"
(emits? (sum-src "(switch 1 (case 1 (match s ((circle (r)) (= out r))"
" (else (= out 0.0)))))")
"switch (s.tag)"))
(test-assert "and so is a closure's, wherever the closure stands"
(emits? (sum-src "(var c (closure () int) (closure () int ((t s))"
" (match t ((circle (r)) (return 1)) (else (return 0)))))")
"switch (t.tag)"))
(test-assert "and a scrutinee that is not a sum"
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
"match wants a sum")))

View File

@@ -106,8 +106,8 @@
(free self)))
;;; A pub sum is pasted whole, so the importer builds its own copy of
;;; the declarations; a constructor is written in place where it is
;;; called, so there is no function for two units to both define.
;;; the declarations -- and of the constructors, which is why those are
;;; static: two units defining colourːrgb would not link.
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
;;; A spec and a realization over the boundary. The written method

View File

@@ -2,15 +2,14 @@
(output "3.14 12.00 0.00"
"held once: a 1"
"nested: -(2 + 3) = -5"
"tagged only: 1"
"rows: 18 5")
"tagged only: 1")
(return 0)
;;; A sum is a tag enum, a payload struct per variant that carries
;;; fields, and the record holding both; a constructor is the value
;;; itself, written in place. match is a switch over the tag whose arms
;;; bind the payload as locals, and the compiler refuses one that leaves
;;; a variant out -- which is the thing the form exists for.
;;; fields, the record holding both, and a constructor each. match is
;;; a switch over the tag whose arms bind the payload as locals, and
;;; the compiler refuses one that leaves a variant out -- which is the
;;; thing the form exists for.
(include stdio.h)
@@ -53,12 +52,6 @@
(sum held ((a ((x int) (y int))) (b ())))
(fn mk-held () held (return (held/a 1 2)))
;;; An array field is handed elements, never an array: C copies neither
;;; into a parameter nor into another array, so both the constructor
;;; and the binder spell it out
(define N 4)
(sum row ((cells ((xs [int N]))) (none ())))
(pub fn main () int
(printf "%.2f %.2f %.2f\n"
(area (mk-shape))
@@ -82,16 +75,4 @@
(match l
((red ()) (printf "tagged only: 0\n"))
((green ()) (printf "tagged only: 1\n")))
(var a [int N] #(5 6 7 8))
(var rows [row 2] #((row/cells a) (row/cells #(1 2 3 4))))
(= [a 0] 50) ; the row holds a copy
(var sum int 0)
(for (var i int 0) (< i 2) (++ i)
(match [rows i]
((cells (xs))
(= [xs 0] 100) ; and so does the binder
(+= sum (+ [xs 1] [xs 2])))
((none ()) (+= sum 0))))
(printf "rows: %d %d\n" sum (. [rows 0] u cells [xs 0]))
(return 0))

View File

@@ -16,7 +16,6 @@
(ops expr
(pub fn eval ((self (* const expr))) float
;; v, e, l and r are copies of the fields, not references to them
(match (* self)
((lit (v)) (return v))
((neg (e)) (return (- (.eval e))))

View File

@@ -11,8 +11,6 @@
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!

View File

@@ -11,8 +11,6 @@
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!

View File

@@ -100,19 +100,10 @@
(define +sum-db+ (make-hash-table))
(define (add-sum! name variants)
(hash-table-set! +sum-db+ (type-key name) variants))
(hash-table-set! +sum-db+ 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))
(hash-table-ref/default +sum-db+ name #f))
(define (variant-fields variants name)
(let ((entry (assq name variants)))
@@ -139,18 +130,17 @@
(define (spec-methods spec) (cdr spec))
(define (add-realization! spec type)
(hash-table-set! +realize-db+ (cons spec (type-key type)) #t))
(hash-table-set! +realize-db+ (cons spec 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))))
(hash-table-exists? +realize-db+ (cons spec 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)))
@@ -166,10 +156,10 @@
(define +ops-db+ (make-hash-table))
(define (add-method! type method qualified)
(hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
(hash-table-set! +ops-db+ (cons type method) qualified))
(define (get-method type method)
(hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))
(hash-table-ref/default +ops-db+ (cons type method) #f))
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
(define (qualify-op type name)
@@ -179,33 +169,13 @@
;;; 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))
(cond ((symbol? type) 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))