From bc1b5b69d4ce93501922257ecb1e670ee4196c01 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 15:26:49 +0300 Subject: [PATCH] hold operators to the operands they take Past them an operator was written as it came: (/ a) as /a, (! a b) as a ! b. (++ a b) steps each in turn, through a comma operator the writer lacked: bare in a for header, and in parens wherever else, since bare it would begin another declarator or argument. --- fmt-c-writer.scm | 45 ++++++++++++++++++++++++++++++++++++++------- sex-fmt-c.scm | 21 +++++++++++++++++++-- tests/codegen.scm | 29 ++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 2cf153d..3098cbb 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -294,15 +294,46 @@ forms, and what remains." ;; Drop comments so they will not generate additional comma (else (check-arity form) - (map walk-expr (remove comment-form? form))))) + (let ((form (remove comment-form? form))) + (if (and (memq (car form) '(++ --)) (pair? (cdr form)) (pair? (cddr form))) + ;; (++ a b) steps each in turn: statements where it is one, + ;; C's comma where it is a value + (cons '%comma (map (lambda (operand) (walk-expr (list (car form) operand))) + (cdr form))) + (map walk-expr form)))))) + +(define +comparisons+ '(< > <= >= == !=)) + +;;; The operands each operator takes, as (fewest . most), most #f for no +;;; limit. Past them an operator was written as it came: (/ a) as `/a', +;;; (! a b) as `a ! b', (< a b c) as C's own chain, which compares a +;;; boolean with c +(define +operator-arity+ + `((,+comparisons+ 2 . 2) + ((+= -= *= /= %= &= ^= |\|=| <<= >>=) 2 . 2) + ((! ~ bit-not bitwise-not) 1 . 1) + ((- + * & ++ --) 1 . #f) + ((/ % << >> && |\|\|| ^ |\|| = bit-and bit-or bit-xor + bitwise-and bitwise-ior bitwise-xor) + 2 . #f))) + +(define (operator-arity head) + (find (lambda (entry) (memq head (car entry))) +operator-arity+)) -;;; Operands past what an operator takes were written as they came: -;;; (< a b c) as C's own chain, which compares a boolean with c (define (check-arity form) - (when (and (memq (car form) '(< > <= >= == !=)) - (not (= (count (lambda (part) (not (comment-form? part))) (cdr form)) 2))) - (sex-error form "a comparison takes two operands; chain them with &&" - (car form)))) + (let ((entry (operator-arity (car form))) + (given (count (lambda (part) (not (comment-form? part))) (cdr form)))) + (when entry + (let ((fewest (cadr entry)) + (most (cddr entry))) + (unless (and (>= given fewest) (or (not most) (<= given most))) + (sex-error form + (if (eq? (car entry) +comparisons+) + "a comparison takes two operands; chain them with &&" + (string-append "takes " + (if (= fewest 1) "one operand" "two operands") + (if most "" " or more"))) + (car form))))))) ;;; #(a b c) is a brace initializer. A `:' inside one ends a type and ;;; turns the whole thing into a C99 compound literal: diff --git a/sex-fmt-c.scm b/sex-fmt-c.scm index 8f22397..f827a68 100644 --- a/sex-fmt-c.scm +++ b/sex-fmt-c.scm @@ -304,6 +304,7 @@ ((%prototype) ((apply c-prototype (cdr x)) st)) ((%var) ((apply c-var (cdr x)) st)) ((%begin) ((apply c-begin (cdr x)) st)) + ((%comma) ((apply c-comma (cdr x)) st)) ((%attribute) ((apply c-attribute (cdr x)) st)) ((%line) ((apply cpp-line (cdr x)) st)) ((%pragma %error %warning) @@ -628,6 +629,19 @@ (define (c-begin . args) (apply c-begin/aux #f args)) + ;; MODIFIED FROM UPSTREAM fmt-c, which has no comma operator: as a + ;; statement, statements; as a value, the expressions in parens, since + ;; bare they would be an initializer's next declarator or a call's next + ;; argument -- except where a for header says it may stand bare. + (define (c-comma . exprs) + (lambda (st) + ((if (fmt-expression? st) + (let ((joined (fmt-join (lambda (e) (c-with-op 'comma (c-expr e))) + exprs ", "))) + (if (eq? (fmt-op st) 'comma) joined (cat "(" joined ")"))) + (apply c-begin exprs)) + st))) + (define (c-begin/aux ret? body0 . body) (if (null? body) (c-expr body0) @@ -714,13 +728,16 @@ (c-in-stmt (apply c-begin body))) fl)))) + ;; MODIFIED FROM UPSTREAM fmt-c: a header's first and last slots are + ;; the one place a comma expression goes bare, so they say so. (define (c-for init check update . body) (c-reset-newline (cat (c-block (c-in-expr - (cat "for (" (c-expr init) "; " (c-in-test (c-expr check)) "; " - (c-expr update ) ")")) + (cat "for (" (c-with-op 'comma (c-expr init)) "; " + (c-in-test (c-expr check)) "; " + (c-with-op 'comma (c-expr update)) ")")) (c-in-stmt (apply c-begin body))) fl))) diff --git a/tests/codegen.scm b/tests/codegen.scm index 55e1269..123be04 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -781,7 +781,34 @@ whitespace aside, THEN?" (test-assert "and not one" (reports? (in-fn "(var x bool (>= a))") "a comparison takes two operands")) (test-assert "two is a comparison" - (emits? (in-fn "(var x bool (<= a b))") "x = a <= b;"))) + (emits? (in-fn "(var x bool (<= a b))") "x = a <= b;")) + ;; ...and (/ a) as `/a', (! a b) as `a ! b' + (test-assert "a division takes two or more" + (reports? (in-fn "(var x int (/ a))") "takes two operands or more")) + (test-assert "so does &&" + (reports? (in-fn "(var x bool (&& a))") "takes two operands or more")) + (test-assert "a compound assignment takes two" + (reports? (in-fn "(+= a b a)") "takes two operands")) + (test-assert "a negation takes one" + (reports? (in-fn "(var x bool (! a b))") "takes one operand")) + (test-assert "a bit-not too" + (reports? (in-fn "(var x int (~ a b))") "takes one operand")) + (test-assert "minus takes one or more" + (reports? (in-fn "(var x int (-))") "takes one operand or more")) + (test-assert "and with more than one is still a subtraction" + (emits? (in-fn "(var x int (- a b a))") "x = a - b - a;")) + ;; ++ and -- step each operand in turn + (test-assert "a statement steps each operand" + (followed-by? (in-fn "(++ a b)") "++a;" "++b;")) + (test-assert "so does a for's step, which is one expression" + (emits? (in-fn "(for (var i int 0) (< i a) (++ i b) (g i))") "; ++i, ++b)")) + (test-assert "and a value is one too, in parens" + (emits? (in-fn "(var x int (-- a b))") "x = (--a, --b);")) + ;; a define's body is pasted, not walked, so it is looked at apart + (test-assert "a define's body is held to the same" + (reports? "(define BAD (< 1 2 3))" "a comparison takes two operands")) + (test-assert "and steps each operand the same way" + (emits? "(define (STEP a b) (++ a b))" "#define STEP(a, b) ((++(a), ++(b)))"))) ;; An array bound was taken only when it was an integer literal, so a ;; symbolic one fell into the type: `[int N]' came out `int N a[]'.