1
0
forked from alex-eg/sex

parenthesize a negative literal under a unary operator

A negative literal is one token in Sex and two in C, so (- -1) came out
--1, which C reads as decrementing a constant.
This commit is contained in:
2026-10-16 13:54:57 +03:00
parent 5cd931cb4e
commit 0852079827
2 changed files with 13 additions and 2 deletions

View File

@@ -1075,10 +1075,16 @@
;; Parenthesises the whole expression rather than the operand: the
;; other way round, `(. (* p) x)' came out `*(p).x', which C reads as
;; `*(p.x)'.
;; MODIFIED FROM UPSTREAM fmt-c: a negative literal is one token in
;; Sex and two in C, so (- -1) is -(-1), not the decrement --1.
(define (c-unary-op op x)
(c-wrap-stmt
(c-maybe-paren (unary-operator op)
(cat (display-to-string op) (c-expr x)))))
(cat (display-to-string op)
(if (and (number? x)
(char=? #\- (string-ref (number->string x) 0)))
(c-paren x)
(c-expr x))))))
;; some convenience definitions