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 8e3e6fcdd1
commit dd5174f472
2 changed files with 56 additions and 0 deletions

View File

@@ -619,6 +619,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))
@@ -735,6 +736,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}}}
@@ -2142,6 +2167,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))