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:
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 \
|
||||||
|
|||||||
@@ -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)
|
||||||
@@ -805,32 +806,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 +872,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))
|
||||||
|
|||||||
@@ -114,7 +114,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
|
||||||
|
|||||||
28
tests/sex-programs/precedence.sex
Normal file
28
tests/sex-programs/precedence.sex
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
(input)
|
||||||
|
(output "conditional operand: 75"
|
||||||
|
"cast as object of .: 7"
|
||||||
|
"cast of bitwise: 1"
|
||||||
|
"cast of conditional: 44")
|
||||||
|
(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.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(record wanted ((distance int)))
|
||||||
|
|
||||||
|
(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))
|
||||||
|
(return 0))
|
||||||
Reference in New Issue
Block a user