walk a define's body, and parenthesise it
The body was pasted as read, so `cast' stayed a call and names kept their dashes; and fmt-c parenthesised only a function-like body, so an object-like one regrouped with the operator it was pasted under. Fixes #55.
This commit is contained in:
@@ -507,7 +507,7 @@ forms, and what remains."
|
||||
;; A target is a filename and not an identifier: GL/gl.h is a path,
|
||||
;; and unkebabify would spell it GLːgl.h
|
||||
(('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))))
|
||||
|
||||
;;; Two toplevel forms are separated by a blank line only if they were
|
||||
|
||||
@@ -433,6 +433,15 @@
|
||||
((pair? x) (cons (replace (car x)) (replace (cdr 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 (name-of x) (c-expr (if (pair? x) (cadr x) x)))
|
||||
(lambda (st)
|
||||
@@ -450,9 +459,8 @@
|
||||
(cat " "
|
||||
(fmt-let 'writer (make-cpp-writer st)
|
||||
(fmt-let 'macro-params params
|
||||
((if (or (not (pair? x))
|
||||
(and (null? (cdr body))
|
||||
(c-literal? (car body))))
|
||||
((if (and (null? (cdr body))
|
||||
(c-atomic-body? (car body)))
|
||||
(lambda (x) x)
|
||||
c-paren)
|
||||
(c-in-expr (apply c-begin body))))))
|
||||
|
||||
@@ -703,6 +703,21 @@ compiles."
|
||||
(test-assert "nor one behind a pointer"
|
||||
(emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]")))
|
||||
|
||||
;; 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 "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"
|
||||
|
||||
@@ -2,18 +2,21 @@
|
||||
(output "conditional operand: 75"
|
||||
"cast as object of .: 7"
|
||||
"cast of bitwise: 1"
|
||||
"cast of conditional: 44")
|
||||
"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 the first and the last two still
|
||||
;;; compiled -- computing 4, 257 and 300.
|
||||
;;; 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)))
|
||||
@@ -25,4 +28,5 @@
|
||||
(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))
|
||||
|
||||
Reference in New Issue
Block a user