parenthesise casts and conditionals by precedence
Neither took part in it: a cast parenthesised its operand only from a list of binary heads that missed `|', and never itself, so `->' took it apart; `?:' was written bare under any operator, swallowing it into the test. Three of the four still compiled, to wrong values. Fixes #59, #61, #62.
This commit is contained in:
@@ -80,6 +80,7 @@
|
||||
((dot arrow post-decrement post-increment) 10)
|
||||
((**) 15) ; Perl
|
||||
((unary+ unary- ! ~ cast unary-* unary-& sizeof) 20) ; ++ --
|
||||
((cast-operand) 25) ; see c-cast
|
||||
((=~ !~) 25) ; Perl
|
||||
((* / %) 30)
|
||||
((+ -) 35)
|
||||
@@ -805,32 +806,22 @@
|
||||
(fmt-join 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
|
||||
;; unparenthesised (cat "(" type ")" expr) silently casts only the
|
||||
;; first operand: (cast (* 2 (sizeof GLfloat)) (* void)) came out as
|
||||
;; `(void *)2 * sizeof(GLfloat)'. Unary operands -- &x, *p, -n,
|
||||
;; sizeof, calls, identifiers, literals -- are already
|
||||
;; unary-expressions and are left alone, so this changes nothing that
|
||||
;; was previously correct.
|
||||
(define c-binary-operators
|
||||
'(+ - * / % << >> < > <= >= == != & ^ && %and %or
|
||||
= *= /= %= &= ^= <<= >>=))
|
||||
|
||||
(define (c-binary-expression? expr)
|
||||
(and (pair? expr)
|
||||
(memq (car expr) c-binary-operators)
|
||||
(pair? (cdr expr))
|
||||
(pair? (cddr expr)))) ; two or more operands
|
||||
|
||||
;; Upstream's bare (cat "(" type ")" expr) cast only the first operand
|
||||
;; of a looser one -- (cast (* 2 (sizeof GLfloat)) (* void)) came out
|
||||
;; `(void *)2 * sizeof(GLfloat)' -- and was itself taken apart by a
|
||||
;; tighter operator around it: `(const wanted *)pa->distance'.
|
||||
;;
|
||||
;; The operand is written at `cast-operand', just looser than a unary
|
||||
;; operator, so a unary operand stays bare -- `(int *)&a' -- while
|
||||
;; anything looser parenthesises itself. The cast is parenthesised
|
||||
;; only under something strictly tighter: `-(int)a', `*(int *)v'.
|
||||
(define (c-cast type expr)
|
||||
(cat "(" (c-type type) ")"
|
||||
(if (c-binary-expression? expr)
|
||||
;; c-with-op stops the operand from parenthesising itself
|
||||
;; a second time inside the parens we just added.
|
||||
(cat "(" (c-with-op 'paren (c-expr expr)) ")")
|
||||
(c-expr expr))))
|
||||
(lambda (st)
|
||||
(let ((cast (cat "(" (c-type type) ")"
|
||||
(c-with-op 'cast-operand (c-expr expr)))))
|
||||
((if (c-op< (fmt-op st) 'cast) (c-paren cast) cast) st))))
|
||||
|
||||
;; { a, b, c } -- on one line if it fits, one element per line if not.
|
||||
(define (c-braced-list ls)
|
||||
@@ -881,7 +872,14 @@
|
||||
tail))))
|
||||
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))
|
||||
(cond
|
||||
((or (eq? c 'else) (eq? c #t))
|
||||
|
||||
Reference in New Issue
Block a user