Compare commits

..

10 Commits

Author SHA1 Message Date
cce756d8aa read a member path and a receiver through a pointer chain
A pointer to a pointer has no fields, so (. pp x) has one meaning, and
it came out pp->x; it is (*pp)->x, and a method receiver alike.
2026-10-19 16:06:13 +03:00
777fb84638 split the C compiler's name from its options
SEX_CC="ccache cc" went looking for an executable of that whole name;
it is read as make reads CC now.
2026-10-19 16:06:13 +03:00
ad4ef97bfe type a value if as the join of its branches
It had no rule, so _ could not be set from one.
2026-10-19 16:06:13 +03:00
df7e4e69e8 read a quoted name as the name
'-fields read as (- fields), so (cat name '-fields) built name(- fields).
A quoted list keeps the rule: it is code a macro returns as often as not.
2026-10-19 16:06:13 +03:00
e16e53cd7c refuse a macro defined after a call to it
The call was compiled as a function's, and the macro went unused there
without a word.
2026-10-19 16:06:13 +03:00
d8309f6ad8 refuse an integer with a leading 0
017 is 17 to Scheme and 15 to C, so whoever reads the Sex and whoever
reads the C would each take it for something else.
2026-10-19 16:06:13 +03:00
cbf77c3e19 refuse a variant named like a C keyword
One with fields is a member of the sum's union, and C takes no member
named int; one without is held to the same, as one rule.
2026-10-19 16:06:13 +03:00
2189be0a76 match a sum through a pointer to it
A pointer was refused, where . reads through one; a match now reads
through the whole chain of them to the sum.
2026-10-19 16:06:13 +03:00
52057c1cf2 take a docstring on a sum
record, struct and enum take one; sum read it where its variants go.
2026-10-19 16:06:13 +03:00
d62a1d4c97 expand a module's toplevel macro calls for its importers
Its interface was read as written, so pub forms a macro wrote never
reached them, which list-macros' is-public? flag exists for. The
module's macros, and those its imports give it, are lent for the
reading and taken back after. One reading a type the module keeps to
itself cannot expand there, and gives importers nothing, as before.
2026-10-19 16:06:13 +03:00
14 changed files with 385 additions and 61 deletions

View File

@@ -90,7 +90,8 @@ sexc ./examples/hello-world.sex -o hello
That's it. Now you should have executable named ~hello~ in your That's it. Now you should have executable named ~hello~ in your
directory. Sex uses C under the hood, the default C compiler is ~cc~, directory. Sex uses C under the hood, the default C compiler is ~cc~,
but you can pass any using ~--c-compiler~ option, or by setting but you can pass any using ~--c-compiler~ option, or by setting
~SEX_CC~ environment variable. ~SEX_CC~ environment variable. Either may carry the compiler's own
options, as ~make~'s ~CC~ does: ~SEX_CC="ccache cc"~.
Everything after ~--~ is handed to the C compiler exactly as written: Everything after ~--~ is handed to the C compiler exactly as written:

View File

@@ -114,7 +114,7 @@
(let ((line (current-line))) (let ((line (current-line)))
(stamp (read-comment port) line))) (stamp (read-comment port) line)))
((char=? c #\") (read-string-lit port)) ((char=? c #\") (read-string-lit port))
((char=? c #\') (get-ch port) (list 'quote (read-datum port))) ((char=? c #\') (get-ch port) (list 'quote (read-quoted port)))
((char=? c #\`) (get-ch port) (list 'quasiquote (read-datum port))) ((char=? c #\`) (get-ch port) (list 'quasiquote (read-datum port)))
((char=? c #\,) ((char=? c #\,)
(get-ch port) (get-ch port)
@@ -196,8 +196,24 @@
(finish-atom (list->string (reverse chars))) (finish-atom (list->string (reverse chars)))
(begin (get-ch port) (loop (cons c chars))))))) (begin (get-ch port) (loop (cons c chars)))))))
;;; ...except quoted, alone: '-fields is a name, as in (cat name '-fields).
;;; A quoted list is code a macro hands back as often as not, and keeps
;;; the rule
(define negating? (make-parameter #t))
(define (read-quoted port)
(if (memv (peek port) '(#\( #\[ #\' #\` #\, #\# #\" #\;))
(read-datum port)
(parameterize ((negating? #f)) (read-datum port))))
(define (finish-atom s) (define (finish-atom s)
(let ((digits (if (substring-index "_" s) (or (ungrouped s (or (c-radix s) 10)) s) s))) (let ((digits (if (substring-index "_" s) (or (ungrouped s (or (c-radix s) 10)) s) s)))
;; 017 is 17 to Scheme and 15 to C: whoever reads the Sex and whoever
;; reads the C would each take it for something else
(when (irregex-match "-?0[0-9]+[uUlL]*" digits)
(error
"a leading 0 is octal to C; write the number without it, or #o for octal"
s))
(cond (cond
((string=? s ".") dot-token) ((string=? s ".") dot-token)
((or (string->number digits) (radix-number digits)) ((or (string->number digits) (radix-number digits))
@@ -350,7 +366,8 @@
;;; -INFINITY is (- INFINITY), as -1 is minus one. No name begins with ;;; -INFINITY is (- INFINITY), as -1 is minus one. No name begins with
;;; a dash, and an operator that does goes on with a non-letter: -> -- ;;; a dash, and an operator that does goes on with a non-letter: -> --
(define (negated-name? s) (define (negated-name? s)
(and (> (string-length s) 1) (and (negating?)
(> (string-length s) 1)
(char=? #\- (string-ref s 0)) (char=? #\- (string-ref s 0))
(let ((c (string-ref s 1))) (let ((c (string-ref s 1)))
(or (char-alphabetic? c) (char=? c #\_))))) (or (char-alphabetic? c) (char=? c #\_)))))

145
semen.scm
View File

@@ -36,8 +36,22 @@
;;; in auxiliary structures and functions. ;;; in auxiliary structures and functions.
(define (process raw-sex-forms) (define (process raw-sex-forms)
(reset-declarations!) (reset-declarations!)
(set! +called-as-functions+ (make-hash-table))
(process-rec raw-sex-forms (list))) (process-rec raw-sex-forms (list)))
;;; The first call of each name compiled as a function's, so that a macro
;;; of that name defined after it is told of rather than going unused
;;; there without a word
(define +called-as-functions+ (make-hash-table))
(define (check-not-called-before form name)
(let ((call (hash-table-ref/default +called-as-functions+ name #f)))
(when call
(sex-error form "a macro defined after a call to it, which is a function's"
name
(string-append "called at " (form-file call) ":"
(number->string (form-line call)))))))
(define (process-rec forms acc) (define (process-rec forms acc)
(cond (cond
((null? forms) (reverse acc)) ((null? forms) (reverse acc))
@@ -130,7 +144,11 @@
(process-imports (get-modules-public-forms sex-form) acc)) (process-imports (get-modules-public-forms sex-form) acc))
((or ('defmacro . rest) ((or ('defmacro . rest)
('pub 'defmacro . rest)) (defmacro rest) acc) ('pub 'defmacro . rest))
(when (and (pair? rest) (pair? (car rest)))
(check-not-called-before sex-form (caar rest)))
(defmacro rest)
acc)
((or ('typedef new-type target) ((or ('typedef new-type target)
('pub 'typedef new-type target)) ('pub 'typedef new-type target))
@@ -252,10 +270,10 @@
;; between name and fields are not skipped, they already confuse the ;; between name and fields are not skipped, they already confuse the
;; writer ;; writer
(match form (match form
(('pub (and kind (or 'struct 'union 'enum)) (('pub (and kind (or 'struct 'union 'enum 'sum))
(? symbol? name) (? string? doc) . rest) (? symbol? name) (? string? doc) . rest)
(values doc (copy-form-source! form `(pub ,kind ,name ,@rest)))) (values doc (copy-form-source! form `(pub ,kind ,name ,@rest))))
(((and kind (or 'struct 'union 'enum)) (((and kind (or 'struct 'union 'enum 'sum))
(? symbol? name) (? string? doc) . rest) (? symbol? name) (? string? doc) . rest)
(values doc (copy-form-source! form `(,kind ,name ,@rest)))) (values doc (copy-form-source! form `(,kind ,name ,@rest))))
(_ (values #f form)))) (_ (values #f form))))
@@ -735,6 +753,9 @@
,receiver ,receiver
,@(walk-operands form (cdr form) env)))))) ,@(walk-operands form (cdr form) env))))))
(else (else
(when (and (symbol? (car form)) (form-source form)
(not (hash-table-exists? +called-as-functions+ (car form))))
(hash-table-set! +called-as-functions+ (car form) form))
(convert-arguments (copy-form-source! form (walk-operands form form env)) (convert-arguments (copy-form-source! form (walk-operands form form env))
env)))))))) env))))))))
@@ -1025,7 +1046,7 @@
;;; binders, or a variant left out with no `else' are all errors here: ;;; binders, or a variant left out with no `else' are all errors here:
;;; a missing case is the thing this form exists to catch. ;;; a missing case is the thing this form exists to catch.
(define (walk-match form env) (define (walk-match form env)
(let* ((scrutinee (and (pair? (cdr form)) (second form))) (let* ((scrutinee (and (pair? (cdr form)) (dereferenced (second form) env form)))
;; a comment between arms goes the way the writer already ;; a comment between arms goes the way the writer already
;; drops one between switch clauses ;; drops one between switch clauses
(arms (remove comment-form? (cddr form))) (arms (remove comment-form? (cddr form)))
@@ -1049,6 +1070,13 @@
,(match-branches name held variants arms)) ,(match-branches name held variants arms))
env))))) env)))))
;;; A pointer to a sum is matched as the sum, read through however many
;;; there are, as `.' reads through them: p : (* * shape) -> (* (* p))
(define (dereferenced expr env form)
(if (pointer-target (expression-type expr env))
(dereferenced (copy-form-source! form (list '* expr)) env form)
expr))
(define (match-binders arms) (define (match-binders arms)
(append-map (lambda (arm) (append-map (lambda (arm)
(if (eq? (car arm) 'else) '() (delete '_ (second (car arm))))) (if (eq? (car arm) 'else) '() (delete '_ (second (car arm)))))
@@ -1174,10 +1202,12 @@
(let ((name (method-head-name (car form)))) (let ((name (method-head-name (car form))))
(unless (pair? (cdr form)) (unless (pair? (cdr form))
(sex-error form "a method call needs a receiver" form)) (sex-error form "a method call needs a receiver" form))
(let* ((receiver (second form)) ;; through a pointer chain, as a path reads through one
(written (expression-type receiver env)) (let*-values (((receiver written)
(target (pointer-target written)) (through-pointers (walk-operand (second form) env)
(key (ops-key (or target written)))) (expression-type (second form) env)))
((target) (pointer-target written))
((key) (ops-key (or target written))))
(unless key (unless key
(sex-error form "receiver of this has no type to dispatch on" name)) (sex-error form "receiver of this has no type to dispatch on" name))
(let ((method (get-method key name))) (let ((method (get-method key name)))
@@ -1187,7 +1217,7 @@
form form
`(,method `(,method
,(method-receiver method (or target written) (and target #t) ,(method-receiver method (or target written) (and target #t)
(walk-operand receiver env) form) receiver form)
,@(walk-operands form (cddr form) env))))))) ,@(walk-operands form (cddr form) env)))))))
;;; A method taking (* Self) wants an address and one taking Self wants ;;; A method taking (* Self) wants an address and one taking Self wants
@@ -1233,8 +1263,9 @@
(told? #f)) (told? #f))
(if (null? fields) (if (null? fields)
(copy-form-source! form expr) (copy-form-source! form expr)
(let* ((target (pointer-target type)) (let*-values (((expr type) (through-pointers expr type))
(known (type-fields (or target type)))) ((target) (pointer-target type))
((known) (type-fields (or target type))))
(when (and (pair? known) (not (assq (field-name (car fields)) known))) (when (and (pair? known) (not (assq (field-name (car fields)) known)))
(sex-error form "no field of this name in" (or target type) (car fields))) (sex-error form "no field of this name in" (or target type) (car fields)))
(unless (or type told?) (unless (or type told?)
@@ -1248,6 +1279,19 @@
(cdr fields) (cdr fields)
(or told? (not type)))))))) (or told? (not type))))))))
;;; A pointer to a pointer has no fields: EXPR read through every pointer
;;; but the last, so a step writes -> once. pp : (* * pt) -> (* pp), (* pt)
(define (through-pointers expr type)
(let ((target (pointer-target type)))
(if (and target (pointer-target target))
(through-pointers (list '* expr) target)
(values expr type))))
;;; What a path reads fields of: TYPE, or what its pointers lead to
(define (pointed-through type)
(let ((target (pointer-target type)))
(if target (pointed-through target) type)))
;;; A field of a path, indexed or not: xs, [xs 0] -> xs ;;; A field of a path, indexed or not: xs, [xs 0] -> xs
(define (field-name field) (define (field-name field)
(if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field))) (if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field)))
@@ -1394,6 +1438,12 @@
;; a shift is the promoted left operand, not a join: ;; a shift is the promoted left operand, not a join:
;; (<< l b), l : long -> long; (>> c b), c : char -> int ;; (<< l b), l : long -> long; (>> c b), c : char -> int
((<< >>) (promoted-type (expression-type (second expr) env))) ((<< >>) (promoted-type (expression-type (second expr) env)))
;; a value if is the join of its values, as an operator is of its
;; operands -- and nothing when one of them says nothing
((if) (let ((types (map (cut expression-type <> env)
(if-values (remove comment-form? (cdr expr))))))
(and (every identity types)
(fold (lambda (t joined) (arith-join joined t)) #f types))))
;; a complement is the operand promoted, as a negation is ;; a complement is the operand promoted, as a negation is
((~ bit-not bitwise-not) (promoted-type (expression-type (second expr) env))) ((~ bit-not bitwise-not) (promoted-type (expression-type (second expr) env)))
;; ...and an increment is the operand unpromoted: ;; ...and an increment is the operand unpromoted:
@@ -1454,6 +1504,12 @@
(let ((type (underlying (parse-type written)))) (let ((type (underlying (parse-type written))))
(or (ptr-type? type) (array-type? type))))) (or (ptr-type? type) (array-type? type)))))
;;; (if t1 v1 t2 v2 e) -> (v1 v2 e)
(define (if-values parts)
(if (and (pair? parts) (pair? (cdr parts)) (pair? (cddr parts)))
(cons (cadr parts) (if-values (cddr parts)))
parts))
(define (arith-join left right) (define (arith-join left right)
(cond (cond
((not left) right) ((not left) right)
@@ -1539,8 +1595,7 @@
(define (member-path-type type fields) (define (member-path-type type fields)
(if (null? fields) (if (null? fields)
type type
(member-path-type (step-type (or (pointer-target type) type) (member-path-type (step-type (pointed-through type) (car fields))
(car fields))
(cdr fields)))) (cdr fields))))
;;; What a step of a path gives: its field, or for [xs i] an element of ;;; What a step of a path gives: its field, or for [xs i] an element of
@@ -2056,35 +2111,53 @@
;;; purpose: a record would typedef shapeːcircle into the ordinary ;;; purpose: a record would typedef shapeːcircle into the ordinary
;;; namespace, where the constructor of that name already lives. As a ;;; namespace, where the constructor of that name already lives. As a
;;; tag it shares nothing. ;;; tag it shares nothing.
(define (process-sum form acc) (define (process-sum written acc)
(let* ((core (if (eq? (car form) 'pub) (cdr form) form)) (let-values (((doc form) (extract-aggregate-docstring written)))
(name (and (pair? (cdr core)) (cadr core))) (let* ((core (if (eq? (car form) 'pub) (cdr form) form))
(declared (and (pair? (cddr core)) (caddr core)))) (name (and (pair? (cdr core)) (cadr core)))
(unless (and (symbol? name) (list? declared) (pair? declared)) (declared (and (pair? (cddr core)) (caddr core))))
(sex-error form "sum needs a name and a list of variants" form)) (unless (and (symbol? name) (list? declared) (pair? declared))
(let ((variants (map (lambda (v) (parse-variant form v)) declared))) (sex-error form "sum needs a name and a list of variants" form))
(for-each (lambda (v) (let ((variants (map (lambda (v) (parse-variant form v)) declared)))
(for-each (lambda (field) (check-variant-field form name field)) (for-each (lambda (v)
(cdr v))) (for-each (lambda (field)
variants) (check-variant-field form name field))
(let* ((names (map car variants)) (cdr v)))
(twice (find (lambda (v) (> (count (cut eq? <> v) names) 1)) names))) variants)
(when twice (let* ((names (map car variants))
(sex-error form "variant is declared twice" twice))) (twice (find (lambda (v) (> (count (cut eq? <> v) names) 1))
(when (get-sum name) names)))
(sex-error form "sum is already defined" name)) (when twice
(add-sum! name variants) (sex-error form "variant is declared twice" twice)))
(for-each (lambda (v) (add-constructor! (qualify-op name (car v)) name (car v))) (when (get-sum name)
variants) (sex-error form "sum is already defined" name))
(fold match-sex-form acc (add-sum! name variants)
(map (lambda (f) (copy-form-source! form f)) (for-each (lambda (v)
(sum-forms name variants)))))) (add-constructor! (qualify-op name (car v)) name (car v)))
variants)
;; the docstring goes ahead of everything the sum is made of
(fold match-sex-form
(if doc (cons (copy-form-source! form (list 'comment doc)) acc) acc)
(map (lambda (f) (copy-form-source! form f))
(sum-forms name variants)))))))
(define (parse-variant form v) (define (parse-variant form v)
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v))) (unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr v)))
(sex-error form "a variant is (name (fields ...))" v)) (sex-error form "a variant is (name (fields ...))" v))
;; one with fields is a member of the sum's union, and C takes no
;; member named `int'; one without is held to the same, as one rule
(when (memq (car v) +c-keywords+)
(sex-error form "a variant cannot be named like a C keyword" (car v)))
(cons (car v) (normalize-fields (cadr v)))) (cons (car v) (normalize-fields (cadr v))))
;;; C11's, and the ones <stdbool.h>, in every unit, makes macros of
(define +c-keywords+
'(auto break case char const continue default do double else enum extern
float for goto if inline int long register restrict return short signed
sizeof static struct switch typedef union unsigned void volatile while
_Alignas _Alignof _Atomic _Bool _Complex _Generic _Imaginary _Noreturn
_Static_assert _Thread_local bool true false))
;;; (sum expr ((neg ((e expr))))) has no size; (neg ((e (* expr)))) does. ;;; (sum expr ((neg ((e expr))))) has no size; (neg ((e (* expr)))) does.
;;; An array field is spelled out element by element wherever it is ;;; An array field is spelled out element by element wherever it is
;;; copied, so its length has to be one sexc can count. ;;; copied, so its length has to be one sexc can count.

View File

@@ -16,7 +16,9 @@
get-macro get-macro
macro? macro?
apply-macro apply-macro
defmacro) defmacro
macro-definition
restore-macro!)
(define (cat-syms s-1 s-2) (define (cat-syms s-1 s-2)
(fmt #f s-1 s-2)) (fmt #f s-1 s-2))
@@ -91,6 +93,17 @@
(sex-error form "macro takes at least this many arguments" (sex-error form "macro takes at least this many arguments"
name required))))) name required)))))
;;; What a name is defined as a macro to, or #f; and putting that back,
;;; for a macro lent for a while -- a module's, while its interface is
;;; read
(define (macro-definition name)
(get name 'sex-macro))
(define (restore-macro! name definition)
(if definition
(put! name 'sex-macro definition)
(remprop! name 'sex-macro)))
(define (defmacro form) (define (defmacro form)
(let ((arglist (car form)) (let ((arglist (car form))
(body (cdr form))) (body (cdr form)))

View File

@@ -1,6 +1,7 @@
(import (import
scheme scheme
(chicken base) (chicken base)
(chicken condition)
(chicken file) (chicken file)
(chicken load) (chicken load)
(chicken module) (chicken module)
@@ -9,6 +10,7 @@
(chicken string) (chicken string)
matchable matchable
reader reader
sex-macros
srfi-1 srfi-1
types types
utils) utils)
@@ -121,9 +123,78 @@
;; pub fn taking it has to paste them that way round, or the prototype ;; pub fn taking it has to paste them that way round, or the prototype
;; names a type C has not seen. The declarations of the types its ;; names a type C has not seen. The declarations of the types its
;; importers see only behind a pointer go first. ;; importers see only behind a pointer go first.
(let ((raw-forms (read-module module-path))) (let ((forms (module-forms (read-module module-path))))
(append (check-public-interface raw-forms) (append (check-public-interface forms)
(interface-forms raw-forms)))) (interface-forms forms))))
;;; A module's forms with its toplevel macro calls expanded, so that the
;;; pub forms a macro writes reach importers as written ones do:
;;; (getter forty-two 42) gives them (pub fn forty-two () int). The
;;; macros are the module's own and those its imports give it, lent for
;;; the expanding and taken back after, so the importer is left with the
;;; pub ones alone. One that reads the type database sees what the
;;; importer knows, which a type private to the module is not; a call
;;; that cannot expand for that -- (print-struct box), box being the
;;; module's -- gives importers nothing, as before, and is the module's
;;; own compile's to check
(define +modules-expanded+ (list))
(define (module-forms raw-forms)
(cond ((assq raw-forms +modules-expanded+) => cdr)
(else (let ((forms (expanded raw-forms)))
(set! +modules-expanded+
(cons (cons raw-forms forms) +modules-expanded+))
forms))))
(define (expanded raw-forms)
(let ((lent (list)))
(define (lend! form)
(let ((name (macro-name form)))
(unless (assq name lent)
(set! lent (cons (cons name (macro-definition name)) lent)))
(defmacro (if (pub-form? form) (cddr form) (cdr form)))))
(define (expand form)
(cond ((macro-name form) (lend! form) (list form))
((splice? form) (append-map expand (cdr form)))
((macro? form)
(let ((result (handle-exceptions exn #f (apply-macro form))))
(cond ((or (not result) (null? result)) (list))
(else (stamp-form-source! result (form-source form))
(expand result)))))
(else (list form))))
(for-each lend! (imported-macro-forms raw-forms))
(let ((forms (append-map expand raw-forms)))
(for-each (lambda (entry) (restore-macro! (car entry) (cdr entry))) lent)
forms)))
(define (splice? form)
(and (pair? form) (eq? (car form) '$) (list? form)))
;;; The macros importing FORMS' modules gives: each one's pub macros and
;;; their helpers, and those of the modules it imports with pub
(define (imported-macro-forms forms)
(let next ((paths (append (imported-modules forms #t)
(imported-modules forms #f)))
(seen (list))
(acc (list)))
(cond ((null? paths) (concatenate (reverse acc)))
((member (car paths) seen) (next (cdr paths) seen acc))
(else
(let* ((raw (read-module (car paths)))
(helpers (helper-macros raw)))
(next (append (cdr paths) (imported-modules raw #t))
(cons (car paths) seen)
(cons (filter (lambda (form)
(and (macro-name form)
(or (pub-form? form) (memq form helpers))))
raw)
acc)))))))
(define (macro-name form)
(match form
(('defmacro (name . _) . _) name)
(('pub 'defmacro (name . _) . _) name)
(_ #f)))
(define (interface-forms raw-forms) (define (interface-forms raw-forms)
(let ((helpers (helper-macros raw-forms))) (let ((helpers (helper-macros raw-forms)))
@@ -138,12 +209,8 @@
;;; the importer expands them all, the way a C header that defines a ;;; the importer expands them all, the way a C header that defines a
;;; macro has to define what it uses too ;;; macro has to define what it uses too
(define (helper-macros raw-forms) (define (helper-macros raw-forms)
(let* ((macro-name (lambda (form) (let* ((private (filter (lambda (form)
(match form (and (macro-name form) (not (pub-form? form))))
(('defmacro (name . _) . _) name)
(('pub 'defmacro (name . _) . _) name)
(_ #f))))
(private (filter (lambda (form) (and (macro-name form) (not (pub-form? form))))
raw-forms)) raw-forms))
(mentions (lambda (form name) (mentions (lambda (form name)
(let walk ((x (cdr form))) (let walk ((x (cdr form)))
@@ -268,7 +335,8 @@
(('pub 'define name . body) body) (('pub 'define name . body) body)
(('pub (or 'record 'struct 'union) name . rest) (('pub (or 'record 'struct 'union) name . rest)
(field-types (find list? (remove comment-form? rest)))) (field-types (find list? (remove comment-form? rest))))
(('pub 'sum name (variants ...)) ((or ('pub 'sum name (variants ...))
('pub 'sum name (? string?) (variants ...)))
(append-map (lambda (v) (append-map (lambda (v)
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list))) (if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
(remove comment-form? variants))) (remove comment-form? variants)))
@@ -384,7 +452,8 @@
(cond ((null? paths) (concatenate (reverse acc))) (cond ((null? paths) (concatenate (reverse acc)))
((member (car paths) seen) (next (cdr paths) seen acc)) ((member (car paths) seen) (next (cdr paths) seen acc))
(else (else
(let ((forms (interface-forms (read-module (car paths))))) (let ((forms (interface-forms
(module-forms (read-module (car paths))))))
(next (append (cdr paths) (imported-modules forms #f)) (next (append (cdr paths) (imported-modules forms #f))
(cons (car paths) seen) (cons (car paths) seen)
(cons (declared-types forms) acc))))))) (cons (declared-types forms) acc)))))))

View File

@@ -217,9 +217,11 @@
(define (compile-to-file sex-forms output args cc-args) (define (compile-to-file sex-forms output args cc-args)
"Hand the generated C to the C compiler. Returns the compiler's exit "Hand the generated C to the C compiler. Returns the compiler's exit
status, which is ours to pass on." status, which is ours to pass on."
(let ((compiler (or (get-arg args 'c-compiler #f) ;; named with its own options, as make's CC is: "ccache cc", "cc -O2"
(get-env-var "SEX_CC") (let ((compiler (let ((words (string-split (or (get-arg args 'c-compiler #f)
"cc")) (get-env-var "SEX_CC")
"cc"))))
(if (null? words) (list "cc") words)))
(out-file (if (eq? output 'default) (out-file (if (eq? output 'default)
"a.out" "a.out"
output)) output))
@@ -233,12 +235,14 @@ status, which is ours to pass on."
(begin (delete-file* c-file) (abort exn)) (begin (delete-file* c-file) (abort exn))
(with-output-to-file c-file (with-output-to-file c-file
(lambda () (emit-c sex-forms))) (lambda () (emit-c sex-forms)))
(let ((proc (process compiler (append (list "-o" out-file) (let ((proc (process (car compiler)
(if (get-arg args 'compile-object #f) (append (cdr compiler)
(list "-c") (list "-o" out-file)
(list)) (if (get-arg args 'compile-object #f)
(list c-file) (list "-c")
cc-args)))) (list))
(list c-file)
cc-args))))
(call-with-values (lambda () (process-wait proc)) (call-with-values (lambda () (process-wait proc))
(lambda (pid normal-exit? status) (lambda (pid normal-exit? status)
(delete-file* c-file) (delete-file* c-file)

View File

@@ -406,6 +406,14 @@ whitespace aside, THEN?"
"macro takes at least this many arguments many 1")) "macro takes at least this many arguments many 1"))
(test-assert "while it may take any more" (test-assert "while it may take any more"
(emits? "(defmacro (many x . more) x) (fn f () int (return (many 1 2 3)))" "return 1;")) (emits? "(defmacro (many x . more) x) (fn f () int (return (many 1 2 3)))" "return 1;"))
;; a call before the macro is defined is a function call, and the
;; macro defined after it went unused there without a word
(test-assert "a macro defined after a call to it is refused"
(reports? "(fn f () int (return (later 1)))\n(defmacro (later x) x)"
"codegen.sex:2: a macro defined after a call to it"))
(test-assert "naming where the call is"
(reports? "(fn f () int\n (return (later 1)))\n(defmacro (later x) x)"
"called at codegen.sex:2"))
(test-assert "an error in a macro's body names the call" (test-assert "an error in a macro's body names the call"
(reports? "(defmacro (boom x) (error \"no good\" x))\n(fn f () int (return (boom 1)))" (reports? "(defmacro (boom x) (error \"no good\" x))\n(fn f () int (return (boom 1)))"
"codegen.sex:2: expanding boom: no good"))) "codegen.sex:2: expanding boom: no good")))
@@ -493,6 +501,13 @@ whitespace aside, THEN?"
;; A string as the first body form (or after the name of a struct, ;; A string as the first body form (or after the name of a struct,
;; union or enum) is a docstring: it becomes a comment immediately ;; union or enum) is a docstring: it becomes a comment immediately
;; before the declaration, not a statement inside it. ;; before the declaration, not a statement inside it.
(test-group "a sum's docstring"
;; record, struct and enum took one, and sum read it as its variants
(test-assert "is a comment ahead of it"
(emits? "(sum shape \"A shape.\" ((dot ()) (box ((w int)))))" "/* A shape. */"))
(test-assert "and the sum is still one"
(emits? "(sum shape \"A shape.\" ((dot ()) (box ((w int)))))" "shapeǂbox")))
(test-group "docstrings" (test-group "docstrings"
(test-assert "appears before the function" (test-assert "appears before the function"
(emits? "(fn greet ((name (* char))) void \"Greet NAME.\" (printf \"hi\" name))" (emits? "(fn greet ((name (* char))) void \"Greet NAME.\" (printf \"hi\" name))"
@@ -831,6 +846,16 @@ whitespace aside, THEN?"
"a function's types are written out")) "a function's types are written out"))
;; a call was typed by asking the global table what a function of ;; a call was typed by asking the global table what a function of
;; that name returns, which a local pointer is not ;; that name returns, which a local pointer is not
;; a value if had no rule either: it is the join of its values
(test-assert "a value if joins its branches"
(emits? (in-fn "(var x _ (if a 1 2.5))") "double x = a ? 1 : 2.5"))
(test-assert "every one of a chain"
(emits? (in-fn "(var x _ (if a 1 b 2 3.5f))") "float x"))
(test-assert "an aggregate as itself"
(emits? "(struct pt ((x int))) (fn f ((a int) (p (struct pt))) void (var q _ (if a p p)))"
"struct pt q = a ? p : p"))
(test-assert "and none when a branch says nothing"
(reports? (in-fn "(var x _ (if a (unheard-of) 1))") "infer"))
;; none of these had a rule ;; none of these had a rule
(test-assert "#t is a bool, as true is" (test-assert "#t is a bool, as true is"
(emits? (in-fn "(var t _ #t)") "bool t = 1")) (emits? (in-fn "(var t _ #t)") "bool t = 1"))
@@ -1294,6 +1319,22 @@ whitespace aside, THEN?"
(emits? poly-src "q->pp[0]->y")) (emits? poly-src "q->pp[0]->y"))
(test-assert "and every index of one is written" (test-assert "and every index of one is written"
(emits? poly-src "q->grid[1][2].x")) (emits? poly-src "q->grid[1][2].x"))
;; a pointer to a pointer has no fields, so a path reads through
;; every pointer to the aggregate, where it wrote pp->x
(test-assert "a pointer to a pointer is read through"
(emits? "(struct pt ((x int))) (fn f ((pp (* * struct pt))) int (return (. pp x)))"
"(*pp)->x"))
(test-assert "however many there are"
(emits? "(struct pt ((x int))) (fn f ((p3 (* * * struct pt))) int (return (. p3 x)))"
"(*(*p3))->x"))
(test-assert "and typed through"
(emits? "(struct pt ((x float))) (fn f ((pp (* * struct pt))) void (var v _ (. pp x)))"
"float v = (*pp)->x"))
(test-assert "a method's receiver too"
(emits? (string-append "(record pt ((x int)))"
"(ops pt (fn get ((self (* pt))) int (return (. self x))))"
"(fn f ((pp (* * pt))) int (return (.get pp)))")
"ptːget(*pp)"))
;; only a written (* T) was a pointer, so a typedef of one read as a ;; only a written (* T) was a pointer, so a typedef of one read as a
;; value, and a method could not find its receiver's type ;; value, and a method could not find its receiver's type
(test-assert "a typedef of a pointer reads with an arrow" (test-assert "a typedef of a pointer reads with an arrow"

View File

@@ -506,6 +506,27 @@ ahead of its pub forms."
(emits? (sum-src "(var c (closure () int) (closure () int ((t s))" (emits? (sum-src "(var c (closure () int) (closure () int ((t s))"
" (match t ((circle (r)) (return 1)) (else (return 0)))))") " (match t ((circle (r)) (return 1)) (else (return 0)))))")
"t.tag == shapeǂcircle")) "t.tag == shapeǂcircle"))
;; a pointer to one was refused, where `.' reads through one
;; a variant with fields is a member of the sum's union, which C
;; would not take named `int'
(test-assert "a variant named like a C keyword is refused"
(reports? "(sum tok ((int ((v int))) (other ())))" "a variant cannot be named like a C keyword"))
(test-assert "one without fields too, to keep one rule"
(reports? "(sum tok ((default ()) (other ())))" "a variant cannot be named like a C keyword"))
(test-assert "and stdbool's names count"
(reports? "(sum b ((true ()) (false ())))" "a variant cannot be named like a C keyword"))
(test-assert "a pointer to a sum is matched as the sum"
(emits? (sum-src "(var p (* shape) (& s))"
"(match p ((circle (r)) (= out r)) (else (= out 0.0)))")
"(*p).tag == shapeǂcircle"))
(test-assert "binding through it"
(emits? (sum-src "(var p (* shape) (& s))"
"(match p ((circle (r)) (= out r)) (else (= out 0.0)))")
"float r = (*p).u.circle.r"))
(test-assert "and through a chain of them"
(emits? (sum-src "(var p (* shape) (& s)) (var pp (* * shape) (& p))"
"(match pp ((circle (r)) (= out r)) (else (= out 0.0)))")
"(*(*pp)).tag == shapeǂcircle"))
(test-assert "and a scrutinee that is not a sum" (test-assert "and a scrutinee that is not a sum"
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))" (reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
"match wants a sum"))) "match wants a sum")))
@@ -552,6 +573,9 @@ ahead of its pub forms."
(test "a private one is solved where it is" (test "a private one is solved where it is"
'() '()
(interface-of "(var n _ 42)")) (interface-of "(var n _ 42)"))
(test-assert "a pub sum's variants are looked at past its docstring"
(interface-reports? "(record cell ((v int))) (pub sum s \"doc\" ((a ((c cell))) (b ())))"
"held by value"))
(test-assert "held by value it is an error" (test-assert "held by value it is an error"
(interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))" (interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))"
"held by value")) "held by value"))

View File

@@ -45,6 +45,14 @@
(process-wait proc) (process-wait proc)
(list (process-exit-status proc) out err)))) (list (process-exit-status proc) out err))))
;;; (status) of PROGRAM run on its own
(define (sexc-run-program program)
(let ((proc (process* program (list))))
(close-output-port (process-input-port proc))
(read-string #f (process-output-port proc))
(process-wait proc)
(list (process-exit-status proc))))
;;; Whether sexc exits 0 when run with ARGS, saying nothing of it ;;; Whether sexc exits 0 when run with ARGS, saying nothing of it
(define (sexc-succeeds? . args) (define (sexc-succeeds? . args)
(let ((proc (process* sexc args))) (let ((proc (process* sexc args)))
@@ -105,6 +113,17 @@
(test "and the source is as it was" hello-source (test "and the source is as it was" hello-source
(call-with-input-file "victim.sex" (lambda (p) (read-string #f p)))) (call-with-input-file "victim.sex" (lambda (p) (read-string #f p))))
;;; A compiler is named with its own options, as make's CC is: SEX_CC
;;; "cc -DCODE=7" went looking for an executable of that whole name
(call-with-output-file "code.sex" (lambda (p) (display "(pub fn main () int (return CODE))" p)))
(test "a compiler given with options takes them"
7
(begin
(set-environment-variable! "SEX_CC" "cc -DCODE=7")
(let ((built (sexc-succeeds? "code.sex" "-o" "code")))
(unset-environment-variable! "SEX_CC")
(and built (car (sexc-run-program "./code"))))))
;;; An error is its message: CHICKEN's call history after it is for ;;; An error is its message: CHICKEN's call history after it is for
;;; working on sexc itself, and asked for by SEXC_DEBUG ;;; working on sexc itself, and asked for by SEXC_DEBUG
(test-assert "a missing input says so" (test-assert "a missing input says so"

View File

@@ -36,6 +36,9 @@
;;; Two modules may import each other with `pub': cycle/a and cycle/b, ;;; Two modules may import each other with `pub': cycle/a and cycle/b,
;;; whose interfaces each import the other back. ;;; whose interfaces each import the other back.
;;; ;;;
;;; gen/made writes its pub forms by macro calls, which are expanded when
;;; its interface is read; the macros it used for that stay its own.
;;;
;;; csi -s check.scm ;;; csi -s check.scm
(import scheme (import scheme
@@ -103,6 +106,20 @@
'("cycle 1 2") '("cycle 1 2")
(output-of "./cycle-app")) (output-of "./cycle-app"))
;;; ...it lists pub forms a macro wrote, and not the macros that wrote them
(test "a module's interface holds what its macros wrote"
'("(pub fn forty-two () int)" "(pub fn seven () int)"
"(pub fn one () int)" "(pub fn two () int)")
(output-of sexc "--public-interface" "gen/made.sex"))
(test-assert "and an importer links with them"
(and (output-of sexc "gen/made.sex" "-c" "-o" "made.o")
(output-of sexc "made-app.sex" "-o" "made-app" "--" "made.o")))
(test "and calls them"
'("made 42 7 1 2")
(output-of "./made-app"))
;;; ...while a realization of a spec importers cannot see is refused by ;;; ...while a realization of a spec importers cannot see is refused by
;;; the module that writes it, not by an importer naming it first ;;; the module that writes it, not by an importer naming it first
(test-assert "a pub realization of a spec imported without pub is refused" (test-assert "a pub realization of a spec imported without pub is refused"
@@ -127,6 +144,7 @@
(for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d" (for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d"
"page.o" "book.o" "shelf-app" "page.o" "book.o" "shelf-app"
"cycle-a.o" "cycle-b.o" "cycle-app")) "cycle-a.o" "cycle-b.o" "cycle-app"
"made.o" "made-app"))
(test-exit) (test-exit)

View File

@@ -0,0 +1,23 @@
;;; Its pub forms are written by macro calls -- its own, one it imports
;;; without pub, and a splice of two -- and reach importers as written
;;; ones do. One reading a record kept private here cannot expand for an
;;; importer, who is given nothing of it, as before, and still builds
(import makers)
(defmacro (getter name value)
`(pub fn ,name () int (return ,value)))
(defmacro (pair-of a b)
`($ (pub fn ,a () int (return 1)) (pub fn ,b () int (return 2))))
(defmacro (field-count name)
(let ((fields (get-fields name)))
(unless fields (error "no such record" name))
`(pub fn ,(cat name "-fields") () int (return ,(length fields)))))
(record box ((w int) (h int)))
(getter forty-two 42)
(field-count box)
(constant seven 7)
(pair-of one two)

View File

@@ -0,0 +1,3 @@
;;; A macro module: what it writes is up to whoever calls it
(pub defmacro (constant name value)
`(pub fn ,name () int (return ,value)))

View File

@@ -0,0 +1,6 @@
(include stdio.h)
(import gen/made)
(pub fn main () int
(printf "made %d %d %d %d\n" (forty-two) (seven) (one) (two))
(return 0))

View File

@@ -60,6 +60,10 @@
(reader-test '((- _x)) "-_x") (reader-test '((- _x)) "-_x")
(reader-test '(-1 -1.5) "-1 -1.5") (reader-test '(-1 -1.5) "-1 -1.5")
(reader-test '(- -> ->> -- -=) "- -> ->> -- -=") (reader-test '(- -> ->> -- -=) "- -> ->> -- -=")
;; ...but a quoted name is a name: (cat name '-fields) built `name(- fields)'
(reader-test '((quote -fields)) "'-fields")
;; while a quoted list is code a macro hands back as often as not
(reader-test '((quote ((- a) b))) "'(-a b)")
;; a number in another radix, spelled the way C does or the way ;; a number in another radix, spelled the way C does or the way
;; Scheme does; either is an ordinary integer from here on ;; Scheme does; either is an ordinary integer from here on
@@ -74,6 +78,15 @@
"1_000_000 0xFF_FF 0b1010_0101 1_000.5 -1_000 1e0_5 #x80_00") "1_000_000 0xFF_FF 0b1010_0101 1_000.5 -1_000 1e0_5 #x80_00")
(reader-test '(_1 1_ 1__0 0x_FF 0xF_p) "_1 1_ 1__0 0x_FF 0xF_p") (reader-test '(_1 1_ 1__0 0x_FF 0xF_p) "_1 1_ 1__0 0x_FF 0xF_p")
;; 017 is 17 to Scheme and 15 to C, so it means one thing to whoever
;; reads the Sex and another to whoever reads the C
(test "a leading 0 is refused"
"a leading 0 is octal to C; write the number without it, or #o for octal"
(read-error "017"))
(test "with a suffix as well" #t (string? (read-error "017u")))
(test "or grouped" #t (string? (read-error "0_17")))
(reader-test '(0 0.5 15) "0 00.5 #o17")
;; Scheme reads numbers C has no literal for, and they reached C as ;; Scheme reads numbers C has no literal for, and they reached C as
;; written: 1/2 is integer division there, and +inf.0 no number at all ;; written: 1/2 is integer division there, and +inf.0 no number at all
(test "a ratio is not a C number" (test "a ratio is not a C number"