Compare commits

..

36 Commits

Author SHA1 Message Date
f9809c19c4 tell what a member path walked from the fields it has left 2026-10-14 20:09:49 +03:00
31b7046048 share reading a test source and catching its error
encapsulation.scm had copies of both from codegen.scm.
2026-10-14 20:09:17 +03:00
2cfce45935 parenthesise a conditional without a helper to do it for 2026-10-14 20:09:17 +03:00
bb252dc244 tell a grouped for header by its shape, not by a var in it
(for ((= s 0) (< s n) (++ s)) ...) passed the check and reached cc as
a call of (= s 0).
2026-10-14 20:08:39 +03:00
eeb5357fd4 read each module once per unit
A pub import chain had its interface check read every module below
again, so the deepest was parsed once per level above it.
2026-10-14 20:08:16 +03:00
1aed7cee4c compile the name spellings once, and skip a name that has none
Read from a table, the patterns were compiled again for every symbol
written; that tripled what unkebabify cost.
2026-10-14 20:07:20 +03:00
4ada1059a2 name the test for output going to stdout
-o - was taught to two copies of it.
2026-10-14 20:07:03 +03:00
9a6e2dcd83 keep the parameter-type rule in types, where both readers find it
sex-modules spelled out its own copy of named-arg-type, and its own
strip-pub; a parameter shape taught to one would not reach the other.
2026-10-14 20:06:51 +03:00
00e75dee48 declare an ops method ahead through the one prototype accessor
fn-prototype repeated sex-fn-prototype, and names? repeated mentions?;
each pair kept the better half.
2026-10-14 20:06:51 +03:00
2904be4499 drop walk-literal, which the rebase left without callers
walk-initializer took its place, but walked the values as statements;
a match among them is refused again wherever the literal stands.
2026-10-14 20:05:29 +03:00
ae9cdbbf31 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-14 19:22:34 +03:00
66d088f6fc 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-14 19:22:34 +03:00
3737f90641 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-14 19:22:34 +03:00
58f145bd95 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-14 19:22:34 +03:00
e6b6ca799d 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-14 19:22:34 +03:00
a6689b8c81 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-14 19:22:34 +03:00
b717aa808d 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-14 19:22:34 +03:00
b49bb8f716 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-14 19:22:34 +03:00
a048925c7a 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-14 19:22:34 +03:00
a14484d1dc 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-14 19:22:34 +03:00
42fe477e35 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-14 19:22:34 +03:00
062cb18c60 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-14 19:22:32 +03:00
4e0938ab1f 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-14 19:21:16 +03:00
2bad5db114 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-14 19:21:16 +03:00
3e01fe41e8 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-14 19:21:13 +03:00
52228e6fc6 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-14 19:12:47 +03:00
08853d2c77 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-14 19:12:47 +03:00
6fbeaedbe6 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-14 19:12:47 +03:00
9d4454afe3 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-14 19:12:47 +03:00
5a887c0654 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-14 19:12:47 +03:00
b5de28342b 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-14 19:12:47 +03:00
dff4a5af53 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-14 19:12:47 +03:00
f24adfa2db 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-14 19:12:47 +03:00
82629867c1 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-14 19:12:47 +03:00
b0dd2d9f94 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-14 19:12:47 +03:00
169933ff2e 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-14 19:12:47 +03:00
8 changed files with 30 additions and 160 deletions

View File

@@ -553,9 +553,8 @@
;;; ;;;
;;; (var n int (match s ...)) -> int n = switch (s.tag) { ... ;;; (var n int (match s ...)) -> int n = switch (s.tag) { ...
;;; ;;;
;;; and in a control form's test, which C reads as an expression too: ;;; A condition is not tracked: `walk-parts' takes every slot of an `if'
;;; ;;; alike.
;;; (if (match s ...) ...) -> if (switch (s.tag) { ...
(define operand-position? (make-parameter #f)) (define operand-position? (make-parameter #f))
(define (walk-operand form env) (define (walk-operand form env)
@@ -591,13 +590,10 @@
(cons walk-embed-result (walk-body expansion env))))) (cons walk-embed-result (walk-body expansion env)))))
(else (else
(case (car form) (case (car form)
((do case case/fallthrough default) ((do for while if switch case case/fallthrough default)
(parameterize ((operand-position? #f)) (parameterize ((operand-position? #f))
(with-scope env (lambda () (walk-parts form env))))) (with-scope env (lambda () (walk-parts form env)))))
((for while if switch)
(with-scope env (lambda () (walk-control form env))))
((lambda) ((lambda)
(let ((name (aux-name! env make-lambda-name))) (let ((name (aux-name! env make-lambda-name)))
(add-aux-code! env (lift-lambda name form)) (add-aux-code! env (lift-lambda name form))
@@ -726,12 +722,7 @@
(convert-to-closure type arg env form) (convert-to-closure type arg env form)
(let ((given (expression-type arg env))) (let ((given (expression-type arg env)))
(cond (cond
((brace-initializer? arg) ((brace-initializer? arg) arg)
(let ((held (array-length type)))
(when (and held (not (= held (length (remove comment-form?
(vector->list arg))))))
(sex-error form "array is not as long as its field" (first field))))
arg)
((not (array-form? given)) ((not (array-form? given))
(sex-error form "an array field takes an array" (first field))) (sex-error form "an array field takes an array" (first field)))
((not (eqv? (array-length given) (array-length type))) ((not (eqv? (array-length given) (array-length type)))
@@ -915,29 +906,6 @@
(define (walk-parts form env) (define (walk-parts form env)
(copy-form-source! form (walk-body form env))) (copy-form-source! form (walk-body form env)))
;;; A control form, its tests walked as values and the rest as
;;; statements
(define (walk-control form env)
(let ((slots (count (lambda (part) (not (comment-form? part))) (cdr form))))
(let loop ((parts (cdr form)) (i 0) (acc (list (car form))))
(cond
((null? parts) (copy-form-source! form (reverse acc)))
((comment-form? (car parts)) (loop (cdr parts) i (cons (car parts) acc)))
(else
(let ((walked (parameterize ((operand-position?
(test-slot? (car form) i slots)))
(walk-body (list (car parts)) env))))
(loop (cdr parts) (+ i 1) (append-reverse walked acc))))))))
;;; Whether the Ith of a control form's SLOTS is read as a value: an
;;; `if''s tests but not its trailing else, a `for''s header, the
;;; scrutinee of a `while' or a `switch'
(define (test-slot? head i slots)
(case head
((if) (and (even? i) (< (+ i 1) slots)))
((for) (< i 3))
(else (= i 0))))
;;; (match s ((circle (r)) ...) ((empty ()) ...)) is a switch over the ;;; (match s ((circle (r)) ...) ((empty ()) ...)) is a switch over the
;;; tag, each arm a block binding that variant's fields as locals. `_' ;;; tag, each arm a block binding that variant's fields as locals. `_'
;;; drops one. An arm the sum does not have, a wrong number of ;;; drops one. An arm the sum does not have, a wrong number of
@@ -1116,8 +1084,7 @@
;;; A type we cannot work out is read as a value, which is what the ;;; A type we cannot work out is read as a value, which is what the
;;; path would have meant before any of this. The warning names what ;;; path would have meant before any of this. The warning names what
;;; could not be typed: the base, or the path up to a field of a struct ;;; could not be typed: the base, or the path up to a field of a struct
;;; only C knows -- (. font handle x) warns of (. font handle), not ;;; only C knows -- (. font handle), not font.
;;; font. A field a struct declared here lacks is an error.
(define (walk-member-path form env) (define (walk-member-path form env)
(let ((base (second form))) (let ((base (second form)))
(let step ((expr (walk-operand base env)) (let step ((expr (walk-operand base env))
@@ -1126,10 +1093,7 @@
(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 ((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?) (unless (or type told?)
(let ((walked (drop-right (cddr form) (length fields)))) (let ((walked (drop-right (cddr form) (length fields))))
(sex-warning form "cannot type this; `.' reads it as a value" (sex-warning form "cannot type this; `.' reads it as a value"
@@ -1139,12 +1103,6 @@
(cdr fields) (cdr fields)
(or told? (not type)))))))) (or told? (not 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)))
(second field)
field))
(define (lvalue? expr) (define (lvalue? expr)
(cond ((symbol? expr) #t) (cond ((symbol? expr) #t)
((vector? expr) #t) ; a compound literal is one ((vector? expr) #t) ; a compound literal is one
@@ -1925,8 +1883,7 @@
core core
(if (> (length core) 3) (if (> (length core) 3)
(append (take core 3) (append (take core 3)
(map bare-braces (walk-operands core (drop core 3) env))
(walk-operands core (drop core 3) env)))
core)) core))
env))) env)))
(form (if qualifier (form (if qualifier
@@ -1936,17 +1893,6 @@
(add-name-type! (second core) (third core))) (add-name-type! (second core) (third core)))
(cons form (append (hash-table-ref env :lambda-aux-code) acc)))) (cons form (append (hash-table-ref env :lambda-aux-code) acc))))
;;; A compound literal is not a constant, though cc lets one pass at file
;;; scope unless asked to be pedantic; where it initializes, its braces
;;; alone say the same:
;;;
;;; (var g t (t/a 1)) -> static t g = {.tag = tǂa, .u = {.a = {1}}}
(define (bare-braces form)
(if (vector? form)
(let ((parts (list-split (vector->list form) ':)))
(list->vector (map bare-braces (last parts))))
form))
;;; Utils ;;; Utils
(define (non-empty-list? form) (define (non-empty-list? form)
(and (list? form) (and (list? form)

View File

@@ -202,10 +202,7 @@
;;; ;;;
;;; (* const cell) -> ((const . #t) (cell . #t)) ;;; (* const cell) -> ((const . #t) (cell . #t))
;;; [cell 4] -> ((cell . #f)) ;;; [cell 4] -> ((cell . #f))
;;; (fn ((cell)) int) -> ((cell . #f) (int . #f)) ;;; (fn ((cell)) int) -> ((cell . #t) (int . #t)) ; declaring is enough
;;;
;;; A function type takes and returns by value whatever it is held
;;; through: C wants the body to define or call one of its functions.
(define (type-uses type behind?) (define (type-uses type behind?)
(cond (cond
((symbol? type) (list (cons type behind?))) ((symbol? type) (list (cons type behind?)))
@@ -218,7 +215,7 @@
(if (array-bound? type) (bound-uses (last type)) (list))))) (if (array-bound? type) (bound-uses (last type)) (list)))))
((fn closure) ((fn closure)
(if (and (pair? (cdr type)) (list? (second type))) (if (and (pair? (cdr type)) (list? (second type)))
(append-map (lambda (t) (type-uses t #f)) (append-map (lambda (t) (type-uses t #t))
(signature-types (second type) (last type))) (signature-types (second type) (last type)))
(list))) (list)))
((struct union enum) ((struct union enum)

View File

@@ -59,8 +59,8 @@
(value #f) (value #f)
(single-char #\m)) (single-char #\m))
(output ,(fmt #f "Write output to file. Default file name is a.out." nl (output ,(fmt #f "Write output to file. Default file name is a.out." nl
(pad padding) "With -C, -m or --public-interface, defaults to stdout," nl (pad padding) "If -C or -m options are provided, defaults to stdout;" nl
(pad padding) "which `-' names too") (pad padding) "`-' is stdout too")
(required #f) (required #f)
(value #t) (value #t)
(single-char #\o)) (single-char #\o))
@@ -145,12 +145,6 @@
(define (stdout-output? output) (define (stdout-output? output)
(or (eq? output 'default) (equal? output "-"))) (or (eq? output 'default) (equal? output "-")))
;;; Whether ARGS ask for text, which `-o -' can send to stdout. An
;;; executable or an object goes to cc's -o, where `-' is a file name
(define (text-output? args)
(any (lambda (option) (get-arg args option #f))
'(emit-c macro-expand public-interface)))
(define (write-to-file-or-stdout output what) (define (write-to-file-or-stdout output what)
(if (stdout-output? output) (if (stdout-output? output)
(what) (what)
@@ -260,8 +254,6 @@ status, which is ours to pass on."
(when help (when help
(print-help) (print-help)
(return #f)) (return #f))
(when (and (equal? output "-") (not (text-output? args)))
(error "-o - is stdout, which takes only -C, -m or --public-interface"))
;; Read time comes before everything, so the features have to be ;; Read time comes before everything, so the features have to be
;; in place before the first form is read ;; in place before the first form is read
(current-features (current-features

View File

@@ -742,8 +742,7 @@ whitespace aside, THEN?"
(not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))" (not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))"
"*(p).x"))) "*(p).x")))
(test-assert "member access through a cast" (test-assert "member access through a cast"
(emits? (string-append "(struct s ((f int)))" (emits? (in-fn "(var n int (. (* (cast a (* (struct s)))) f))")
(in-fn "(var n int (. (* (cast a (* (struct s)))) f))"))
"(*(struct s*)a).f")) "(*(struct s*)a).f"))
;; ...without gaining parens where none are due ;; ...without gaining parens where none are due
(test-assert "a bare dereference is left alone" (test-assert "a bare dereference is left alone"
@@ -775,14 +774,7 @@ whitespace aside, THEN?"
(test-assert "nor is an aggregate tag" (test-assert "nor is an aggregate tag"
(emits? "(struct t ((z int))) (struct s ((a [struct t])))" "struct t a[]")) (emits? "(struct t ((z int))) (struct s ((a [struct t])))" "struct t a[]"))
(test-assert "nor one behind a pointer" (test-assert "nor one behind a pointer"
(emits? "(struct t ((z int))) (struct s ((a [* struct t])))" "struct t* a[]")) (emits? "(struct t ((z int))) (struct s ((a [* struct t])))" "struct t* a[]")))
;; a qualifier waits for a type name only when none came before it
(test-assert "a name after a qualified type is a bound"
(emits? (in-fn "(var b [int const N])") "int const b[N]"))
(test-assert "after a qualified name too"
(emits? (in-fn "(var b [mytype const N])") "mytype const b[N]"))
(test-assert "but a qualifier alone still waits for the type"
(emits? "(typedef mytype int) (struct s ((a [const mytype])))" "const mytype a[]")))
;; `sizeof' takes a type or an expression, and `(* x)' is either. ;; `sizeof' takes a type or an expression, and `(* x)' is either.
;; What is in scope says which: a name C alone knows is a type ;; What is in scope says which: a name C alone knows is a type
@@ -892,14 +884,6 @@ whitespace aside, THEN?"
(test-assert "which one step through it does not need" (test-assert "which one step through it does not need"
(not (warns? (in-fn "(var t (* struct tm) 0) (= (. t a) 1)") (not (warns? (in-fn "(var t (* struct tm) 0) (= (. t a) 1)")
"cannot type"))) "cannot type")))
(test-assert "but a struct declared here has only the fields it names"
(reports? (in-line "(= (. p z) 1)") "no field of this name"))
(test-assert "the last step included"
(reports? (in-line "(= (. lp a z) 1)") "no field of this name"))
(test-assert "an indexed one by its name"
(reports? (string-append "(struct v ((xs [int 2])))"
(in-fn "(var w (struct v)) (= (. w [ys 0]) 1)"))
"no field of this name"))
;; the arrow is `%->' and nothing else writes one, so a bare `->' ;; the arrow is `%->' and nothing else writes one, so a bare `->'
;; is an ordinary head -- a threading macro that nobody imported, ;; is an ordinary head -- a threading macro that nobody imported,
;; and a call like any other unknown ;; and a call like any other unknown

View File

@@ -229,11 +229,7 @@ ahead of its pub forms."
(not (emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))" (not (emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))"
"t tːa ("))) "t tːa (")))
(test-assert "so it is a constant at file scope" (test-assert "so it is a constant at file scope"
(emits? "(sum t ((a ((x int)))))(var g t (t/a 1))" (emits? "(sum t ((a ((x int)))))(var g t (t/a 1))" "static t g = (t){"))
"static t g = {.tag = tǂa, .u = {.a = {1}}};"))
(test-assert "as is any literal a global starts from"
(emits? "(record point ((x int) (y int)))(var g point #(point : 1 2))"
"static point g = {1, 2};"))
(test-assert "and an lvalue a method can take the address of" (test-assert "and an lvalue a method can take the address of"
(emits? (string-append (emits? (string-append
"(sum t ((a ((x int)))))" "(sum t ((a ((x int)))))"
@@ -258,12 +254,6 @@ ahead of its pub forms."
(test-assert "and as many as the field holds" (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)))" (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")) "array is not as long as its field"))
(test-assert "and so must a literal"
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1 2 3))))"
"array is not as long as its field"))
(test-assert "not filling it with zeros either"
(reports? "(sum t ((a ((xs [int 2])))))(fn f () t (return (t/a #(1))))"
"array is not as long as its field"))
(test-assert "an array field takes an array" (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)))" (reports? "(sum t ((a ((xs [int 2])))))(fn f () t (var p (* int) 0) (return (t/a p)))"
"an array field takes an array")) "an array field takes an array"))
@@ -468,20 +458,6 @@ ahead of its pub forms."
(test-assert "not even as an argument" (test-assert "not even as an argument"
(reports? (sum-src "(= out (match s ((circle (r)) r) (else 0.0)))") (reports? (sum-src "(= out (match s ((circle (r)) r) (else 0.0)))")
"match is a statement")) "match is a statement"))
(test-assert "nor as an if's test"
(reports? (sum-src "(if (match s ((circle (r)) 1) (else 0)) (= out 1.0))")
"match is a statement"))
(test-assert "nor as a while's"
(reports? (sum-src "(while (match s ((circle (r)) 1) (else 0)) (= out 1.0))")
"match is a statement"))
(test-assert "nor in a for's header"
(reports? (sum-src "(for (var i int 0) (match s ((circle (r)) 1) (else 0)) (++ i)"
" (= out 1.0))")
"match is a statement"))
(test-assert "but an if's branches are statements, the else among them"
(emits? (sum-src "(if (== out 0.0) (match s ((circle (r)) (= out r)) (else (= out 1.0)))"
" (match s ((circle (r)) (= out 2.0)) (else (= out 3.0))))")
"out = 3.0;"))
(test-assert "but a case body 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))" (emits? (sum-src "(switch 1 (case 1 (match s ((circle (r)) (= out r))"
" (else (= out 0.0)))))") " (else (= out 0.0)))))")
@@ -527,15 +503,6 @@ ahead of its pub forms."
"held by value")) "held by value"))
(test-assert "returned" (test-assert "returned"
(interface-reports? "(record cell ((v int))) (pub fn f () cell)" "held by value")) (interface-reports? "(record cell ((v int))) (pub fn f () cell)" "held by value"))
(test-assert "or taken by a function a pub one takes"
(interface-reports? "(record cell ((v int))) (pub fn take ((cb (fn ((c cell)) void))) void)"
"held by value"))
(test-assert "or returned by one"
(interface-reports? "(record cell ((v int))) (pub fn take ((cb (fn () cell))) void)"
"held by value"))
(test "but a pointer there is still a pointer"
'((record cell))
(interface-of "(record cell ((v int))) (pub fn take ((cb (fn ((c (* cell))) void))) void)"))
(test-assert "or taken by a pub method" (test-assert "or taken by a pub method"
(interface-reports? "(record cell ((v int))) (ops cell (pub fn get ((self cell)) int))" (interface-reports? "(record cell ((v int))) (ops cell (pub fn get ((self cell)) int))"
"held by value")) "held by value"))

View File

@@ -1,14 +1,12 @@
# The failure paths. # The failure paths.
# #
# Each needs a process to show itself, so none fits the unit suite: # Both need a process to show themselves, so neither fits the unit
# sexc has to fail when cc fails -- exiting 0 after a failed compile # suite: sexc has to fail when cc fails -- exiting 0 after a failed
# makes every driver, sextest included, read it as success -- it has to # compile makes every driver, sextest included, read it as success --
# clean up its temporary .c when its own emission throws, and it has to # and it has to clean up its temporary .c when its own emission throws.
# refuse `-o -' for an executable, which cc would write to a file named
# `-'.
# #
# Everything is built inside a scratch directory, so nothing is left # Everything is built inside a scratch TMPDIR, so nothing is left here
# here to clean up. # to clean up.
SEXC ?= ../../sexc SEXC ?= ../../sexc
@@ -23,10 +21,6 @@ check:
echo "exit code FAILED: sexc reported success after cc failed"; \ echo "exit code FAILED: sexc reported success after cc failed"; \
rm -rf $$d; exit 1; \ rm -rf $$d; exit 1; \
fi; \ fi; \
if (cd $$d && $(abspath $(SEXC)) $(CURDIR)/hello.sex -o - >/dev/null 2>&1) || [ -e $$d/- ]; then \
echo "exit code FAILED: -o - built an executable"; \
rm -rf $$d; exit 1; \
fi; \
rm -rf $$d; echo "exit code ok" rm -rf $$d; echo "exit code ok"
.PHONY: check .PHONY: check

View File

@@ -1,6 +1,6 @@
(compilation "-- -std=c11 -pedantic-errors") (compilation "-- -std=c11 -pedantic-errors")
(input) (input)
(output "c11: 42 43 6 4 6") (output "c11: 42 43 6")
(return 0) (return 0)
;;; C11 is the floor, and this is the program that holds it there: the ;;; C11 is the floor, and this is the program that holds it there: the
@@ -10,8 +10,7 @@
;;; ;;;
;;; A capturing closure is the one that leans on C11 itself, through ;;; A capturing closure is the one that leans on C11 itself, through
;;; max_align_t in the environment and `_Static_assert' on its size; ;;; max_align_t in the environment and `_Static_assert' on its size;
;;; the record, the method and the match are here for what they emit, ;;; the record, the method and the match are here for what they emit.
;;; and the globals for initializers that have to be constants.
(include stdio.h) (include stdio.h)
@@ -23,9 +22,6 @@
(sum shape ((square ((side int))) (dot ()))) (sum shape ((square ((side int))) (dot ())))
(var corner point #(point : 2 3))
(var unit shape (shape/square 1))
(fn sides ((s shape)) int (fn sides ((s shape)) int
(var n int 0) (var n int 0)
(match s (match s
@@ -38,7 +34,6 @@
(var k int 1) (var k int 1)
(var add-k (closure ((int)) int) (var add-k (closure ((int)) int)
(closure ((v int)) int (k) (return (+ v k)))) (closure ((v int)) int (k) (return (+ v k))))
(printf "c11: %d %d %d %d %d\n" (printf "c11: %d %d %d\n"
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2) (.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2))
(sides unit) (.area corner))
(return 0)) (return 0))

View File

@@ -409,14 +409,14 @@
arglist)) arglist))
;;; The last element is a bound unless what precedes it is still ;;; The last element is a bound unless what precedes it is still
;;; waiting for a type name -- a `*', a tag's keyword, a qualifier with ;;; waiting for a type name -- a `*', a qualifier, a tag's keyword. C
;;; no type before it. C spells a type with one name at most, so a name ;;; spells a type with one name at most, so a name after a name, or
;;; after a name, or after a grouped type, can only be the bound: ;;; after a grouped type, can only be the bound:
;;; ;;;
;;; (¤ int 4) four of int (¤ unsigned int) unsized ;;; (¤ int 4) four of int (¤ unsigned int) unsized
;;; (¤ pthread-t CAP) CAP of pthread-t (¤ struct point) unsized ;;; (¤ pthread-t CAP) CAP of pthread-t (¤ struct point) unsized
;;; (¤ (* T) CAP) CAP of (* T) (¤ const mytype) unsized ;;; (¤ (* T) CAP) CAP of (* T) (¤ const mytype) unsized
;;; (¤ int const N) N of const int (¤ * size-t) unsized ;;; (¤ * size-t) unsized
;;; ;;;
;;; TYPE is the whole `(¤ ...)' form. ;;; TYPE is the whole `(¤ ...)' form.
(define (array-bound? type) (define (array-bound? type)
@@ -426,13 +426,8 @@
(cond (cond
((not (symbol? bound)) #t) ((not (symbol? bound)) #t)
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f) ((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
((memq preceding '(* struct union enum)) #f) (else (not (or (memq preceding '(* struct union enum))
((memq preceding +c-qualifiers+) (memq preceding +c-qualifiers+))))))))
(any (lambda (word)
(not (or (memq word '(* struct union enum))
(memq word +c-qualifiers+))))
(drop-right (cdr type) 2)))
(else #t)))))
;;; What one element of a written array type is: ;;; What one element of a written array type is:
;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char) ;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char)