Compare commits
16 Commits
more-tests
...
79d9860fc1
| Author | SHA1 | Date | |
|---|---|---|---|
| 79d9860fc1 | |||
| b73cf36160 | |||
| baf2f92fce | |||
| ea0bed6deb | |||
| 1dc0c1a2fc | |||
| 6583ed958c | |||
| 448b908c40 | |||
| f9fafd78d2 | |||
| 08044ce4a1 | |||
| d5b003ef91 | |||
| df3aa1789c | |||
| 504f35722a | |||
| 8774400f1f | |||
| 9a490a942c | |||
| 97eb123a42 | |||
| db9801331f |
1
Makefile
1
Makefile
@@ -118,6 +118,7 @@ SEX_TEST_PROGRAMS = c11 \
|
|||||||
lists \
|
lists \
|
||||||
operators \
|
operators \
|
||||||
ops \
|
ops \
|
||||||
|
precedence \
|
||||||
records \
|
records \
|
||||||
serialize \
|
serialize \
|
||||||
specs \
|
specs \
|
||||||
|
|||||||
@@ -46,10 +46,10 @@ source location. Only statement positions may be walked this way: a
|
|||||||
(append-map (lambda (s)
|
(append-map (lambda (s)
|
||||||
(let ((src (form-source s)))
|
(let ((src (form-source s)))
|
||||||
(if src
|
(if src
|
||||||
(list (line-directive src) (walk-expr s))
|
(list (line-directive src) (walk-statement s))
|
||||||
(list (walk-expr s)))))
|
(list (walk-statement s)))))
|
||||||
(pack-comments stmts))
|
(pack-comments stmts))
|
||||||
(map walk-expr (pack-comments stmts))))
|
(map walk-statement (pack-comments stmts))))
|
||||||
|
|
||||||
(define (walk-stmt s)
|
(define (walk-stmt s)
|
||||||
"A statement in a slot that holds exactly one form -- an `if' arm.
|
"A statement in a slot that holds exactly one form -- an `if' arm.
|
||||||
@@ -59,8 +59,18 @@ as an `else if' chain, so the anchor and the statement are wrapped in
|
|||||||
surrounding c-block supplies them)."
|
surrounding c-block supplies them)."
|
||||||
(let ((src (and (pair? s) (anchor-statements?) (form-source s))))
|
(let ((src (and (pair? s) (anchor-statements?) (form-source s))))
|
||||||
(if src
|
(if src
|
||||||
`(%begin ,(line-directive src) ,(walk-expr s))
|
`(%begin ,(line-directive src) ,(walk-statement s))
|
||||||
(walk-expr s))))
|
(walk-statement s))))
|
||||||
|
|
||||||
|
;;; Only here is an `if' a statement, with statements for arms that can
|
||||||
|
;;; each be anchored. Anywhere else it is `?:', and a #line between its
|
||||||
|
;;; parts does not compile
|
||||||
|
(define (walk-statement s)
|
||||||
|
(match s
|
||||||
|
(('if . clauses)
|
||||||
|
(with-comments (filter comment-form? clauses)
|
||||||
|
(cons 'if (walk-if-clauses (remove comment-form? clauses)))))
|
||||||
|
(_ (walk-expr s))))
|
||||||
|
|
||||||
;;; A `;' comment reads as a form, so one written inside a construct
|
;;; A `;' comment reads as a form, so one written inside a construct
|
||||||
;;; with positional slots lands in a slot and shifts everything after
|
;;; with positional slots lands in a slot and shifts everything after
|
||||||
@@ -84,6 +94,22 @@ forms, and what remains."
|
|||||||
form
|
form
|
||||||
`(%begin ,@(map walk-expr comments) ,form)))
|
`(%begin ,@(map walk-expr comments) ,form)))
|
||||||
|
|
||||||
|
;;; (for init test step body ...). C's parens around the header, carried
|
||||||
|
;;; over, leave a call of a `var' in init's slot and the body in test's
|
||||||
|
(define (check-for-header form slots)
|
||||||
|
(cond ((and (pair? slots) (pair? (car slots)) (pair? (caar slots))
|
||||||
|
(eq? 'var (car (caar slots))))
|
||||||
|
(sex-error form "a for header is not grouped: write (for init test step body ...)"))
|
||||||
|
((< (length slots) 3)
|
||||||
|
(sex-error form "malformed for: write (for init test step body ...)"))))
|
||||||
|
|
||||||
|
;;; A case body that declares is a block, so two cases can each declare
|
||||||
|
;;; a `bits', and a declaration can follow the label before C23
|
||||||
|
(define (walk-case-body body)
|
||||||
|
(if (any (lambda (form) (and (pair? form) (eq? 'var (car form)))) body)
|
||||||
|
(list (cons '%block-begin (walk-body body)))
|
||||||
|
(walk-body body)))
|
||||||
|
|
||||||
(define (walk-if-clauses clauses)
|
(define (walk-if-clauses clauses)
|
||||||
"(test stmt test stmt ... [else-stmt]) -- tests stay expressions."
|
"(test stmt test stmt ... [else-stmt]) -- tests stay expressions."
|
||||||
(let loop ((cs clauses) (acc (list)))
|
(let loop ((cs clauses) (acc (list)))
|
||||||
@@ -102,15 +128,22 @@ forms, and what remains."
|
|||||||
;;; hand-written vec2__length would collide, and an identifier holding
|
;;; hand-written vec2__length would collide, and an identifier holding
|
||||||
;;; `__' is reserved in C++, which a pub header may be included from.
|
;;; `__' is reserved in C++, which a pub header may be included from.
|
||||||
;;; Lifted closures carry a ƛ for the same reason.
|
;;; Lifted closures carry a ƛ for the same reason.
|
||||||
|
;;;
|
||||||
|
;;; zero? -> zeroʔ, clear! -> clearꜝ: a glottal stop and a raised
|
||||||
|
;;; exclamation mark, both letters. Not the retroflex click ǃ, which
|
||||||
|
;;; looks more like `!' -- enough that clang warns at every use.
|
||||||
|
(define +name-spellings+
|
||||||
|
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ")))
|
||||||
|
|
||||||
(define (unkebabify sym)
|
(define (unkebabify sym)
|
||||||
(case sym
|
(case sym
|
||||||
((- -- -> -= / /=) sym) ; bare operators, not names
|
((- -- -> -= / /= ! != ?) sym) ; bare operators, not names
|
||||||
(else
|
(else
|
||||||
(string->symbol
|
(string->symbol
|
||||||
(irregex-replace/all
|
(fold (lambda (spelling name)
|
||||||
"/"
|
(irregex-replace/all (car spelling) name (cdr spelling)))
|
||||||
(irregex-replace/all "-(?!>)" (symbol->string sym) "_")
|
(symbol->string sym)
|
||||||
"ː")))))
|
+name-spellings+)))))
|
||||||
|
|
||||||
(define (atom-to-fmt-c atom)
|
(define (atom-to-fmt-c atom)
|
||||||
(case atom
|
(case atom
|
||||||
@@ -195,6 +228,12 @@ forms, and what remains."
|
|||||||
;; name `->' itself being the threading macro
|
;; name `->' itself being the threading macro
|
||||||
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
||||||
(('var . _) (walk-var form))
|
(('var . _) (walk-var form))
|
||||||
|
;; semen has told a type from an expression; only it knows the names
|
||||||
|
;; in scope that make `(* p)' one or the other
|
||||||
|
(('sizeof ('%type type)) `(%sizeof-type ,(walk-type type)))
|
||||||
|
;; semen has already put the target first, the way C writes it
|
||||||
|
(('typedef type name)
|
||||||
|
`(typedef ,(walk-type type) ,(atom-to-fmt-c name)))
|
||||||
;; An expression has no room for a statement, so a comment in a
|
;; An expression has no room for a statement, so a comment in a
|
||||||
;; cast is dropped rather than relocated.
|
;; cast is dropped rather than relocated.
|
||||||
(('cast . rest)
|
(('cast . rest)
|
||||||
@@ -209,15 +248,14 @@ forms, and what remains."
|
|||||||
;; and each is spliced or wrapped according to what the
|
;; and each is spliced or wrapped according to what the
|
||||||
;; corresponding fmt-c procedure accepts.
|
;; corresponding fmt-c procedure accepts.
|
||||||
(('do . stmts) (cons '%block-begin (walk-body stmts)))
|
(('do . stmts) (cons '%block-begin (walk-body stmts)))
|
||||||
(('if . clauses)
|
(('if . clauses) (cons 'if (map walk-expr (remove comment-form? clauses))))
|
||||||
(with-comments (filter comment-form? clauses)
|
|
||||||
(cons 'if (walk-if-clauses (remove comment-form? clauses)))))
|
|
||||||
(('while . rest)
|
(('while . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 1)))
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
(cons* 'while (walk-expr (car slots)) (walk-body body)))))
|
(cons* 'while (walk-expr (car slots)) (walk-body body)))))
|
||||||
(('for . rest)
|
(('for . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 3)))
|
(let-values (((comments slots body) (take-slots rest 3)))
|
||||||
|
(check-for-header form slots)
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
(cons* 'for (walk-expr (car slots)) (walk-expr (cadr slots))
|
(cons* 'for (walk-expr (car slots)) (walk-expr (cadr slots))
|
||||||
(walk-expr (caddr slots))
|
(walk-expr (caddr slots))
|
||||||
@@ -236,12 +274,12 @@ forms, and what remains."
|
|||||||
(('case . rest)
|
(('case . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 1)))
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
(cons* 'case (walk-expr (car slots)) (walk-body body)))))
|
(cons* 'case (walk-expr (car slots)) (walk-case-body body)))))
|
||||||
(('case/fallthrough . rest)
|
(('case/fallthrough . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 1)))
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-body body)))))
|
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-case-body body)))))
|
||||||
(('default . body) (cons 'default (walk-body body)))
|
(('default . body) (cons 'default (walk-case-body body)))
|
||||||
|
|
||||||
;; Drop comments so they will not generate additional comma
|
;; Drop comments so they will not generate additional comma
|
||||||
(else (map walk-expr (remove comment-form? form)))))
|
(else (map walk-expr (remove comment-form? form)))))
|
||||||
@@ -316,7 +354,7 @@ forms, and what remains."
|
|||||||
(match form
|
(match form
|
||||||
(('¤ . array-type)
|
(('¤ . array-type)
|
||||||
(if (array-bound? form)
|
(if (array-bound? form)
|
||||||
`(%array ,(walk-type (array-element-type form)) ,(last array-type))
|
`(%array ,(walk-type (array-element-type form)) ,(walk-expr (last array-type)))
|
||||||
;; sugar for pointer... Do we really need it? Guess why not,
|
;; sugar for pointer... Do we really need it? Guess why not,
|
||||||
;; it's a strong semantic cue
|
;; it's a strong semantic cue
|
||||||
`(%array ,(walk-type (array-element-type form)))))
|
`(%array ,(walk-type (array-element-type form)))))
|
||||||
@@ -447,11 +485,17 @@ forms, and what remains."
|
|||||||
(('enum (? symbol? name))
|
(('enum (? symbol? name))
|
||||||
`(enum ,(atom-to-fmt-c name)))
|
`(enum ,(atom-to-fmt-c name)))
|
||||||
(('enum (values ...))
|
(('enum (values ...))
|
||||||
`(enum ,(map atom-to-fmt-c values)))
|
`(enum ,(map walk-enumerator values)))
|
||||||
(('enum name (values ...))
|
(('enum name (values ...))
|
||||||
`(enum ,(atom-to-fmt-c name) ,(map atom-to-fmt-c values)))
|
`(enum ,(atom-to-fmt-c name) ,(map walk-enumerator values)))
|
||||||
(else (sex-error form "malformed enum" form))))
|
(else (sex-error form "malformed enum" form))))
|
||||||
|
|
||||||
|
;;; red -> red, (wire-varint 0) -> wire_varint = 0
|
||||||
|
(define (walk-enumerator value)
|
||||||
|
(match value
|
||||||
|
((name expr) (list (atom-to-fmt-c name) (walk-expr expr)))
|
||||||
|
(_ (atom-to-fmt-c value))))
|
||||||
|
|
||||||
(define (walk-extern form)
|
(define (walk-extern form)
|
||||||
(match form
|
(match form
|
||||||
(('fn . _)
|
(('fn . _)
|
||||||
@@ -499,7 +543,7 @@ forms, and what remains."
|
|||||||
;; A target is a filename and not an identifier: GL/gl.h is a path,
|
;; A target is a filename and not an identifier: GL/gl.h is a path,
|
||||||
;; and unkebabify would spell it GLːgl.h
|
;; and unkebabify would spell it GLːgl.h
|
||||||
(('include target) (list '%include target))
|
(('include target) (list '%include target))
|
||||||
(('define name . rest) `(%define ,(atom-to-fmt-c name) ,@rest))
|
(('define name . body) `(%define ,(walk-expr name) ,@(map walk-expr body)))
|
||||||
(else (walk-expr form))))
|
(else (walk-expr form))))
|
||||||
|
|
||||||
;;; Two toplevel forms are separated by a blank line only if they were
|
;;; Two toplevel forms are separated by a blank line only if they were
|
||||||
|
|||||||
42
reader.scm
42
reader.scm
@@ -179,10 +179,37 @@
|
|||||||
(cond
|
(cond
|
||||||
((string=? s ".") dot-token)
|
((string=? s ".") dot-token)
|
||||||
((string->number s) => identity)
|
((string->number s) => identity)
|
||||||
|
((radix-number s) => identity)
|
||||||
|
((negated-name? s) (list '- (string->symbol (substring s 1))))
|
||||||
(else (string->symbol s))))
|
(else (string->symbol s))))
|
||||||
|
|
||||||
;;; #-dispatch: booleans, characters, vectors, block/datum comments,
|
;;; 0xE0 and 0b101 are C's spelling of 224 and 5, and are read as those
|
||||||
;;; feature expressions
|
;;; numbers: the C written from them says 224 and 5
|
||||||
|
(define (radix-number s)
|
||||||
|
(let* ((negative? (and (> (string-length s) 0) (char=? #\- (string-ref s 0))))
|
||||||
|
(digits (if negative? (substring s 1) s)))
|
||||||
|
(and (> (string-length digits) 2)
|
||||||
|
(char=? #\0 (string-ref digits 0))
|
||||||
|
(let ((radix (case (string-ref digits 1)
|
||||||
|
((#\x #\X) 16)
|
||||||
|
((#\b #\B) 2)
|
||||||
|
(else #f)))
|
||||||
|
(body (substring digits 2)))
|
||||||
|
(and radix
|
||||||
|
(not (memv (string-ref body 0) '(#\+ #\-)))
|
||||||
|
(let ((n (string->number body radix)))
|
||||||
|
(and (exact-integer? n) (if negative? (- n) n))))))))
|
||||||
|
|
||||||
|
;;; -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)
|
||||||
|
(char=? #\- (string-ref s 0))
|
||||||
|
(let ((c (string-ref s 1)))
|
||||||
|
(or (char-alphabetic? c) (char=? c #\_)))))
|
||||||
|
|
||||||
|
;;; #-dispatch: booleans, characters, vectors, numbers in a radix,
|
||||||
|
;;; block/datum comments, feature expressions
|
||||||
(define (read-hash port)
|
(define (read-hash port)
|
||||||
(let ((c (get-ch port)))
|
(let ((c (get-ch port)))
|
||||||
(cond
|
(cond
|
||||||
@@ -191,6 +218,9 @@
|
|||||||
((or (char=? c #\f) (char=? c #\F)) (read-bool port #f))
|
((or (char=? c #\f) (char=? c #\F)) (read-bool port #f))
|
||||||
((char=? c #\\) (read-char-lit port))
|
((char=? c #\\) (read-char-lit port))
|
||||||
((char=? c #\() (list->vector (read-list port close-paren)))
|
((char=? c #\() (list->vector (read-list port close-paren)))
|
||||||
|
((memv c '(#\x #\X)) (read-radix port 16))
|
||||||
|
((memv c '(#\b #\B)) (read-radix port 2))
|
||||||
|
((memv c '(#\o #\O)) (read-radix port 8))
|
||||||
((char=? c #\|) (skip-block-comment port 1) (next-token port))
|
((char=? c #\|) (skip-block-comment port 1) (next-token port))
|
||||||
((char=? c #\;) (read-datum port) (next-token port)) ; datum comment
|
((char=? c #\;) (read-datum port) (next-token port)) ; datum comment
|
||||||
((char=? c #\+) (read-conditional port #t))
|
((char=? c #\+) (read-conditional port #t))
|
||||||
@@ -244,6 +274,14 @@
|
|||||||
((datum-token? guarded) (next-token port))
|
((datum-token? guarded) (next-token port))
|
||||||
(else guarded))))
|
(else guarded))))
|
||||||
|
|
||||||
|
;;; #x80, #b101, #o10
|
||||||
|
(define (read-radix port radix)
|
||||||
|
(let* ((digits (read-atom-string port))
|
||||||
|
(n (string->number digits radix)))
|
||||||
|
(if (exact-integer? n)
|
||||||
|
n
|
||||||
|
(error "Malformed number" digits))))
|
||||||
|
|
||||||
;;; #t / #true / #f / #false. `val' is the boolean; consume any
|
;;; #t / #true / #f / #false. `val' is the boolean; consume any
|
||||||
;;; trailing name characters and validate
|
;;; trailing name characters and validate
|
||||||
(define (read-bool port val)
|
(define (read-bool port val)
|
||||||
|
|||||||
54
semen.scm
54
semen.scm
@@ -486,9 +486,8 @@
|
|||||||
;;; (while (< v 0) (var v char 1) ...)
|
;;; (while (< v 0) (var v char 1) ...)
|
||||||
;;; (var m _ (+ v 1)) ; double, not char
|
;;; (var m _ (+ v 1)) ; double, not char
|
||||||
;;;
|
;;;
|
||||||
;;; One frame per form, not one per arm: a `case' label opens no scope
|
;;; A `case' body gets one too, being written as a block. An `if' arm
|
||||||
;;; in C, and an `if' arm can only declare inside a `do', which brings
|
;;; gets none: it can only declare inside a `do', which brings its own.
|
||||||
;;; its own.
|
|
||||||
|
|
||||||
(define (declare-name! env name type)
|
(define (declare-name! env name type)
|
||||||
(hash-table-set! (car (hash-table-ref env :scopes)) name type))
|
(hash-table-set! (car (hash-table-ref env :scopes)) name type))
|
||||||
@@ -527,6 +526,7 @@
|
|||||||
|
|
||||||
(define (walk-statement form env)
|
(define (walk-statement form env)
|
||||||
(cond
|
(cond
|
||||||
|
((vector? form) (walk-initializer form env))
|
||||||
((not (list? form)) form)
|
((not (list? form)) form)
|
||||||
((null? form) form)
|
((null? form) form)
|
||||||
;; expanded here rather than before the walk, so the macro body can
|
;; expanded here rather than before the walk, so the macro body can
|
||||||
@@ -543,7 +543,7 @@
|
|||||||
(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 for while if switch)
|
((do for while if switch case case/fallthrough default)
|
||||||
(with-scope env (lambda () (walk-parts form env))))
|
(with-scope env (lambda () (walk-parts form env))))
|
||||||
|
|
||||||
((lambda)
|
((lambda)
|
||||||
@@ -613,6 +613,14 @@
|
|||||||
|
|
||||||
((dot-access) (walk-member-path form env))
|
((dot-access) (walk-member-path form env))
|
||||||
|
|
||||||
|
((sizeof)
|
||||||
|
(if (and (= 2 (length form)) (written-type? (second form) env))
|
||||||
|
(copy-form-source!
|
||||||
|
form
|
||||||
|
`(sizeof (%type ,(resolve-closure-types
|
||||||
|
(expand-type (second form) env)))))
|
||||||
|
(walk-parts form env)))
|
||||||
|
|
||||||
(else
|
(else
|
||||||
(if (method-head? (car form))
|
(if (method-head? (car form))
|
||||||
(walk-method-call form env)
|
(walk-method-call form env)
|
||||||
@@ -630,6 +638,34 @@
|
|||||||
(cdr form)))))
|
(cdr form)))))
|
||||||
(convert-arguments (walk-parts form env) env)))))))))
|
(convert-arguments (walk-parts form env) env)))))))))
|
||||||
|
|
||||||
|
;;; #(struct point : (. p x) 2) -- the values are expressions, and the
|
||||||
|
;;; words before a `:' a type that is left as written
|
||||||
|
(define (walk-initializer form env)
|
||||||
|
(let-values (((prefix rest) (break (lambda (e) (eq? e ':))
|
||||||
|
(vector->list form))))
|
||||||
|
(let ((walk (lambda (es) (map (lambda (e) (walk-statement e env)) es))))
|
||||||
|
(list->vector
|
||||||
|
(if (null? rest)
|
||||||
|
(walk prefix)
|
||||||
|
(append prefix (list ':) (walk (cdr rest))))))))
|
||||||
|
|
||||||
|
;;; Is the operand of `sizeof' a type? `(* x)' and `(¤ x i)' are
|
||||||
|
;;; expressions when x is: a name in scope, or a list that is not
|
||||||
|
;;; itself a type. A name nothing declared is one only C knows of, and
|
||||||
|
;;; there that is a type:
|
||||||
|
;;;
|
||||||
|
;;; (* char) (* const char) (* size-t) (unsigned int) types
|
||||||
|
;;; (* p) (* (g)) (¤ xs 0) expressions
|
||||||
|
(define (written-type? form env)
|
||||||
|
(and (pair? form)
|
||||||
|
;; the length a dereference and a subscript have
|
||||||
|
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1)))
|
||||||
|
(let ((operand (second form)))
|
||||||
|
(if (symbol? operand)
|
||||||
|
(not (or (lookup-name env operand) (get-name-type operand)))
|
||||||
|
(written-type? operand env)))
|
||||||
|
(type-head? form))))
|
||||||
|
|
||||||
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
||||||
(define (resolve-wildcard form env)
|
(define (resolve-wildcard form env)
|
||||||
(if (and (>= (length form) 3) (wildcard-type? (third form)))
|
(if (and (>= (length form) 3) (wildcard-type? (third form)))
|
||||||
@@ -894,11 +930,14 @@
|
|||||||
;;; (. l a x) l : (struct line), l.a : (* struct point) -> l.a->x
|
;;; (. l a x) l : (struct line), l.a : (* struct point) -> l.a->x
|
||||||
;;;
|
;;;
|
||||||
;;; 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.
|
;;; 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
|
||||||
|
;;; only C knows -- (. font handle), not font.
|
||||||
(define (walk-member-path form env)
|
(define (walk-member-path form env)
|
||||||
(let ((base (second form)))
|
(let ((base (second form)))
|
||||||
(let step ((expr (walk-statement base env))
|
(let step ((expr (walk-statement base env))
|
||||||
(type (expression-type base env))
|
(type (expression-type base env))
|
||||||
|
(read (list)) ; the fields walked, reversed
|
||||||
(fields (cddr form))
|
(fields (cddr form))
|
||||||
(told? #f))
|
(told? #f))
|
||||||
(if (null? fields)
|
(if (null? fields)
|
||||||
@@ -906,9 +945,12 @@
|
|||||||
(let ((target (pointer-target type)))
|
(let ((target (pointer-target type)))
|
||||||
(unless (or type told?)
|
(unless (or type told?)
|
||||||
(sex-warning form "cannot type this; `.' reads it as a value"
|
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||||
base))
|
(if (null? read)
|
||||||
|
base
|
||||||
|
`(|.| ,base ,@(reverse read)))))
|
||||||
(step (list (if target '%-> 'dot-access) expr (car fields))
|
(step (list (if target '%-> 'dot-access) expr (car fields))
|
||||||
(field-type (or target type) (car fields))
|
(field-type (or target type) (car fields))
|
||||||
|
(cons (car fields) read)
|
||||||
(cdr fields)
|
(cdr fields)
|
||||||
(or told? (not type))))))))
|
(or told? (not type))))))))
|
||||||
|
|
||||||
|
|||||||
@@ -80,6 +80,7 @@
|
|||||||
((dot arrow post-decrement post-increment) 10)
|
((dot arrow post-decrement post-increment) 10)
|
||||||
((**) 15) ; Perl
|
((**) 15) ; Perl
|
||||||
((unary+ unary- ! ~ cast unary-* unary-& sizeof) 20) ; ++ --
|
((unary+ unary- ! ~ cast unary-* unary-& sizeof) 20) ; ++ --
|
||||||
|
((cast-operand) 25) ; see c-cast
|
||||||
((=~ !~) 25) ; Perl
|
((=~ !~) 25) ; Perl
|
||||||
((* / %) 30)
|
((* / %) 30)
|
||||||
((+ -) 35)
|
((+ -) 35)
|
||||||
@@ -287,6 +288,7 @@
|
|||||||
((%comment) ((apply c-comment (cdr x)) st))
|
((%comment) ((apply c-comment (cdr x)) st))
|
||||||
((:) ((apply c-label (cdr x)) st))
|
((:) ((apply c-label (cdr x)) st))
|
||||||
((%cast) ((apply c-cast (cdr x)) st))
|
((%cast) ((apply c-cast (cdr x)) st))
|
||||||
|
((%sizeof-type) ((cat "sizeof(" (c-type (cadr x)) ")") st))
|
||||||
((%compound) ((apply c-compound (cdr x)) st))
|
((%compound) ((apply c-compound (cdr x)) st))
|
||||||
((%designate) ((apply c-designate (cdr x)) st))
|
((%designate) ((apply c-designate (cdr x)) st))
|
||||||
((+ - & * / % ! ~ ^ && < > <= >= == != << >>
|
((+ - & * / % ! ~ ^ && < > <= >= == != << >>
|
||||||
@@ -432,6 +434,15 @@
|
|||||||
((pair? x) (cons (replace (car x)) (replace (cdr x))))
|
((pair? x) (cons (replace (car x)) (replace (cdr x))))
|
||||||
(else x))))
|
(else x))))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: an object-like body is parenthesised
|
||||||
|
;; as a function-like one is. Upstream left it bare, so with
|
||||||
|
;; `#define D -1.0 / 6.0', `x * D' was `(x * -1.0) / 6.0'. Only a
|
||||||
|
;; name or a literal that cannot lose its sign to a neighbour is
|
||||||
|
;; left as written.
|
||||||
|
(define (c-atomic-body? x)
|
||||||
|
(or (symbol? x)
|
||||||
|
(and (c-literal? x) (not (and (number? x) (negative? x))))))
|
||||||
|
|
||||||
(define (cpp-define x . body)
|
(define (cpp-define x . body)
|
||||||
(define (name-of x) (c-expr (if (pair? x) (cadr x) x)))
|
(define (name-of x) (c-expr (if (pair? x) (cadr x) x)))
|
||||||
(lambda (st)
|
(lambda (st)
|
||||||
@@ -449,9 +460,8 @@
|
|||||||
(cat " "
|
(cat " "
|
||||||
(fmt-let 'writer (make-cpp-writer st)
|
(fmt-let 'writer (make-cpp-writer st)
|
||||||
(fmt-let 'macro-params params
|
(fmt-let 'macro-params params
|
||||||
((if (or (not (pair? x))
|
((if (and (null? (cdr body))
|
||||||
(and (null? (cdr body))
|
(c-atomic-body? (car body)))
|
||||||
(c-literal? (car body))))
|
|
||||||
(lambda (x) x)
|
(lambda (x) x)
|
||||||
c-paren)
|
c-paren)
|
||||||
(c-in-expr (apply c-begin body))))))
|
(c-in-expr (apply c-begin body))))))
|
||||||
@@ -805,32 +815,22 @@
|
|||||||
(fmt-join c-expr name ", ")
|
(fmt-join c-expr name ", ")
|
||||||
(c-expr name))))))
|
(c-expr name))))))
|
||||||
|
|
||||||
;; MODIFIED FROM UPSTREAM fmt-c: parenthesise a binary operand.
|
;; MODIFIED FROM UPSTREAM fmt-c: take part in precedence, both ways.
|
||||||
;;
|
;;
|
||||||
;; A cast binds tighter than every binary operator, so upstream's
|
;; Upstream's bare (cat "(" type ")" expr) cast only the first operand
|
||||||
;; unparenthesised (cat "(" type ")" expr) silently casts only the
|
;; of a looser one -- (cast (* 2 (sizeof GLfloat)) (* void)) came out
|
||||||
;; first operand: (cast (* 2 (sizeof GLfloat)) (* void)) came out as
|
;; `(void *)2 * sizeof(GLfloat)' -- and was itself taken apart by a
|
||||||
;; `(void *)2 * sizeof(GLfloat)'. Unary operands -- &x, *p, -n,
|
;; tighter operator around it: `(const wanted *)pa->distance'.
|
||||||
;; sizeof, calls, identifiers, literals -- are already
|
;;
|
||||||
;; unary-expressions and are left alone, so this changes nothing that
|
;; The operand is written at `cast-operand', just looser than a unary
|
||||||
;; was previously correct.
|
;; operator, so a unary operand stays bare -- `(int *)&a' -- while
|
||||||
(define c-binary-operators
|
;; anything looser parenthesises itself. The cast is parenthesised
|
||||||
'(+ - * / % << >> < > <= >= == != & ^ && %and %or
|
;; only under something strictly tighter: `-(int)a', `*(int *)v'.
|
||||||
= *= /= %= &= ^= <<= >>=))
|
|
||||||
|
|
||||||
(define (c-binary-expression? expr)
|
|
||||||
(and (pair? expr)
|
|
||||||
(memq (car expr) c-binary-operators)
|
|
||||||
(pair? (cdr expr))
|
|
||||||
(pair? (cddr expr)))) ; two or more operands
|
|
||||||
|
|
||||||
(define (c-cast type expr)
|
(define (c-cast type expr)
|
||||||
(cat "(" (c-type type) ")"
|
(lambda (st)
|
||||||
(if (c-binary-expression? expr)
|
(let ((cast (cat "(" (c-type type) ")"
|
||||||
;; c-with-op stops the operand from parenthesising itself
|
(c-with-op 'cast-operand (c-expr expr)))))
|
||||||
;; a second time inside the parens we just added.
|
((if (c-op< (fmt-op st) 'cast) (c-paren cast) cast) st))))
|
||||||
(cat "(" (c-with-op 'paren (c-expr expr)) ")")
|
|
||||||
(c-expr expr))))
|
|
||||||
|
|
||||||
;; { a, b, c } -- on one line if it fits, one element per line if not.
|
;; { a, b, c } -- on one line if it fits, one element per line if not.
|
||||||
(define (c-braced-list ls)
|
(define (c-braced-list ls)
|
||||||
@@ -881,7 +881,14 @@
|
|||||||
tail))))
|
tail))))
|
||||||
st))))
|
st))))
|
||||||
|
|
||||||
(define (c-if/expr c p . rest)
|
;; MODIFIED FROM UPSTREAM fmt-c: parenthesised as the operator it is.
|
||||||
|
;; Upstream wrote it bare whatever surrounded it, and `?:' binds
|
||||||
|
;; looser than every binary operator: (/ 300 (if c c 1)) came out
|
||||||
|
;; `300 / c ? c : 1', a conditional on the quotient.
|
||||||
|
(define (c-if/expr . args)
|
||||||
|
(c-maybe-paren '? (apply c-if/expr/aux args)))
|
||||||
|
|
||||||
|
(define (c-if/expr/aux c p . rest)
|
||||||
(let lp ((c c) (p p) (ls rest))
|
(let lp ((c c) (p p) (ls rest))
|
||||||
(cond
|
(cond
|
||||||
((or (eq? c 'else) (eq? c #t))
|
((or (eq? c 'else) (eq? c #t))
|
||||||
|
|||||||
@@ -13,6 +13,13 @@
|
|||||||
(test '_this_->_member_ (unkebabify '-this-->-member-))
|
(test '_this_->_member_ (unkebabify '-this-->-member-))
|
||||||
(test '__->>> (unkebabify '--->>>))
|
(test '__->>> (unkebabify '--->>>))
|
||||||
|
|
||||||
|
;; A Lisp predicate and mutator, in letters C takes. The operators
|
||||||
|
;; spelled with the same characters are not names
|
||||||
|
(test 'ident_startʔ (unkebabify 'ident-start?))
|
||||||
|
(test 'resetꜝ (unkebabify 'reset!))
|
||||||
|
(test '! (unkebabify '!))
|
||||||
|
(test '!= (unkebabify '!=))
|
||||||
|
|
||||||
;; Non-ASCII identifiers must survive intact. The `regex' egg's
|
;; Non-ASCII identifiers must survive intact. The `regex' egg's
|
||||||
;; string-substitute drops one trailing character per multi-byte
|
;; string-substitute drops one trailing character per multi-byte
|
||||||
;; character, which renames things silently -- the C still compiles,
|
;; character, which renames things silently -- the C still compiles,
|
||||||
|
|||||||
@@ -53,6 +53,15 @@ compiles."
|
|||||||
(let ((m (error-message source)))
|
(let ((m (error-message source)))
|
||||||
(and m (string-contains m fragment) #t)))
|
(and m (string-contains m fragment) #t)))
|
||||||
|
|
||||||
|
(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)))))))
|
||||||
|
|
||||||
(define (in-fn body)
|
(define (in-fn body)
|
||||||
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
||||||
|
|
||||||
@@ -78,6 +87,22 @@ compiles."
|
|||||||
;; c-switch handed its scrutinee straight to `cat', which only works
|
;; c-switch handed its scrutinee straight to `cat', which only works
|
||||||
;; when it is an atom. Anything else was displayed as a raw
|
;; when it is an atom. Anything else was displayed as a raw
|
||||||
;; s-expression: `switch ((%. e type))'.
|
;; s-expression: `switch ((%. e type))'.
|
||||||
|
;; 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))))")
|
||||||
|
"{" "{"))))
|
||||||
|
|
||||||
(test-group "switch scrutinee"
|
(test-group "switch scrutinee"
|
||||||
(test-assert "member access"
|
(test-assert "member access"
|
||||||
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"
|
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"
|
||||||
@@ -114,7 +139,35 @@ compiles."
|
|||||||
"(int *)&a"))
|
"(int *)&a"))
|
||||||
(test-assert "sizeof is left bare"
|
(test-assert "sizeof is left bare"
|
||||||
(emits? (in-fn "(var n int (cast (sizeof int) int))")
|
(emits? (in-fn "(var n int (cast (sizeof int) int))")
|
||||||
"(int)sizeof(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)")))
|
||||||
|
|
||||||
;; A comment among a call's arguments used to become an argument,
|
;; 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
|
;; and c-apply put a comma on each side of it -- which does not
|
||||||
@@ -172,6 +225,16 @@ compiles."
|
|||||||
(emits? "(pub enum color (red green blue))" "enum color"))
|
(emits? "(pub enum color (red green blue))" "enum color"))
|
||||||
(test-assert "with its values"
|
(test-assert "with its values"
|
||||||
(emits? "(pub enum color (red green blue))" "red"))
|
(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"
|
(test-assert "and a non-pub enum still is too"
|
||||||
(emits? "(enum color (red green blue))" "enum color"))
|
(emits? "(enum color (red green blue))" "enum color"))
|
||||||
;; Naming an enum as a type, rather than defining it, had no
|
;; Naming an enum as a type, rather than defining it, had no
|
||||||
@@ -246,6 +309,22 @@ compiles."
|
|||||||
|
|
||||||
;; A nested pointer chain used to silently lose a level:
|
;; A nested pointer chain used to silently lose a level:
|
||||||
;; (var p (* (* char))) emitted `char *p'
|
;; (var p (* (* char))) emitted `char *p'
|
||||||
|
;; 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 "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:")))
|
||||||
|
|
||||||
(test-group "malformed types are rejected"
|
(test-group "malformed types are rejected"
|
||||||
(test-assert "nested pointer chain"
|
(test-assert "nested pointer chain"
|
||||||
(reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat"))
|
(reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat"))
|
||||||
@@ -328,7 +407,7 @@ compiles."
|
|||||||
(emits? "(fn f ((c (closure ((long)) int))) int (return (c 1)))"
|
(emits? "(fn f ((c (closure ((long)) int))) int (return (c 1)))"
|
||||||
"ƛc.code(&ƛc.env, ƛa0)"))
|
"ƛc.code(&ƛc.env, ƛa0)"))
|
||||||
(test-assert "a subscript receiver is evaluated once"
|
(test-assert "a subscript receiver is evaluated once"
|
||||||
(emits? "(fn f ((t (¤ (closure ((int)) int) 4)) (i int)) int (return ((¤ t (++ i)) 1)))"
|
(emits? "(fn f ((t [(closure ((int)) int) 4]) (i int)) int (return ([t (++ i)] 1)))"
|
||||||
"ƛint_int_call(t[++i], 1)"))
|
"ƛint_int_call(t[++i], 1)"))
|
||||||
(test-assert "so is a member receiver"
|
(test-assert "so is a member receiver"
|
||||||
(emits? "(struct h ((cb (closure ((int)) int))))
|
(emits? "(struct h ((cb (closure ((int)) int))))
|
||||||
@@ -449,7 +528,7 @@ compiles."
|
|||||||
(test-group "macro list accessors"
|
(test-group "macro list accessors"
|
||||||
(test-assert "third reads an array's length"
|
(test-assert "third reads an array's length"
|
||||||
(emits? "(defmacro (len t) (third t))
|
(emits? "(defmacro (len t) (third t))
|
||||||
(fn f () int (return (len (¤ int 7))))"
|
(fn f () int (return (len [int 7])))"
|
||||||
"return 7;"))
|
"return 7;"))
|
||||||
(test-assert "second reads a tag"
|
(test-assert "second reads a tag"
|
||||||
(emits? "(defmacro (tag t) (symbol->string (second t)))
|
(emits? "(defmacro (tag t) (symbol->string (second t)))
|
||||||
@@ -523,7 +602,7 @@ compiles."
|
|||||||
(fn f ((s (struct p))) void (var x (* _) (& s)))"
|
(fn f ((s (struct p))) void (var x (* _) (& s)))"
|
||||||
"struct p* x = &s"))
|
"struct p* x = &s"))
|
||||||
(test-assert "a wildcard inside an array"
|
(test-assert "a wildcard inside an array"
|
||||||
(emits? (in-fn "(var t (¤ _ 3) #((¤ int 3) : 1 2 3))") "int t[3]"))
|
(emits? (in-fn "(var t [_ 3] #([int 3] : 1 2 3))") "int t[3]"))
|
||||||
;; a compound literal carries its own type, where a brace
|
;; a compound literal carries its own type, where a brace
|
||||||
;; initializer has none and takes one from its context
|
;; initializer has none and takes one from its context
|
||||||
(test-assert "a compound literal answers a bare wildcard"
|
(test-assert "a compound literal answers a bare wildcard"
|
||||||
@@ -534,11 +613,11 @@ compiles."
|
|||||||
(reports? (in-fn "(var x _ #(1 2))") "cannot infer the type"))
|
(reports? (in-fn "(var x _ #(1 2))") "cannot infer the type"))
|
||||||
;; ...but its elements still solve the hole in an array type
|
;; ...but its elements still solve the hole in an array type
|
||||||
(test-assert "elements solve an array's element 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}"))
|
(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"
|
(test-assert "including when there are fewer than the length"
|
||||||
(emits? (in-fn "(var t (¤ _ 10) #(1 2))") "int t[10] = {1, 2}"))
|
(emits? (in-fn "(var t [_ 10] #(1 2))") "int t[10] = {1, 2}"))
|
||||||
(test-assert "and they have to agree with each other"
|
(test-assert "and they have to agree with each other"
|
||||||
(reports? (in-fn "(var t (¤ _ 2) #(1 \"s\"))") "type mismatch"))
|
(reports? (in-fn "(var t [_ 2] #(1 \"s\"))") "type mismatch"))
|
||||||
;; a closure type reaches the solver as the struct that stands for
|
;; a closure type reaches the solver as the struct that stands for
|
||||||
;; it, which is the spelling `parse-type' knows
|
;; it, which is the spelling `parse-type' knows
|
||||||
(test-assert "a closure, from the signature that produced it"
|
(test-assert "a closure, from the signature that produced it"
|
||||||
@@ -642,26 +721,95 @@ compiles."
|
|||||||
(emits? (in-fn "(var n int (+ (- a) b))") "-a + b")))
|
(emits? (in-fn "(var n int (+ (- a) b))") "-a + b")))
|
||||||
|
|
||||||
;; An array bound was taken only when it was an integer literal, so a
|
;; 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[]'.
|
;; symbolic one fell into the type: `[int N]' came out `int N a[]'.
|
||||||
(test-group "array bounds"
|
(test-group "array bounds"
|
||||||
(test-assert "a symbolic bound"
|
(test-assert "a symbolic bound"
|
||||||
(emits? "(define N 4) (struct s ((a (¤ int N))))" "int a[N]"))
|
(emits? "(define N 4) (struct s ((a [int N])))" "int a[N]"))
|
||||||
(test-assert "an expression bound"
|
(test-assert "an expression bound"
|
||||||
(emits? "(define N 4) (struct s ((a (¤ char (* 2 N)))))" "char a[2 * N]"))
|
(emits? "(define N 4) (struct s ((a [char (* 2 N)])))" "char a[2 * N]"))
|
||||||
(test-assert "an integer bound still works"
|
(test-assert "an integer bound still works"
|
||||||
(emits? "(struct s ((a (¤ int 4))))" "int a[4]"))
|
(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
|
;; a multi-word type is keywords all the way down, so a trailing
|
||||||
;; keyword belongs to the type and leaves the array unsized
|
;; keyword belongs to the type and leaves the array unsized
|
||||||
(test-assert "a multi-word type is not a bound"
|
(test-assert "a multi-word type is not a bound"
|
||||||
(emits? "(struct s ((a (¤ unsigned int))))" "unsigned int a[]"))
|
(emits? "(struct s ((a [unsigned int])))" "unsigned int a[]"))
|
||||||
;; ...and a tag always follows its keyword
|
;; ...and a tag always follows its keyword
|
||||||
(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[]")))
|
||||||
|
|
||||||
|
;; `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 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,
|
;; `.' 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
|
;; 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-group "member access"
|
||||||
(test-assert "a value reads with a dot"
|
(test-assert "a value reads with a dot"
|
||||||
(emits? (in-line "(= (. p x) 1)") "p.x = 1"))
|
(emits? (in-line "(= (. p x) 1)") "p.x = 1"))
|
||||||
@@ -685,6 +833,14 @@ compiles."
|
|||||||
(emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1"))
|
(emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1"))
|
||||||
(test-assert "and said so"
|
(test-assert "and said so"
|
||||||
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this"))
|
(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")))
|
||||||
;; 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
|
||||||
|
|||||||
@@ -86,7 +86,10 @@
|
|||||||
" (: done)" ; 52
|
" (: done)" ; 52
|
||||||
" (var svar u64 (sizeof (struct pt)))" ; 53
|
" (var svar u64 (sizeof (struct pt)))" ; 53
|
||||||
" (var cvar int (cast mvar int))" ; 54
|
" (var cvar int (cast mvar int))" ; 54
|
||||||
" (return 122))" ; 55
|
" (putchar (if (< mvar 123)" ; 55 a conditional as an
|
||||||
|
" (putchar 124)" ; 56 argument: an expression,
|
||||||
|
" 125))" ; 57 so no anchor inside it
|
||||||
|
" (return 122))" ; 58
|
||||||
))
|
))
|
||||||
|
|
||||||
(define fixture (string-intersperse fixture-lines "\n"))
|
(define fixture (string-intersperse fixture-lines "\n"))
|
||||||
@@ -231,7 +234,14 @@ after that is one more. Lines before the first directive get #f."
|
|||||||
;; Expressions that are their own statement
|
;; Expressions that are their own statement
|
||||||
(test-group "expressions"
|
(test-group "expressions"
|
||||||
(maps/tokens "sizeof" "(var svar" "sizeof")
|
(maps/tokens "sizeof" "(var svar" "sizeof")
|
||||||
(maps/tokens "cast" "(var cvar" "(int)mvar"))
|
(maps/tokens "cast" "(var cvar" "(int)mvar")
|
||||||
|
;; an `if' arm is a statement only where the `if' is one: a
|
||||||
|
;; directive between `?' and `:' does not compile
|
||||||
|
(maps "conditional argument" "123")
|
||||||
|
(test-assert "holds no directive"
|
||||||
|
(let ((start (string-contains c-out "123")))
|
||||||
|
(not (string-contains (substring c-out start (string-contains c-out "125" start))
|
||||||
|
"#line")))))
|
||||||
|
|
||||||
;; A location is (file . line); every directive must name the file the
|
;; A location is (file . line); every directive must name the file the
|
||||||
;; form was read from.
|
;; form was read from.
|
||||||
|
|||||||
@@ -38,6 +38,22 @@
|
|||||||
;; a `.'-prefixed symbol is an ordinary symbol, not dot-access
|
;; a `.'-prefixed symbol is an ordinary symbol, not dot-access
|
||||||
(reader-test '((.field obj)) "(.field obj)")
|
(reader-test '((.field obj)) "(.field obj)")
|
||||||
|
|
||||||
|
;; a leading `-' on a name is negation, the way it is on a number:
|
||||||
|
;; no name is spelled with one, and -INFINITY otherwise mangled into
|
||||||
|
;; _INFINITY. The operators that start with one are left alone
|
||||||
|
(reader-test '((- INFINITY)) "-INFINITY")
|
||||||
|
(reader-test '((var best double (- INFINITY))) "(var best double -INFINITY)")
|
||||||
|
(reader-test '((- _x)) "-_x")
|
||||||
|
(reader-test '(-1 -1.5) "-1 -1.5")
|
||||||
|
(reader-test '(- -> ->> -- -=) "- -> ->> -- -=")
|
||||||
|
|
||||||
|
;; a number in another radix, spelled the way C does or the way
|
||||||
|
;; Scheme does; either is an ordinary integer from here on
|
||||||
|
(reader-test '(224 224 5 5 -16) "0xE0 0Xe0 0b101 0B101 -0x10")
|
||||||
|
(reader-test '(128 128 5 8 -16) "#x80 #X80 #b101 #o10 #x-10")
|
||||||
|
;; ...and only a number: a prefix with nothing after it is a name
|
||||||
|
(reader-test '(0x 0xg) "0x 0xg")
|
||||||
|
|
||||||
;; `;' comments are preserved as (comment "...") forms
|
;; `;' comments are preserved as (comment "...") forms
|
||||||
(reader-test '((comment " hi")) "; hi")
|
(reader-test '((comment " hi")) "; hi")
|
||||||
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
||||||
|
|||||||
@@ -66,12 +66,12 @@
|
|||||||
|
|
||||||
(printf "Through a parameter: %d\n" (apply-twice (make-adder 50) 10))
|
(printf "Through a parameter: %d\n" (apply-twice (make-adder 50) 10))
|
||||||
|
|
||||||
(var table (¤ (closure ((int)) int) 3))
|
(var table [(closure ((int)) int) 3])
|
||||||
(var i int 0)
|
(var i int 0)
|
||||||
(for (= i 0) (< i 3) (++ i)
|
(for (= i 0) (< i 3) (++ i)
|
||||||
(= (¤ table i) (make-adder i)))
|
(= [table i] (make-adder i)))
|
||||||
(printf "From an array: %d %d %d\n"
|
(printf "From an array: %d %d %d\n"
|
||||||
((¤ table 0) 1) ((¤ table 1) 1) ((¤ table 2) 1))
|
([table 0] 1) ([table 1] 1) ([table 2] 1))
|
||||||
|
|
||||||
;; the index must be evaluated once, so `i' ends at 1 and not 2 --
|
;; the index must be evaluated once, so `i' ends at 1 and not 2 --
|
||||||
;; read in a separate statement, since reading and bumping it in one
|
;; read in a separate statement, since reading and bumping it in one
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
"through a pointer 9"
|
"through a pointer 9"
|
||||||
"array 10 20 30"
|
"array 10 20 30"
|
||||||
"argument 6"
|
"argument 6"
|
||||||
"mixed 1 5")
|
"mixed 1 5"
|
||||||
|
"read through a pointer 1 5")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; `:' inside #(...) ends a type and makes the rest a C99 compound
|
;;; `:' inside #(...) ends a type and makes the rest a C99 compound
|
||||||
@@ -38,7 +39,7 @@
|
|||||||
|
|
||||||
;; an array literal decays the way an array does
|
;; an array literal decays the way an array does
|
||||||
(var a (* int) #([int 3] : 10 20 30))
|
(var a (* int) #([int 3] : 10 20 30))
|
||||||
(printf "array %d %d %d\n" (¤ a 0) (¤ a 1) (¤ a 2))
|
(printf "array %d %d %d\n" [a 0] [a 1] [a 2])
|
||||||
|
|
||||||
(printf "argument %d\n" (sum #(struct point : 2 4)))
|
(printf "argument %d\n" (sum #(struct point : 2 4)))
|
||||||
|
|
||||||
@@ -46,4 +47,10 @@
|
|||||||
(var m (struct point) #(struct point : 1 .y 5))
|
(var m (struct point) #(struct point : 1 .y 5))
|
||||||
(printf "mixed %d %d\n" (. m x) (. m y))
|
(printf "mixed %d %d\n" (. m x) (. m y))
|
||||||
|
|
||||||
|
;; what a literal holds is walked like any expression, so a path in
|
||||||
|
;; it reads through a pointer as it does outside one
|
||||||
|
(var pm (* (struct point)) (& m))
|
||||||
|
(var c (struct point) #(struct point : (. pm x) (. pm y)))
|
||||||
|
(printf "read through a pointer %d %d\n" (. c x) (. c y))
|
||||||
|
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -158,7 +158,7 @@
|
|||||||
;;;
|
;;;
|
||||||
;;; 1. SETTLED. `type-match' took `_' on the pattern side, so
|
;;; 1. SETTLED. `type-match' took `_' on the pattern side, so
|
||||||
;;; `(closure _ int)' above is one clause rather than one per
|
;;; `(closure _ int)' above is one clause rather than one per
|
||||||
;;; signature, and `(* _)' and `(¤ _ _)' say "any pointer" and "any
|
;;; signature, and `(* _)' and `[_ _]' say "any pointer" and "any
|
||||||
;;; array". A `_' written last takes the rest, since a type's words
|
;;; array". A `_' written last takes the rest, since a type's words
|
||||||
;;; are spread and not nested: `(* const char)' is three elements.
|
;;; are spread and not nested: `(* const char)' is three elements.
|
||||||
;;; Nothing destructures -- a macro body is Scheme and a type is a
|
;;; Nothing destructures -- a macro body is Scheme and a type is a
|
||||||
|
|||||||
@@ -7,7 +7,8 @@
|
|||||||
"unsigned wins either way: 4294967295 4294967295"
|
"unsigned wins either way: 4294967295 4294967295"
|
||||||
"toplevel: 1 2.5 hi 12"
|
"toplevel: 1 2.5 hi 12"
|
||||||
"closure in place: 5"
|
"closure in place: 5"
|
||||||
"a type is not a call: 7")
|
"a type is not a call: 7"
|
||||||
|
"radix literals: 224 5 128")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; The walk types an expression by its head, and the heads it had a
|
;;; The walk types an expression by its head, and the heads it had a
|
||||||
@@ -61,10 +62,10 @@
|
|||||||
(printf "increment: %d\n" inc)
|
(printf "increment: %d\n" inc)
|
||||||
|
|
||||||
;; an array operand decays, so this is a pointer and not an array
|
;; an array operand decays, so this is a pointer and not an array
|
||||||
(var xs (¤ int 4) #(1 2 3 4))
|
(var xs [int 4] #(1 2 3 4))
|
||||||
(var p _ (+ xs 1))
|
(var p _ (+ xs 1))
|
||||||
(var q _ (+ 1 xs))
|
(var q _ (+ 1 xs))
|
||||||
(var names (¤ (* const char) 2) #("hi" "there"))
|
(var names [(* const char) 2] #("hi" "there"))
|
||||||
(var np _ (+ names 1))
|
(var np _ (+ names 1))
|
||||||
(printf "decayed: %d %d %s\n" (* p) (* q) (* np))
|
(printf "decayed: %d %d %s\n" (* p) (* q) (* np))
|
||||||
|
|
||||||
@@ -87,4 +88,8 @@
|
|||||||
(var f (closure ((int)) int) (make-adder 1))
|
(var f (closure ((int)) int) (make-adder 1))
|
||||||
(var fp (fn ((f int) (g int)) int) area)
|
(var fp (fn ((f int) (g int)) int) area)
|
||||||
(printf "a type is not a call: %d\n" (f 6))
|
(printf "a type is not a call: %d\n" (f 6))
|
||||||
|
|
||||||
|
;; a mask reads best in the radix it is a mask in
|
||||||
|
(var lead int 0xE5)
|
||||||
|
(printf "radix literals: %d %d %d\n" (& lead 0xE0) (& lead 0b111) #x80)
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
32
tests/sex-programs/precedence.sex
Normal file
32
tests/sex-programs/precedence.sex
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
(input)
|
||||||
|
(output "conditional operand: 75"
|
||||||
|
"cast as object of .: 7"
|
||||||
|
"cast of bitwise: 1"
|
||||||
|
"cast of conditional: 44"
|
||||||
|
"define body: 6")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; A Sex form is parenthesised by construction, and C is not: whatever
|
||||||
|
;;; the writer leaves bare, C regroups by its own precedence. Each line
|
||||||
|
;;; below came out regrouped, and all but the member access still
|
||||||
|
;;; compiled -- computing 4, 257, 300, and 4 from `2 * 1 + 2'.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(record wanted ((distance int)))
|
||||||
|
|
||||||
|
(define SUM (+ 1 2))
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var compared int 4)
|
||||||
|
(printf "conditional operand: %d\n" (/ 300 (if (> compared 0) compared 1)))
|
||||||
|
|
||||||
|
(var w wanted #(wanted : 7))
|
||||||
|
(var pa (* const void) (& w))
|
||||||
|
(printf "cast as object of .: %d\n" (. (cast pa (* const wanted)) distance))
|
||||||
|
|
||||||
|
(var x int 1)
|
||||||
|
(printf "cast of bitwise: %d\n" (cast (| x 256) u8))
|
||||||
|
(printf "cast of conditional: %d\n" (cast (if x 300 1) u8))
|
||||||
|
(printf "define body: %d\n" (* 2 SUM))
|
||||||
|
(return 0))
|
||||||
@@ -5,12 +5,15 @@
|
|||||||
"through a pointer: 55"
|
"through a pointer: 55"
|
||||||
"unsized of a typedef: 1 2"
|
"unsized of a typedef: 1 2"
|
||||||
"unsized of a pointer: 5"
|
"unsized of a pointer: 5"
|
||||||
"unnamed parameters: 7 -1 2")
|
"named bound: 2 4 b"
|
||||||
|
"unnamed parameters: 7 -1 2"
|
||||||
|
"typedefs: hi 4 2"
|
||||||
|
"sizeof a pointer type: 1 1")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; Three questions about a written type that used to be answered in
|
;;; Three questions about a written type that used to be answered in
|
||||||
;;; three places and disagreed: is `(a b)' a named parameter or a bare
|
;;; three places and disagreed: is `(a b)' a named parameter or a bare
|
||||||
;;; type, is the last element of a `¤' its bound or the last word of
|
;;; type, is the last element of a `[...]' its bound or the last word of
|
||||||
;;; its element type, and what is one element of an array.
|
;;; its element type, and what is one element of an array.
|
||||||
;;;
|
;;;
|
||||||
;;; They are one question -- where does the type end -- so the answer
|
;;; They are one question -- where does the type end -- so the answer
|
||||||
@@ -21,6 +24,11 @@
|
|||||||
(struct point ((x int) (y int)))
|
(struct point ((x int) (y int)))
|
||||||
|
|
||||||
(typedef small int)
|
(typedef small int)
|
||||||
|
(typedef cstr (* const char))
|
||||||
|
(typedef vec4 [float 4])
|
||||||
|
(typedef unary (fn ((small)) small))
|
||||||
|
|
||||||
|
(define CAP 2)
|
||||||
|
|
||||||
;;; a parameter that names nothing is a type, however many words it
|
;;; a parameter that names nothing is a type, however many words it
|
||||||
;;; takes: `(unsigned int)' is one of them, not a `unsigned' called
|
;;; takes: `(unsigned int)' is one of them, not a `unsigned' called
|
||||||
@@ -37,36 +45,54 @@
|
|||||||
|
|
||||||
(pub fn main () int
|
(pub fn main () int
|
||||||
;; an element keeps every word of its type, tag and all
|
;; an element keeps every word of its type, tag and all
|
||||||
(var pts (¤ (struct point) 2) #(#((struct point) : 1 2)
|
(var pts [(struct point) 2] #(#((struct point) : 1 2)
|
||||||
#((struct point) : 3 4)))
|
#((struct point) : 3 4)))
|
||||||
(var p _ (¤ pts 1))
|
(var p _ [pts 1])
|
||||||
(printf "aggregate element: %d %d\n" (. p x) (. p y))
|
(printf "aggregate element: %d %d\n" (. p x) (. p y))
|
||||||
|
|
||||||
(var names (¤ (* const char) 2) #("hi" "there"))
|
(var names [(* const char) 2] #("hi" "there"))
|
||||||
(var s _ (¤ names 1))
|
(var s _ [names 1])
|
||||||
(printf "pointer element: %s\n" s)
|
(printf "pointer element: %s\n" s)
|
||||||
|
|
||||||
(var nums (¤ unsigned int 3) #(7 8 9))
|
(var nums [unsigned int 3] #(7 8 9))
|
||||||
(var u _ (¤ nums 2))
|
(var u _ [nums 2])
|
||||||
(printf "multi-word element: %u\n" u)
|
(printf "multi-word element: %u\n" u)
|
||||||
|
|
||||||
;; subscripting a pointer answers the same as subscripting an array
|
;; subscripting a pointer answers the same as subscripting an array
|
||||||
(var q (* (struct point)) (& (¤ pts 0)))
|
(var q (* (struct point)) (& [pts 0]))
|
||||||
(var r _ (¤ q 1))
|
(var r _ [q 1])
|
||||||
(printf "through a pointer: %d\n" (+ (. r x) (* 13 (. r y))))
|
(printf "through a pointer: %d\n" (+ (. r x) (* 13 (. r y))))
|
||||||
|
|
||||||
;; the last word of an unsized array's type is not its bound: neither
|
;; the last word of an unsized array's type is not its bound: neither
|
||||||
;; a typedef name nor the target of a `*' can be one
|
;; a typedef name nor the target of a `*' can be one
|
||||||
(var tail (¤ const small) #(1 2))
|
(var tail [const small] #(1 2))
|
||||||
(printf "unsized of a typedef: %d %d\n" (¤ tail 0) (¤ tail 1))
|
(printf "unsized of a typedef: %d %d\n" [tail 0] [tail 1])
|
||||||
|
|
||||||
(var one size-t 5)
|
(var one size-t 5)
|
||||||
(var sizes (¤ * size-t) #((& one)))
|
(var sizes [* size-t] #((& one)))
|
||||||
(var w _ (¤ sizes 0))
|
(var w _ [sizes 0])
|
||||||
(printf "unsized of a pointer: %d\n" (cast (* w) int))
|
(printf "unsized of a pointer: %d\n" (cast (* w) int))
|
||||||
|
|
||||||
|
;; ...but after a whole type, a name is: `size-t' is a typedef only C
|
||||||
|
;; knows of, and `(* const char)' a type grouped into one element
|
||||||
|
(var ws [size-t CAP] #(3 4))
|
||||||
|
(var gs [(* const char) CAP] #("a" "b"))
|
||||||
|
(printf "named bound: %zu %zu %s\n"
|
||||||
|
(/ (sizeof ws) (sizeof [ws 0])) [ws 1] [gs 1])
|
||||||
|
|
||||||
;; the same question in type position: `(fn ((unsigned int)) int)'
|
;; the same question in type position: `(fn ((unsigned int)) int)'
|
||||||
;; takes one parameter, not two
|
;; takes one parameter, not two
|
||||||
(var fp (fn ((unsigned int)) int) width)
|
(var fp (fn ((unsigned int)) int) width)
|
||||||
(printf "unnamed parameters: %d %d %d\n" (fp 7) (sign #\a) (twice 1))
|
(printf "unnamed parameters: %d %d %d\n" (fp 7) (sign #\a) (twice 1))
|
||||||
|
|
||||||
|
;; a typedef's target is every one of those shapes as well
|
||||||
|
(var greeting cstr "hi")
|
||||||
|
(var v vec4 #(1.0 2.0 3.0 4.0))
|
||||||
|
(var op unary twice)
|
||||||
|
(printf "typedefs: %s %zu %d\n" greeting (/ (sizeof v) (sizeof [v 0])) (op 1))
|
||||||
|
|
||||||
|
;; `(* T)' is a type or a dereference, and only what is in scope says
|
||||||
|
(printf "sizeof a pointer type: %d %d\n"
|
||||||
|
(== (sizeof (* const char)) (sizeof greeting))
|
||||||
|
(== (sizeof (* greeting)) 1))
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
(input)
|
(input)
|
||||||
(output "café 日本語 🍺" "café1" "naïve: 3")
|
(output "café 日本語 🍺" "café1" "naïve: 3" "predicate: 1 0")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; Non-ASCII string literals and identifiers.
|
;;; Non-ASCII string literals and identifiers.
|
||||||
@@ -13,12 +13,22 @@
|
|||||||
;;; non-ASCII characters must survive unkebabify intact -- getting this
|
;;; non-ASCII characters must survive unkebabify intact -- getting this
|
||||||
;;; wrong truncates the name silently, and the program still compiles
|
;;; wrong truncates the name silently, and the program still compiles
|
||||||
;;; and runs, just under a different name than the one written.
|
;;; and runs, just under a different name than the one written.
|
||||||
|
;;;
|
||||||
|
;;; `?' and `!' end Lisp predicates and mutators, and C takes neither:
|
||||||
|
;;; they become the letters ʔ and ꜝ.
|
||||||
|
|
||||||
(include stdio.h)
|
(include stdio.h)
|
||||||
|
|
||||||
|
(fn zero? ((n int)) bool (return (== n 0)))
|
||||||
|
(fn clear! ((n (* int))) void (= (* n) 0))
|
||||||
|
|
||||||
(pub fn main () int
|
(pub fn main () int
|
||||||
(puts "café 日本語 🍺")
|
(puts "café 日本語 🍺")
|
||||||
(puts "café1")
|
(puts "café1")
|
||||||
(var naïve-count int 3)
|
(var naïve-count int 3)
|
||||||
(printf "naïve: %d\n" naïve-count)
|
(printf "naïve: %d\n" naïve-count)
|
||||||
|
(var n int 5)
|
||||||
|
(var before bool (zero? n))
|
||||||
|
(clear! (& n))
|
||||||
|
(printf "predicate: %d %d\n" (zero? n) before)
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -25,8 +25,8 @@
|
|||||||
(fn origin-x ((p (* (struct point)))) int
|
(fn origin-x ((p (* (struct point)))) int
|
||||||
(return (. (* p) x)))
|
(return (. (* p) x)))
|
||||||
|
|
||||||
(fn second-of ((xs (¤ float 4))) float
|
(fn second-of ((xs [float 4])) float
|
||||||
(return (¤ xs 1)))
|
(return [xs 1]))
|
||||||
|
|
||||||
(fn say ((fmt (* const char)) ...) void
|
(fn say ((fmt (* const char)) ...) void
|
||||||
(var ap va-list)
|
(var ap va-list)
|
||||||
@@ -43,8 +43,8 @@
|
|||||||
(var f (fn ((* (struct point))) int) origin-x)
|
(var f (fn ((* (struct point))) int) origin-x)
|
||||||
(printf "aggregate: %d\n" (f (& p)))
|
(printf "aggregate: %d\n" (f (& p)))
|
||||||
|
|
||||||
(var xs (¤ float 4) #(1.5 2.5 3.5 4.5))
|
(var xs [float 4] #(1.5 2.5 3.5 4.5))
|
||||||
(var g (fn ((¤ float 4)) float) second-of)
|
(var g (fn ([float 4]) float) second-of)
|
||||||
(printf "array: %g\n" (g xs))
|
(printf "array: %g\n" (g xs))
|
||||||
|
|
||||||
(say "variadic: %d %s\n" 1 "two")
|
(say "variadic: %d %s\n" 1 "two")
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
"loop: 0 1 2"
|
"loop: 0 1 2"
|
||||||
"shadowed: 9 then 42"
|
"shadowed: 9 then 42"
|
||||||
"still an int: 200"
|
"still an int: 200"
|
||||||
|
"case scope: 200"
|
||||||
"partial: 1 2.5"
|
"partial: 1 2.5"
|
||||||
"joined: 43.5 84 49 1"
|
"joined: 43.5 84 49 1"
|
||||||
"promoted: 200 60000 3705032704"
|
"promoted: 200 60000 3705032704"
|
||||||
@@ -44,11 +45,11 @@
|
|||||||
(var pp _ (& p))
|
(var pp _ (& p))
|
||||||
(printf "pointers: %d %g\n" (. pp x) (. pp y))
|
(printf "pointers: %d %g\n" (. pp x) (. pp y))
|
||||||
|
|
||||||
(var table (¤ int 3))
|
(var table [int 3])
|
||||||
(= (¤ table 0) 10)
|
(= [table 0] 10)
|
||||||
(= (¤ table 1) 20)
|
(= [table 1] 20)
|
||||||
(var first _ (¤ table 0))
|
(var first _ [table 0])
|
||||||
(var second _ (¤ table 1))
|
(var second _ [table 1])
|
||||||
(printf "arrays: %d\n" (+ first second))
|
(printf "arrays: %d\n" (+ first second))
|
||||||
|
|
||||||
;; a for opens a scope, and its initializer is declared inside it
|
;; a for opens a scope, and its initializer is declared inside it
|
||||||
@@ -67,14 +68,18 @@
|
|||||||
(var wide int 200)
|
(var wide int 200)
|
||||||
(while false (var wide char 1) (printf "%d" wide))
|
(while false (var wide char 1) (printf "%d" wide))
|
||||||
(if false (do (var wide char 1) (printf "%d" wide)))
|
(if false (do (var wide char 1) (printf "%d" wide)))
|
||||||
;; a statement before the declaration: a label may not be followed by
|
(switch a (case 1 (var wide char 1) (printf "%d" wide)))
|
||||||
;; one until C23
|
|
||||||
(switch a (case 1 (printf "") (var wide char 1) (printf "%d" wide) (break)))
|
|
||||||
;; a copy, not a sum: an arithmetic result would be promoted to `int'
|
;; a copy, not a sum: an arithmetic result would be promoted to `int'
|
||||||
;; whatever leaked, and say nothing
|
;; whatever leaked, and say nothing
|
||||||
(var copy _ wide)
|
(var copy _ wide)
|
||||||
(printf "still an int: %d\n" copy)
|
(printf "still an int: %d\n" copy)
|
||||||
|
|
||||||
|
;; each case is a block of its own: the second sees the outer `wide',
|
||||||
|
;; not the first one's
|
||||||
|
(switch 2
|
||||||
|
(case 1 (var wide double 1.5) (printf "%g" wide))
|
||||||
|
(case 2 (var seen _ wide) (printf "case scope: %d\n" seen)))
|
||||||
|
|
||||||
;; a wildcard inside a written type: only it is solved
|
;; a wildcard inside a written type: only it is solved
|
||||||
(var pp2 (* _) (& p))
|
(var pp2 (* _) (& p))
|
||||||
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))
|
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))
|
||||||
@@ -103,7 +108,7 @@
|
|||||||
;; a brace initializer has no type of its own, but its elements solve
|
;; a brace initializer has no type of its own, but its elements solve
|
||||||
;; the hole in the array type around it -- and the length stays as
|
;; the hole in the array type around it -- and the length stays as
|
||||||
;; written, whether or not every slot is initialized
|
;; written, whether or not every slot is initialized
|
||||||
(var squares (¤ _ 4) #(0 1 4 9))
|
(var squares [_ 4] #(0 1 4 9))
|
||||||
(var sparse (¤ _ 8) #(0 1))
|
(var sparse [_ 8] #(0 1))
|
||||||
(printf "from elements: %d %d %d\n" (¤ squares 2) (¤ squares 3) (¤ sparse 7))
|
(printf "from elements: %d %d %d\n" [squares 2] [squares 3] [sparse 7])
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -40,7 +40,6 @@
|
|||||||
|
|
||||||
type-head?
|
type-head?
|
||||||
named-arg?
|
named-arg?
|
||||||
typedef-name?
|
|
||||||
array-bound?
|
array-bound?
|
||||||
array-element-type)
|
array-element-type)
|
||||||
"../types.scm")
|
"../types.scm")
|
||||||
|
|||||||
@@ -139,6 +139,10 @@
|
|||||||
(test "enum values are fields whose type is the enum"
|
(test "enum values are fields whose type is the enum"
|
||||||
'((red (enum t-color)) (green (enum t-color)) (blue (enum t-color)))
|
'((red (enum t-color)) (green (enum t-color)) (blue (enum t-color)))
|
||||||
(map-fields 't-color (lambda (name type) (list name type))))
|
(map-fields 't-color (lambda (name type) (list name type))))
|
||||||
|
(add-enum 't-wire '(enum t-wire ((t-varint 0) (t-fixed64 1) t-bytes)))
|
||||||
|
(test "an enumerator with a value is still named by its name"
|
||||||
|
'(t-varint t-fixed64 t-bytes)
|
||||||
|
(map-fields 't-wire (lambda (name type) name)))
|
||||||
(test "a typedef has no fields to map"
|
(test "a typedef has no fields to map"
|
||||||
#f
|
#f
|
||||||
(map-fields 't-u8 (lambda (name type) name)))
|
(map-fields 't-u8 (lambda (name type) name)))
|
||||||
@@ -217,4 +221,24 @@
|
|||||||
(get-name-type 't-never-declared))
|
(get-name-type 't-never-declared))
|
||||||
(test "nor a return type"
|
(test "nor a return type"
|
||||||
#f
|
#f
|
||||||
(get-return-type 't-never-declared)))
|
(get-return-type 't-never-declared))
|
||||||
|
|
||||||
|
;; Where an array's element type ends is a matter of shape, not of
|
||||||
|
;; what the database happens to hold: a typedef from a C header is
|
||||||
|
;; never in it
|
||||||
|
(test-group "array bounds"
|
||||||
|
(test-assert "a number" (array-bound? '(¤ int 4)))
|
||||||
|
(test-assert "a name after a specifier" (array-bound? '(¤ int N)))
|
||||||
|
(test-assert "a name after a type name unknown to Sex"
|
||||||
|
(array-bound? '(¤ pthread-t N)))
|
||||||
|
(test-assert "a name after a grouped type"
|
||||||
|
(array-bound? '(¤ (* t-point) N)))
|
||||||
|
(test-assert "a name after a tag" (array-bound? '(¤ struct t-point N)))
|
||||||
|
(test-assert "not a lone type" (not (array-bound? '(¤ pthread-t))))
|
||||||
|
(test-assert "not a specifier" (not (array-bound? '(¤ unsigned int))))
|
||||||
|
(test-assert "not a qualified name" (not (array-bound? '(¤ const t))))
|
||||||
|
(test-assert "not a pointer's target" (not (array-bound? '(¤ * size-t))))
|
||||||
|
(test-assert "not a tag" (not (array-bound? '(¤ struct t-point))))
|
||||||
|
(test "the element of a grouped type is the group"
|
||||||
|
'(* t-point)
|
||||||
|
(array-element-type '(¤ (* t-point) N)))))
|
||||||
|
|||||||
@@ -40,7 +40,6 @@
|
|||||||
|
|
||||||
type-head?
|
type-head?
|
||||||
named-arg?
|
named-arg?
|
||||||
typedef-name?
|
|
||||||
array-bound?
|
array-bound?
|
||||||
array-element-type)
|
array-element-type)
|
||||||
"types.scm")
|
"types.scm")
|
||||||
|
|||||||
34
types.scm
34
types.scm
@@ -304,7 +304,8 @@
|
|||||||
(else (equal? pattern type))))
|
(else (equal? pattern type))))
|
||||||
|
|
||||||
;;; Map function to each field/value of a structure/union/enum
|
;;; Map function to each field/value of a structure/union/enum
|
||||||
;;; For enums, field-type is the type of the enum (since C 23)
|
;;; For enums, field-type is the type of the enum (since C 23), and
|
||||||
|
;;; field-name the enumerator's name, (wire-varint 0) or not
|
||||||
;;; (map-fields type-name
|
;;; (map-fields type-name
|
||||||
;;; (lambda (field-name field-type) ...))
|
;;; (lambda (field-name field-type) ...))
|
||||||
;;;
|
;;;
|
||||||
@@ -320,7 +321,8 @@
|
|||||||
;; declared, since `enum some-typedef' is not a C type.
|
;; declared, since `enum some-typedef' is not a C type.
|
||||||
((enum)
|
((enum)
|
||||||
(let ((type (list 'enum (cadr info))))
|
(let ((type (list 'enum (cadr info))))
|
||||||
(map (lambda (value) (fn value type))
|
(map (lambda (value)
|
||||||
|
(fn (if (pair? value) (car value) value) type))
|
||||||
(caddr info))))
|
(caddr info))))
|
||||||
(else #f)))))
|
(else #f)))))
|
||||||
|
|
||||||
@@ -357,22 +359,15 @@
|
|||||||
(pair? (cdr arg)) ; 1 element args are always type
|
(pair? (cdr arg)) ; 1 element args are always type
|
||||||
(not (type-head? arg))))
|
(not (type-head? arg))))
|
||||||
|
|
||||||
;;; A typedef and a `define' share +type-db+; only the typedef is part
|
;;; The last element is a bound unless what precedes it is still
|
||||||
;;; of a type:
|
;;; waiting for a type name -- a `*', a qualifier, a tag's keyword. C
|
||||||
;;;
|
;;; spells a type with one name at most, so a name after a name, or
|
||||||
;;; (typedef small int) -> (¤ small N) is N of small
|
;;; after a grouped type, can only be the bound:
|
||||||
;;; (define CAP 4) -> (¤ int CAP) is CAP of int
|
|
||||||
(define (typedef-name? name)
|
|
||||||
(let ((info (and (symbol? name) (get-type-info name))))
|
|
||||||
(and info (memq (car info) '(typedef struct union enum)) #t)))
|
|
||||||
|
|
||||||
;;; The last element is a bound only where what precedes it already
|
|
||||||
;;; spells a whole type -- a specifier, a tag after its keyword, or a
|
|
||||||
;;; typedef we have seen declared:
|
|
||||||
;;;
|
;;;
|
||||||
;;; (¤ int 4) four of int (¤ unsigned int) unsized
|
;;; (¤ int 4) four of int (¤ unsigned int) unsized
|
||||||
;;; (¤ mytype CAP) CAP of mytype (¤ struct point) unsized
|
;;; (¤ pthread-t CAP) CAP of pthread-t (¤ struct point) unsized
|
||||||
;;; (¤ const mytype) unsized (¤ * size-t) unsized
|
;;; (¤ (* T) CAP) CAP of (* T) (¤ const mytype) unsized
|
||||||
|
;;; (¤ * size-t) unsized
|
||||||
;;;
|
;;;
|
||||||
;;; TYPE is the whole `(¤ ...)' form.
|
;;; TYPE is the whole `(¤ ...)' form.
|
||||||
(define (array-bound? type)
|
(define (array-bound? type)
|
||||||
@@ -382,11 +377,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 +c-specifiers+) #t)
|
(else (not (or (memq preceding '(* struct union enum))
|
||||||
;; a tag always follows its keyword, so `(¤ * struct tt)' ends
|
(memq preceding +c-qualifiers+))))))))
|
||||||
;; in a name belonging to the type
|
|
||||||
((memq preceding '(struct union enum)) #f)
|
|
||||||
(else (typedef-name? preceding))))))
|
|
||||||
|
|
||||||
;;; 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user