From c707a9db41536abd75b5e765e2a87caeab87510b Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 10:23:59 +0300 Subject: [PATCH] say which macro call an error came from A wrong argument count was CHICKEN's to report, about a procedure with no name, and an error raised in a macro's body said nothing of the call. --- sex-macros.scm | 34 +++++++++++++++++++++++++++++++--- tests/codegen.scm | 17 ++++++++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/sex-macros.scm b/sex-macros.scm index 54017df..66906e6 100644 --- a/sex-macros.scm +++ b/sex-macros.scm @@ -2,9 +2,12 @@ scheme (only fmt fmt) (chicken base) + (chicken condition) + (chicken irregex) (chicken module) (chicken plist) - (chicken string)) + (chicken string) + utils) (export cat @@ -55,11 +58,36 @@ (symbol? (car form)) (get (car form) 'sex-macro))) +;;; CHICKEN would report a wrong argument count about a procedure with +;;; no name, and an error raised in a macro's body without the call: both +;;; are told here, where the call is known (define (apply-macro form) (assert (macro? form) (fmt #f (car form) " is not a macro")) - (apply (get-macro (car form)) - (cdr form))) + (let ((name (car form))) + (check-macro-arity form name (cadr (get name 'sex-macro)) (length (cdr form))) + (handle-exceptions e + (let ((message ((condition-property-accessor 'exn 'message #f) e))) + ;; one raised about a form of its own already says where + (if (and message (not (irregex-search "^[^ ]+:[0-9]+: " message))) + (apply sex-error form + (string-append "expanding " (symbol->string name) + ": " message) + ((condition-property-accessor 'exn 'arguments (list)) e)) + (abort e))) + (apply (get-macro name) (cdr form))))) + +;;; (defmacro (one x) ...) takes one; (defmacro (many x . more) ...) one +;;; or more +(define (check-macro-arity form name arglist given) + (let count ((args arglist) (required 0)) + (cond ((pair? args) (count (cdr args) (+ required 1))) + ((null? args) + (unless (= given required) + (sex-error form "macro takes this many arguments" name required))) + ((< given required) + (sex-error form "macro takes at least this many arguments" + name required))))) (define (defmacro form) (let ((arglist (car form)) diff --git a/tests/codegen.scm b/tests/codegen.scm index db3adb4..f647145 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -315,7 +315,22 @@ (test-assert "the offending form is shown too" (reports? "(include stdio.h)\n(wat 1 2)" "(wat 1 2)")) (test-assert "pub with nothing to define" - (reports? "(pub 1)" "codegen.sex:1:"))) + (reports? "(pub 1)" "codegen.sex:1:")) + ;; a macro's arity was CHICKEN's to report, about a procedure with + ;; no name; and an error in its body said nothing of the call + (test-assert "a macro given the wrong number of arguments" + (reports? "(defmacro (one x) x)\n(fn f () int\n (return (one 1 2)))" + "codegen.sex:3: macro takes this many arguments one 1")) + (test-assert "at toplevel too" + (reports? "(defmacro (one x) x)\n(one)" "codegen.sex:2: macro takes this many arguments")) + (test-assert "and one taking the rest, too few" + (reports? "(defmacro (many x . more) x)\n(fn f () int (return (many)))" + "macro takes at least this many arguments many 1")) + (test-assert "while it may take any more" + (emits? "(defmacro (many x . more) x) (fn f () int (return (many 1 2 3)))" "return 1;")) + (test-assert "an error in a macro's body names the call" + (reports? "(defmacro (boom x) (error \"no good\" x))\n(fn f () int (return (boom 1)))" + "codegen.sex:2: expanding boom: no good"))) ;; A grouped header, C's parens carried over, died in the writer with ;; `(caddr) bad argument type' and no location