file methods, realizations and sums under what a typedef names
A typedef had the fields of its record but none of its methods, and a typedef of a sum was not a sum to match. A typedef that ends in a scalar still keeps its own, so (typedef meters float) does not lend its methods to every float.
This commit is contained in:
@@ -133,6 +133,33 @@
|
||||
(emits? (string-append "(defmacro (the a) a)"
|
||||
(ops-src "(var q float (. (the p) x))"))
|
||||
"p->x"))
|
||||
(test-assert "a typedef of the type has its methods"
|
||||
(emits? (string-append
|
||||
"(record w ((x int)))"
|
||||
"(typedef alias w)"
|
||||
"(ops w (fn get ((self (* const w))) int (return (. self x))))"
|
||||
"(fn f () int (var a alias #(alias : 1)) (return (.get a)))")
|
||||
"wːget(&a)"))
|
||||
(test-assert "and a method written on the typedef is the type's"
|
||||
(emits? (string-append
|
||||
"(record w ((x int)))"
|
||||
"(typedef alias w)"
|
||||
"(ops alias (fn get ((self (* const alias))) int (return 1)))"
|
||||
"(fn f () int (var a w #(w : 1)) (return (.get a)))")
|
||||
"aliasːget(&a)"))
|
||||
(test-assert "a typedef of a scalar keeps its own"
|
||||
(reports? (string-append
|
||||
"(typedef meters float)"
|
||||
"(ops meters (fn half ((self meters)) meters (return self)))"
|
||||
"(fn f () void (var x float 1.0) (.half x))")
|
||||
"no such method on"))
|
||||
(test-assert "which a typedef of it shares"
|
||||
(emits? (string-append
|
||||
"(typedef meters float)"
|
||||
"(typedef km meters)"
|
||||
"(ops meters (fn half ((self meters)) meters (return self)))"
|
||||
"(fn f () void (var x km 1.0) (var y km (.half x)))")
|
||||
"metersːhalf(x)"))
|
||||
(test-assert "an address cannot be taken of a call"
|
||||
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
|
||||
;; the inner call has to be typed to find the outer one's receiver
|
||||
@@ -208,6 +235,9 @@
|
||||
(test-assert "a spec is declared once"
|
||||
(reports? "(spec (p T) (fn s ((a T)) void))(spec (p T) (fn q ((a T)) void))"
|
||||
"spec is already declared"))
|
||||
(test-assert "a typedef realizes what its type does"
|
||||
(reports? (string-append (spec-src "") "(typedef alias v)(realize (p alias))")
|
||||
"spec is already realized for"))
|
||||
(test-assert "a type realizes a spec once"
|
||||
(reports? (string-append (spec-src "") "(realize (p v)"
|
||||
" (fn s ((a (* const v))) void (return)))")
|
||||
@@ -298,6 +328,13 @@
|
||||
(sum-src "(match (the s) ((circle (r)) (= out r))"
|
||||
" (else (= out 0.0)))"))
|
||||
"shape ƛm0 = s"))
|
||||
(test-assert "a typedef of a sum is matched as the sum"
|
||||
(emits? (string-append
|
||||
"(sum shape ((circle ((r int))) (empty ())))"
|
||||
"(typedef alias shape)"
|
||||
"(fn f () int (var s alias (shape/circle 1))"
|
||||
" (match s ((circle (r)) (return r)) ((empty ()) (return 0))))")
|
||||
"case shapeǂcircle"))
|
||||
(test-assert "else becomes default"
|
||||
(emits? (sum-src "(match s ((circle (r)) (= out r)) (else (= out 0.0)))")
|
||||
"default:"))
|
||||
|
||||
34
types.scm
34
types.scm
@@ -100,10 +100,10 @@
|
||||
(define +sum-db+ (make-hash-table))
|
||||
|
||||
(define (add-sum! name variants)
|
||||
(hash-table-set! +sum-db+ name variants))
|
||||
(hash-table-set! +sum-db+ (type-key name) variants))
|
||||
|
||||
(define (get-sum name)
|
||||
(hash-table-ref/default +sum-db+ name #f))
|
||||
(hash-table-ref/default +sum-db+ (type-key name) #f))
|
||||
|
||||
(define (variant-fields variants name)
|
||||
(let ((entry (assq name variants)))
|
||||
@@ -130,11 +130,11 @@
|
||||
(define (spec-methods spec) (cdr spec))
|
||||
|
||||
(define (add-realization! spec type)
|
||||
(hash-table-set! +realize-db+ (cons spec type) #t))
|
||||
(hash-table-set! +realize-db+ (cons spec (type-key type)) #t))
|
||||
|
||||
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
|
||||
(define (realizes? spec type)
|
||||
(hash-table-exists? +realize-db+ (cons spec type)))
|
||||
(hash-table-exists? +realize-db+ (cons spec (type-key type))))
|
||||
|
||||
;;; Both of these refuse a second declaration of the same name, which
|
||||
;;; is per translation unit rather than per process -- so a process
|
||||
@@ -156,10 +156,10 @@
|
||||
(define +ops-db+ (make-hash-table))
|
||||
|
||||
(define (add-method! type method qualified)
|
||||
(hash-table-set! +ops-db+ (cons type method) qualified))
|
||||
(hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
|
||||
|
||||
(define (get-method type method)
|
||||
(hash-table-ref/default +ops-db+ (cons type method) #f))
|
||||
(hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))
|
||||
|
||||
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
|
||||
(define (qualify-op type name)
|
||||
@@ -169,13 +169,33 @@
|
||||
;;; The name an `ops' block was written against: `(struct point)' and a
|
||||
;;; `const' wrapper both name `point', and anything else names nothing.
|
||||
(define (ops-key type)
|
||||
(cond ((symbol? type) type)
|
||||
(cond ((symbol? type) (type-key type))
|
||||
((not (and (pair? type) (symbol? (car type)))) #f)
|
||||
((memq (car type) '(struct union enum))
|
||||
(and (pair? (cdr type)) (symbol? (cadr type)) (cadr type)))
|
||||
((memq (car type) '(const volatile)) (ops-key (cadr type)))
|
||||
(else #f)))
|
||||
|
||||
;;; The name methods, realizations and sums are filed under. A typedef
|
||||
;;; is followed to the aggregate it names, and one that ends in anything
|
||||
;;; else stops at the last typedef, so a scalar does not share them:
|
||||
;;;
|
||||
;;; (record v ...) (typedef alias v) alias -> v
|
||||
;;; (typedef meters float) meters -> meters
|
||||
;;; (typedef km meters) km -> meters
|
||||
(define (type-key name)
|
||||
(let follow ((name name) (seen (list)))
|
||||
(let ((target (and (not (memq name seen)) (typedef-target name))))
|
||||
(cond ((not target) name)
|
||||
((symbol? target)
|
||||
(if (typedef-target target) (follow target (cons name seen)) name))
|
||||
((target-tag target))
|
||||
(else name)))))
|
||||
|
||||
(define (typedef-target name)
|
||||
(let ((info (and (symbol? name) (get-type-info name))))
|
||||
(and info (eq? (car info) 'typedef) (caddr info))))
|
||||
|
||||
(define (add-name-type! name type)
|
||||
(hash-table-set! +name-db+ name type))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user