refuse an if used as a value with no else
With nothing for the false case the writer made (if c 1) c || 1, a boolean; and a value if's branches were walked as statements, so one nested there got through as well.
This commit is contained in:
10
semen.scm
10
semen.scm
@@ -917,16 +917,20 @@
|
||||
(copy-form-source! form (walk-body form env)))
|
||||
|
||||
;;; 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)
|
||||
(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))))
|
||||
(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)))
|
||||
(or valued? (test-slot? (car form) i slots))))
|
||||
(walk-body (list (car parts)) env))))
|
||||
(loop (cdr parts) (+ i 1) (append-reverse walked acc))))))))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user