1
0
forked from alex-eg/sex
Files
sex/sex-modules.scm
alex-eg 6181a6c8d4 implement spec and realize
A spec is a contract that emits nothing; a realization is that
contract's methods for one type, mangled typeːspecːmethod. A default
is copied into every realizing type with the parameter substituted, so
the `show' a default calls is that type's own and the whole thing
stays static dispatch with nothing left at runtime.

The methods land in the ops registry beside inherent ones, which is
what lets (.show v) find them with no second lookup -- and makes the
duplicate check already there catch a spec method colliding with an
ops method or with another spec's. Coherence came free.

A realization's signatures are checked against the spec, arguments and
return type both, after substituting the parameter; a missing required
method is named, and so is one the spec has not got. That is what
makes a spec a contract rather than a naming convention.

The bound is asked rather than declared: realizes? goes into the macro
API beside type-of, and a generic calls it and raises its own error.
That is the return this document was written for -- `plain does not
realize ord' where the line is, instead of a wall of C errors from a
monomorphization two stages downstream.

A default is static even when the realization is pub, and does not
travel on export: every importing unit synthesizes its own copy, so
sending one too would be a second definition of one symbol. The cost
is that a module whose defaults call printf has to pub include
stdio.h -- which is also why pub include and pub import now work, both
having been accepted by read-public-interface and refused by semen.
2026-10-04 12:56:24 +03:00

152 lines
5.5 KiB
Scheme
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(import
scheme
brev-separate
(chicken base)
(chicken file)
(chicken load)
(chicken pathname)
(chicken process-context)
(chicken string)
fmt
matchable
reader
srfi-1
types
utils)
(define +persistent-module-paths+ (list))
;;; for guarding against multiple imports (sort of mandatory #pragma
;;; once)
(define +imported-modules+ (list))
(define (get-modules-public-forms module-list)
;; Module list is a list of symbols
;; How Sex handles modules:
;; For each module in a list, construct path, find module by path in
;; module path directories, extract public definitions from the
;; module, paste them in current one in emulation of C include
;; directives.
(fold-right append (list)
(map (fn (import-module (symbol->string x)))
module-list)))
(define (import-module name)
(let ((module-path (locate-module name)))
(assert module-path (fmt #f "Failed to find module " name " in "
(get-module-paths)))
(if (member module-path +imported-modules+)
(list)
(begin
(set! +imported-modules+ (cons module-path +imported-modules+))
(read-public-interface module-path)))))
(define (get-module-paths)
(cons (current-directory)
+persistent-module-paths+))
(define (locate-module name)
;; Module locations: relative to file being compiled, or in what was
;; in SEX_MODULE_PATH env var at the start of the process (see
;; load-persistent-module-paths function)
(let ((search-paths (get-module-paths)))
(let loop ((paths search-paths))
(if (null? paths)
#f
(or (module-exists? name (car paths))
(loop (cdr paths)))))))
(define (module-exists? name module-dir)
;; returns absolute path to module, if it exists
(and (directory-exists? module-dir)
(let ((module-path (make-absolute-pathname module-dir name "sex")))
(and (file-exists? module-path)
(file-readable? module-path)
module-path))))
(define (read-public-interface module-path)
;; pub fns are reduced to prototypes, other pub forms are just pasted.
;; Declaration order is kept: a module that declares a type and then a
;; pub fn taking it has to paste them that way round, or the prototype
;; names a type C has not seen.
(let ((raw-forms (read-from-file module-path)))
(reverse
(fold
process-public-interface-form
(list)
raw-forms))))
(define (public-fn-interface raw-form)
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
;; the importer can emit it above the declaration
(let ((form (strip-header-comments raw-form 5)))
(match form
(('pub 'fn name args ret)
form)
(('pub 'fn name args ret ('comment . _) . rest)
(public-fn-interface `(pub fn ,name ,args ,ret ,@rest)))
(('pub 'fn name args ret (? string? doc) . _)
`(pub fn ,name ,args ,ret ,doc))
(('pub 'fn name args ret . _)
`(pub fn ,name ,args ,ret)))))
;;; An ops block is not pub itself -- its methods are. The block leaves
;;; as a block, its pub methods reduced to prototypes, so the importer
;;; runs its own `process-ops' over it: prototypes alone would say that
;;; vec2ːlength exists without saying that vec2 has a `length', and
;;; (. v length) needs the second thing.
(define (ops-public-interface form)
(let ((type (and (pair? (cdr form)) (cadr form)))
(methods (filter (lambda (m)
(and (pair? m) (eq? (car m) 'pub)
(pair? (cdr m)) (eq? (cadr m) 'fn)))
(cddr form))))
(and (symbol? type)
(pair? methods)
(copy-form-source! form
`(ops ,type ,@(map public-fn-interface methods))))))
;;; A realization leaves with the methods written in it, reduced to
;;; prototypes. Its defaults do not travel: the importer synthesizes
;;; its own static copies from the spec, which is why a default is
;;; static even when the realization is pub.
(define (realize-public-interface form)
(let ((core (cdr form)))
(copy-form-source!
form
`(pub realize ,(second core)
;; pub goes back on at the realize, not on its methods
,@(map (lambda (m) (cdr (public-fn-interface (cons 'pub m))))
(remove comment-form? (cddr core)))))))
;;; TODO: use semen facilities to analyze modules
(define (process-public-interface-form form acc)
(match form
;; Reduced to a prototype, still `pub', so the importer declares it
;; with external linkage
(('pub 'fn . _)
(cons (copy-form-source! form (public-fn-interface form)) acc))
(('pub 'var . _)
(match-let ((('pub 'var name type . _) (strip-header-comments form 4)))
(cons (copy-form-source! form `(extern var ,name ,type)) acc)))
(('pub (or 'define 'defmacro 'enum 'import 'include 'record 'spec 'struct 'sum 'typedef 'union) . _)
(cons (copy-form-source! form (cdr form)) acc))
(('ops . _)
(let ((exported (ops-public-interface form)))
(if exported (cons exported acc) acc)))
(('pub 'realize . _)
(cons (realize-public-interface form) acc))
(('pub . _)
(sex-error form "pub must be followed by a definition" form))
(_ acc)))
(define (load-persistent-module-paths)
(let ((sex-module-path-env-var
(get-env-var "SEX_MODULE_PATH")))
(when sex-module-path-env-var
(set! +persistent-module-paths+
(map (lambda (p)
(make-absolute-pathname p #f #f))
(string-split sex-module-path-env-var ":"))))))