1
0
forked from alex-eg/sex

refuse a match in a control form's test

An if, while or for walked every slot as a statement, so a match in
its test came out as a switch inside the condition, which cc refuses.
This commit is contained in:
2026-10-15 11:15:19 +03:00
parent 0a565046af
commit 0a5eb1e0ce
2 changed files with 44 additions and 3 deletions

View File

@@ -546,8 +546,9 @@
;;;
;;; (var n int (match s ...)) -> int n = switch (s.tag) { ...
;;;
;;; A condition is not tracked: `walk-parts' takes every slot of an `if'
;;; alike.
;;; and in a control form's test, which C reads as an expression too:
;;;
;;; (if (match s ...) ...) -> if (switch (s.tag) { ...
(define operand-position? (make-parameter #f))
(define (walk-operand form env)
@@ -583,10 +584,13 @@
(cons walk-embed-result (walk-body expansion env)))))
(else
(case (car form)
((do for while if switch)
((do)
(parameterize ((operand-position? #f))
(with-scope env (lambda () (walk-parts form env)))))
((for while if switch)
(with-scope env (lambda () (walk-control form env))))
((case case/fallthrough default)
(parameterize ((operand-position? #f))
(walk-parts form env)))
@@ -874,6 +878,29 @@
(define (walk-parts form env)
(copy-form-source! form (walk-body form env)))
;;; A control form, its tests walked as values and the rest as
;;; statements
(define (walk-control form env)
(let ((slots (count (lambda (part) (not (comment-form? part))) (cdr form))))
(let loop ((parts (cdr form)) (i 0) (acc (list (car form))))
(cond
((null? parts) (copy-form-source! form (reverse acc)))
((comment-form? (car parts)) (loop (cdr parts) i (cons (car parts) acc)))
(else
(let ((walked (parameterize ((operand-position?
(test-slot? (car form) i slots)))
(walk-body (list (car parts)) env))))
(loop (cdr parts) (+ i 1) (append-reverse walked acc))))))))
;;; Whether the Ith of a control form's SLOTS is read as a value: an
;;; `if''s tests but not its trailing else, a `for''s header, the
;;; scrutinee of a `while' or a `switch'
(define (test-slot? head i slots)
(case head
((if) (and (even? i) (< (+ i 1) slots)))
((for) (< i 3))
(else (= i 0))))
;;; (match s ((circle (r)) ...) ((empty ()) ...)) is a switch over the
;;; tag, each arm a block binding that variant's fields as locals. `_'
;;; drops one. An arm the sum does not have, a wrong number of

View File

@@ -432,6 +432,20 @@
(test-assert "not even as an argument"
(reports? (sum-src "(= out (match s ((circle (r)) r) (else 0.0)))")
"match is a statement"))
(test-assert "nor as an if's test"
(reports? (sum-src "(if (match s ((circle (r)) 1) (else 0)) (= out 1.0))")
"match is a statement"))
(test-assert "nor as a while's"
(reports? (sum-src "(while (match s ((circle (r)) 1) (else 0)) (= out 1.0))")
"match is a statement"))
(test-assert "nor in a for's header"
(reports? (sum-src "(for (var i int 0) (match s ((circle (r)) 1) (else 0)) (++ i)"
" (= out 1.0))")
"match is a statement"))
(test-assert "but an if's branches are statements, the else among them"
(emits? (sum-src "(if (== out 0.0) (match s ((circle (r)) (= out r)) (else (= out 1.0)))"
" (match s ((circle (r)) (= out 2.0)) (else (= out 3.0))))")
"out = 3.0;"))
(test-assert "but a case body is a statement"
(emits? (sum-src "(switch 1 (case 1 (match s ((circle (r)) (= out r))"
" (else (= out 0.0)))))")