Add encapsulation, sum types, type constraints and all the required machinery. Also -> threading macro #64
24
semen.scm
24
semen.scm
@@ -970,13 +970,25 @@
|
|||||||
;; ...and an increment is the operand unpromoted:
|
;; ...and an increment is the operand unpromoted:
|
||||||
;; (++ c), c : char -> char
|
;; (++ c), c : char -> char
|
||||||
((++ --) (expression-type (second expr) env))
|
((++ --) (expression-type (second expr) env))
|
||||||
;; otherwise a call: a closure answers with its own return type,
|
;; otherwise a call: a method answers for the name it resolves
|
||||||
;; anything else with what its signature says
|
;; to, a closure with its own return type, anything else with
|
||||||
|
;; what its signature says
|
||||||
(else
|
(else
|
||||||
(let ((closure (receiver-closure-type (car expr) env)))
|
(cond
|
||||||
(if closure
|
((method-head? (car expr)) (method-call-type expr env))
|
||||||
(third closure)
|
((receiver-closure-type (car expr) env) => third)
|
||||||
(and (symbol? (car expr)) (get-return-type (car expr))))))))))
|
(else (and (symbol? (car expr)) (get-return-type (car expr))))))))))
|
||||||
|
|
||||||
|
;;; (.length-sq (.sub a b)) types the inner call to find the outer
|
||||||
|
;;; one's receiver, so a method call has to answer like any other
|
||||||
|
(define (method-call-type expr env)
|
||||||
|
(and (pair? (cdr expr))
|
||||||
|
(let* ((written (expression-type (second expr) env))
|
||||||
|
(target (pointer-target written))
|
||||||
|
(key (ops-key (or target written))))
|
||||||
|
(and key
|
||||||
|
(let ((method (get-method key (method-head-name (car expr)))))
|
||||||
|
(and method (get-return-type method)))))))
|
||||||
|
|
||||||
;;; C's usual arithmetic conversions, far enough to answer `_':
|
;;; C's usual arithmetic conversions, far enough to answer `_':
|
||||||
;;;
|
;;;
|
||||||
|
|||||||
@@ -109,7 +109,15 @@
|
|||||||
(test-assert "and so is a receiver with no type to dispatch on"
|
(test-assert "and so is a receiver with no type to dispatch on"
|
||||||
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
|
(reports? "(fn f () void (var n int 1) (.get n))" "no such method on"))
|
||||||
(test-assert "an address cannot be taken of a call"
|
(test-assert "an address cannot be taken of a call"
|
||||||
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue")))
|
(reports? (ops-src "(.length (mk))") "receiver is not an lvalue"))
|
||||||
|
;; the inner call has to be typed to find the outer one's receiver
|
||||||
|
(test-assert "a method call can be another's receiver"
|
||||||
|
(emits? (string-append
|
||||||
|
"(record w ((x int)))"
|
||||||
|
"(ops w (fn twice ((a w)) w (return a))"
|
||||||
|
" (fn get ((a w)) int (return (. a x))))"
|
||||||
|
"(fn f () int (var a w #(w : 1)) (return (.get (.twice a))))")
|
||||||
|
"wːget(wːtwice(a))")))
|
||||||
|
|
||||||
;; A sum is the tag enum, a payload struct per variant that carries
|
;; A sum is the tag enum, a payload struct per variant that carries
|
||||||
;; fields, the record holding both, and a constructor each.
|
;; fields, the record holding both, and a constructor each.
|
||||||
|
|||||||
Reference in New Issue
Block a user