Compare commits
10 Commits
79d9860fc1
...
a8f8b23b49
| Author | SHA1 | Date | |
|---|---|---|---|
| a8f8b23b49 | |||
| 5b7ed9793b | |||
| 7a15cde637 | |||
| 4d83ae152b | |||
| b0275be368 | |||
| 199834466d | |||
| 360470d0e0 | |||
| 2d4e5f6b16 | |||
| 0d26b1f6a5 | |||
| ce236d876c |
50
semen.scm
50
semen.scm
@@ -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))
|
||||||
@@ -1661,7 +1695,9 @@
|
|||||||
(name (and (pair? (cdr f)) (symbol? (cadr f)) (cadr f))))
|
(name (and (pair? (cdr f)) (symbol? (cadr f)) (cadr f))))
|
||||||
;; An anonymous aggregate has a field list where the name would be,
|
;; An anonymous aggregate has a field list where the name would be,
|
||||||
;; and nothing can refer to it by name anyway
|
;; and nothing can refer to it by name anyway
|
||||||
(when name
|
;; ...and a bodyless one adds nothing to what is known: an importer
|
||||||
|
;; may be handed a declaration after the definition it declares
|
||||||
|
(when (and name (not (and (null? (cddr f)) (get-tag-info name))))
|
||||||
(case (car f)
|
(case (car f)
|
||||||
((struct) (add-struct name form))
|
((struct) (add-struct name form))
|
||||||
((union) (add-union name form))
|
((union) (add-union name form))
|
||||||
|
|||||||
@@ -36,6 +36,7 @@
|
|||||||
scheme
|
scheme
|
||||||
(scheme base) ; string->utf8, bytevector accessors
|
(scheme base) ; string->utf8, bytevector accessors
|
||||||
(chicken base)
|
(chicken base)
|
||||||
|
(chicken flonum) ; flonum-print-precision
|
||||||
fmt
|
fmt
|
||||||
srfi-1
|
srfi-1
|
||||||
srfi-13)
|
srfi-13)
|
||||||
@@ -97,6 +98,7 @@
|
|||||||
;;((|\|\||) 75)
|
;;((|\|\||) 75)
|
||||||
;;((.. ...) 77) ; Perl
|
;;((.. ...) 77) ; Perl
|
||||||
((?) 80)
|
((?) 80)
|
||||||
|
((conditional-else) 81) ; see c-if/expr
|
||||||
((= *= /= %= &= ^= <<= >>=) 85) ; |\|=| ; += -=
|
((= *= /= %= &= ^= <<= >>=) 85) ; |\|=| ; += -=
|
||||||
((comma) 90)
|
((comma) 90)
|
||||||
((=>) 90) ; Perl
|
((=>) 90) ; Perl
|
||||||
@@ -111,6 +113,13 @@
|
|||||||
|
|
||||||
(define (c-paren x) (cat "(" (c-expr x) ")"))
|
(define (c-paren x) (cat "(" (c-expr x) ")"))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: a subscript is postfix, tighter than
|
||||||
|
;; any operator, so what it subscripts is written at the level of
|
||||||
|
;; `.' -- (* q) comes out `(*q)[i]', not `*q[i]', which is *(q[i]).
|
||||||
|
;; The index has brackets of its own and starts afresh.
|
||||||
|
(define (c-subscript array index)
|
||||||
|
(cat (c-with-op "." (c-expr array)) "[" (c-with-op 'paren (c-expr index)) "]"))
|
||||||
|
|
||||||
;; `.' and `->' associate left, and their right operand is always a
|
;; `.' and `->' associate left, and their right operand is always a
|
||||||
;; name, so a member access written inside another is the left one
|
;; name, so a member access written inside another is the left one
|
||||||
;; and needs no parens of its own: `s.u.circle.r', not `((s.u).circle).r'
|
;; and needs no parens of its own: `s.u.circle.r', not `((s.u).circle).r'
|
||||||
@@ -184,7 +193,21 @@
|
|||||||
((8) (cat "0" (number->string x 8)))
|
((8) (cat "0" (number->string x 8)))
|
||||||
(else (dsp (number->string x))))
|
(else (dsp (number->string x))))
|
||||||
st))
|
st))
|
||||||
(dsp (number->string x))))
|
(dsp (c-format-flonum x))))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: a double in as few digits as read
|
||||||
|
;; back as the same double. number->string stops at
|
||||||
|
;; flonum-print-precision, 15 unless set, and 0.16666666666666666 came
|
||||||
|
;; out 0.166666666666667: a different double in the C. Seventeen
|
||||||
|
;; always do.
|
||||||
|
(define (c-format-flonum x)
|
||||||
|
(let ((precision (flonum-print-precision)))
|
||||||
|
(let try ((digits 15))
|
||||||
|
(flonum-print-precision digits)
|
||||||
|
(let ((written (number->string x)))
|
||||||
|
(if (or (= digits 17) (= (string->number written) x))
|
||||||
|
(begin (flonum-print-precision precision) written)
|
||||||
|
(try (+ digits 1)))))))
|
||||||
|
|
||||||
(define (c-format-string x)
|
(define (c-format-string x)
|
||||||
(lambda (st) ((cat #\" (apply-cat (c-string-escaped x)) #\") st)))
|
(lambda (st) ((cat #\" (apply-cat (c-string-escaped x)) #\") st)))
|
||||||
@@ -249,12 +272,9 @@
|
|||||||
;; non C-keywords must have some character invalid in a C
|
;; non C-keywords must have some character invalid in a C
|
||||||
;; identifier to avoid conflicts - by default we prefix %
|
;; identifier to avoid conflicts - by default we prefix %
|
||||||
((vector-ref)
|
((vector-ref)
|
||||||
((c-wrap-stmt
|
((c-wrap-stmt (c-subscript (cadr x) (caddr x))) st))
|
||||||
(cat (c-expr (cadr x)) "[" (c-expr (caddr x)) "]"))
|
|
||||||
st))
|
|
||||||
((vector-set!)
|
((vector-set!)
|
||||||
((c= (c-in-expr
|
((c= (c-in-expr (c-subscript (cadr x) (caddr x)))
|
||||||
(cat (c-expr (cadr x)) "[" (c-expr (caddr x)) "]"))
|
|
||||||
(c-expr (cadddr x)))
|
(c-expr (cadddr x)))
|
||||||
st))
|
st))
|
||||||
((extern/C) ((apply c-extern/C (cdr x)) st))
|
((extern/C) ((apply c-extern/C (cdr x)) st))
|
||||||
@@ -885,6 +905,10 @@
|
|||||||
;; Upstream wrote it bare whatever surrounded it, and `?:' binds
|
;; Upstream wrote it bare whatever surrounded it, and `?:' binds
|
||||||
;; looser than every binary operator: (/ 300 (if c c 1)) came out
|
;; looser than every binary operator: (/ 300 (if c c 1)) came out
|
||||||
;; `300 / c ? c : 1', a conditional on the quotient.
|
;; `300 / c ? c : 1', a conditional on the quotient.
|
||||||
|
;;
|
||||||
|
;; It groups to the right, so its else arm is written at
|
||||||
|
;; `conditional-else', just looser: a chain there stays bare,
|
||||||
|
;; `a ? x : b ? y : z', and an assignment does not.
|
||||||
(define (c-if/expr . args)
|
(define (c-if/expr . args)
|
||||||
(c-maybe-paren '? (apply c-if/expr/aux args)))
|
(c-maybe-paren '? (apply c-if/expr/aux args)))
|
||||||
|
|
||||||
@@ -897,9 +921,10 @@
|
|||||||
(c-expr p)))
|
(c-expr p)))
|
||||||
((pair? ls)
|
((pair? ls)
|
||||||
(cat (c-in-test (c-expr c)) " ? " (c-expr p) " : "
|
(cat (c-in-test (c-expr c)) " ? " (c-expr p) " : "
|
||||||
|
(c-with-op 'conditional-else
|
||||||
(if (pair? (cdr ls))
|
(if (pair? (cdr ls))
|
||||||
(lp (car ls) (cadr ls) (cddr ls))
|
(lp (car ls) (cadr ls) (cddr ls))
|
||||||
(lp 'else (car ls) '()))))
|
(lp 'else (car ls) '())))))
|
||||||
(else
|
(else
|
||||||
(c-or (c-in-test (c-expr c)) (c-expr p))))))
|
(c-or (c-in-test (c-expr c)) (c-expr p))))))
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
(module sex-modules
|
(module sex-modules
|
||||||
(get-modules-public-forms
|
(get-modules-public-forms
|
||||||
load-persistent-module-paths
|
load-persistent-module-paths
|
||||||
|
check-public-interface
|
||||||
|
module-dependencies
|
||||||
read-public-interface)
|
read-public-interface)
|
||||||
"sex-modules.scm")
|
"sex-modules.scm")
|
||||||
|
|||||||
224
sex-modules.scm
224
sex-modules.scm
@@ -78,17 +78,230 @@
|
|||||||
(file-readable? module-path)
|
(file-readable? module-path)
|
||||||
module-path))))
|
module-path))))
|
||||||
|
|
||||||
|
;;; Every module file read for this unit, first read first: what its
|
||||||
|
;;; output depends on, besides the unit itself
|
||||||
|
(define +module-dependencies+ (list))
|
||||||
|
|
||||||
|
(define (module-dependencies) (reverse +module-dependencies+))
|
||||||
|
|
||||||
|
(define (read-module path)
|
||||||
|
(unless (member path +module-dependencies+)
|
||||||
|
(set! +module-dependencies+ (cons path +module-dependencies+)))
|
||||||
|
(read-from-file path))
|
||||||
|
|
||||||
(define (read-public-interface module-path)
|
(define (read-public-interface module-path)
|
||||||
;; pub fns are reduced to prototypes, other pub forms are just pasted.
|
;; 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
|
;; 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
|
;; pub fn taking it has to paste them that way round, or the prototype
|
||||||
;; names a type C has not seen.
|
;; names a type C has not seen. The declarations of the types its
|
||||||
(let ((raw-forms (read-from-file module-path)))
|
;; importers see only behind a pointer go first.
|
||||||
|
(let ((raw-forms (read-module module-path)))
|
||||||
|
(append (check-public-interface raw-forms)
|
||||||
|
(interface-forms raw-forms))))
|
||||||
|
|
||||||
|
(define (interface-forms raw-forms)
|
||||||
|
(reverse (fold process-public-interface-form (list) raw-forms)))
|
||||||
|
|
||||||
|
;;; What importers can see of a module's types
|
||||||
|
;;;
|
||||||
|
;;; They are given the pub forms and nothing else, so a pub form that
|
||||||
|
;;; names a type not among them compiled here and failed there:
|
||||||
|
;;; (pub record shape ((cells (* cell)))) with `cell' private told the
|
||||||
|
;;; importer `unknown type name cell'. A record behind a pointer needs
|
||||||
|
;;; only declaring, and its declaration goes out with the pub forms.
|
||||||
|
;;; Held by value it needs its body; and a typedef or an enum has no
|
||||||
|
;;; declaration short of its definition. Those are errors here, where
|
||||||
|
;;; they were written. A name Sex has no declaration for is one only C
|
||||||
|
;;; knows, and is left to C.
|
||||||
|
|
||||||
|
;;; The bodyless declarations FORMS' importers need, in the order the
|
||||||
|
;;; pub forms ask for them. An error when a pub form needs more than a
|
||||||
|
;;; declaration of a type they cannot see.
|
||||||
|
(define (check-public-interface forms)
|
||||||
|
(let ((visible (visible-types forms))
|
||||||
|
(hidden (hidden-types forms)))
|
||||||
(reverse
|
(reverse
|
||||||
(fold
|
(fold
|
||||||
process-public-interface-form
|
(lambda (form acc)
|
||||||
|
(fold (lambda (use acc) (check-type-use form use visible hidden acc))
|
||||||
|
acc
|
||||||
|
(append-map (lambda (type) (type-uses type #f))
|
||||||
|
(interface-types form))))
|
||||||
(list)
|
(list)
|
||||||
raw-forms))))
|
forms))))
|
||||||
|
|
||||||
|
;;; USE is (name . behind-a-pointer?)
|
||||||
|
(define (check-type-use form use visible hidden acc)
|
||||||
|
(let* ((name (car use))
|
||||||
|
(seen (assq name visible))
|
||||||
|
(kept (assq name hidden)))
|
||||||
|
(cond
|
||||||
|
((and seen (third seen)) acc) ; pub with its body
|
||||||
|
((and seen (cdr use)) acc) ; a pub handle, held by one
|
||||||
|
(seen
|
||||||
|
(sex-error form "only a handle of this type is pub, so hold it through a pointer"
|
||||||
|
name))
|
||||||
|
((not kept) acc)
|
||||||
|
((not (memq (second kept) '(record struct union sum)))
|
||||||
|
(sex-error form (string-append "importers cannot see this "
|
||||||
|
(symbol->string (second kept))
|
||||||
|
", and C declares one only by defining it; "
|
||||||
|
(if (fourth kept)
|
||||||
|
"import its module with pub"
|
||||||
|
"make it pub"))
|
||||||
|
name))
|
||||||
|
((not (cdr use))
|
||||||
|
(sex-error form (string-append "importers cannot see this type, held by value; "
|
||||||
|
(if (fourth kept)
|
||||||
|
"import its module with pub"
|
||||||
|
"make it pub")
|
||||||
|
", or hold it through a pointer")
|
||||||
|
name))
|
||||||
|
((any (lambda (f) (eq? name (cadr f))) acc) acc)
|
||||||
|
(else
|
||||||
|
(cons (copy-form-source!
|
||||||
|
form
|
||||||
|
(list (if (memq (second kept) '(record sum)) 'record (second kept))
|
||||||
|
name))
|
||||||
|
acc)))))
|
||||||
|
|
||||||
|
;;; The type forms a pub form shows its importers
|
||||||
|
;;;
|
||||||
|
;;; (pub fn f ((c (* cell)) (n int)) shape) -> ((* cell) int shape)
|
||||||
|
(define (interface-types form)
|
||||||
|
(match form
|
||||||
|
(('pub 'fn . _)
|
||||||
|
(match (strip-header-comments form 5)
|
||||||
|
(('pub 'fn name (args ...) ret . _) (signature-types args ret))
|
||||||
|
(_ (list))))
|
||||||
|
(('pub 'var . _)
|
||||||
|
(match (strip-header-comments form 4)
|
||||||
|
(('pub 'var name type . _) (list type))
|
||||||
|
(_ (list))))
|
||||||
|
(('pub 'typedef name target) (list target))
|
||||||
|
(('pub 'define name . body) body)
|
||||||
|
(('pub (or 'record 'struct 'union) name . rest)
|
||||||
|
(field-types (find list? (remove comment-form? rest))))
|
||||||
|
(('pub 'sum name (variants ...))
|
||||||
|
(append-map (lambda (v)
|
||||||
|
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
|
||||||
|
(remove comment-form? variants)))
|
||||||
|
(('ops type . methods)
|
||||||
|
(append-map interface-types
|
||||||
|
(filter (lambda (m) (and (pair? m) (eq? (car m) 'pub))) methods)))
|
||||||
|
(('pub 'realize spec . methods)
|
||||||
|
(append-map (lambda (m) (interface-types (cons 'pub m)))
|
||||||
|
(remove comment-form? methods)))
|
||||||
|
(_ (list))))
|
||||||
|
|
||||||
|
(define (field-types fields)
|
||||||
|
(if (list? fields) (map second (normalize-fields fields)) (list)))
|
||||||
|
|
||||||
|
(define (signature-types args ret)
|
||||||
|
(cons ret (map arg-type (remove comment-form? args))))
|
||||||
|
|
||||||
|
;;; (f1 float) -> float, (n unsigned int) -> (unsigned int), (int) -> (int)
|
||||||
|
(define (arg-type arg)
|
||||||
|
(cond ((not (named-arg? arg)) arg)
|
||||||
|
((null? (cddr arg)) (second arg))
|
||||||
|
(else (cdr arg))))
|
||||||
|
|
||||||
|
;;; Each name TYPE mentions, and whether it is behind a pointer:
|
||||||
|
;;;
|
||||||
|
;;; (* const cell) -> ((const . #t) (cell . #t))
|
||||||
|
;;; [cell 4] -> ((cell . #f))
|
||||||
|
;;; (fn ((cell)) int) -> ((cell . #t) (int . #t)) ; declaring is enough
|
||||||
|
(define (type-uses type behind?)
|
||||||
|
(cond
|
||||||
|
((symbol? type) (list (cons type behind?)))
|
||||||
|
((not (pair? type)) (list))
|
||||||
|
(else
|
||||||
|
(case (car type)
|
||||||
|
((*) (append-map (lambda (t) (type-uses t #t)) (cdr type)))
|
||||||
|
((¤) (let ((element (array-element-type type)))
|
||||||
|
(append (if element (type-uses element behind?) (list))
|
||||||
|
(if (array-bound? type) (bound-uses (last type)) (list)))))
|
||||||
|
((fn closure)
|
||||||
|
(if (and (pair? (cdr type)) (list? (second type)))
|
||||||
|
(append-map (lambda (t) (type-uses t #t))
|
||||||
|
(signature-types (second type) (last type)))
|
||||||
|
(list)))
|
||||||
|
((struct union enum)
|
||||||
|
(if (and (pair? (cdr type)) (symbol? (second type)))
|
||||||
|
(list (cons (second type) behind?))
|
||||||
|
(list)))
|
||||||
|
(else (append-map (lambda (t) (type-uses t behind?)) type))))))
|
||||||
|
|
||||||
|
;;; [int (* 2 N)] needs N, as a value: there is no declaring it
|
||||||
|
(define (bound-uses bound)
|
||||||
|
(cond ((symbol? bound) (list (cons bound #f)))
|
||||||
|
((pair? bound) (append-map bound-uses (cdr bound)))
|
||||||
|
(else (list))))
|
||||||
|
|
||||||
|
;;; The types and constants FORMS declare, as (name kind body?):
|
||||||
|
;;;
|
||||||
|
;;; (pub record tally) -> (tally record #f)
|
||||||
|
;;; (sum colour ((none ()))) -> (colour sum #t)
|
||||||
|
;;; (define (sq x) (* x x)) -> (sq define #t)
|
||||||
|
(define (declared-types forms)
|
||||||
|
(filter-map
|
||||||
|
(lambda (form)
|
||||||
|
(and (pair? form)
|
||||||
|
(let* ((core (if (pub-form? form) (cdr form) form))
|
||||||
|
(name (and (pair? core) (pair? (cdr core))
|
||||||
|
(if (and (eq? (car core) 'define) (pair? (second core)))
|
||||||
|
(car (second core))
|
||||||
|
(second core)))))
|
||||||
|
(and (memq (car core) '(record struct union enum sum typedef define))
|
||||||
|
(symbol? name)
|
||||||
|
(list name
|
||||||
|
(car core)
|
||||||
|
(or (memq (car core) '(typedef define))
|
||||||
|
(any list? (remove comment-form? (cddr core)))))))))
|
||||||
|
forms))
|
||||||
|
|
||||||
|
(define (pub-form? form) (and (pair? form) (eq? (car form) 'pub)))
|
||||||
|
|
||||||
|
;;; Pub here or through a pub import. A type declared pub both bodyless
|
||||||
|
;;; and whole is whole
|
||||||
|
(define (visible-types forms)
|
||||||
|
(let ((all (append (declared-types (filter pub-form? forms))
|
||||||
|
(append-map exported-types (imported-modules forms #t)))))
|
||||||
|
(map (lambda (entry)
|
||||||
|
(list (first entry) (second entry)
|
||||||
|
(any third (filter (lambda (e) (eq? (first e) (first entry))) all))))
|
||||||
|
all)))
|
||||||
|
|
||||||
|
;;; Declared here without pub, or brought by an import without it, as
|
||||||
|
;;; (name kind body? imported?)
|
||||||
|
(define (hidden-types forms)
|
||||||
|
(append (map (lambda (entry) (append entry (list #f)))
|
||||||
|
(declared-types (remove pub-form? forms)))
|
||||||
|
(map (lambda (entry) (append entry (list #t)))
|
||||||
|
(append-map exported-types (imported-modules forms #f)))))
|
||||||
|
|
||||||
|
;;; The names of the modules FORMS import, with pub or without
|
||||||
|
(define (imported-modules forms pub?)
|
||||||
|
(append-map (lambda (form)
|
||||||
|
(match form
|
||||||
|
(('pub 'import . modules) (if pub? modules (list)))
|
||||||
|
(('import . modules) (if pub? (list) modules))
|
||||||
|
(_ (list))))
|
||||||
|
forms))
|
||||||
|
|
||||||
|
;;; The types a module gives its importers, its pub imports' included.
|
||||||
|
;;; Two modules may import each other, so each is read once
|
||||||
|
(define (exported-types module)
|
||||||
|
(let read ((modules (list module)) (seen (list)) (acc (list)))
|
||||||
|
(if (null? modules)
|
||||||
|
acc
|
||||||
|
(let ((path (locate-module (symbol->string (car modules)))))
|
||||||
|
(if (or (not path) (member path seen))
|
||||||
|
(read (cdr modules) seen acc)
|
||||||
|
(let ((forms (interface-forms (read-module path))))
|
||||||
|
(read (append (cdr modules) (imported-modules forms #f))
|
||||||
|
(cons path seen)
|
||||||
|
(append acc (declared-types forms)))))))))
|
||||||
|
|
||||||
(define (public-fn-interface raw-form)
|
(define (public-fn-interface raw-form)
|
||||||
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
|
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
|
||||||
@@ -159,6 +372,5 @@
|
|||||||
(get-env-var "SEX_MODULE_PATH")))
|
(get-env-var "SEX_MODULE_PATH")))
|
||||||
(when sex-module-path-env-var
|
(when sex-module-path-env-var
|
||||||
(set! +persistent-module-paths+
|
(set! +persistent-module-paths+
|
||||||
(map (lambda (p)
|
(map to-absolute-pathname
|
||||||
(make-absolute-pathname p #f #f))
|
|
||||||
(string-split sex-module-path-env-var ":"))))))
|
(string-split sex-module-path-env-var ":"))))))
|
||||||
|
|||||||
71
sexc.scm
71
sexc.scm
@@ -59,10 +59,16 @@
|
|||||||
(value #f)
|
(value #f)
|
||||||
(single-char #\m))
|
(single-char #\m))
|
||||||
(output ,(fmt #f "Write output to file. Default file name is a.out." nl
|
(output ,(fmt #f "Write output to file. Default file name is a.out." nl
|
||||||
(pad padding) "If -E or -m options are provided, defaults to stdout")
|
(pad padding) "If -C or -m options are provided, defaults to stdout;" nl
|
||||||
|
(pad padding) "`-' is stdout too")
|
||||||
(required #f)
|
(required #f)
|
||||||
(value #t)
|
(value #t)
|
||||||
(single-char #\o))
|
(single-char #\o))
|
||||||
|
(depfile
|
||||||
|
,(fmt #f "Write a make rule for the output to FILE: it depends on" nl
|
||||||
|
(pad padding) "the source and on every module read for it. Needs -o")
|
||||||
|
(required #f)
|
||||||
|
(value #t))
|
||||||
(line-directives
|
(line-directives
|
||||||
,(fmt #f "How much #line information to emit: statement (default)," nl
|
,(fmt #f "How much #line information to emit: statement (default)," nl
|
||||||
(pad padding) "toplevel, or none. `statement' is what makes a debugger" nl
|
(pad padding) "toplevel, or none. `statement' is what makes a debugger" nl
|
||||||
@@ -100,6 +106,17 @@
|
|||||||
(list)
|
(list)
|
||||||
(cdr tail))))
|
(cdr tail))))
|
||||||
|
|
||||||
|
;;; getopt-long reads a lone `-' as an option, so `-o -' died there as
|
||||||
|
;;; one missing its value. `--output=-' is the spelling it takes
|
||||||
|
(define (spell-stdout-output argv)
|
||||||
|
(let loop ((as argv) (acc (list)))
|
||||||
|
(cond ((null? as) (reverse acc))
|
||||||
|
((and (member (car as) '("-o" "--output"))
|
||||||
|
(pair? (cdr as))
|
||||||
|
(string=? (cadr as) "-"))
|
||||||
|
(loop (cddr as) (cons "--output=-" acc)))
|
||||||
|
(else (loop (cdr as) (cons (car as) acc))))))
|
||||||
|
|
||||||
(define (get-rest-args args)
|
(define (get-rest-args args)
|
||||||
(cdr (assoc '@ args)))
|
(cdr (assoc '@ args)))
|
||||||
|
|
||||||
@@ -126,7 +143,7 @@
|
|||||||
(car rest-args))))
|
(car rest-args))))
|
||||||
|
|
||||||
(define (write-to-file-or-stdout output what)
|
(define (write-to-file-or-stdout output what)
|
||||||
(if (eq? output 'default)
|
(if (or (eq? output 'default) (equal? output "-"))
|
||||||
(what)
|
(what)
|
||||||
(with-output-to-file output
|
(with-output-to-file output
|
||||||
(fn (what)))))
|
(fn (what)))))
|
||||||
@@ -138,6 +155,28 @@
|
|||||||
(map pp sex-forms)
|
(map pp sex-forms)
|
||||||
(emit-c sex-forms)))))
|
(emit-c sex-forms)))))
|
||||||
|
|
||||||
|
;;; app.o: app.sex /m/greet.sex, and an empty rule per module, so that
|
||||||
|
;;; make takes a module deleted since as out of date rather than as a
|
||||||
|
;;; file it has no rule to make
|
||||||
|
(define (dependency-rule target source modules)
|
||||||
|
(let ((spell (lambda (path) (string-translate* path '((" " . "\\ "))))))
|
||||||
|
(apply string-append
|
||||||
|
(spell target) ":"
|
||||||
|
(append (map (lambda (path) (string-append " " (spell path)))
|
||||||
|
(if source (cons source modules) modules))
|
||||||
|
(list "\n")
|
||||||
|
(map (lambda (path) (string-append "\n" (spell path) ":\n"))
|
||||||
|
modules)))))
|
||||||
|
|
||||||
|
(define (write-dependencies depfile output input)
|
||||||
|
(when (or (eq? output 'default) (equal? output "-"))
|
||||||
|
(error "--depfile needs -o, to name what depends on the modules"))
|
||||||
|
(with-output-to-file depfile
|
||||||
|
(lambda ()
|
||||||
|
(display (dependency-rule output
|
||||||
|
(and (not (eq? input 'stdin)) input)
|
||||||
|
(module-dependencies))))))
|
||||||
|
|
||||||
(define (compile-to-file sex-forms output args cc-args)
|
(define (compile-to-file sex-forms output args cc-args)
|
||||||
"Hand the generated C to the C compiler. Returns the compiler's exit
|
"Hand the generated C to the C compiler. Returns the compiler's exit
|
||||||
status, which is ours to pass on."
|
status, which is ours to pass on."
|
||||||
@@ -168,11 +207,15 @@ status, which is ours to pass on."
|
|||||||
(delete-file* c-file)
|
(delete-file* c-file)
|
||||||
(if normal-exit? status 1)))))))
|
(if normal-exit? status 1)))))))
|
||||||
|
|
||||||
(define (semantic-process-forms raw-forms input-source)
|
;;; The unit's own forms are checked for what its importers will need,
|
||||||
|
;;; the prelude's not: every unit has those
|
||||||
|
(define (semantic-process-forms unit-forms input-source)
|
||||||
|
(let ((process (lambda ()
|
||||||
|
(check-public-interface unit-forms)
|
||||||
|
(semen-process (append prelude unit-forms)))))
|
||||||
(if (eq? input-source 'stdin)
|
(if (eq? input-source 'stdin)
|
||||||
(semen-process raw-forms)
|
(process)
|
||||||
(with-directory input-source
|
(with-directory input-source (process)))))
|
||||||
(semen-process raw-forms))))
|
|
||||||
|
|
||||||
(define prelude
|
(define prelude
|
||||||
(append
|
(append
|
||||||
@@ -196,7 +239,7 @@ status, which is ours to pass on."
|
|||||||
(let* ((argv (command-line-arguments))
|
(let* ((argv (command-line-arguments))
|
||||||
(raw-args (args-before-separator argv))
|
(raw-args (args-before-separator argv))
|
||||||
(cc-args (args-after-separator argv))
|
(cc-args (args-after-separator argv))
|
||||||
(args (getopt-long raw-args
|
(args (getopt-long (spell-stdout-output raw-args)
|
||||||
opts-grammar))
|
opts-grammar))
|
||||||
(output (get-arg args 'output 'default))
|
(output (get-arg args 'output 'default))
|
||||||
(help (help-arg? args))
|
(help (help-arg? args))
|
||||||
@@ -216,16 +259,18 @@ status, which is ours to pass on."
|
|||||||
(platform-features))
|
(platform-features))
|
||||||
(cli-features args)))
|
(cli-features args)))
|
||||||
|
|
||||||
|
;; before the interface too, which asks the modules it imports
|
||||||
|
;; what types they export
|
||||||
|
(load-persistent-module-paths)
|
||||||
|
|
||||||
(when (get-arg args 'public-interface #f)
|
(when (get-arg args 'public-interface #f)
|
||||||
(assert (not (eq? input 'stdin)) "Error: --public-interface requires file argument")
|
(assert (not (eq? input 'stdin)) "Error: --public-interface requires file argument")
|
||||||
|
|
||||||
(write-to-file-or-stdout
|
(write-to-file-or-stdout
|
||||||
output
|
output
|
||||||
(fn
|
(fn
|
||||||
(map pp (reverse
|
(map pp (read-public-interface input))))
|
||||||
(read-public-interface input)))))
|
|
||||||
(return #f))
|
(return #f))
|
||||||
(load-persistent-module-paths)
|
|
||||||
|
|
||||||
;; The file name in a #line directive now comes from the form's
|
;; The file name in a #line directive now comes from the form's
|
||||||
;; own recorded location, so imported modules report themselves
|
;; own recorded location, so imported modules report themselves
|
||||||
@@ -235,8 +280,10 @@ status, which is ours to pass on."
|
|||||||
(not (get-arg args 'line-directives #f)))
|
(not (get-arg args 'line-directives #f)))
|
||||||
(sex-line-directives 'none))
|
(sex-line-directives 'none))
|
||||||
|
|
||||||
(let* ((raw-forms (append prelude (read-raw-forms input)))
|
(let ((sex-forms (semantic-process-forms (read-raw-forms input) input)))
|
||||||
(sex-forms (semantic-process-forms raw-forms input)))
|
(let ((depfile (get-arg args 'depfile #f)))
|
||||||
|
(when depfile
|
||||||
|
(write-dependencies depfile output input)))
|
||||||
(if (or (get-arg args 'macro-expand #f)
|
(if (or (get-arg args 'macro-expand #f)
|
||||||
(get-arg args 'emit-c #f))
|
(get-arg args 'emit-c #f))
|
||||||
;; Emit processed and macro-expanded sex code, or emit C code
|
;; Emit processed and macro-expanded sex code, or emit C code
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
|
|||||||
MODULES = utils types infer sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
|
MODULES = utils types infer sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
|
||||||
SEX_OBJ = $(MODULES:%=%.o)
|
SEX_OBJ = $(MODULES:%=%.o)
|
||||||
|
|
||||||
TESTS = basic semen reader fmt-c-writer utils line-directives codegen encapsulation args types infer
|
TESTS = basic semen reader fmt-c-writer utils line-directives codegen encapsulation args module-path types infer
|
||||||
TEST_SRCS = $(TESTS:%=%.scm)
|
TEST_SRCS = $(TESTS:%=%.scm)
|
||||||
|
|
||||||
sex-tests: run.scm $(TEST_SRCS) $(SEX_OBJ)
|
sex-tests: run.scm $(TEST_SRCS) $(SEX_OBJ)
|
||||||
|
|||||||
@@ -52,4 +52,32 @@
|
|||||||
|
|
||||||
(test "an empty command line is handled"
|
(test "an empty command line is handled"
|
||||||
'()
|
'()
|
||||||
(args-before-separator '())))
|
(args-before-separator '()))
|
||||||
|
|
||||||
|
;; getopt-long reads a lone `-' as an option and refused `-o -', so
|
||||||
|
;; it is handed the one spelling it takes a dash in
|
||||||
|
(test "-o - reaches getopt-long as a value"
|
||||||
|
'("x.sex" "--output=-" "-C")
|
||||||
|
(spell-stdout-output '("x.sex" "-o" "-" "-C")))
|
||||||
|
(test "and so does --output -"
|
||||||
|
'("--output=-")
|
||||||
|
(spell-stdout-output '("--output" "-")))
|
||||||
|
(test "any other output is left alone"
|
||||||
|
'("-o" "out" "-")
|
||||||
|
(spell-stdout-output '("-o" "out" "-"))))
|
||||||
|
|
||||||
|
;;; --depfile: an import pastes another file's forms into the output, so
|
||||||
|
;;; the output depends on that file as a C object does on a header
|
||||||
|
(test-group "dependency rule"
|
||||||
|
(test "the target, the source and the modules, then a rule per module"
|
||||||
|
"app.o: app.sex /m/greet.sex /m/ledger.sex\n\n/m/greet.sex:\n\n/m/ledger.sex:\n"
|
||||||
|
(dependency-rule "app.o" "app.sex" '("/m/greet.sex" "/m/ledger.sex")))
|
||||||
|
(test "no modules, no rules of their own"
|
||||||
|
"app.o: app.sex\n"
|
||||||
|
(dependency-rule "app.o" "app.sex" '()))
|
||||||
|
(test "a space is escaped, the way make reads it"
|
||||||
|
"a\\ b.o: a\\ b.sex\n"
|
||||||
|
(dependency-rule "a b.o" "a b.sex" '()))
|
||||||
|
(test "stdin is no file to depend on"
|
||||||
|
"app.o: /m/greet.sex\n\n/m/greet.sex:\n"
|
||||||
|
(dependency-rule "app.o" #f '("/m/greet.sex"))))
|
||||||
|
|||||||
@@ -167,7 +167,30 @@ whitespace aside, THEN?"
|
|||||||
"int x = a ? a : b"))
|
"int x = a ? a : b"))
|
||||||
(test-assert "left bare as an argument"
|
(test-assert "left bare as an argument"
|
||||||
(emits? (in-fn "(g (if a a b) b)")
|
(emits? (in-fn "(g (if a a b) b)")
|
||||||
"g(a ? a : b, b)")))
|
"g(a ? a : b, b)"))
|
||||||
|
;; ?: groups to the right, so a chain needs none in its else arm
|
||||||
|
(test-assert "a chain in the else arm is left bare"
|
||||||
|
(emits? (in-fn "(var x int (if a a (if b b 0)))")
|
||||||
|
"a ? a : b ? b : 0"))
|
||||||
|
(test-assert "but an assignment there is not"
|
||||||
|
(emits? (in-fn "(var x int (if a a (= b 1)))")
|
||||||
|
"a ? a : (b = 1)")))
|
||||||
|
|
||||||
|
;; A subscript binds tighter than anything, so what it subscripts is
|
||||||
|
;; parenthesised whenever it is an operator: `*q[i]' is *(q[i])
|
||||||
|
(test-group "subscript precedence"
|
||||||
|
(test-assert "a dereference"
|
||||||
|
(emits? "(fn f ((q (* * int)) (i int)) int (return [(* q) i]))" "(*q)[i]"))
|
||||||
|
(test-assert "a sum"
|
||||||
|
(emits? "(fn f ((p (* int))) int (return [(+ p 1) 0]))" "(p + 1)[0]"))
|
||||||
|
(test-assert "a cast"
|
||||||
|
(emits? "(fn f ((v (* void))) int (return [(cast v (* int)) 0]))" "((int *)v)[0]"))
|
||||||
|
(test-assert "a path is left bare"
|
||||||
|
(emits? (in-line "(var x int [(. p x) 0])") "p.x[0]"))
|
||||||
|
;; ...while the index is in brackets of its own
|
||||||
|
(test-assert "an index is never parenthesised"
|
||||||
|
(emits? "(fn f ((p (* int)) (i int)) int (return (cast [p (+ i 1)] int)))"
|
||||||
|
"(int)p[i + 1]")))
|
||||||
|
|
||||||
;; A comment among a call's arguments used to become an argument,
|
;; A comment among a call's arguments used to become an argument,
|
||||||
;; and c-apply put a comma on each side of it -- which does not
|
;; and c-apply put a comma on each side of it -- which does not
|
||||||
@@ -766,6 +789,17 @@ whitespace aside, THEN?"
|
|||||||
(test-assert "a subscript"
|
(test-assert "a subscript"
|
||||||
(emits? (in-fn "(var xs [int 4]) (var n size-t (sizeof [xs 0]))") "sizeof(xs[0])")))
|
(emits? (in-fn "(var xs [int 4]) (var n size-t (sizeof [xs 0]))") "sizeof(xs[0])")))
|
||||||
|
|
||||||
|
;; A double is written in as few digits as read back as the same
|
||||||
|
;; double: CHICKEN prints 15 by default, and 0.16666666666666666 came
|
||||||
|
;; out 0.166666666666667, another number
|
||||||
|
(test-group "double literals"
|
||||||
|
(test-assert "keep every digit they need"
|
||||||
|
(emits? (in-fn "(var d double 0.16666666666666666)") "d = 0.16666666666666666;"))
|
||||||
|
(test-assert "and gain none they do not"
|
||||||
|
(emits? (in-fn "(var d double 0.1)") "d = 0.1;"))
|
||||||
|
(test-assert "sixteen when that is enough"
|
||||||
|
(emits? (in-fn "(var d double 0.9871465422297019)") "d = 0.9871465422297019;")))
|
||||||
|
|
||||||
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
||||||
;; stayed a call, and (* x D) regrouped with D's own operator
|
;; stayed a call, and (* x D) regrouped with D's own operator
|
||||||
(test-group "defines"
|
(test-group "defines"
|
||||||
|
|||||||
@@ -8,6 +8,22 @@
|
|||||||
;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm,
|
;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm,
|
||||||
;;; which run.scm includes first.
|
;;; which run.scm includes first.
|
||||||
|
|
||||||
|
(import sex-modules)
|
||||||
|
|
||||||
|
(define (interface-of source)
|
||||||
|
"The declarations SOURCE, compiled as a module, hands its importers
|
||||||
|
ahead of its pub forms."
|
||||||
|
(check-public-interface
|
||||||
|
(with-input-from-string source
|
||||||
|
(lambda ()
|
||||||
|
(parameterize ((current-source-file "codegen.sex"))
|
||||||
|
(parse-all (current-input-port)))))))
|
||||||
|
|
||||||
|
(define (interface-reports? source fragment)
|
||||||
|
(let ((m (handle-exceptions e ((condition-property-accessor 'exn 'message) e)
|
||||||
|
(begin (interface-of source) #f))))
|
||||||
|
(and m (string-contains m fragment) #t)))
|
||||||
|
|
||||||
(define (ops-src . body)
|
(define (ops-src . body)
|
||||||
(string-append
|
(string-append
|
||||||
"(record vec2 ((x float) (y float)))"
|
"(record vec2 ((x float) (y float)))"
|
||||||
@@ -18,6 +34,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 +101,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.
|
||||||
@@ -271,4 +302,66 @@
|
|||||||
"arm is written twice"))
|
"arm is written twice"))
|
||||||
(test-assert "and a scrutinee that is not a sum"
|
(test-assert "and a scrutinee that is not a sum"
|
||||||
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
||||||
"match wants a sum"))))
|
"match wants a sum")))
|
||||||
|
|
||||||
|
;; An importer is given the pub forms and nothing else, so a type one
|
||||||
|
;; of them names has to be among them, or C is told no more of it
|
||||||
|
;; than a declaration can say
|
||||||
|
(test-group "what importers can see"
|
||||||
|
(test "a private record behind a pointer is declared for them"
|
||||||
|
'((record cell))
|
||||||
|
(interface-of "(record cell ((v int))) (pub record shape ((c (* cell))))"))
|
||||||
|
(test "once"
|
||||||
|
'((record cell))
|
||||||
|
(interface-of "(record cell ((v int))) (pub fn f ((a (* cell)) (b (* cell))) void)"))
|
||||||
|
(test "a struct as a struct"
|
||||||
|
'((struct cell))
|
||||||
|
(interface-of "(struct cell ((v int))) (pub fn f ((a (* struct cell))) void)"))
|
||||||
|
(test "a pub one needs nothing"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub record cell ((v int))) (pub record shape ((c cell)))"))
|
||||||
|
(test "nor one only C knows"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub fn f ((p (* FILE)) (n size-t)) void)"))
|
||||||
|
(test "nor a private fn's"
|
||||||
|
'()
|
||||||
|
(interface-of "(record cell ((v int))) (fn f ((c cell)) void)"))
|
||||||
|
(test-assert "held by value it is an error"
|
||||||
|
(interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))"
|
||||||
|
"held by value"))
|
||||||
|
(test-assert "naming the line"
|
||||||
|
(interface-reports? "(record cell ((v int)))\n(pub record shape ((c cell)))"
|
||||||
|
"codegen.sex:2:"))
|
||||||
|
(test-assert "in an array too"
|
||||||
|
(interface-reports? "(record cell ((v int))) (pub record shape ((cs [cell 4])))"
|
||||||
|
"held by value"))
|
||||||
|
(test-assert "returned"
|
||||||
|
(interface-reports? "(record cell ((v int))) (pub fn f () cell)" "held by value"))
|
||||||
|
(test-assert "or taken by a pub method"
|
||||||
|
(interface-reports? "(record cell ((v int))) (ops cell (pub fn get ((self cell)) int))"
|
||||||
|
"held by value"))
|
||||||
|
(test-assert "a typedef has no declaration to give, pointer or not"
|
||||||
|
(interface-reports? "(typedef small int) (pub fn f ((p (* small))) void)"
|
||||||
|
"this typedef, and C declares one only by defining it"))
|
||||||
|
(test-assert "nor has an enum"
|
||||||
|
(interface-reports? "(enum mood (up down)) (pub var m (* enum mood))"
|
||||||
|
"this enum, and C declares"))
|
||||||
|
(test-assert "a pub handle is held through a pointer"
|
||||||
|
(interface-reports? "(pub record h) (record h ((n int))) (pub fn f ((x h)) void)"
|
||||||
|
"only a handle"))
|
||||||
|
(test "which is all it takes"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub record h) (record h ((n int))) (pub fn f ((x (* h))) void)"))
|
||||||
|
;; A constant has no declaration either, and an array bound or a
|
||||||
|
;; pub define's body hands its name to importers all the same
|
||||||
|
(test-assert "a constant sizing a pub record's array"
|
||||||
|
(interface-reports? "(define N 4) (pub record r ((xs [int N])))"
|
||||||
|
"this define, and C declares one only by defining it"))
|
||||||
|
(test-assert "or used by a pub define"
|
||||||
|
(interface-reports? "(define N 4) (pub define M (* 2 N))" "this define"))
|
||||||
|
(test "is fine when pub"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub define N 4) (pub record r ((xs [int N])))"))
|
||||||
|
(test "as is a literal bound"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub record r ((xs [int 4])))"))))
|
||||||
|
|||||||
22
tests/module-path.scm
Normal file
22
tests/module-path.scm
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
;;; Where imports are looked for. SEX_MODULE_PATH is read once, at the
|
||||||
|
;;; start, from the directory sexc was started in.
|
||||||
|
|
||||||
|
(import (chicken pathname)
|
||||||
|
(chicken process-context)
|
||||||
|
sex-modules)
|
||||||
|
|
||||||
|
(test-group "module path"
|
||||||
|
;; make-absolute-pathname took `src' for a directory under the root,
|
||||||
|
;; and an import searched /src/
|
||||||
|
(set-environment-variable! "SEX_MODULE_PATH" "src:/abs/lib")
|
||||||
|
(load-persistent-module-paths)
|
||||||
|
(test "a relative entry is from the working directory"
|
||||||
|
(make-absolute-pathname (current-directory) "src")
|
||||||
|
(second (get-module-paths)))
|
||||||
|
(test "an absolute one is as written"
|
||||||
|
"/abs/lib"
|
||||||
|
(third (get-module-paths)))
|
||||||
|
|
||||||
|
(set-environment-variable! "SEX_MODULE_PATH" "")
|
||||||
|
(load-persistent-module-paths)
|
||||||
|
(unset-environment-variable! "SEX_MODULE_PATH"))
|
||||||
@@ -17,15 +17,31 @@
|
|||||||
# alone, its fields left behind and its methods still found.
|
# alone, its fields left behind and its methods still found.
|
||||||
#
|
#
|
||||||
# The public forms carry comments in their headers, which the reduction
|
# The public forms carry comments in their headers, which the reduction
|
||||||
# to a prototype and to an extern both have to look past.
|
# to a prototype and to an extern both have to look past. And a type the
|
||||||
|
# app cannot see -- private to greet, or from a module greet imports
|
||||||
|
# without `pub' -- crosses behind a pointer, declared and no more.
|
||||||
|
#
|
||||||
|
# The interface prints as importers get it: the declarations first, then
|
||||||
|
# the pub forms in the order they were written.
|
||||||
|
#
|
||||||
|
# The app's dependency rule names both modules: greet, which it imports,
|
||||||
|
# and ledger, which only greet does but which decides what the app is
|
||||||
|
# told of it.
|
||||||
|
|
||||||
SEXC ?= ../../sexc
|
SEXC ?= ../../sexc
|
||||||
|
|
||||||
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\ncolour 123
|
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\nboard 42\nledger 2\ncolour 123
|
||||||
|
|
||||||
check:
|
check:
|
||||||
|
@if [ "`$(SEXC) --public-interface greet.sex | head -3`" != "`printf '(record cell)\n(record ledger)\n(include stdio.h)'`" ]; then \
|
||||||
|
echo "modules FAILED, the interface begins:"; $(SEXC) --public-interface greet.sex | head -3; exit 1; \
|
||||||
|
fi
|
||||||
|
@$(SEXC) ledger.sex -c -o ledger.o
|
||||||
@$(SEXC) greet.sex -c -o greet.o
|
@$(SEXC) greet.sex -c -o greet.o
|
||||||
@$(SEXC) greet-app.sex -o greet-app -- greet.o
|
@$(SEXC) greet-app.sex -o greet-app --depfile=greet-app.d -- greet.o ledger.o
|
||||||
|
@if ! head -1 greet-app.d | grep -q '^greet-app: greet-app.sex .*/greet.sex .*/ledger.sex$$'; then \
|
||||||
|
echo "modules FAILED, the dependency rule is:"; cat greet-app.d; $(MAKE) clean; exit 1; \
|
||||||
|
fi
|
||||||
@if [ "`./greet-app`" = "`printf '$(EXPECTED)\n'`" ]; then \
|
@if [ "`./greet-app`" = "`printf '$(EXPECTED)\n'`" ]; then \
|
||||||
echo "modules ok"; \
|
echo "modules ok"; \
|
||||||
else \
|
else \
|
||||||
@@ -34,6 +50,6 @@ check:
|
|||||||
@$(MAKE) --no-print-directory clean
|
@$(MAKE) --no-print-directory clean
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@rm -f greet.o greet-app
|
@rm -f greet.o ledger.o greet-app greet-app.d
|
||||||
|
|
||||||
.PHONY: check clean
|
.PHONY: check clean
|
||||||
|
|||||||
@@ -47,6 +47,11 @@
|
|||||||
(printf "tally %d\n" (.count t))
|
(printf "tally %d\n" (.count t))
|
||||||
(.drop t)
|
(.drop t)
|
||||||
|
|
||||||
|
;; neither cell nor ledger is visible here, only declared
|
||||||
|
(var b board (make-board))
|
||||||
|
(printf "board %d\n" (board-total (& b)))
|
||||||
|
(printf "ledger %d\n" (ledger-total (greet-ledger)))
|
||||||
|
|
||||||
(var c colour (colour/rgb 1 2 3))
|
(var c colour (colour/rgb 1 2 3))
|
||||||
(match c
|
(match c
|
||||||
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
(pub include stdio.h)
|
(pub include stdio.h)
|
||||||
(include stdlib.h)
|
(include stdlib.h)
|
||||||
|
|
||||||
|
(import ledger)
|
||||||
|
|
||||||
(pub var greet-count ;; a comment in the header of a public form is
|
(pub var greet-count ;; a comment in the header of a public form is
|
||||||
;; not part of it: what the importer is given has
|
;; not part of it: what the importer is given has
|
||||||
;; to be `extern int greet-count', not a form
|
;; to be `extern int greet-count', not a form
|
||||||
@@ -30,12 +32,36 @@
|
|||||||
(lambda (name field-type)
|
(lambda (name field-type)
|
||||||
`(printf "%s " ,(symbol->string name))))))
|
`(printf "%s " ,(symbol->string name))))))
|
||||||
|
|
||||||
|
(var greetings ledger #(ledger : 0))
|
||||||
|
|
||||||
(pub fn greet ;; ...and here the prototype would lose its return type
|
(pub fn greet ;; ...and here the prototype would lose its return type
|
||||||
((name (* const char))) void
|
((name (* const char))) void
|
||||||
"Print a greeting for NAME."
|
"Print a greeting for NAME."
|
||||||
(++ greet-count)
|
(++ greet-count)
|
||||||
|
(ledger-add (& greetings) 1)
|
||||||
(printf "hello, %s\n" name))
|
(printf "hello, %s\n" name))
|
||||||
|
|
||||||
|
;;; Types the importer cannot see, crossing behind a pointer. `cell' is
|
||||||
|
;;; private and `ledger' was imported without `pub', and the importer
|
||||||
|
;;; is handed a declaration of each and nothing else.
|
||||||
|
(record cell ((v int)))
|
||||||
|
|
||||||
|
(var the-cell cell #(cell : 40))
|
||||||
|
|
||||||
|
(pub record board ((first (* cell)) (size int)))
|
||||||
|
|
||||||
|
(pub fn make-board () board
|
||||||
|
(return #(board : (& the-cell) 2)))
|
||||||
|
|
||||||
|
(pub fn board-total ((b (* const board))) int
|
||||||
|
(return (+ (. b first v) (. b size))))
|
||||||
|
|
||||||
|
(pub fn greet-ledger () (* ledger)
|
||||||
|
(return (& greetings)))
|
||||||
|
|
||||||
|
(pub fn ledger-total ((l (* const ledger))) int
|
||||||
|
(return (. l total)))
|
||||||
|
|
||||||
;;; A closure type crossing the boundary. Both units generate the
|
;;; A closure type crossing the boundary. Both units generate the
|
||||||
;;; struct for this signature independently, so they have to agree on
|
;;; struct for this signature independently, so they have to agree on
|
||||||
;;; its tag and its layout, or the value is passed wrong and nothing
|
;;; its tag and its layout, or the value is passed wrong and nothing
|
||||||
|
|||||||
8
tests/modules/ledger.sex
Normal file
8
tests/modules/ledger.sex
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
;;; A module greet.sex imports without `pub'. greet-app.sex never
|
||||||
|
;;; imports it, yet holds a (* ledger) that greet hands out: what greet
|
||||||
|
;;; exports has to declare the type for it.
|
||||||
|
|
||||||
|
(pub record ledger ((total int)))
|
||||||
|
|
||||||
|
(pub fn ledger-add ((l (* ledger)) (n int)) void
|
||||||
|
(+= (. l total) n))
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
(include "codegen.scm")
|
(include "codegen.scm")
|
||||||
(include "encapsulation.scm")
|
(include "encapsulation.scm")
|
||||||
(include "args.scm")
|
(include "args.scm")
|
||||||
|
(include "module-path.scm")
|
||||||
(include "types.scm")
|
(include "types.scm")
|
||||||
(include "infer.scm")
|
(include "infer.scm")
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,8 @@
|
|||||||
"toplevel: 1 2.5 hi 12"
|
"toplevel: 1 2.5 hi 12"
|
||||||
"closure in place: 5"
|
"closure in place: 5"
|
||||||
"a type is not a call: 7"
|
"a type is not a call: 7"
|
||||||
"radix literals: 224 5 128")
|
"radix literals: 224 5 128"
|
||||||
|
"a double reaches C whole: 1")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; The walk types an expression by its head, and the heads it had a
|
;;; The walk types an expression by its head, and the heads it had a
|
||||||
@@ -92,4 +93,8 @@
|
|||||||
;; a mask reads best in the radix it is a mask in
|
;; a mask reads best in the radix it is a mask in
|
||||||
(var lead int 0xE5)
|
(var lead int 0xE5)
|
||||||
(printf "radix literals: %d %d %d\n" (& lead 0xE0) (& lead 0b111) #x80)
|
(printf "radix literals: %d %d %d\n" (& lead 0xE0) (& lead 0b111) #x80)
|
||||||
|
|
||||||
|
;; ...and a double in all the digits it was written with
|
||||||
|
(var sixth double (/ 1.0 6.0))
|
||||||
|
(printf "a double reaches C whole: %d\n" (== 0.16666666666666666 sixth))
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -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))
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
(chicken io)
|
(chicken io)
|
||||||
(chicken pathname)
|
(chicken pathname)
|
||||||
(chicken port)
|
(chicken port)
|
||||||
|
(chicken flonum) ; flonum-print-precision
|
||||||
(chicken process)
|
(chicken process)
|
||||||
(chicken process-context)
|
(chicken process-context)
|
||||||
(chicken string) ; string-split
|
(chicken string) ; string-split
|
||||||
@@ -93,6 +94,9 @@
|
|||||||
(let* ((proc (process compiler (append (list "-o" compiled-file) flags)))
|
(let* ((proc (process compiler (append (list "-o" compiled-file) flags)))
|
||||||
(sexc-stdin (process-input-port proc)))
|
(sexc-stdin (process-input-port proc)))
|
||||||
(symbol-escape #f)
|
(symbol-escape #f)
|
||||||
|
;; ...nor any double in the 15 digits CHICKEN prints by default,
|
||||||
|
;; which make 0.16666666666666666 another number
|
||||||
|
(flonum-print-precision 17)
|
||||||
(with-output-to-port sexc-stdin
|
(with-output-to-port sexc-stdin
|
||||||
(fn (map (fn (fmt #t x)) src)))
|
(fn (map (fn (fmt #t x)) src)))
|
||||||
(close-output-port sexc-stdin)
|
(close-output-port sexc-stdin)
|
||||||
|
|||||||
Reference in New Issue
Block a user