more-tests-and-corner-cases #72

Merged
pkulev merged 74 commits from more-tests-and-corner-cases into main 2026-10-27 22:21:02 +01:00
2 changed files with 19 additions and 4 deletions
Showing only changes of commit 65aaf69835 - Show all commits

View File

@@ -917,16 +917,20 @@
(copy-form-source! form (walk-body form env))) (copy-form-source! form (walk-body form env)))
;;; A control form, its tests walked as values and the rest as ;;; A control form, its tests walked as values and the rest as
;;; statements ;;; statements -- but an if that is a value has values for branches,
;;; and one for the false case too, or C has none to give it
(define (walk-control form env) (define (walk-control form env)
(let ((slots (count (lambda (part) (not (comment-form? part))) (cdr form)))) (let ((slots (count (lambda (part) (not (comment-form? part))) (cdr form)))
(valued? (and (eq? (car form) 'if) (operand-position?))))
(when (and valued? (even? slots))
(sex-error form "an if used as a value needs an else"))
(let loop ((parts (cdr form)) (i 0) (acc (list (car form)))) (let loop ((parts (cdr form)) (i 0) (acc (list (car form))))
(cond (cond
((null? parts) (copy-form-source! form (reverse acc))) ((null? parts) (copy-form-source! form (reverse acc)))
((comment-form? (car parts)) (loop (cdr parts) i (cons (car parts) acc))) ((comment-form? (car parts)) (loop (cdr parts) i (cons (car parts) acc)))
(else (else
(let ((walked (parameterize ((operand-position? (let ((walked (parameterize ((operand-position?
(test-slot? (car form) i slots))) (or valued? (test-slot? (car form) i slots))))
(walk-body (list (car parts)) env)))) (walk-body (list (car parts)) env))))
(loop (cdr parts) (+ i 1) (append-reverse walked acc)))))))) (loop (cdr parts) (+ i 1) (append-reverse walked acc))))))))

View File

@@ -109,7 +109,18 @@
"a ? a : b ? b : 0")) "a ? a : b ? b : 0"))
(test-assert "but an assignment there is not" (test-assert "but an assignment there is not"
(emits? (in-fn "(var x int (if a a (= b 1)))") (emits? (in-fn "(var x int (if a a (= b 1)))")
"a ? a : (b = 1)"))) "a ? a : (b = 1)"))
;; with nothing for the false case it came out `a || 1', a boolean
(test-assert "a value needs an else"
(reports? (in-fn "(var x int (if a 1))") "an if used as a value needs an else"))
(test-assert "so does a chain"
(reports? (in-fn "(var x int (if a 1 b 2))") "an if used as a value needs an else"))
(test-assert "and an if in a value's branch"
(reports? (in-fn "(var x int (if a 1 (if b 2)))") "an if used as a value needs an else"))
(test-assert "a chain with one is a chain"
(emits? (in-fn "(var x int (if a 1 b 2 3))") "a ? 1 : b ? 2 : 3"))
(test-assert "and a statement needs none"
(emits? (in-fn "(if a (g a))") "if (a)")))
;; A subscript binds tighter than anything, so what it subscripts is ;; A subscript binds tighter than anything, so what it subscripts is
;; parenthesised whenever it is an operator: `*q[i]' is *(q[i]) ;; parenthesised whenever it is an operator: `*q[i]' is *(q[i])