let a method call one written below it

Each method was registered just before its own body was walked, so an
earlier one could not find it. All are registered first now, and one
called above its definition is declared ahead of the bodies, which C
needs as well.

Fixes #44.
This commit is contained in:
2026-10-09 17:37:03 +03:00
parent 0d26b1f6a5
commit 2d4e5f6b16
3 changed files with 63 additions and 8 deletions

View File

@@ -403,17 +403,30 @@
;;; is (pub fn vec2/length ...) plus a registry entry, so that ;;; is (pub fn vec2/length ...) plus a registry entry, so that
;;; (. v length) can find it later. The block itself never reaches the ;;; (. v length) can find it later. The block itself never reaches the
;;; writer; `pub' inside it means what it means outside. ;;; 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) (define (process-ops form acc)
(let ((type (and (pair? (cdr form)) (cadr form)))) (let ((type (and (pair? (cdr form)) (cadr form))))
(unless (symbol? type) (unless (symbol? type)
(sex-error form "ops needs a type name" form)) (sex-error form "ops needs a type name" form))
(fold (lambda (method acc) (process-op type method acc)) (let* ((methods (map (lambda (m) (register-op type m)) (cddr form)))
acc (fns (remove comment-form? methods))
(cddr form)))) (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) (if (comment-form? form)
(cons form acc) form
(let ((core (and (pair? form) (fn-core form)))) (let ((core (and (pair? form) (fn-core form))))
(unless (and (pair? core) (eq? (car core) 'fn) (unless (and (pair? core) (eq? (car core) 'fn)
(pair? (cdr core)) (symbol? (second core))) (pair? (cdr core)) (symbol? (second core)))
@@ -423,7 +436,28 @@
(sex-error form "method is already defined on" type name)) (sex-error form "method is already defined on" type name))
(let ((qualified (qualify-op type name))) (let ((qualified (qualify-op type name)))
(add-method! type name qualified) (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) (define (rename-fn form name)
(if (memq (first form) '(pub extern)) (if (memq (first form) '(pub extern))

View File

@@ -18,6 +18,13 @@
"(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) " "(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) "
(apply string-append body) ")")) (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) (define (sum-src . body)
(string-append (string-append
"(sum shape ((circle ((r float))) (rect ((w float) (h float))) (empty ())))" "(sum shape ((circle ((r float))) (rect ((w float) (h float))) (empty ())))"
@@ -78,7 +85,15 @@
" (fn g ((s v)) int (return 2)))") " (fn g ((s v)) int (return 2)))")
"method is already defined on")) "method is already defined on"))
(test-assert "pub goes on the methods, not the block" (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 ;; The method sits in the head, so the shape of a form can be read
;; without knowing the receiver's type. ;; without knowing the receiver's type.

View File

@@ -2,7 +2,8 @@
(input) (input)
(output "5.0 5.0 10.0 14.0 14.0 6.0" (output "5.0 5.0 10.0 14.0 14.0 6.0"
"struct receiver: 14" "struct receiver: 14"
"private: 8.0") "private: 8.0"
"forward call: 28.0")
(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
@@ -22,6 +23,10 @@
(record vec2 ((x float) (y float))) (record vec2 ((x float) (y float)))
(ops vec2 (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 (pub fn length ((self (* const vec2))) float
"Pointer self, so (.length v) 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))
@@ -55,4 +60,5 @@
(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))
(printf "forward call: %.1f\n" (.twice-sum v))
(return 0)) (return 0))