diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2023-04-24 21:11:16 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2023-04-24 21:11:16 +0200 |
commit | 987b1e0e574007b272b04605f3b57d420bde4fb4 (patch) | |
tree | 003c430e6a46cbcff09f60d9783f861506115de8 /module/hnh/util/type.scm | |
parent | Add tests for href stuff. (diff) | |
parent | Document type and object system. (diff) | |
download | calp-987b1e0e574007b272b04605f3b57d420bde4fb4.tar.gz calp-987b1e0e574007b272b04605f3b57d420bde4fb4.tar.xz |
Merge remote-tracking branch 'origin/new-object-system' into datarewrite-structures
Diffstat (limited to 'module/hnh/util/type.scm')
-rw-r--r-- | module/hnh/util/type.scm | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/module/hnh/util/type.scm b/module/hnh/util/type.scm new file mode 100644 index 00000000..800834e5 --- /dev/null +++ b/module/hnh/util/type.scm @@ -0,0 +1,46 @@ +(define-module (hnh util type) + :use-module ((srfi srfi-1) :select (every)) + :export (build-validator-body + list-of pair-of + typecheck + current-procedure-name)) + +(define-syntax list-of + (syntax-rules () + ((_ variable (rule ...)) + (and (list? variable) + (every (lambda (x) (build-validator-body x (rule ...))) variable))) + ((_ variable rule) + (and (list? variable) + (every rule variable))))) + +(define-syntax-rule (pair-of variable a b) + (and (pair? variable) + (build-validator-body (car variable) a) + (build-validator-body (cdr variable) b))) + +;; DSL for specifying type predicates +;; Basically a procedure body, but the variable to test is implicit. +(define-syntax build-validator-body + (syntax-rules (and or list-of) + ((_ variable (and clauses ...)) (and (build-validator-body variable clauses) ...)) + ((_ variable (or clauses ...)) (or (build-validator-body variable clauses) ...)) + ((_ variable (proc args ...)) (proc variable args ...)) + ((_ variable proc) (proc variable)))) + +(define-syntax-rule (current-procedure-name) + ;; 1 since make-stack is at top of stack + (frame-procedure-name (stack-ref (make-stack #t) 1))) + +(define-syntax typecheck + (syntax-rules () + ((_ variable type-clause) + (let ((procedure-name (current-procedure-name))) + (typecheck variable type-clause procedure-name))) + ((_ variable type-clause procedure-name) + (unless (build-validator-body variable type-clause) + (scm-error 'wrong-type-arg procedure-name + "Invalid value for ~s. Expected ~s, got ~s" + (list (quote variable) (quote type-clause) variable) + #f))))) + |