Add encapsulation, sum types, type constraints and all the required machinery. Also -> threading macro #64
85
semen.scm
85
semen.scm
@@ -470,22 +470,22 @@
|
|||||||
|
|
||||||
((match) (walk-match form env))
|
((match) (walk-match form env))
|
||||||
|
|
||||||
((dot-access ->) (walk-member-access form env))
|
|
||||||
|
|
||||||
(else
|
(else
|
||||||
|
(if (method-head? (car form))
|
||||||
|
(walk-method-call form env)
|
||||||
(let ((closure (receiver-closure-type (car form) env)))
|
(let ((closure (receiver-closure-type (car form) env)))
|
||||||
(if closure
|
(if closure
|
||||||
;; the receiver is walked first: `((closure ((x int)) int
|
;; the receiver is walked first: `((closure ((x int))
|
||||||
;; () ...) 5)' registers `struct ƛint_int' on the way, and
|
;; int () ...) 5)' registers `struct ƛint_int' on the
|
||||||
;; the call helper's signature names it
|
;; way, and the call helper's signature names it
|
||||||
(let ((receiver (walk-statement (car form) env)))
|
(let ((receiver (walk-statement (car form) env)))
|
||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
form
|
form
|
||||||
`(,(register-closure-call! closure form)
|
`(,(register-closure-call! closure form)
|
||||||
,receiver
|
,receiver
|
||||||
,@(map (lambda (argument) (walk-statement argument env))
|
,@(map (lambda (a) (walk-statement a env))
|
||||||
(cdr form)))))
|
(cdr form)))))
|
||||||
(convert-arguments (walk-parts form env) env))))))))
|
(convert-arguments (walk-parts form env) env)))))))))
|
||||||
|
|
||||||
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
||||||
(define (resolve-wildcard form env)
|
(define (resolve-wildcard form env)
|
||||||
@@ -686,47 +686,62 @@
|
|||||||
,(first field)))))
|
,(first field)))))
|
||||||
binders fields))
|
binders fields))
|
||||||
|
|
||||||
;;; (. v length) is member access while `length' is a field of v, and a
|
;;; (.length v) is a call to vec2ːlength; (. v length) is the field
|
||||||
;;; call to vec2ːlength once it is a method:
|
;;; `length', always. Keeping them apart means the shape of a form can
|
||||||
|
;;; be read without knowing the receiver's type -- (. a b c d) is a
|
||||||
|
;;; path and (.b a c d) is a call, and adding a field named like a
|
||||||
|
;;; method cannot quietly turn calls into reads.
|
||||||
;;;
|
;;;
|
||||||
;;; (. v length) -> (vec2/length (& v))
|
;;; (.length v) -> (vec2ːlength (& v))
|
||||||
;;; (-> p length) -> (vec2/length p)
|
;;; (.length p) -> (vec2ːlength p)
|
||||||
;;; (. v x) -> untouched, v.x
|
;;; (.sum-of p) -> (vec2ːsum_of (* p))
|
||||||
;;;
|
;;;
|
||||||
;;; A field wins, so adding a method cannot quietly shadow one.
|
;;; Which of those is written is decided by the receiver's type against
|
||||||
(define (walk-member-access form env)
|
;;; the method's own, so the caller never has to match them up.
|
||||||
(let* ((arrow? (eq? (first form) '->))
|
(define (method-head? head)
|
||||||
(object (second form))
|
(and (symbol? head)
|
||||||
(name (and (>= (length form) 3) (third form)))
|
(let ((spelling (symbol->string head)))
|
||||||
(type (let ((written (expression-type object env)))
|
(and (> (string-length spelling) 1)
|
||||||
(if arrow? (pointer-target written) written)))
|
(char=? #\. (string-ref spelling 0))))))
|
||||||
(key (and (symbol? name) (ops-key type)))
|
|
||||||
(method (and key
|
(define (method-head-name head)
|
||||||
(not (member-path-type type (list name)))
|
(string->symbol (substring (symbol->string head) 1)))
|
||||||
(get-method key name))))
|
|
||||||
(if method
|
(define (walk-method-call form env)
|
||||||
|
(let ((name (method-head-name (car form))))
|
||||||
|
(unless (pair? (cdr form))
|
||||||
|
(sex-error form "a method call needs a receiver" form))
|
||||||
|
(let* ((receiver (second form))
|
||||||
|
(written (expression-type receiver env))
|
||||||
|
(target (pointer-target written))
|
||||||
|
(key (ops-key (or target written))))
|
||||||
|
(unless key
|
||||||
|
(sex-error form "receiver of this has no type to dispatch on" name))
|
||||||
|
(let ((method (get-method key name)))
|
||||||
|
(unless method
|
||||||
|
(sex-error form "no such method on" key name))
|
||||||
(copy-form-source!
|
(copy-form-source!
|
||||||
form
|
form
|
||||||
`(,method
|
`(,method
|
||||||
,(method-receiver method arrow? (walk-statement object env) form)
|
,(method-receiver method (and target #t)
|
||||||
|
(walk-statement receiver env) form)
|
||||||
,@(map (lambda (argument) (walk-statement argument env))
|
,@(map (lambda (argument) (walk-statement argument env))
|
||||||
(cdddr form))))
|
(cddr form))))))))
|
||||||
(convert-arguments (walk-parts form env) env))))
|
|
||||||
|
|
||||||
;;; A method taking (* Self) wants an address and one taking Self wants
|
;;; A method taking (* Self) wants an address and one taking Self wants
|
||||||
;;; a value; the receiver was written as one or the other, so one of
|
;;; a value; the receiver is one or the other, so one of the two gets
|
||||||
;;; the two needs `&' or `*' put in front of it
|
;;; `&' or `*' put in front of it
|
||||||
(define (method-receiver method arrow? object form)
|
(define (method-receiver method pointer? receiver form)
|
||||||
(let ((pointer? (let ((self (method-self-type method)))
|
(let ((wants-pointer? (let ((self (method-self-type method)))
|
||||||
(and (pair? self) (eq? '* (car self))))))
|
(and (pair? self) (eq? '* (car self))))))
|
||||||
(cond ((eq? pointer? arrow?) object)
|
(cond ((eq? wants-pointer? pointer?) receiver)
|
||||||
((not pointer?) `(* ,object))
|
((not wants-pointer?) `(* ,receiver))
|
||||||
;; `&(f())' is not C, and the method cannot be given a
|
;; `&(f())' is not C, and the method cannot be given a
|
||||||
;; temporary from inside an expression
|
;; temporary from inside an expression
|
||||||
((not (lvalue? object))
|
((not (lvalue? receiver))
|
||||||
(sex-error form "receiver is not an lvalue; bind it to a var first"
|
(sex-error form "receiver is not an lvalue; bind it to a var first"
|
||||||
method))
|
method))
|
||||||
(else `(& ,object)))))
|
(else `(& ,receiver)))))
|
||||||
|
|
||||||
(define (lvalue? expr)
|
(define (lvalue? expr)
|
||||||
(cond ((symbol? expr) #t)
|
(cond ((symbol? expr) #t)
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
|
|
||||||
;; the bare record name, then the method both ways round
|
;; the bare record name, then the method both ways round
|
||||||
(var v vec2 #(vec2 : 3 4))
|
(var v vec2 #(vec2 : 3 4))
|
||||||
(printf "vec2 %d %d\n" (. v manhattan) (vec2/manhattan (& v)))
|
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
|
||||||
|
|
||||||
(var c colour (colour/rgb 1 2 3))
|
(var c colour (colour/rgb 1 2 3))
|
||||||
(match c
|
(match c
|
||||||
|
|||||||
@@ -5,9 +5,14 @@
|
|||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; An ops block is mangled fns plus a registry entry, so a method is
|
;;; An ops block is mangled fns plus a registry entry, so a method is
|
||||||
;;; reachable both ways: spelled out as vec2/length, or through the
|
;;; reachable both ways: spelled out as vec2/length, or as (.length v).
|
||||||
;;; receiver as (. v length). A field still wins over a method of the
|
;;; The method sits in the head, not in a field position, so (. v x) is
|
||||||
;;; same name, and `ops' works on a plain C struct too -- it keys on
|
;;; always the field x and the two cannot be confused -- here vec2 has
|
||||||
|
;;; both a field x and methods, and nothing arbitrates between them.
|
||||||
|
;;;
|
||||||
|
;;; Whether the receiver is addressed, dereferenced or passed along is
|
||||||
|
;;; decided by its type against the method's own, so (.length v) and
|
||||||
|
;;; (.length p) both work. `ops' takes a plain C struct too: it keys on
|
||||||
;;; the tag, not on the typedef a record happens to add.
|
;;; the tag, not on the typedef a record happens to add.
|
||||||
|
|
||||||
(include stdio.h)
|
(include stdio.h)
|
||||||
@@ -17,7 +22,7 @@
|
|||||||
|
|
||||||
(ops vec2
|
(ops vec2
|
||||||
(pub fn length ((self (* const vec2))) float
|
(pub fn length ((self (* const vec2))) float
|
||||||
"Pointer self, so (. v length) has to take the address."
|
"Pointer self, so (.length v) has to take the address."
|
||||||
(return (sqrtf (+ (* (-> self x) (-> self x))
|
(return (sqrtf (+ (* (-> self x) (-> self x))
|
||||||
(* (-> self y) (-> self y))))))
|
(* (-> self y) (-> self y))))))
|
||||||
|
|
||||||
@@ -26,7 +31,7 @@
|
|||||||
(*= (-> self y) k))
|
(*= (-> self y) k))
|
||||||
|
|
||||||
(fn sum-of ((self vec2)) float
|
(fn sum-of ((self vec2)) float
|
||||||
;; value self, so (-> p sum-of) has to dereference
|
;; value self, so (.sum-of p) has to dereference
|
||||||
(return (+ (. self x) (. self y)))))
|
(return (+ (. self x) (. self y)))))
|
||||||
|
|
||||||
(struct raw ((a int) (b int)))
|
(struct raw ((a int) (b int)))
|
||||||
@@ -39,13 +44,13 @@
|
|||||||
(var v vec2 #(vec2 : 3.0 4.0))
|
(var v vec2 #(vec2 : 3.0 4.0))
|
||||||
(var p (* vec2) (& v))
|
(var p (* vec2) (& v))
|
||||||
|
|
||||||
(printf "%.1f %.1f " (. v length) (vec2/length (& v)))
|
(printf "%.1f %.1f " (.length v) (vec2/length (& v)))
|
||||||
(. v scale 2.0)
|
(.scale v 2.0)
|
||||||
(printf "%.1f %.1f %.1f %.1f\n"
|
(printf "%.1f %.1f %.1f %.1f\n"
|
||||||
(-> p length) (. v sum-of) (-> p sum-of) (. v x))
|
(.length p) (.sum-of v) (.sum-of p) (. v x))
|
||||||
|
|
||||||
(var r (struct raw) #(struct raw : 6 8))
|
(var r (struct raw) #(struct raw : 6 8))
|
||||||
(printf "struct receiver: %d\n" (. r total))
|
(printf "struct receiver: %d\n" (.total r))
|
||||||
|
|
||||||
(var u vec2 #(vec2 : 8.0 0.0))
|
(var u vec2 #(vec2 : 8.0 0.0))
|
||||||
(printf "private: %.1f\n" (vec2/sum-of u))
|
(printf "private: %.1f\n" (vec2/sum-of u))
|
||||||
|
|||||||
Reference in New Issue
Block a user