Compare commits
8 Commits
6f7b28cfa5
...
77acd6d160
| Author | SHA1 | Date | |
|---|---|---|---|
| 77acd6d160 | |||
| bdc45e38e4 | |||
| 0024ea4b8d | |||
| a4c32a71d2 | |||
| 952ecd4359 | |||
| 2c2e56ffc5 | |||
| 28facf925f | |||
| ef101c5916 |
22
semen.scm
22
semen.scm
@@ -429,16 +429,12 @@
|
||||
(sex-error form "ops needs a type name" form))
|
||||
(let* ((methods (map (lambda (m) (register-op type m)) (cddr form)))
|
||||
(fns (remove comment-form? methods))
|
||||
;; the signatures too, which a call's receiver is fitted to
|
||||
(protos (map (lambda (m) (process-fn (sex-fn-prototype m) (list))) fns))
|
||||
(bodies (map (lambda (m)
|
||||
(if (comment-form? m) (list m) (process-fn m (list))))
|
||||
methods))
|
||||
(early (filter-map (lambda (m proto)
|
||||
(and (called-before? m methods bodies) proto))
|
||||
fns protos)))
|
||||
(append (concatenate (reverse bodies))
|
||||
(fold append acc early)))))
|
||||
(early (filter (lambda (m) (called-before? m methods bodies)) fns)))
|
||||
(append (append-map identity (reverse bodies))
|
||||
(fold (lambda (m acc) (process-fn (sex-fn-prototype m) acc)) acc early)))))
|
||||
|
||||
;;; The method's fn form, renamed to vec2/length, once it is registered
|
||||
;;; under `length'; a comment is passed along
|
||||
@@ -455,6 +451,8 @@
|
||||
(let* ((qualified (qualify-op type name))
|
||||
(renamed (rename-fn form qualified)))
|
||||
(add-method! type name qualified)
|
||||
;; its signature too, which a call's receiver is fitted to
|
||||
(process-fn (sex-fn-prototype renamed) (list))
|
||||
renamed)))))
|
||||
|
||||
;;; Does a body written above METHOD call it?
|
||||
@@ -799,12 +797,14 @@
|
||||
;;; (* char) (* const char) (* size-t) (unsigned int) types
|
||||
;;; (* p) (* (g)) (¤ xs 0) expressions
|
||||
(define (written-type? form env)
|
||||
(match form
|
||||
((or ('* operand) ('¤ operand _))
|
||||
(and (pair? form)
|
||||
;; the length a dereference and a subscript have
|
||||
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1)))
|
||||
(let ((operand (second form)))
|
||||
(if (symbol? operand)
|
||||
(not (or (lookup-name env operand) (get-name-type operand)))
|
||||
(written-type? operand env)))
|
||||
(_ (type-head? form))))
|
||||
(type-head? form))))
|
||||
|
||||
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
|
||||
(define (resolve-wildcard form env)
|
||||
@@ -1145,7 +1145,7 @@
|
||||
|
||||
;;; A field of a path, indexed or not: xs, [xs 0] -> xs
|
||||
(define (field-name field)
|
||||
(if (and (array-form? field) (pair? (cdr field)))
|
||||
(if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field)))
|
||||
(second field)
|
||||
field))
|
||||
|
||||
|
||||
@@ -196,7 +196,8 @@
|
||||
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
|
||||
(remove comment-form? variants)))
|
||||
(('ops type . methods)
|
||||
(append-map interface-types (filter pub-form? 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)))
|
||||
@@ -300,23 +301,10 @@
|
||||
(let ((path (locate-module (symbol->string (car modules)))))
|
||||
(if (or (not path) (member path seen))
|
||||
(next (cdr modules) seen acc)
|
||||
(let ((exports (module-exports path)))
|
||||
(next (append (cdr modules) (cdr exports))
|
||||
(let ((forms (interface-forms (read-module path))))
|
||||
(next (append (cdr modules) (imported-modules forms #f))
|
||||
(cons path seen)
|
||||
(cons (car exports) acc))))))))
|
||||
|
||||
;;; The types the module at PATH declares to its importers, and the
|
||||
;;; modules its interface imports; worked out once, though the check of
|
||||
;;; every module above it on a pub import chain walks there
|
||||
(define +module-exports+ (list))
|
||||
|
||||
(define (module-exports path)
|
||||
(cond ((assoc path +module-exports+) => cdr)
|
||||
(else (let* ((forms (interface-forms (read-module path)))
|
||||
(exports (cons (declared-types forms)
|
||||
(imported-modules forms #f))))
|
||||
(set! +module-exports+ (cons (cons path exports) +module-exports+))
|
||||
exports))))
|
||||
(cons (declared-types forms) acc))))))))
|
||||
|
||||
(define (public-fn-interface raw-form)
|
||||
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
|
||||
|
||||
@@ -51,12 +51,13 @@ the way CHICKEN prints it -- or #f if it raises none."
|
||||
((condition-property-accessor 'exn 'arguments) e))))
|
||||
(begin (thunk) #f)))
|
||||
|
||||
(define (fails-with? thunk fragment)
|
||||
(let ((m (failure-message thunk)))
|
||||
(and m (string-contains m fragment) #t)))
|
||||
(define (error-message source)
|
||||
"Compile SOURCE and return its error text, or #f if it compiles."
|
||||
(failure-message (lambda () (sex->c source))))
|
||||
|
||||
(define (reports? source fragment)
|
||||
(fails-with? (lambda () (sex->c source)) fragment))
|
||||
(let ((m (error-message source)))
|
||||
(and m (string-contains m fragment) #t)))
|
||||
|
||||
(define (followed-by? source first then)
|
||||
"Is FIRST in the C compiled from SOURCE, and the next thing past it,
|
||||
@@ -89,6 +90,9 @@ whitespace aside, THEN?"
|
||||
|
||||
(test-group "codegen"
|
||||
|
||||
;; c-switch handed its scrutinee straight to `cat', which only works
|
||||
;; when it is an atom. Anything else was displayed as a raw
|
||||
;; s-expression: `switch ((%. e type))'.
|
||||
;; A case body shared the switch's scope, so two cases could not
|
||||
;; declare one name, and a label could not be followed by a
|
||||
;; declaration before C23. Only a body that declares needs a block
|
||||
@@ -105,9 +109,6 @@ whitespace aside, THEN?"
|
||||
(not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))")
|
||||
"{" "{"))))
|
||||
|
||||
;; c-switch handed its scrutinee straight to `cat', which only works
|
||||
;; when it is an atom. Anything else was displayed as a raw
|
||||
;; s-expression: `switch ((%. e type))'.
|
||||
(test-group "switch scrutinee"
|
||||
(test-assert "member access"
|
||||
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"
|
||||
@@ -335,6 +336,8 @@ whitespace aside, THEN?"
|
||||
(test-assert "pub with nothing to define"
|
||||
(reports? "(pub 1)" "codegen.sex:1:")))
|
||||
|
||||
;; A nested pointer chain used to silently lose a level:
|
||||
;; (var p (* (* char))) emitted `char *p'
|
||||
;; A grouped header, C's parens carried over, died in the writer with
|
||||
;; `(caddr) bad argument type' and no location
|
||||
(test-group "malformed for"
|
||||
@@ -354,8 +357,6 @@ whitespace aside, THEN?"
|
||||
(reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))"
|
||||
"codegen.sex:2:")))
|
||||
|
||||
;; A nested pointer chain used to silently lose a level:
|
||||
;; (var p (* (* char))) emitted `char *p'
|
||||
(test-group "malformed types are rejected"
|
||||
(test-assert "nested pointer chain"
|
||||
(reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat"))
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
;;; is checked here is what the compiler refuses, and that it refuses
|
||||
;;; it with the name of whatever is wrong.
|
||||
;;;
|
||||
;;; `sex->c', `read-source', `fails-with?', `emits?', `reports?'
|
||||
;;; `sex->c', `read-source', `failure-message', `emits?', `reports?'
|
||||
;;; and `in-fn' come from codegen.scm, which run.scm includes first.
|
||||
|
||||
(import sex-modules)
|
||||
@@ -16,7 +16,8 @@ ahead of its pub forms."
|
||||
(check-public-interface (read-source source)))
|
||||
|
||||
(define (interface-reports? source fragment)
|
||||
(fails-with? (lambda () (interface-of source)) fragment))
|
||||
(let ((m (failure-message (lambda () (interface-of source)))))
|
||||
(and m (string-contains m fragment) #t)))
|
||||
|
||||
(define (ops-src . body)
|
||||
(string-append
|
||||
|
||||
Reference in New Issue
Block a user