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) (walk-statement (car expansion) env)
(cons walk-embed-result (walk-body expansion env))))) (cons walk-embed-result (walk-body expansion env)))))
(else (else
(check-shape form)
(case (car form) (case (car form)
((do default) ((do default)
(parameterize ((operand-position? #f)) (parameterize ((operand-position? #f))
@@ -735,6 +736,30 @@
(convert-arguments (copy-form-source! form (walk-operands form form env)) (convert-arguments (copy-form-source! form (walk-operands form form env))
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/circle 1.0) is the value itself, written where it is used:
;;; ;;;
;;; (shape){.tag = shapeǂcircle, .u = {.circle = (struct shapeːcircle){1.0}}} ;;; (shape){.tag = shapeǂcircle, .u = {.circle = (struct shapeːcircle){1.0}}}
@@ -2142,6 +2167,7 @@
;;; (var n _ 1) -> int n = 1 ;;; (var n _ 1) -> int n = 1
;;; (var o v #(v : (.get a))) -> v o = (v){vːget(&a)} ;;; (var o v #(v : (.get a))) -> v o = (v){vːget(&a)}
(define (process-global-var sex-var acc) (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)) (let* ((resolved (resolve-closure-types sex-var))
(qualifier (and (memq (car resolved) '(pub extern)) (car resolved))) (qualifier (and (memq (car resolved) '(pub extern)) (car resolved)))
(core (if qualifier (cdr resolved) resolved)) (core (if qualifier (cdr resolved) resolved))

View File

@@ -351,6 +351,36 @@
(reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))" (reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))"
"codegen.sex:2:"))) "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 ;; a block of nothing had nothing for c-begin to begin with, and the
;; writer died on its arity ;; writer died on its arity
(test-group "empty bodies" (test-group "empty bodies"