1
0
forked from alex-eg/sex
Files
sex/tests/sex-programs/precedence.sex
alex-eg 8bc27cba26 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.
2026-10-19 16:04:51 +03:00

29 lines
870 B
Scheme

(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))