From e29c6ee5c93acb7cebbc109141bef2d4769cbee0 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 9 Oct 2026 18:03:21 +0300 Subject: [PATCH] parenthesise what a subscript subscripts The array was written at whatever level surrounded it, so [(* q) i] came out *q[i] -- *(q[i]) to C, compiling with q an int ** and reading the wrong int. The index now starts afresh too: inside a cast it had taken the cast's level and gained parens, and so had a chain in the else arm of ?:, which groups to the right and needs none. --- sex-fmt-c.scm | 26 ++++++++++++++++++-------- tests/codegen.scm | 25 ++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/sex-fmt-c.scm b/sex-fmt-c.scm index e9ae69a..215617a 100644 --- a/sex-fmt-c.scm +++ b/sex-fmt-c.scm @@ -97,6 +97,7 @@ ;;((|\|\||) 75) ;;((.. ...) 77) ; Perl ((?) 80) + ((conditional-else) 81) ; see c-if/expr ((= *= /= %= &= ^= <<= >>=) 85) ; |\|=| ; += -= ((comma) 90) ((=>) 90) ; Perl @@ -111,6 +112,13 @@ (define (c-paren x) (cat "(" (c-expr x) ")")) + ;; MODIFIED FROM UPSTREAM fmt-c: a subscript is postfix, tighter than + ;; any operator, so what it subscripts is written at the level of + ;; `.' -- (* q) comes out `(*q)[i]', not `*q[i]', which is *(q[i]). + ;; The index has brackets of its own and starts afresh. + (define (c-subscript array index) + (cat (c-with-op "." (c-expr array)) "[" (c-with-op 'paren (c-expr index)) "]")) + ;; `.' and `->' associate left, and their right operand is always a ;; name, so a member access written inside another is the left one ;; and needs no parens of its own: `s.u.circle.r', not `((s.u).circle).r' @@ -249,12 +257,9 @@ ;; non C-keywords must have some character invalid in a C ;; identifier to avoid conflicts - by default we prefix % ((vector-ref) - ((c-wrap-stmt - (cat (c-expr (cadr x)) "[" (c-expr (caddr x)) "]")) - st)) + ((c-wrap-stmt (c-subscript (cadr x) (caddr x))) st)) ((vector-set!) - ((c= (c-in-expr - (cat (c-expr (cadr x)) "[" (c-expr (caddr x)) "]")) + ((c= (c-in-expr (c-subscript (cadr x) (caddr x))) (c-expr (cadddr x))) st)) ((extern/C) ((apply c-extern/C (cdr x)) st)) @@ -885,6 +890,10 @@ ;; 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. + ;; + ;; It groups to the right, so its else arm is written at + ;; `conditional-else', just looser: a chain there stays bare, + ;; `a ? x : b ? y : z', and an assignment does not. (define (c-if/expr . args) (c-maybe-paren '? (apply c-if/expr/aux args))) @@ -897,9 +906,10 @@ (c-expr p))) ((pair? ls) (cat (c-in-test (c-expr c)) " ? " (c-expr p) " : " - (if (pair? (cdr ls)) - (lp (car ls) (cadr ls) (cddr ls)) - (lp 'else (car ls) '())))) + (c-with-op 'conditional-else + (if (pair? (cdr ls)) + (lp (car ls) (cadr ls) (cddr ls)) + (lp 'else (car ls) '()))))) (else (c-or (c-in-test (c-expr c)) (c-expr p)))))) diff --git a/tests/codegen.scm b/tests/codegen.scm index d2dcf53..2b5847a 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -167,7 +167,30 @@ whitespace aside, THEN?" "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)"))) + "g(a ? a : b, b)")) + ;; ?: groups to the right, so a chain needs none in its else arm + (test-assert "a chain in the else arm is left bare" + (emits? (in-fn "(var x int (if a a (if b b 0)))") + "a ? a : b ? b : 0")) + (test-assert "but an assignment there is not" + (emits? (in-fn "(var x int (if a a (= b 1)))") + "a ? a : (b = 1)"))) + + ;; A subscript binds tighter than anything, so what it subscripts is + ;; parenthesised whenever it is an operator: `*q[i]' is *(q[i]) + (test-group "subscript precedence" + (test-assert "a dereference" + (emits? "(fn f ((q (* * int)) (i int)) int (return [(* q) i]))" "(*q)[i]")) + (test-assert "a sum" + (emits? "(fn f ((p (* int))) int (return [(+ p 1) 0]))" "(p + 1)[0]")) + (test-assert "a cast" + (emits? "(fn f ((v (* void))) int (return [(cast v (* int)) 0]))" "((int *)v)[0]")) + (test-assert "a path is left bare" + (emits? (in-line "(var x int [(. p x) 0])") "p.x[0]")) + ;; ...while the index is in brackets of its own + (test-assert "an index is never parenthesised" + (emits? "(fn f ((p (* int)) (i int)) int (return (cast [p (+ i 1)] int)))" + "(int)p[i + 1]"))) ;; 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