diff options
| author | 2025-03-06 14:32:48 -0500 | |
|---|---|---|
| committer | 2025-03-06 14:32:48 -0500 | |
| commit | 6763215c2c4bbfb3cae3273864be58bcc70ddac9 (patch) | |
| tree | 0267af0462bf0553bb42158761ec30394e4fc6a8 | |
| parent | test overrides (diff) | |
better flags handling
| -rw-r--r-- | README.md | 34 | ||||
| -rw-r--r-- | lib/SAHP.scm | 6 | ||||
| -rw-r--r-- | lib/SAHP/internal-common.scm | 2 | ||||
| -rw-r--r-- | tests/run.scm | 10 |
4 files changed, 30 insertions, 22 deletions
@@ -118,14 +118,16 @@ dispatch on (in order of precedence): * global scope The scope resolution tries to find the most specific implementation in -the closest scope. If the implementation in closest scope is not the -most specific implementation, then: +the closest scope. The search process is: -* If the implementation in closest scope is labeled `overridable`, then - the implementation in the next closest scope is selected, and this - disambiguation process continues again. -* If the implementation in closest scope is not labeled `overridable`, - then it is selected. +1. Set the current scope to local scope, and set the closest implementation + `I` to nothing. +2. If there are no more scopes to visit, return `I`. +3. Find an implementation in the current scope. +4. If the implementation is not overridable, return it. +5. If the implementation is overridable, and it is more specific than `I`, + set `I` to this implementation and continue at 2 with the next scope. +6. If there is no implementation, continue to the next scope. The SAHP will raise an error object that satisfies the predicate `SAHP-implementation-not-found-error?` if no implementation is found. @@ -139,13 +141,19 @@ The phrase "the SAHP resolves to `procedure` given `T`" means that if the SAHP is called with a value as its first argument of type `T`, it will call `procedure` with all of the arguments passed to it. +### Flags + +Flags are a list of symbols, which may be: + +* `overridable`: See above. + ### Type Expressions A type expression is defined as: * The symbols `*` `boolean`, `char`, `null`, `pair`, `procedure`, `symbol`, `bytevector`, `eof-object`, `port`, `string`, `vector`, - `exact-integers`, `exact-rationals`, `reals`, `complex`, or `numbers`, or + `exact-integers`, `exact-rationals`, `reals`, `complex`, or `number`, or * the value bound to a name of a record type, or * any other implementation defined value. @@ -169,8 +177,6 @@ or (set-global-SAHP! SAHP type flag procedure) -The flag can be omitted, or the symbol `overridable`. - Set `SAHP` to resolve to `procedure` given `type` in global scope. (define-global-SAHP (name (type argument) . arg-rest) body ...) @@ -184,6 +190,8 @@ resolve to this procedure given `type` in global scope. The flag can be omitted, or the symbol `overridable`. +`flag` can be omitted. + ### Dynamic Scope (parameterize-SAHP ((SAHP (type value) ...) ...) body ...) @@ -198,11 +206,11 @@ Each SAHP will have each `type` under it resolve to `value` in dynamic scope. These procedures take precedence over global scope procedures. The SAHPs are the same SAHP in the block in the sense of `eq?`. -The flag can be omitted, or the symbol `overridable`. +`flag` can be omitted. ### Local Scope - (letrec-SAHP ((SAHP (type type value) ...) ...) body ...) + (letrec-SAHP ((SAHP (type flag value) ...) ...) body ...) Each `SAHP` must be an identifier that resolves to a SAHP. @@ -220,7 +228,7 @@ If the SAHPs passed to `letrec-SAHP` had local bindings, then `letrec-SAHP` will replace old bindings and add new ones without affecting the previous SAHP object. -The flag can be omitted, or the symbol `overridable`. +`flag` can be omitted. ## TODO diff --git a/lib/SAHP.scm b/lib/SAHP.scm index 1d17e45..125afb6 100644 --- a/lib/SAHP.scm +++ b/lib/SAHP.scm @@ -21,7 +21,7 @@ (define set-global-SAHP! (case-lambda ((SAHP type procedure) - (set-global-SAHP! SAHP type #f procedure)) + (set-global-SAHP! SAHP type '() procedure)) ((SAHP type flag procedure) (%SAHP-set (SAHP-global-scope (extract-SAHP-descriptor SAHP)) (list (list type flag procedure)))))) @@ -43,7 +43,7 @@ (define-syntax parameterize-SAHP (syntax-rules () ((_ ((SAHP (type value) ...) ...) body ...) - (parameterize-SAHP ((SAHP (type #f value) ...) ...) body ...)) + (parameterize-SAHP ((SAHP (type '() value) ...) ...) body ...)) ((_ ((SAHP (type flag value) ...) ...) body ...) (let ((param (SAHP-dynamic-scope-parameter (extract-SAHP-descriptor SAHP))) @@ -57,7 +57,7 @@ (define-syntax letrec-SAHP (syntax-rules () ((_ ((SAHP (type value) ...) ...) body ...) - (letrec-SAHP ((SAHP (type #f value) ...) ...) body ...)) + (letrec-SAHP ((SAHP (type '() value) ...) ...) body ...)) ((_ ((SAHP (type flag value) ...) ...) body ...) (let ((%SAHP (extract-SAHP-descriptor SAHP)) ...) (letrec ((SAHP (%SAHP/local-scope diff --git a/lib/SAHP/internal-common.scm b/lib/SAHP/internal-common.scm index c40be51..3bb0543 100644 --- a/lib/SAHP/internal-common.scm +++ b/lib/SAHP/internal-common.scm @@ -43,7 +43,7 @@ (flag SAHP-entry-flag)) (define (entry-overridable? entry) - (eq? (SAHP-entry-flag entry) 'overridable)) + (member 'overridable (SAHP-entry-flag entry))) (define (SAHP=? sahp1 sahp2) (eq? (SAHP-global-scope (extract-SAHP-descriptor sahp1)) diff --git a/tests/run.scm b/tests/run.scm index b41f536..3a5c81a 100644 --- a/tests/run.scm +++ b/tests/run.scm @@ -194,7 +194,7 @@ (gsampling (inexact-complex-generator)))))))) (test-group "local scope overrides dynamic scope subtyping even with overridable" (parameterize-SAHP ((sub ('number (lambda (x) 1)))) - (letrec-SAHP ((sub ('exact-rational 'overridable (lambda (x) 2)))) + (letrec-SAHP ((sub ('exact-rational '(overridable) (lambda (x) 2)))) (define (test-local n) (= 2 (sub n))) (define (test-dynamic n) @@ -227,7 +227,7 @@ (list (gsampling (inexact-complex-generator))))))) (test-group "dynamic scope overrides global scope even with overridable" - (parameterize-SAHP ((sub ('exact-rational 'overridable (lambda (x) 2)))) + (parameterize-SAHP ((sub ('exact-rational '(overridable) (lambda (x) 2)))) (test-group "dynamic" (test-property test-dynamic (list @@ -256,11 +256,11 @@ (test-group "local scope overrides dynamic scope overrides global scope even when overridable" (parameterize-SAHP ((sub ('exact-rational - 'overridable + '(overridable) (lambda (x) 2)))) (letrec-SAHP ((sub ('exact-integer - 'overridable + '(overridable) (lambda (x) 3)))) (define (test-local n) (= 3 (sub n))) @@ -289,7 +289,7 @@ (exact-rational-generator)))))) (test-group "override in local scope to global scope" (letrec-SAHP ((sub ('number - 'overridable + '(overridable) (lambda (x) 2)))) (define (is-local-scope x) (= (sub x) 2)) |
