Compare commits
10 Commits
2bc07a5a5f
...
cce756d8aa
| Author | SHA1 | Date | |
|---|---|---|---|
| cce756d8aa | |||
| 777fb84638 | |||
| ad4ef97bfe | |||
| df7e4e69e8 | |||
| e16e53cd7c | |||
| d8309f6ad8 | |||
| cbf77c3e19 | |||
| 2189be0a76 | |||
| 52057c1cf2 | |||
| d62a1d4c97 |
@@ -90,7 +90,8 @@ sexc ./examples/hello-world.sex -o hello
|
||||
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~,
|
||||
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:
|
||||
|
||||
|
||||
21
reader.scm
21
reader.scm
@@ -114,7 +114,7 @@
|
||||
(let ((line (current-line)))
|
||||
(stamp (read-comment port) line)))
|
||||
((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)
|
||||
@@ -196,8 +196,24 @@
|
||||
(finish-atom (list->string (reverse 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)
|
||||
(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
|
||||
((string=? s ".") dot-token)
|
||||
((or (string->number digits) (radix-number digits))
|
||||
@@ -350,7 +366,8 @@
|
||||
;;; -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: -> --
|
||||
(define (negated-name? s)
|
||||
(and (> (string-length s) 1)
|
||||
(and (negating?)
|
||||
(> (string-length s) 1)
|
||||
(char=? #\- (string-ref s 0))
|
||||
(let ((c (string-ref s 1)))
|
||||
(or (char-alphabetic? c) (char=? c #\_)))))
|
||||
|
||||
145
semen.scm
145
semen.scm
@@ -36,8 +36,22 @@
|
||||
;;; in auxiliary structures and functions.
|
||||
(define (process raw-sex-forms)
|
||||
(reset-declarations!)
|
||||
(set! +called-as-functions+ (make-hash-table))
|
||||
(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)
|
||||
(cond
|
||||
((null? forms) (reverse acc))
|
||||
@@ -130,7 +144,11 @@
|
||||
(process-imports (get-modules-public-forms sex-form) acc))
|
||||
|
||||
((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)
|
||||
('pub 'typedef new-type target))
|
||||
@@ -252,10 +270,10 @@
|
||||
;; between name and fields are not skipped, they already confuse the
|
||||
;; writer
|
||||
(match form
|
||||
(('pub (and kind (or 'struct 'union 'enum))
|
||||
(('pub (and kind (or 'struct 'union 'enum 'sum))
|
||||
(? symbol? name) (? string? doc) . 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)
|
||||
(values doc (copy-form-source! form `(,kind ,name ,@rest))))
|
||||
(_ (values #f form))))
|
||||
@@ -735,6 +753,9 @@
|
||||
,receiver
|
||||
,@(walk-operands form (cdr form) env))))))
|
||||
(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))
|
||||
env))))))))
|
||||
|
||||
@@ -1025,7 +1046,7 @@
|
||||
;;; binders, or a variant left out with no `else' are all errors here:
|
||||
;;; a missing case is the thing this form exists to catch.
|
||||
(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
|
||||
;; drops one between switch clauses
|
||||
(arms (remove comment-form? (cddr form)))
|
||||
@@ -1049,6 +1070,13 @@
|
||||
,(match-branches name held variants arms))
|
||||
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)
|
||||
(append-map (lambda (arm)
|
||||
(if (eq? (car arm) 'else) '() (delete '_ (second (car arm)))))
|
||||
@@ -1174,10 +1202,12 @@
|
||||
(let ((name (method-head-name (car form))))
|
||||
(unless (pair? (cdr form))
|
||||
(sex-error form "a method call needs a receiver" form))
|
||||
(let* ((receiver (second form))
|
||||
(written (expression-type receiver env))
|
||||
(target (pointer-target written))
|
||||
(key (ops-key (or target written))))
|
||||
;; through a pointer chain, as a path reads through one
|
||||
(let*-values (((receiver written)
|
||||
(through-pointers (walk-operand (second form) env)
|
||||
(expression-type (second form) env)))
|
||||
((target) (pointer-target written))
|
||||
((key) (ops-key (or target written))))
|
||||
(unless key
|
||||
(sex-error form "receiver of this has no type to dispatch on" name))
|
||||
(let ((method (get-method key name)))
|
||||
@@ -1187,7 +1217,7 @@
|
||||
form
|
||||
`(,method
|
||||
,(method-receiver method (or target written) (and target #t)
|
||||
(walk-operand receiver env) form)
|
||||
receiver form)
|
||||
,@(walk-operands form (cddr form) env)))))))
|
||||
|
||||
;;; A method taking (* Self) wants an address and one taking Self wants
|
||||
@@ -1233,8 +1263,9 @@
|
||||
(told? #f))
|
||||
(if (null? fields)
|
||||
(copy-form-source! form expr)
|
||||
(let* ((target (pointer-target type))
|
||||
(known (type-fields (or target type))))
|
||||
(let*-values (((expr type) (through-pointers expr type))
|
||||
((target) (pointer-target type))
|
||||
((known) (type-fields (or target type))))
|
||||
(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)))
|
||||
(unless (or type told?)
|
||||
@@ -1248,6 +1279,19 @@
|
||||
(cdr fields)
|
||||
(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
|
||||
(define (field-name 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:
|
||||
;; (<< l b), l : long -> long; (>> c b), c : char -> int
|
||||
((<< >>) (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
|
||||
((~ bit-not bitwise-not) (promoted-type (expression-type (second expr) env)))
|
||||
;; ...and an increment is the operand unpromoted:
|
||||
@@ -1454,6 +1504,12 @@
|
||||
(let ((type (underlying (parse-type written))))
|
||||
(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)
|
||||
(cond
|
||||
((not left) right)
|
||||
@@ -1539,8 +1595,7 @@
|
||||
(define (member-path-type type fields)
|
||||
(if (null? fields)
|
||||
type
|
||||
(member-path-type (step-type (or (pointer-target type) type)
|
||||
(car fields))
|
||||
(member-path-type (step-type (pointed-through type) (car fields))
|
||||
(cdr fields))))
|
||||
|
||||
;;; 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
|
||||
;;; namespace, where the constructor of that name already lives. As a
|
||||
;;; tag it shares nothing.
|
||||
(define (process-sum form acc)
|
||||
(let* ((core (if (eq? (car form) 'pub) (cdr form) form))
|
||||
(name (and (pair? (cdr core)) (cadr core)))
|
||||
(declared (and (pair? (cddr core)) (caddr core))))
|
||||
(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))))))
|
||||
(define (process-sum written acc)
|
||||
(let-values (((doc form) (extract-aggregate-docstring written)))
|
||||
(let* ((core (if (eq? (car form) 'pub) (cdr form) form))
|
||||
(name (and (pair? (cdr core)) (cadr core)))
|
||||
(declared (and (pair? (cddr core)) (caddr core))))
|
||||
(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)
|
||||
;; 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)
|
||||
(unless (and (list? v) (= 2 (length v)) (symbol? (car v)) (list? (cadr 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))))
|
||||
|
||||
;;; 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.
|
||||
;;; An array field is spelled out element by element wherever it is
|
||||
;;; copied, so its length has to be one sexc can count.
|
||||
|
||||
@@ -16,7 +16,9 @@
|
||||
get-macro
|
||||
macro?
|
||||
apply-macro
|
||||
defmacro)
|
||||
defmacro
|
||||
macro-definition
|
||||
restore-macro!)
|
||||
|
||||
(define (cat-syms s-1 s-2)
|
||||
(fmt #f s-1 s-2))
|
||||
@@ -91,6 +93,17 @@
|
||||
(sex-error form "macro takes at least this many arguments"
|
||||
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)
|
||||
(let ((arglist (car form))
|
||||
(body (cdr form)))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
(import
|
||||
scheme
|
||||
(chicken base)
|
||||
(chicken condition)
|
||||
(chicken file)
|
||||
(chicken load)
|
||||
(chicken module)
|
||||
@@ -9,6 +10,7 @@
|
||||
(chicken string)
|
||||
matchable
|
||||
reader
|
||||
sex-macros
|
||||
srfi-1
|
||||
types
|
||||
utils)
|
||||
@@ -121,9 +123,78 @@
|
||||
;; 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
|
||||
;; importers see only behind a pointer go first.
|
||||
(let ((raw-forms (read-module module-path)))
|
||||
(append (check-public-interface raw-forms)
|
||||
(interface-forms raw-forms))))
|
||||
(let ((forms (module-forms (read-module module-path))))
|
||||
(append (check-public-interface 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)
|
||||
(let ((helpers (helper-macros raw-forms)))
|
||||
@@ -138,12 +209,8 @@
|
||||
;;; the importer expands them all, the way a C header that defines a
|
||||
;;; macro has to define what it uses too
|
||||
(define (helper-macros raw-forms)
|
||||
(let* ((macro-name (lambda (form)
|
||||
(match form
|
||||
(('defmacro (name . _) . _) name)
|
||||
(('pub 'defmacro (name . _) . _) name)
|
||||
(_ #f))))
|
||||
(private (filter (lambda (form) (and (macro-name form) (not (pub-form? form))))
|
||||
(let* ((private (filter (lambda (form)
|
||||
(and (macro-name form) (not (pub-form? form))))
|
||||
raw-forms))
|
||||
(mentions (lambda (form name)
|
||||
(let walk ((x (cdr form)))
|
||||
@@ -268,7 +335,8 @@
|
||||
(('pub 'define name . body) body)
|
||||
(('pub (or 'record 'struct 'union) name . 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)
|
||||
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
|
||||
(remove comment-form? variants)))
|
||||
@@ -384,7 +452,8 @@
|
||||
(cond ((null? paths) (concatenate (reverse acc)))
|
||||
((member (car paths) seen) (next (cdr paths) seen acc))
|
||||
(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))
|
||||
(cons (car paths) seen)
|
||||
(cons (declared-types forms) acc)))))))
|
||||
|
||||
22
sexc.scm
22
sexc.scm
@@ -217,9 +217,11 @@
|
||||
(define (compile-to-file sex-forms output args cc-args)
|
||||
"Hand the generated C to the C compiler. Returns the compiler's exit
|
||||
status, which is ours to pass on."
|
||||
(let ((compiler (or (get-arg args 'c-compiler #f)
|
||||
(get-env-var "SEX_CC")
|
||||
"cc"))
|
||||
;; named with its own options, as make's CC is: "ccache cc", "cc -O2"
|
||||
(let ((compiler (let ((words (string-split (or (get-arg args 'c-compiler #f)
|
||||
(get-env-var "SEX_CC")
|
||||
"cc"))))
|
||||
(if (null? words) (list "cc") words)))
|
||||
(out-file (if (eq? output 'default)
|
||||
"a.out"
|
||||
output))
|
||||
@@ -233,12 +235,14 @@ status, which is ours to pass on."
|
||||
(begin (delete-file* c-file) (abort exn))
|
||||
(with-output-to-file c-file
|
||||
(lambda () (emit-c sex-forms)))
|
||||
(let ((proc (process compiler (append (list "-o" out-file)
|
||||
(if (get-arg args 'compile-object #f)
|
||||
(list "-c")
|
||||
(list))
|
||||
(list c-file)
|
||||
cc-args))))
|
||||
(let ((proc (process (car compiler)
|
||||
(append (cdr compiler)
|
||||
(list "-o" out-file)
|
||||
(if (get-arg args 'compile-object #f)
|
||||
(list "-c")
|
||||
(list))
|
||||
(list c-file)
|
||||
cc-args))))
|
||||
(call-with-values (lambda () (process-wait proc))
|
||||
(lambda (pid normal-exit? status)
|
||||
(delete-file* c-file)
|
||||
|
||||
@@ -406,6 +406,14 @@ whitespace aside, THEN?"
|
||||
"macro takes at least this many arguments many 1"))
|
||||
(test-assert "while it may take any more"
|
||||
(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"
|
||||
(reports? "(defmacro (boom x) (error \"no good\" x))\n(fn f () int (return (boom 1)))"
|
||||
"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,
|
||||
;; union or enum) is a docstring: it becomes a comment immediately
|
||||
;; 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-assert "appears before the function"
|
||||
(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 call was typed by asking the global table what a function of
|
||||
;; 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
|
||||
(test-assert "#t is a bool, as true is"
|
||||
(emits? (in-fn "(var t _ #t)") "bool t = 1"))
|
||||
@@ -1294,6 +1319,22 @@ whitespace aside, THEN?"
|
||||
(emits? poly-src "q->pp[0]->y"))
|
||||
(test-assert "and every index of one is written"
|
||||
(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
|
||||
;; value, and a method could not find its receiver's type
|
||||
(test-assert "a typedef of a pointer reads with an arrow"
|
||||
|
||||
@@ -506,6 +506,27 @@ ahead of its pub forms."
|
||||
(emits? (sum-src "(var c (closure () int) (closure () int ((t s))"
|
||||
" (match t ((circle (r)) (return 1)) (else (return 0)))))")
|
||||
"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"
|
||||
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
||||
"match wants a sum")))
|
||||
@@ -552,6 +573,9 @@ ahead of its pub forms."
|
||||
(test "a private one is solved where it is"
|
||||
'()
|
||||
(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"
|
||||
(interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))"
|
||||
"held by value"))
|
||||
|
||||
@@ -45,6 +45,14 @@
|
||||
(process-wait proc)
|
||||
(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
|
||||
(define (sexc-succeeds? . args)
|
||||
(let ((proc (process* sexc args)))
|
||||
@@ -105,6 +113,17 @@
|
||||
(test "and the source is as it was" hello-source
|
||||
(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
|
||||
;;; working on sexc itself, and asked for by SEXC_DEBUG
|
||||
(test-assert "a missing input says so"
|
||||
|
||||
@@ -36,6 +36,9 @@
|
||||
;;; Two modules may import each other with `pub': cycle/a and cycle/b,
|
||||
;;; 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
|
||||
|
||||
(import scheme
|
||||
@@ -103,6 +106,20 @@
|
||||
'("cycle 1 2")
|
||||
(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
|
||||
;;; 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"
|
||||
@@ -127,6 +144,7 @@
|
||||
|
||||
(for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d"
|
||||
"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)
|
||||
|
||||
23
tests/modules/gen/made.sex
Normal file
23
tests/modules/gen/made.sex
Normal 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)
|
||||
3
tests/modules/gen/makers.sex
Normal file
3
tests/modules/gen/makers.sex
Normal 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)))
|
||||
6
tests/modules/made-app.sex
Normal file
6
tests/modules/made-app.sex
Normal 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))
|
||||
@@ -60,6 +60,10 @@
|
||||
(reader-test '((- _x)) "-_x")
|
||||
(reader-test '(-1 -1.5) "-1 -1.5")
|
||||
(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
|
||||
;; 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")
|
||||
(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
|
||||
;; written: 1/2 is integer division there, and +inf.0 no number at all
|
||||
(test "a ratio is not a C number"
|
||||
|
||||
Reference in New Issue
Block a user