refuse a statement of the wrong shape

(goto), (var q), (cast i), (while) and the like died in the writer, in
CHICKEN's words about a procedure of its own and with no location;
(return 1 2), (break 1) and (sizeof a b) dropped what was extra.
This commit is contained in:
2026-10-19 10:23:59 +03:00
parent 45ef052f43
commit fcb7524ba1
2 changed files with 56 additions and 0 deletions

View File

@@ -623,6 +623,7 @@
(walk-statement (car expansion) env)
(cons walk-embed-result (walk-body expansion env)))))
(else
(check-shape form)
(case (car form)
((do default)
(parameterize ((operand-position? #f))
@@ -739,6 +740,30 @@
(convert-arguments (copy-form-source! form (walk-operands form form env))
env))))))))
;;; How many operands each of these takes, and how it is written: one
;;; with any other number died in the writer, in CHICKEN's words about a
;;; procedure of the writer's
(define +shapes+
'((goto 1 1 "(goto label)")
(: 1 1 "(: label)")
(var 2 3 "(var name type value)")
(cast 2 2 "(cast value type)")
(while 1 #f "(while test body ...)")
(if 2 #f "(if test then else)")
(return 0 1 "(return value)")
(break 0 0 "(break)")
(continue 0 0 "(continue)")
(sizeof 1 1 "(sizeof value)")))
(define (check-shape form)
(let ((shape (assq (car form) +shapes+))
(given (count (lambda (part) (not (comment-form? part))) (cdr form))))
(when (and shape
(not (and (>= given (second shape))
(or (not (third shape)) (<= given (third shape))))))
(sex-error form (string-append "malformed " (symbol->string (car form))
": write " (fourth shape))))))
;;; (shape/circle 1.0) is the value itself, written where it is used:
;;;
;;; (shape){.tag = shapeǂcircle, .u = {.circle = (struct shapeːcircle){1.0}}}
@@ -2150,6 +2175,7 @@
;;; (var n _ 1) -> int n = 1
;;; (var o v #(v : (.get a))) -> v o = (v){vːget(&a)}
(define (process-global-var sex-var acc)
(check-shape (if (memq (car sex-var) '(pub extern)) (cdr sex-var) sex-var))
(let* ((resolved (resolve-closure-types sex-var))
(qualifier (and (memq (car resolved) '(pub extern)) (car resolved)))
(core (if qualifier (cdr resolved) resolved))

View File

@@ -421,6 +421,36 @@ whitespace aside, THEN?"
(reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))"
"codegen.sex:2:")))
;; each of these died in the writer, in CHICKEN's words about a
;; procedure of the writer's, and with no location
(test-group "malformed forms"
(test-assert "a goto names one label"
(reports? (in-fn "(goto)") "malformed goto: write (goto label)"))
(test-assert "and only one"
(reports? (in-fn "(goto a b)") "malformed goto"))
(test-assert "so does a label"
(reports? (in-fn "(: a b)") "malformed :: write (: label)"))
(test-assert "a var has a name and a type"
(reports? (in-fn "(var q)") "malformed var"))
(test-assert "and a value at most"
(reports? (in-fn "(var q int 1 2)") "malformed var"))
(test-assert "a cast a value and a type"
(reports? (in-fn "(var n int (cast a))") "malformed cast: write (cast value type)"))
(test-assert "a while its test"
(reports? (in-fn "(while)") "malformed while"))
(test-assert "an if its test and what it does"
(reports? (in-fn "(if a)") "malformed if"))
(test-assert "a return one value at most"
(reports? (in-fn "(return 1 2)") "malformed return"))
(test-assert "a break none"
(reports? (in-fn "(while a (break 1))") "malformed break"))
(test-assert "a sizeof one operand"
(reports? (in-fn "(var n size-t (sizeof a b))") "malformed sizeof"))
(test-assert "a global var is held to the same"
(reports? "(var q)" "malformed var"))
(test-assert "each naming its line"
(reports? "(fn f () void\n (goto))" "codegen.sex:2: malformed goto")))
;; a block of nothing had nothing for c-begin to begin with, and the
;; writer died on its arity
(test-group "empty bodies"