bugfixes #66
46
semen.scm
46
semen.scm
@@ -415,17 +415,30 @@
|
||||
;;; is (pub fn vec2/length ...) plus a registry entry, so that
|
||||
;;; (. v length) can find it later. The block itself never reaches the
|
||||
;;; writer; `pub' inside it means what it means outside.
|
||||
;;;
|
||||
;;; Every method is registered before any body is walked, so one may
|
||||
;;; call another written below it; that one is then declared ahead of
|
||||
;;; the first body, since C wants it declared before the call.
|
||||
(define (process-ops form acc)
|
||||
(let ((type (and (pair? (cdr form)) (cadr form))))
|
||||
(unless (symbol? type)
|
||||
(sex-error form "ops needs a type name" form))
|
||||
(fold (lambda (method acc) (process-op type method acc))
|
||||
acc
|
||||
(cddr form))))
|
||||
(let* ((methods (map (lambda (m) (register-op type m)) (cddr form)))
|
||||
(fns (remove comment-form? methods))
|
||||
(bodies (map (lambda (m)
|
||||
(if (comment-form? m) (list m) (process-fn m (list))))
|
||||
methods))
|
||||
(early (filter (lambda (m)
|
||||
(called-before? (sex-fn-name m) m methods bodies))
|
||||
fns)))
|
||||
(append (append-map identity (reverse bodies))
|
||||
(fold (lambda (m acc) (process-fn (fn-prototype m) acc)) acc early)))))
|
||||
|
||||
(define (process-op type form acc)
|
||||
;;; The method's fn form, renamed to vec2/length, once it is registered
|
||||
;;; under `length'; a comment is passed along
|
||||
(define (register-op type form)
|
||||
(if (comment-form? form)
|
||||
(cons form acc)
|
||||
form
|
||||
(let ((core (and (pair? form) (fn-core form))))
|
||||
(unless (and (pair? core) (eq? (car core) 'fn)
|
||||
(pair? (cdr core)) (symbol? (second core)))
|
||||
@@ -435,7 +448,28 @@
|
||||
(sex-error form "method is already defined on" type name))
|
||||
(let ((qualified (qualify-op type name)))
|
||||
(add-method! type name qualified)
|
||||
(process-fn (rename-fn form qualified) acc))))))
|
||||
;; its signature too, which a call's receiver is fitted to
|
||||
(process-fn (fn-prototype (rename-fn form qualified)) (list))
|
||||
(rename-fn form qualified))))))
|
||||
|
||||
;;; Does a body written above METHOD call it?
|
||||
(define (called-before? name method methods bodies)
|
||||
(let loop ((ms methods) (bs bodies))
|
||||
(and (not (eq? (car ms) method))
|
||||
(or (names? (car bs) name)
|
||||
(loop (cdr ms) (cdr bs))))))
|
||||
|
||||
;;; NAME anywhere in FORM, a literal's contents included
|
||||
(define (names? form name)
|
||||
(cond ((eq? form name) #t)
|
||||
((pair? form) (or (names? (car form) name) (names? (cdr form) name)))
|
||||
((vector? form) (names? (vector->list form) name))
|
||||
(else #f)))
|
||||
|
||||
;;; (pub fn name args ret ...) -> (pub fn name args ret)
|
||||
(define (fn-prototype form)
|
||||
(let ((header (strip-fn-header-comments form)))
|
||||
(copy-form-source! form (take header (fn-header-length header)))))
|
||||
|
||||
(define (rename-fn form name)
|
||||
(if (memq (first form) '(pub extern))
|
||||
|
||||
@@ -19,6 +19,13 @@
|
||||
"(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) "
|
||||
(apply string-append body) ")"))
|
||||
|
||||
(define forward-src
|
||||
(string-append
|
||||
"(record pb ((n int)))"
|
||||
"(ops pb"
|
||||
" (fn twice ((self (* pb))) int (return (* 2 (.len self))))"
|
||||
" (pub fn len ((self (* pb))) int (return (. self n))))"))
|
||||
|
||||
(define (sum-src . body)
|
||||
(string-append
|
||||
"(sum shape ((circle ((r float))) (rect ((w float) (h float))) (empty ())))"
|
||||
@@ -79,7 +86,15 @@
|
||||
" (fn g ((s v)) int (return 2)))")
|
||||
"method is already defined on"))
|
||||
(test-assert "pub goes on the methods, not the block"
|
||||
(reports? "(record v ((x int)))(pub ops v)" "pub belongs on the methods")))
|
||||
(reports? "(record v ((x int)))(pub ops v)" "pub belongs on the methods"))
|
||||
;; Every method is known before any body is walked, and C is told of
|
||||
;; one before a body above it calls it -- and only then
|
||||
(test-assert "a method can call one written below it"
|
||||
(emits? forward-src "return 2 * pbːlen(self)"))
|
||||
(test-assert "which is declared ahead of the call"
|
||||
(emits? forward-src "int pbːlen (pb * self);"))
|
||||
(test-assert "and nothing else is"
|
||||
(not (emits? (ops-src "") "vec2ːsum_of (vec2 self);"))))
|
||||
|
||||
;; The method sits in the head, so the shape of a form can be read
|
||||
;; without knowing the receiver's type.
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
(input)
|
||||
(output "5.0 5.0 10.0 14.0 14.0 6.0"
|
||||
"struct receiver: 14"
|
||||
"private: 8.0")
|
||||
"private: 8.0"
|
||||
"forward call: 28.0")
|
||||
(return 0)
|
||||
|
||||
;;; An ops block is mangled fns plus a registry entry, so a method is
|
||||
@@ -22,6 +23,10 @@
|
||||
(record vec2 ((x float) (y float)))
|
||||
|
||||
(ops vec2
|
||||
;; a method may call one written below it
|
||||
(fn twice-sum ((self vec2)) float
|
||||
(return (* 2.0 (.sum-of self))))
|
||||
|
||||
(pub fn length ((self (* const vec2))) float
|
||||
"Pointer self, so (.length v) has to take the address."
|
||||
(return (sqrtf (+ (* (. self x) (. self x))
|
||||
@@ -55,4 +60,5 @@
|
||||
|
||||
(var u vec2 #(vec2 : 8.0 0.0))
|
||||
(printf "private: %.1f\n" (vec2/sum-of u))
|
||||
(printf "forward call: %.1f\n" (.twice-sum v))
|
||||
(return 0))
|
||||
|
||||
Reference in New Issue
Block a user