;;; Codegen details that are easy to get subtly wrong, and that the ;;; walk-* unit tests cannot see: they check the intermediate form we ;;; hand to fmt-c, not the C that fmt-c renders from it. ;;; ;;; Both cases below were found by writing an OpenGL example, not by ;;; the existing suite, because both need an operand shape that no ;;; earlier test program happened to use. (import (chicken condition) (chicken port) (chicken string) srfi-13 fmt-c-writer reader semen utils) (define (read-source source) (with-input-from-string source (lambda () (parameterize ((current-source-file "codegen.sex")) (parse-all (current-input-port)))))) (define (sex->c source) "Compile SOURCE, a string of Sex, and return the generated C." (let ((forms (read-source source))) (with-output-to-string (lambda () (parameterize ((sex-line-directives 'none)) (emit-c (semen-process forms))))))) ;;; Warnings are what `warns?' is for; a test that happens to trip one ;;; should not print it (define (silently thunk) "THUNK's value, with whatever it wrote to stderr dropped." (let ((value #f)) (with-error-output-to-string (lambda () (set! value (thunk)))) value)) (define (emits? source fragment) (and (string-contains (silently (lambda () (sex->c source))) fragment) #t)) (define (failure-message thunk) "The error THUNK raises as the user sees it -- message plus arguments, the way CHICKEN prints it -- or #f if it raises none." (handle-exceptions e (with-output-to-string (lambda () (display ((condition-property-accessor 'exn 'message) e)) (for-each (lambda (a) (display " ") (write a)) ((condition-property-accessor 'exn 'arguments) e)))) (begin (thunk) #f))) (define (fails-with? thunk fragment) (let ((m (failure-message thunk))) (and m (string-contains m fragment) #t))) (define (reports? source fragment) (fails-with? (lambda () (sex->c source)) fragment)) (define (followed-by? source first then) "Is FIRST in the C compiled from SOURCE, and the next thing past it, whitespace aside, THEN?" (let* ((c (silently (lambda () (sex->c source)))) (at (string-contains c first))) (and at (let ((next (string-skip c char-whitespace? (+ at (string-length first))))) (and next (string-prefix? then (substring c next))))))) ;;; A record reached through a typedef of a pointer to it (define pt-ref-src (string-append "(record pt ((x int)))" "(ops pt (fn get ((self (* pt))) int (return (. self x))))" "(typedef pt-ref (* pt))" "(fn f ((q pt-ref)) int (return (+ (. q x) (. (* q) x) (.get q))))")) ;;; Paths through arrays of a struct and of pointers to one (define poly-src (string-append "(struct pt ((x int) (y int)))" "(struct poly ((pts [(struct pt) 4]) (pp [* struct pt 4]) (grid [(struct pt) 2 3])))" "(fn f ((q (* struct poly))) int" " (return (+ (. q [pts 2] y) (. q [pp 0] y) (. q [grid 1 2] x))))")) (define (in-fn body) (string-append "(fn f ((a int) (b int)) void " body ")")) ;;; A struct, a struct holding a pointer to one, and locals of every ;;; shape a member path can start from (define (in-line body) (string-append "(struct point ((x int) (y int)))" "(struct line ((a (* struct point)) (b (struct point))))" "(fn f ((lp (* struct line))) void" " (var p (struct point)) (var pp (* struct point) (& p))" " (var l (struct line)) " body ")")) (define (warnings source) "What compiling SOURCE writes to stderr." (with-error-output-to-string (lambda () (sex->c source)))) (define (warns? source fragment) (and (string-contains (warnings source) fragment) #t)) (test-group "codegen" ;; A case body shared the switch's scope, so two cases could not ;; declare one name, and a label could not be followed by a ;; declaration before C23. Only a body that declares needs a block (test-group "case bodies" (test-assert "are blocks" (followed-by? (in-fn "(switch a (case 1 (var x int 1) (g x)))") "case 1:" "{")) (test-assert "so is a default" (followed-by? (in-fn "(switch a (default (var x int 1) (g x)))") "default:" "{")) (test-assert "where they declare" (followed-by? (in-fn "(switch a (case 1 (g a)))") "case 1:" "g(a)")) (test-assert "and one already a block is not wrapped again" (not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))") "{" "{")))) ;; (case (1 2) ...) is two labels, so (case (+ 1 1) ...) came out as ;; three, the first of them `case +:' (test-group "case labels" (test-assert "a list of them is a case each" (followed-by? (in-fn "(switch a (case (1 B) (g a)))") "case 1:" "case B:")) (test-assert "an operator is not one" (reports? (in-fn "(switch a (case (+ 1 1) (g a)))") "a case label is a value")) (test-assert "nor is an expression in the list" (reports? (in-fn "(switch a (case (1 (- 2)) (g a)))") "a case label is a value")) (test-assert "a value is written once" (reports? (in-fn "(switch a (case (1 2 1) (g a)))") "case label written twice")) (test-assert "in the whole switch" (reports? (in-fn "(switch a (case 1 (g a)) (case/fallthrough (2 1)) (default (g b)))") "case label written twice")) (test-assert "each switch apart" (emits? (in-fn "(switch a (case 1 (g a))) (switch b (case 1 (g b)))") "case 1:"))) ;; c-switch handed its scrutinee straight to `cat', which only works ;; when it is an atom. Anything else was displayed as a raw ;; s-expression: `switch ((%. e type))'. (test-group "switch scrutinee" (test-assert "member access" (emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))" "switch (e.type)")) (test-assert "call" (emits? (in-fn "(switch (g a) (case 1 (h)))") "switch (g(a))")) (test-assert "arithmetic" (emits? (in-fn "(switch (+ a b) (case 1 (h)))") "switch (a + b)"))) ;; A cast binds tighter than every binary operator, so an operand ;; that is itself a binary expression has to be parenthesised -- ;; otherwise the cast silently applies to the first operand only. (test-group "cast precedence" (test-assert "binary operand is parenthesised" (emits? (in-fn "(var p (* void) (cast (* 2 (sizeof int)) (* void)))") "(void *)(2 * sizeof(int))")) (test-assert "subtraction operand is parenthesised" (emits? (in-fn "(var f float (cast (- a b) float))") "(float)(a - b)")) ;; ...but exactly once. The operand used to parenthesise itself ;; again inside the parens the cast had just added. (test-assert "and not parenthesised twice" (not (emits? (in-fn "(var f float (cast (- a b) float))") "(float)((a - b))"))) ;; Unary operands are already unary-expressions and must be left ;; alone, or every existing cast in the tree gains noise. (test-assert "identifier is left bare" (emits? (in-fn "(var f float (cast a float))") "(float)a")) (test-assert "address-of is left bare" (emits? (in-fn "(var p (* int) (cast (& a) (* int)))") "(int *)&a")) (test-assert "sizeof is left bare" (emits? (in-fn "(var n int (cast (sizeof int) int))") "(int)sizeof(int)")) ;; `|' reaches fmt-c as a string, which a list of binary heads to ;; parenthesise did not have (test-assert "bitwise operand is parenthesised" (emits? (in-fn "(var f float (cast (| a b) float))") "(float)(a | b)")) (test-assert "conditional operand is parenthesised" (emits? (in-fn "(var f float (cast (if a a b) float))") "(float)(a ? a : b)")) ;; A cast is the operand here, and binds looser than `->' (test-assert "cast under a member access is parenthesised" (emits? (in-line "(var v (* void) pp) (var x int (. (cast v (* struct point)) x))") "((struct point*)v)->x")) (test-assert "cast under a unary operator is left bare" (emits? (in-line "(var v (* void) pp) (var x int (* (cast v (* int))))") "*(int *)v"))) ;; `?:' binds looser than every binary operator, so as an operand it ;; has to be parenthesised, or C takes the operator into its test (test-group "conditional expression precedence" (test-assert "operand of a binary operator" (emits? (in-fn "(var x int (/ a (if a a b)))") "a / (a ? a : b)")) (test-assert "left bare where it is the whole value" (emits? (in-fn "(var x int (if a a b))") "int x = a ? a : b")) (test-assert "left bare as an argument" (emits? (in-fn "(g (if a a b) b)") "g(a ? a : b, b)")) ;; ?: groups to the right, so a chain needs none in its else arm (test-assert "a chain in the else arm is left bare" (emits? (in-fn "(var x int (if a a (if b b 0)))") "a ? a : b ? b : 0")) (test-assert "but an assignment there is not" (emits? (in-fn "(var x int (if a a (= b 1)))") "a ? a : (b = 1)")) ;; with nothing for the false case it came out `a || 1', a boolean (test-assert "a value needs an else" (reports? (in-fn "(var x int (if a 1))") "an if used as a value needs an else")) (test-assert "so does a chain" (reports? (in-fn "(var x int (if a 1 b 2))") "an if used as a value needs an else")) (test-assert "and an if in a value's branch" (reports? (in-fn "(var x int (if a 1 (if b 2)))") "an if used as a value needs an else")) (test-assert "a chain with one is a chain" (emits? (in-fn "(var x int (if a 1 b 2 3))") "a ? 1 : b ? 2 : 3")) (test-assert "and a statement needs none" (emits? (in-fn "(if a (g a))") "if (a)"))) ;; A subscript binds tighter than anything, so what it subscripts is ;; parenthesised whenever it is an operator: `*q[i]' is *(q[i]) (test-group "subscript precedence" (test-assert "a dereference" (emits? "(fn f ((q (* * int)) (i int)) int (return [(* q) i]))" "(*q)[i]")) (test-assert "a sum" (emits? "(fn f ((p (* int))) int (return [(+ p 1) 0]))" "(p + 1)[0]")) (test-assert "a cast" (emits? "(fn f ((v (* void))) int (return [(cast v (* int)) 0]))" "((int *)v)[0]")) (test-assert "a path is left bare" (emits? (in-line "(var x int [(. p x) 0])") "p.x[0]")) ;; ...while the index is in brackets of its own (test-assert "an index is never parenthesised" (emits? "(fn f ((p (* int)) (i int)) int (return (cast [p (+ i 1)] int)))" "(int)p[i + 1]"))) ;; A comment among a call's arguments used to become an argument, ;; and c-apply put a comma on each side of it -- which does not ;; compile. It is dropped, as in any other expression context. (test-group "comments among arguments" (test-assert "no stray comma" (not (emits? (in-fn "(g 1 ;; c\n 2)") "*/,"))) (test-assert "the arguments survive" (emits? (in-fn "(g 1 ;; c\n 2)") "g(1, 2)"))) ;; The reader leaves the `;'s that introduced each line, and a run of ;; comment lines arrives as one form per line. (test-group "comment rendering" (test-assert "the markers are stripped" (emits? "(fn f () void ;;; Foo\n (g))" "/* Foo */")) (test-assert "so none survive into the C" (not (emits? "(fn f () void ;;; Foo\n (g))" ";;"))) (test-assert "consecutive lines are packed into one comment" (emits? "(fn f () void\n ;; first\n ;; second\n (g))" "/* first\n second */")) ;; Packing compares locations rather than just looking for adjacent ;; comment forms, so a blank line still separates them. (test-assert "a blank line keeps them apart" (emits? "(fn f () void\n ;; first\n\n ;; second\n (g))" "/* first */"))) ;; A `;' comment is a form, so one written inside a construct with ;; positional slots used to land in a slot and shift everything after ;; it -- silently. In an `if' the comment became the then-arm and the ;; then-arm became an `else if' condition, and it still compiled. ;; Comments are now taken out of the slots and emitted just before the ;; statement; comments in a body stay where they were written. (test-group "comments in positional slots" (test-assert "an if arm is not shifted" (not (emits? (in-fn "(if 1 ;; c\n (g 1) (g 2))") "else if"))) (test-assert "and both arms survive" (emits? (in-fn "(if 1 ;; c\n (g 1) (g 2))") "g(1)")) (test-assert "the comment survives too" (emits? (in-fn "(if 1 ;; kept here\n (g 1) (g 2))") "kept here")) (test-assert "a for header is not shifted" (emits? (in-fn "(for ;; c\n (var i int 0) (< i 2) (++ i) (g i))") "for (int i = 0; i < 2; ++i)")) (test-assert "a while condition is not shifted" (emits? (in-fn "(while ;; c\n (< a b) (g 1))") "while (a < b)")) (test-assert "a var is not shifted" (emits? (in-fn "(var ;; c\n x int 5)") "int x = 5")) (test-assert "a cast is not shifted" (emits? (in-fn "(var y int (cast ;; c\n a int))") "(int)a")) ;; Bodies are a statement sequence, so comments there stay put. (test-assert "a comment in a body stays in the body" (emits? (in-fn "(while (< a b) ;; inside\n (g 1))") "while (a < b) {"))) (test-group "pub enum" (test-assert "is emitted" (emits? "(pub enum color (red green blue))" "enum color")) (test-assert "with its values" (emits? "(pub enum color (red green blue))" "red")) ;; A value made the enumerator a pair, which went out unconverted (test-assert "explicit values" (emits? "(pub enum wire ((wire-varint 0) (wire-fixed64 1)))" "wire_varint = 0, wire_fixed64 = 1")) (test-assert "a value is an expression" (emits? "(enum wire ((wire-last (+ WIRE-BASE 1))))" "wire_last = WIRE_BASE + 1")) (test-assert "and an anonymous enum takes them too" (emits? "(fn f () void (var e (enum ((up-one 1) down)) up-one))" "up_one = 1, down")) (test-assert "and a non-pub enum still is too" (emits? "(enum color (red green blue))" "enum color")) ;; Naming an enum as a type, rather than defining it, had no ;; walk-enum clause and died with `(match) no matching pattern' (test-assert "and it can then be used as a type" (emits? "(enum color (red green blue)) (fn f () void (var m (enum color) red))" "enum color m = red")) (test-assert "a malformed enum is rejected with its location" (reports? "(enum)" "codegen.sex:1:")) ;; c-type handed the declarator's name to c-enum as the enum tag, ;; so this emitted `enum m { up, down }' with no variable at all (test-assert "an anonymous enum keeps the variable" (emits? "(fn f () void (var e (enum (up down)) up))" "} e = up")) (test-assert "and a named definition keeps both" (emits? "(fn f () void (var n (enum named (a b)) a))" "enum named{"))) ;; `|', `||' and `|=' read as ordinary symbols -- our own reader has ;; no |symbol| syntax for them to collide with -- but fmt-c cannot ;; dispatch on a symbol whose name it cannot write in Scheme source, ;; so they used to fall through to the function-call path and emit ;; `|\||(a, b)'. The writer renames them to heads fmt-c spells with ;; a string. (test-group "bitwise and logical operators" (test-assert "bit-or" (emits? (in-fn "(var x int (| a b))") "int x = a | b")) (test-assert "logical or" (emits? (in-fn "(var x int (|| a b))") "int x = a || b")) (test-assert "or-assign" (emits? (in-fn "(|= a b)") "a |= b")) (test-assert "bit-and" (emits? (in-fn "(var x int (& a b))") "int x = a & b")) (test-assert "logical and" (emits? (in-fn "(var x int (&& a b))") "int x = a && b")) ;; Precedence too: the operator reaches fmt-c as a string it looks ;; up, not as a symbol in its table (test-assert "parenthesised where C needs it" (emits? (in-fn "(var x int (& (| a b) a))") "(a | b) & a")) (test-assert "and left alone where it does not" (emits? (in-fn "(var x int (| a (& a b)))") "int x = a | a & b")) ;; The spelling from before they could be written directly (test-assert "c-or is still accepted" (emits? (in-fn "(var x int (c-or a b))") "a || b")) (test-assert "c-bit-or is still accepted" (emits? (in-fn "(var x int (c-bit-or a b))") "a | b"))) ;; A `;' comment is a form. In a macro body `comment' is a no-op ;; that swallows the comment itself. Inside a quasiquoted payload ;; the same form is data, never evaluated, and reaches the writer ;; intact (test-group "comments in macros" (test-assert "a comment in the payload reaches the C" (emits? "(defmacro (m-kept) `(fn f () void\n ;; this survives\n (g)))\n(m-kept)" "this survives")) (test-assert "a comment about the macro does not" (not (emits? "(defmacro (m-dropped)\n ;; this vanishes\n `(fn f () void (g)))\n(m-dropped)" "this vanishes"))) (test-assert "and the macro still expands" (emits? "(defmacro (m-both)\n ;; about the macro\n `(fn f () void (g)))\n(m-both)" "void f (void)"))) ;; Diagnostics name also the place. Every form carries a (file ;; . line), so an error can cite it (test-group "errors cite the source location" (test-assert "unknown toplevel form" (reports? "(include stdio.h)\n(wat 1 2)" "codegen.sex:2: unknown top level form")) (test-assert "the offending form is shown too" (reports? "(include stdio.h)\n(wat 1 2)" "(wat 1 2)")) (test-assert "pub with nothing to define" (reports? "(pub 1)" "codegen.sex:1:"))) ;; A grouped header, C's parens carried over, died in the writer with ;; `(caddr) bad argument type' and no location (test-group "malformed for" (test-assert "a grouped header is named" (reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i))") "header is not grouped")) (test-assert "whatever its init is" (reports? (in-fn "(for ((= a 0) (< a 4) (++ a)) (g a))") "header is not grouped")) (test-assert "even when the body fills the slots it left" (reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i) (g i) (g i))") "header is not grouped")) (test-assert "a short one gets the shape" (reports? (in-fn "(for (var i int 0) (< i 4))") "(for init test step body ...)")) (test-assert "and both name the line" (reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))" "codegen.sex:2:"))) ;; A nested pointer chain used to silently lose a level: ;; (var p (* (* char))) emitted `char *p' (test-group "malformed types are rejected" (test-assert "nested pointer chain" (reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat")) (test-assert "and names the line" (reports? "(pub fn f () void\n (var p (* (* char))))" "codegen.sex:2:")) ;; A sublist that only groups has no `*' in it and must still work. (test-assert "grouping sublist still accepted" (emits? (in-fn "(var s (* (const struct suc)) 0)") "const struct suc * s")) (test-assert "flat chain still accepted" (emits? (in-fn "(var q (* * const char) 0)") "const char * * q"))) ;; 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 "docstrings" (test-assert "appears before the function" (emits? "(fn greet ((name (* char))) void \"Greet NAME.\" (printf \"hi\" name))" "/* Greet NAME. */")) (test-assert "and not inside the body as a statement" (not (emits? "(fn greet ((name (* char))) void \"Greet NAME.\" (printf \"hi\" name))" "\"Greet NAME.\""))) (test-assert "multiline keeps its paragraphs" (emits? "(pub fn main () int \"Entry point.\n\nARGC and ARGV.\" (return 0))" "Entry point.")) (test-assert "and the second paragraph too" (emits? "(pub fn main () int \"Entry point.\n\nARGC and ARGV.\" (return 0))" "ARGC and ARGV.")) (test-assert "a prototype with only a docstring stays a prototype" (emits? "(fn helper ((a int)) int \"Forward.\")" "helper (int a);")) (test-assert "a string after the first statement is left alone" (emits? "(fn f () void (g) \"not a docstring\")" "\"not a docstring\"")) (test-assert "a struct docstring sits above the struct" (emits? "(struct point \"A 2D point.\" ((x int) (y int)))" "/* A 2D point. */")) (test-assert "and an enum docstring too" (emits? "(enum color \"RGB.\" (red green blue))" "/* RGB. */"))) ;; `static-assert' is the keyword rather than the macro, so ;; a static assertion costs no include. Mapped in `atom-to-fmt-c' ;; because `unkebabify' alone would spell it `static_assert'. (test-group "static-assert" (test-assert "emits the C11 keyword" (emits? (in-fn "(static-assert (== (sizeof int) 4) \"int is four bytes\")") "_Static_assert(sizeof(int) == 4, \"int is four bytes\")")) (test-assert "and not the header macro" (not (emits? (in-fn "(static-assert (== (sizeof int) 4) \"x\")") "static_assert(")))) ;; A closure is a code pointer beside its captures. The struct is ;; named from the signature, so separate translation units agree on ;; it, and calling one goes through `code' with `env' passed first. ;; ;; A closure struct, and the helper its calls go through, are each ;; emitted once per signature; the registries deciding that are ;; compile-time state like the type databases, and outlive a single ;; `sex->c' here. So every case below that looks for a *definition* ;; uses a signature of its own -- cases looking at a call site can ;; share one. (test-group "closures" (test-assert "the type becomes a struct named for its signature" (emits? "(fn f ((c (closure ((int)) int))) int (return (c 1)))" "struct ƛint_int")) ;; The environment is one shared union, declared in the prelude -- ;; its layout is part of the ABI two units agree on, so it cannot ;; depend on what either file contains. `sex->c' has no prelude, so ;; what is visible here is the member (test-assert "whose environment is the shared union" (emits? "(fn f ((c (closure ((float)) int))) int (return (c 1.0)))" "union ƛenv env;")) ;; The receiver goes through a helper rather than being written ;; out twice, so that `[table (++ i)]' evaluates its index once, ;; exactly as it would for an array of function pointers (test-assert "a call passes the receiver to a helper" (emits? "(fn f ((c (closure ((int)) int))) int (return (c 1)))" "ƛint_int_call(c, 1)")) (test-assert "and the helper is what dereferences it" (emits? "(fn f ((c (closure ((long)) int))) int (return (c 1)))" "ƛc.code(&ƛc.env, ƛa0)")) (test-assert "a subscript receiver is evaluated once" (emits? "(fn f ((t [(closure ((int)) int) 4]) (i int)) int (return ([t (++ i)] 1)))" "ƛint_int_call(t[++i], 1)")) (test-assert "so is a member receiver" (emits? "(struct h ((cb (closure ((int)) int)))) (fn f ((s (struct h))) int (return ((. s cb) 1)))" "ƛint_int_call(s.cb, 1)")) (test-assert "a captured name is rebound in the lifted body" (emits? "(fn f ((n int)) (closure ((char)) int) (return (closure ((x char)) int (n) (return n))))" "int n = ƛcaptures->n;")) (test-assert "captures are checked against the environment" (emits? "(fn f ((n int)) (closure ((short)) int) (return (closure ((x short)) int (n) (return n))))" "_Static_assert(sizeof(struct")) ;; A closure over nothing has no record to point at, and C has no ;; empty struct to declare for it (test-assert "no captures means no capture record" (not (emits? "(fn f () (closure () int) (return (closure () int () (return 7))))" "_captures {"))) ;; `(name expr)' names a capture and gives what it holds, so the ;; expression is evaluated once, where the closure is written (test-assert "a named capture takes its type from the expression" (emits? "(struct p ((x int) (y int))) (fn f ((s (struct p))) (closure () int) (return (closure () int ((sum (+ (. s x) (. s y)))) (return sum))))" "int sum;")) (test-assert "and the constructor is handed the expression" (emits? "(struct p ((x int) (y int))) (fn f ((s (struct p))) (closure () int) (return (closure () int ((sum (+ (. s x) (. s y)))) (return sum))))" "_make(s.x + s.y)")) (test-assert "capturing a pointer is how by-reference is spelled" (emits? "(struct p ((x int))) (fn f ((s (* (struct p)))) (closure () int) (return (closure () int ((q s)) (return (-> q x)))))" "struct p* q;")) ;; A bare function is a closure that captures nothing, so it ;; converts wherever one is expected -- the pointer goes in the ;; environment and one thunk per signature reads it back out (test-assert "a named function in a var initializer" (emits? "(fn g ((n int)) int (return n)) (fn f () void (var c (closure ((int)) int) g))" "ƛint_int_fromfn(g)")) (test-assert "a lambda, which is a bare function too" (emits? "(fn f () void (var c (closure ((int)) int) (lambda ((n int)) int (return n))))" "ƛint_int_fromfn(λ0_f)")) (test-assert "an argument, against the parameter that signature wrote" (emits? "(fn g ((n int)) int (return n)) (fn h ((c (closure ((int)) int))) int (return (c 1))) (fn f () int (return (h g)))" "h(ƛint_int_fromfn(g))")) (test-assert "a return, against the declared return type" (emits? "(fn g ((n int)) int (return n)) (fn f () (closure ((int)) int) (return g))" "return ƛint_int_fromfn(g)")) (test-assert "the thunk reads the pointer out of the environment" (emits? "(fn g ((n float)) int (return 1)) (fn f () void (var c (closure ((float)) int) g))" "return ƛcaptures->f(ƛa0)")) ;; a signature that does not match is left alone, and C rejects it (test-assert "a function of the wrong signature does not convert" (not (emits? "(fn g ((n float)) int (return 1)) (fn f () void (var c (closure ((int)) int) g))" "_fromfn(g)"))) ;; a closure is lifted into the function it was written in, so ;; there has to be one (test-assert "a closure at toplevel is refused" (reports? "(var c (closure () int) (closure () int () (return 1)))" "only be written inside a function")) (test-assert "and so is one in a struct field" (reports? "(struct s ((f (closure () int) (closure () int () (return 1)))))" "only be written inside a function")) ;; A block opens a scope, so what it declares ends with it (test-assert "a name shadowed in a block does not escape it" (emits? "(fn mk () (closure () int) (return (closure () int () (return 1)))) (fn f () int (var c (closure () int) (mk)) (do (var c int 9) (g c)) (return (c)))" "ƛvoid_int_call(c)")) (test-assert "and the shadowing declaration is what the block sees" (emits? "(fn mk () (closure () int) (return (closure () int () (return 1)))) (fn f () int (var c (closure () int) (mk)) (do (var c int 9) (g c)) (return (c)))" "g(c)")) ;; The receiver is written twice, so a name used as an argument ;; must not be mistaken for a call of its own (test-assert "a closure passed as an argument stays a value" (emits? "(fn g ((c (closure ((int)) int))) int (return 0)) (fn f ((c (closure ((int)) int))) int (return (g c)))" "g(c)"))) ;; An include target is a path, so none of the identifier mangling ;; applies to it: GL/gl.h is a file and GLːgl.h is not. (test-group "include targets are paths" (test-assert "a slash survives" (emits? "(include SDL3/SDL.h)" "#include ")) (test-assert "and so does a dash" (emits? "(include my-header.h)" "#include ")) (test-assert "pub include emits the same" (emits? "(pub include GL/gl.h)" "#include "))) ;; The walker offered WALK-FN the cdr of every form as well as the ;; form, so the tail (t) of (fn g () t) read as a call to a macro ;; named t, and any function returning a type that shared a name ;; with a macro died on that macro's arity. (test-group "a form's tail is not a form" (test-assert "a return type may share a name with a macro" (emits? "(defmacro (t x) 1)(typedef t int)(fn g () t (return 1))" "t g (void)")) (test-assert "so may a parameter's type" (emits? "(defmacro (u x) 1)(typedef u int)(fn g ((a u)) void (return))" "void g (u a)")) (test-assert "and the macro still expands where it is called" (emits? "(defmacro (t x) x)(fn g () int (return (t 7)))" "return 7"))) ;; a macro body reads types as lists, so the srfi-1 accessors are in ;; scope beside the type database (test-group "macro list accessors" (test-assert "third reads an array's length" (emits? "(defmacro (len t) (third t)) (fn f () int (return (len [int 7])))" "return 7;")) (test-assert "second reads a tag" (emits? "(defmacro (tag t) (symbol->string (second t))) (fn f () void (g (tag (struct point))))" "g(\"point\")"))) ;; `type-of' hands a macro the type of an *expression*, where ;; `get-name-type' only answers for a name. The macro is expanded ;; during the walk rather than before it, so the scope is still live. (test-group "type-of" (test-assert "a local, from its declaration" (emits? "(defmacro (t x) (type-match (type-of x) (int 1) (else 0))) (fn f () int (var n int 0) (return (t n)))" "return 1;")) (test-assert "an expression, not just a name" (emits? "(defmacro (t x) (type-match (type-of x) (double 1) (else 0))) (fn f () int (var d double 0.0) (return (t (+ d 1))))" "return 1;")) (test-assert "a call, through the callee's signature" (emits? "(fn g () float (return 1.0)) (defmacro (t x) (type-match (type-of x) (float 1) (else 0))) (fn f () int (return (t (g))))" "return 1;")) ;; a macro is shown the written spelling, not the generated struct (test-assert "a closure, spelled the way it was written" (emits? "(fn mk () (closure ((int)) int) (return (closure ((b int)) int () (return b)))) (defmacro (t x) (type-match (type-of x) ((closure _ _) 1) (else 0))) (fn f () int (var c _ (mk)) (return (t c)))" "return 1;")) (test-assert "and calling one has the closure's return type" (emits? "(fn mk () (closure ((int)) int) (return (closure ((b int)) int () (return b)))) (defmacro (t x) (type-match (type-of x) (int 1) (else 0))) (fn f () int (var c _ (mk)) (return (t (c 1))))" "return 1;")) ;; outside an expansion there is no scope to ask about (test-assert "a name the walk has not reached is unknown" (emits? "(defmacro (t x) (type-match (type-of x) (int 1) (else 0))) (fn f () int (return (t nope)))" "return 0;"))) ;; `_' as a type is written out from what the initializer says. The ;; answer comes from declarations and from the signature a call names, ;; never from unification -- a partial type would need one. (test-group "wildcard types" (test-assert "an integer literal" (emits? (in-fn "(var x _ 42)") "int x = 42")) (test-assert "a float literal" (emits? (in-fn "(var x _ 3.5)") "double x = 3.5")) (test-assert "a string literal" (emits? (in-fn "(var x _ \"hi\")") "const char * x")) (test-assert "a call, through the name table" (emits? "(fn g ((a int)) float (return 1.0)) (fn f () void (var x _ (g 1)))" "float x = g(1)")) (test-assert "a struct member" (emits? "(struct p ((a int) (b float))) (fn f ((s (struct p))) void (var x _ (. s b)))" "float x = s.b")) (test-assert "an address, which composes" (emits? "(struct p ((a int))) (fn f ((s (struct p))) void (var x _ (& s)))" "struct p* x = &s")) (test-assert "a comparison is a bool" (emits? (in-fn "(var x _ (< a b))") "bool x = a < b")) ;; a wildcard inside a spelling is solved in place, leaving the rest ;; of the written type alone -- this is what needs the unifier (test-assert "a wildcard inside a pointer" (emits? "(struct p ((a int))) (fn f ((s (struct p))) void (var x (* _) (& s)))" "struct p* x = &s")) (test-assert "a wildcard inside an array" (emits? (in-fn "(var t [_ 3] #([int 3] : 1 2 3))") "int t[3]")) ;; a variable had nowhere to keep a qualifier written on it, and ;; what it was solved to came out without (test-assert "a qualified wildcard keeps its qualifier" (emits? (in-fn "(var k (const _) 5)") "const int k = 5")) (test-assert "behind a pointer too" (emits? (in-fn "(var p (* const _) (& a))") "const int * p = &a")) (test-assert "and says it once, solved to a type already saying it" (emits? (in-fn "(var k (const int) 1) (var p (* const _) (& k))") "const int * p = &k")) ;; an operator's result is a value, which has no qualifier -- the ;; spelling was the first operand's, so it had one only that way round (test-assert "a sum is unqualified" (emits? (in-fn "(var k (const int) 1) (var j _ (+ k 1))") " int j = k + 1")) (test-assert "whichever side the qualified operand is on" (emits? (in-fn "(var k (const int) 1) (var j _ (+ 1 k))") " int j = 1 + k")) (test-assert "and so is a pointer it gives" (emits? (in-fn "(var s (* char) 0) (var p (const * char) s) (var q _ (+ p 1))") "char * q = p + 1")) ;; the join took the first pointer, and declared the distance one (test-assert "one pointer from another is a distance" (emits? (in-fn "(var p (* int) 0) (var q (* int) 0) (var d _ (- p q))") "ptrdiff_t d = p - q")) (test-assert "an array decays to one for it" (emits? (in-fn "(var xs [int 4]) (var p (* int) xs) (var d _ (- p xs))") "ptrdiff_t d = p - xs")) (test-assert "a number from a pointer is still a pointer" (emits? (in-fn "(var p (* int) 0) (var r _ (- p 1))") "int * r = p - 1")) ;; one operand was a join with nothing, which left it unpromoted (test-assert "a negation promotes its operand" (emits? (in-fn "(var c char 1) (var n _ (- c))") "int n = -c")) (test-assert "so does a unary plus" (emits? (in-fn "(var h short 1) (var n _ (+ h))") "int n = +h")) (test-assert "and leaves a wide one as it is" (emits? (in-fn "(var l long 1) (var n _ (- l))") "long n = -l")) ;; a compound literal carries its own type, where a brace ;; initializer has none and takes one from its context (test-assert "a compound literal answers a bare wildcard" (emits? "(struct p ((a int) (b int))) (fn f () void (var x _ #((struct p) : 1 2)))" "struct p x = (struct p){1, 2}")) (test-assert "a brace initializer cannot" (reports? (in-fn "(var x _ #(1 2))") "cannot infer the type")) ;; ...but its elements still solve the hole in an array type (test-assert "elements solve an array's element type" (emits? (in-fn "(var t [_ 4] #(0 1 4 9))") "int t[4] = {0, 1, 4, 9}")) (test-assert "including when there are fewer than the length" (emits? (in-fn "(var t [_ 10] #(1 2))") "int t[10] = {1, 2}")) (test-assert "and they have to agree with each other" (reports? (in-fn "(var t [_ 2] #(1 \"s\"))") "type mismatch")) ;; a closure type reaches the solver as the struct that stands for ;; it, which is the spelling `parse-type' knows (test-assert "a closure, from the signature that produced it" (emits? "(fn mk () (closure ((int)) int) (return (closure ((b int)) int () (return b)))) (fn f () void (var c _ (mk)))" "struct ƛint_int c = mk()")) (test-assert "and it is callable once inferred" (emits? "(fn mk () (closure ((int)) int) (return (closure ((b int)) int () (return b)))) (fn f () int (var c _ (mk)) (return (c 1)))" "ƛint_int_call(c, 1)")) ;; C's usual arithmetic conversions, far enough to answer `_' (test-assert "floating beats integral" (emits? (in-fn "(var d double 1.0) (var x _ (+ a d))") "double x = a + d")) (test-assert "the wider integer wins" (emits? (in-fn "(var l long 1) (var x _ (+ a l))") "long x = a + l")) (test-assert "double beats float" (emits? (in-fn "(var g float 1.0) (var d double 1.0) (var x _ (+ g d))") "double x = g + d")) (test-assert "and same-width operands stay put" (emits? (in-fn "(var g float 1.0) (var x _ (+ g g))") "float x = g + g")) (test-assert "a pointer operand makes it pointer arithmetic" (emits? "(struct p ((a int))) (fn f ((s (struct p))) void (var x _ (+ (& s) 1)))" "struct p* x = &s + 1")) ;; a written type that cannot match what the initializer gives (test-assert "a mismatch is reported, not papered over" (reports? (in-fn "(var p (* _) 42)") "type mismatch")) ;; a wildcard that cannot be answered is an error, not a guess (test-assert "with no initializer there is nothing to infer from" (reports? (in-fn "(var x _)") "cannot infer the type")) (test-assert "nor from a name the compiler never saw declared" (reports? (in-fn "(var x _ (never-declared))") "cannot infer the type"))) ;; `(car res)' on the expansion assumed it was a pair, so a macro ;; computing a value rather than building a form crashed the compiler. (test-group "macro expanding to an atom" (test-assert "a number" (emits? "(defmacro (two) 2) (fn f () int (return (two)))" "return 2;")) (test-assert "a string" (emits? "(defmacro (who) \"sex\") (fn f () void (g (who)))" "g(\"sex\")")) ;; a symbol expansion can stand where a type does, which is what ;; makes a macro able to compute one (test-assert "a symbol, used as a type" (emits? "(defmacro (ty) 'int) (fn f () void (var x (ty) 0))" "int x = 0")) ;; ...and nothing at all, for a macro that only registers something (test-assert "nothing, at toplevel" (emits? "(defmacro (quiet) (list)) (quiet) (fn f () int (return 1))" "return 1;")) (test-assert "nothing, in a body" (emits? "(defmacro (quiet) (list)) (fn f () int (quiet) (return 1))" "return 1;")) ;; several forms need `$', which is what tells a splice from a call (test-assert "$ splices" (emits? "(defmacro (pair) (list '$ '(fn a () int (return 1)) '(fn b () int (return 2)))) (pair)" "b (void)")) (test-assert "and ($) is nothing at all" (emits? "(defmacro (quiet) (list '$)) (quiet) (fn f () int (return 1))" "return 1;")) ;; only the outermost was spliced: an inner one was an unknown ;; toplevel form, and in a body a call of `$' (test-assert "a $ inside one splices too" (emits? "(defmacro (pairs) (list '$ (list '$ '(fn a () int (return 1))) '(fn b () int (return 2)))) (pairs)" "a (void)")) (test-assert "in a body as well" (let ((c (silently (lambda () (sex->c "(defmacro (twice) (list '$ (list '$ '(g 1)) '(g 2))) (fn f () void (twice))"))))) (and (string-contains c "g(1);") (string-contains c "g(2);") (not (string-contains c "$"))))) (test-assert "and an inner ($) is nothing" (emits? "(defmacro (quiet) (list '$ (list '$))) (quiet) (fn f () int (return 1))" "return 1;")) ;; without `$' a list is one form, so a head that is itself a form ;; stays a call rather than becoming two statements (test-assert "a computed callee stays one form" (emits? "(fn mk () (closure ((int)) int) (return (closure ((b int)) int () (return b)))) (defmacro (apply-it x) `((mk) ,x)) (fn f () int (return (apply-it 5)))" "ƛint_int_call(mk(), 5)")) ;; spliced, it would have become two forms in the `return' -- the ;; comma operator, and the wrong answer (test-assert "rather than two forms in its context" (not (emits? "(fn mk () (closure ((int)) int) (return (closure ((b int)) int () (return b)))) (defmacro (apply-it x) `((mk) ,x)) (fn f () int (return (apply-it 5)))" "return mk(), 5")))) ;; A unary expression parenthesised its operand rather than itself, so ;; the parens landed inside: `*(p).x', which C reads as `*(p.x)'. (test-group "unary operand precedence" (test-assert "member access through a dereference" (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))" "(*p).x")) (test-assert "and not with the parens inside" (not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))" "*(p).x"))) (test-assert "member access through a cast" (emits? (string-append "(struct s ((f int)))" (in-fn "(var n int (. (* (cast a (* (struct s)))) f))")) "(*(struct s*)a).f")) ;; ...without gaining parens where none are due (test-assert "a bare dereference is left alone" (emits? (in-fn "(var p (* int) 0) (= a (* p))") "a = *p")) (test-assert "so is address-of in an argument" (emits? (in-fn "(g (& a))") "g(&a)")) (test-assert "and negation beside a binary operator" (emits? (in-fn "(var n int (+ (- a) b))") "-a + b"))) ;; c-struct/aux wrote the tag and the first `*' after it, and ;; dropped the rest of the chain (test-group "a pointer chain to a struct" (test-assert "keeps every star" (emits? (in-fn "(var q (* * struct p))") "struct p* * q;")) (test-assert "and every qualifier between them" (emits? (in-fn "(var q (* const * union u))") "union u* const * q;"))) ;; Operands past what an operator takes were written as they came: ;; (< a b c) as C's own chain, which compares a boolean with c (test-group "operator arity" (test-assert "a comparison takes two operands" (reports? (in-fn "(var x bool (< a b a))") "a comparison takes two operands")) (test-assert "equality too" (reports? (in-fn "(var x bool (== a b a))") "a comparison takes two operands")) (test-assert "and not one" (reports? (in-fn "(var x bool (>= a))") "a comparison takes two operands")) (test-assert "two is a comparison" (emits? (in-fn "(var x bool (<= a b))") "x = a <= b;")) ;; ...and (/ a) as `/a', (! a b) as `a ! b' (test-assert "a division takes two or more" (reports? (in-fn "(var x int (/ a))") "takes two operands or more")) (test-assert "so does &&" (reports? (in-fn "(var x bool (&& a))") "takes two operands or more")) (test-assert "a compound assignment takes two" (reports? (in-fn "(+= a b a)") "takes two operands")) (test-assert "a negation takes one" (reports? (in-fn "(var x bool (! a b))") "takes one operand")) (test-assert "a bit-not too" (reports? (in-fn "(var x int (~ a b))") "takes one operand")) (test-assert "minus takes one or more" (reports? (in-fn "(var x int (-))") "takes one operand or more")) (test-assert "and with more than one is still a subtraction" (emits? (in-fn "(var x int (- a b a))") "x = a - b - a;")) ;; ++ and -- step each operand in turn (test-assert "a statement steps each operand" (followed-by? (in-fn "(++ a b)") "++a;" "++b;")) (test-assert "so does a for's step, which is one expression" (emits? (in-fn "(for (var i int 0) (< i a) (++ i b) (g i))") "; ++i, ++b)")) (test-assert "and a value is one too, in parens" (emits? (in-fn "(var x int (-- a b))") "x = (--a, --b);")) ;; a define's body is pasted, not walked, so it is looked at apart (test-assert "a define's body is held to the same" (reports? "(define BAD (< 1 2 3))" "a comparison takes two operands")) (test-assert "and steps each operand the same way" (emits? "(define (STEP a b) (++ a b))" "#define STEP(a, b) ((++(a), ++(b)))"))) ;; An array bound was taken only when it was an integer literal, so a ;; symbolic one fell into the type: `[int N]' came out `int N a[]'. (test-group "array bounds" (test-assert "a symbolic bound" (emits? "(define N 4) (struct s ((a [int N])))" "int a[N]")) (test-assert "an expression bound" (emits? "(define N 4) (struct s ((a [char (* 2 N)])))" "char a[2 * N]")) (test-assert "an integer bound still works" (emits? "(struct s ((a [int 4])))" "int a[4]")) ;; ...and it is spelled the way the same name is in an expression (test-assert "a bound's name is converted" (emits? (in-fn "(var xs [u16 CHUNK-AREA])") "u16 xs[CHUNK_AREA]")) (test-assert "so is one inside an expression bound" (emits? (in-fn "(var xs [u16 (* 2 CHUNK-AREA)])") "u16 xs[2 * CHUNK_AREA]")) ;; a multi-word type is keywords all the way down, so a trailing ;; keyword belongs to the type and leaves the array unsized (test-assert "a multi-word type is not a bound" (emits? "(struct s ((a [unsigned int])))" "unsigned int a[]")) ;; ...and a tag always follows its keyword (test-assert "nor is an aggregate tag" (emits? "(struct t ((z int))) (struct s ((a [struct t])))" "struct t a[]")) (test-assert "nor one behind a pointer" (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[]")) ;; [m 1 2] is m[1][2], so [int 2 3] declares what it indexes. A ;; second bound was taken for the last word of the element type (test-assert "every trailing bound is a dimension" (emits? (in-fn "(var m [int 2 3])") "int m[2][3];")) (test-assert "however many there are" (emits? (in-fn "(var c [u8 2 3 4])") "u8 c[2][3][4];")) (test-assert "named ones too" (emits? "(define N 2) (struct s ((a [* char N 3])))" "char * a[N][3]")) ;; a list after a tag's keyword is the aggregate's body (test-assert "an inline aggregate's fields are not one" (emits? "(struct s ((a [struct ((x int)) 2 3])))" "a[2][3]")) (test-assert "but an expression after a tag is" (emits? "(define N 2) (struct t ((z int))) (struct s ((a [struct t (* 2 N)])))" "struct t a[2 * N]"))) ;; ...and an index after the first was dropped (test-group "subscripts" (test-assert "each index subscripts what the one before it gave" (emits? (in-fn "(var m [int 2 3]) (= [m 1 2] a)") "m[1][2] = a;")) (test-assert "which is an element to inference" (emits? (in-fn "(var m [int 2 3]) (var e _ [m 1 2])") "int e = m[1][2];")) (test-assert "and an expression to sizeof" (emits? (in-fn "(var m [int 2 3]) (var n size-t (sizeof [m 1 1]))") "sizeof(m[1][1])")) (test-assert "nested by hand it is the same" (emits? (in-fn "(var m [int 2 3]) (= [[m 1] 2] a)") "m[1][2] = a;"))) ;; `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 (test-group "sizeof" (test-assert "a pointer type" (emits? (in-fn "(var n size-t (sizeof (* char)))") "sizeof(char *)")) (test-assert "a qualified one" (emits? (in-fn "(var n size-t (sizeof (* const char)))") "sizeof(const char *)")) (test-assert "to a type only C knows of" (emits? (in-fn "(var n size-t (sizeof (* size-t)))") "sizeof(size_t *)")) (test-assert "a multi-word type" (emits? (in-fn "(var n size-t (sizeof (unsigned int)))") "sizeof(unsigned int)")) (test-assert "an array type" (emits? (in-fn "(var n size-t (sizeof [int 4]))") "sizeof(int [4])")) (test-assert "a dereference of a local" (emits? (in-fn "(var p (* int) 0) (var n size-t (sizeof (* p)))") "sizeof(*p)")) (test-assert "of a parameter" (emits? "(fn f ((p (* int))) void (var n size-t (sizeof (* p))))" "sizeof(*p)")) (test-assert "of a call" (emits? (in-fn "(var n size-t (sizeof (* (g))))") "sizeof(*g())")) (test-assert "a subscript" (emits? (in-fn "(var xs [int 4]) (var n size-t (sizeof [xs 0]))") "sizeof(xs[0])"))) ;; A double is written in as few digits as read back as the same ;; double: CHICKEN prints 15 by default, and 0.16666666666666666 came ;; out 0.166666666666667, another number (test-group "double literals" (test-assert "keep every digit they need" (emits? (in-fn "(var d double 0.16666666666666666)") "d = 0.16666666666666666;")) (test-assert "and gain none they do not" (emits? (in-fn "(var d double 0.1)") "d = 0.1;")) (test-assert "sixteen when that is enough" (emits? (in-fn "(var d double 0.9871465422297019)") "d = 0.9871465422297019;"))) ;; A suffix is C's, and reached C inside |pipes|, the way CHICKEN ;; writes a symbol that starts with a digit (test-group "suffixed literals" (test-assert "an integer keeps its suffix" (emits? (in-fn "(var n (unsigned long long) 1ULL)") "n = 1ULL;")) (test-assert "and is written in decimal, which C11 has a suffix for" (emits? (in-fn "(var n (unsigned int) 0b101u)") "n = 5u;")) (test-assert "a float keeps its digits, which a double would round" (emits? (in-fn "(var x (long double) 3.14159265358979323846264338327950288L)") "x = 3.14159265358979323846264338327950288L;")) (test-assert "a hex float is written as it was" (emits? (in-fn "(var x double 0x1.8p3)") "x = 0x1.8p3;")) (test-assert "suffix and all" (emits? (in-fn "(var x float -0x1p-2f)") "x = -0x1p-2f;")) (test-assert "grouped digits reach C ungrouped" (emits? (in-fn "(var x float 1_000.5f)") "x = 1000.5f;")) (test-assert "a minus is C's own" (emits? (in-fn "(var n (unsigned int) -1u)") "n = -1u;")) (test-assert "a number too wide for long long is unsigned, and says so" (emits? (in-fn "(var n (unsigned long long) 0xFFFFFFFFFFFFFFFF)") "n = 18446744073709551615u;"))) ;; A define's body was pasted unwalked, and unparenthesised: `cast' ;; stayed a call, and (* x D) regrouped with D's own operator (test-group "defines" (test-assert "an atom is left bare" (emits? "(define N 4)" "#define N 4")) (test-assert "and its name converted" (emits? "(define MAX-N MIN-N)" "#define MAX_N MIN_N")) (test-assert "an expression is walked and parenthesised" (emits? "(define K (/ (cast 255 float) (cast 60 float)))" "#define K ((float)255 / (float)60)")) (test-assert "and a negated name" (emits? "(define LOWEST -INFINITY)" "#define LOWEST (-INFINITY)")) (test-assert "so is a negative literal" (emits? "(define NEG -1)" "#define NEG (-1)")) (test-assert "a function-like one is converted too" (emits? "(define (sq-of x) (* x x))" "#define sq_of(x) ((x) * (x))"))) ;; A typedef's target is a type, and was walked as an expression: a ;; pointer read as a dereference, an array as a subscript (test-group "typedefs" (test-assert "of a name" (emits? "(typedef small int)" "typedef int small;")) (test-assert "of a pointer" (emits? "(typedef cstr (* const char))" "typedef const char * cstr;")) (test-assert "of an array" (emits? "(typedef vec4 [float 4])" "typedef float vec4[4];")) (test-assert "of a function pointer" (emits? "(typedef colour-fn (fn ((i32)) u32))" "typedef u32 (*colour_fn)(i32);"))) ;; `.' is C's `.' or its `->' depending on what is being read from, ;; so a path reads the same however the thing it starts from is held ;; ...and inside a literal, which the walk used to pass by (test-group "member access in a literal" (test-assert "compound" (emits? (in-line "(var q (struct point) #(struct point : (. pp x) (. pp y)))") "(struct point){pp->x, pp->y}")) (test-assert "brace initializer" (emits? (in-line "(var q (struct point) #((. pp x) (. pp y)))") "{pp->x, pp->y}")) (test-assert "designated" (emits? (in-line "(var q (struct point) #(.y (. pp x)))") "{.y = pp->x}"))) (test-group "member access" (test-assert "a value reads with a dot" (emits? (in-line "(= (. p x) 1)") "p.x = 1")) ;; an indexed step was looked up as a field whole, so its type was ;; lost and the step after it guessed (test-assert "an indexed step is its field's element" (emits? poly-src "q->pts[2].y")) (test-assert "and says so without a warning" (not (warns? poly-src "cannot type"))) (test-assert "so a pointer element is read through" (emits? poly-src "q->pp[0]->y")) (test-assert "and every index of one is written" (emits? poly-src "q->grid[1][2].x")) ;; 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" (emits? pt-ref-src "q->x")) (test-assert "dereferences to what it points at" (not (warns? pt-ref-src "cannot type"))) (test-assert "and calls its target's methods" (emits? pt-ref-src "ptːget(q)")) (test-assert "a pointer reads with an arrow" (emits? (in-line "(= (. pp x) 1)") "pp->x = 1")) ;; each step asks the type in hand, so one path may be both (test-assert "a pointer field inside a value" (emits? (in-line "(= (. l a x) 1)") "l.a->x = 1")) (test-assert "a value field inside a pointer" (emits? (in-line "(= (. lp b y) 1)") "lp->b.y = 1")) (test-assert "and two pointers in a row" (emits? (in-line "(= (. lp a x) 1)") "lp->a->x = 1")) ;; a path is left-associative and its right operand is always a ;; name, so none of it needs parenthesising (test-assert "a path carries no parens" (not (emits? (in-line "(= (. lp a x) 1)") "(lp->a)"))) ;; ...which is not to say nothing does (test-assert "though a dereference still does" (emits? (in-line "(= (. (* pp) x) 1)") "(*pp).x = 1")) (test-assert "a type we cannot work out is read as a value" (emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1")) (test-assert "and said so" (warns? (in-line "(= (. (guess) x) 1)") "cannot type this")) ;; Past a typed base it is a field that cannot be typed -- one of a ;; struct only C knows -- and the warning named the base regardless (test-assert "naming the path read so far" (warns? (in-fn "(var t (* struct tm) 0) (= (. t a b) 1)") "as a value (. t a)\n")) (test-assert "which one step through it does not need" (not (warns? (in-fn "(var t (* struct tm) 0) (= (. t a) 1)") "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 `->' ;; is an ordinary head -- a threading macro that nobody imported, ;; and a call like any other unknown (test-assert "a bare -> is not the arrow" (emits? (in-line "(= (-> pp x) 1)") "->(pp, x) = 1"))) ;; A global's initializer is an expression like a local's, and walked ;; the same way (test-group "global initializers" (test-assert "a path is dispatched on its type" (emits? (string-append "(struct pt ((x int)))" "(var a (struct pt) #((struct pt) : 1))" "(var q (* int) (& (. a x)))") "int * q = &a.x")) (test-assert "a lambda is lifted, and named after the global" (emits? "(var f (fn ((x int)) int) (lambda ((x int)) int (return x)))" "int (*f)(int) = λ0_f")) (test-assert "ahead of it" (let ((c (sex->c "(var f (fn ((x int)) int) (lambda ((x int)) int (return x)))"))) (< (string-contains c "static int λ0_f (int x)") (string-contains c "(*f)(int) = λ0_f"))))) ;; Toplevel forms used to be separated by a blank line unconditionally, ;; which spread a block of adjacent typedefs over twice the lines its ;; author wrote (test-group "vertical spacing" (test-assert "adjacent forms stay adjacent" (emits? "(include a.h)\n(include b.h)" "#include \n#include ")) (test-assert "a blank line is kept" (emits? "(include a.h)\n\n(include b.h)" "#include \n\n#include ")) ;; the gap is measured from where the form ended, not where it began (test-assert "a multi-line form does not invent one" (emits? "(struct s\n ((x int)))\n(include b.h)" "};\n#include ")) (test-assert "nor swallows one" (emits? "(struct s\n ((x int)))\n\n(include b.h)" "};\n\n#include "))))