Compare commits
10 Commits
ae9cdbbf31
...
f9809c19c4
| Author | SHA1 | Date | |
|---|---|---|---|
| f9809c19c4 | |||
| 31b7046048 | |||
| 2cfce45935 | |||
| bb252dc244 | |||
| eeb5357fd4 | |||
| 1aed7cee4c | |||
| 4ada1059a2 | |||
| 9a6e2dcd83 | |||
| 00e75dee48 | |||
| 2904be4499 |
@@ -95,10 +95,10 @@ forms, and what remains."
|
|||||||
`(%begin ,@(map walk-expr comments) ,form)))
|
`(%begin ,@(map walk-expr comments) ,form)))
|
||||||
|
|
||||||
;;; (for init test step body ...). C's parens around the header, carried
|
;;; (for init test step body ...). C's parens around the header, carried
|
||||||
;;; over, leave a call of a `var' in init's slot and the body in test's
|
;;; over, leave all three parts in init's slot and the body in test's
|
||||||
(define (check-for-header form slots)
|
(define (check-for-header form slots)
|
||||||
(cond ((and (pair? slots) (pair? (car slots)) (pair? (caar slots))
|
(cond ((and (pair? slots) (list? (car slots)) (= 3 (length (car slots)))
|
||||||
(eq? 'var (car (caar slots))))
|
(every pair? (car slots)))
|
||||||
(sex-error form "a for header is not grouped: write (for init test step body ...)"))
|
(sex-error form "a for header is not grouped: write (for init test step body ...)"))
|
||||||
((< (length slots) 3)
|
((< (length slots) 3)
|
||||||
(sex-error form "malformed for: write (for init test step body ...)"))))
|
(sex-error form "malformed for: write (for init test step body ...)"))))
|
||||||
@@ -133,17 +133,19 @@ forms, and what remains."
|
|||||||
;;; exclamation mark, both letters. Not the retroflex click ǃ, which
|
;;; exclamation mark, both letters. Not the retroflex click ǃ, which
|
||||||
;;; looks more like `!' -- enough that clang warns at every use.
|
;;; looks more like `!' -- enough that clang warns at every use.
|
||||||
(define +name-spellings+
|
(define +name-spellings+
|
||||||
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ")))
|
(map (lambda (spelling) (cons (irregex (car spelling)) (cdr spelling)))
|
||||||
|
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ"))))
|
||||||
|
|
||||||
(define (unkebabify sym)
|
(define (unkebabify sym)
|
||||||
(case sym
|
(let ((name (symbol->string sym)))
|
||||||
((- -- -> -= / /= ! != ?) sym) ; bare operators, not names
|
(if (or (memq sym '(- -- -> -= / /= ! != ?)) ; bare operators, not names
|
||||||
(else
|
(not (string-index name (lambda (c) (memv c '(#\- #\/ #\? #\!))))))
|
||||||
(string->symbol
|
sym
|
||||||
(fold (lambda (spelling name)
|
(string->symbol
|
||||||
(irregex-replace/all (car spelling) name (cdr spelling)))
|
(fold (lambda (spelling name)
|
||||||
(symbol->string sym)
|
(irregex-replace/all (car spelling) name (cdr spelling)))
|
||||||
+name-spellings+)))))
|
name
|
||||||
|
+name-spellings+)))))
|
||||||
|
|
||||||
(define (atom-to-fmt-c atom)
|
(define (atom-to-fmt-c atom)
|
||||||
(case atom
|
(case atom
|
||||||
|
|||||||
86
semen.scm
86
semen.scm
@@ -428,11 +428,9 @@
|
|||||||
(bodies (map (lambda (m)
|
(bodies (map (lambda (m)
|
||||||
(if (comment-form? m) (list m) (process-fn m (list))))
|
(if (comment-form? m) (list m) (process-fn m (list))))
|
||||||
methods))
|
methods))
|
||||||
(early (filter (lambda (m)
|
(early (filter (lambda (m) (called-before? m methods bodies)) fns)))
|
||||||
(called-before? (sex-fn-name m) m methods bodies))
|
|
||||||
fns)))
|
|
||||||
(append (append-map identity (reverse bodies))
|
(append (append-map identity (reverse bodies))
|
||||||
(fold (lambda (m acc) (process-fn (fn-prototype m) acc)) acc early)))))
|
(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
|
;;; The method's fn form, renamed to vec2/length, once it is registered
|
||||||
;;; under `length'; a comment is passed along
|
;;; under `length'; a comment is passed along
|
||||||
@@ -446,28 +444,23 @@
|
|||||||
(let ((name (second core)))
|
(let ((name (second core)))
|
||||||
(when (get-method type name)
|
(when (get-method type name)
|
||||||
(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))
|
||||||
|
(renamed (rename-fn form qualified)))
|
||||||
(add-method! type name qualified)
|
(add-method! type name qualified)
|
||||||
;; its signature too, which a call's receiver is fitted to
|
;; its signature too, which a call's receiver is fitted to
|
||||||
(process-fn (fn-prototype (rename-fn form qualified)) (list))
|
(process-fn (sex-fn-prototype renamed) (list))
|
||||||
(rename-fn form qualified))))))
|
renamed)))))
|
||||||
|
|
||||||
;;; Does a body written above METHOD call it?
|
;;; Does a body written above METHOD call it?
|
||||||
(define (called-before? name method methods bodies)
|
(define (called-before? method methods bodies)
|
||||||
(let loop ((ms methods) (bs bodies))
|
(let ((name (sex-fn-name method)))
|
||||||
(and (not (eq? (car ms) method))
|
(let loop ((ms methods) (bs bodies))
|
||||||
(or (names? (car bs) name)
|
(and (not (eq? (car ms) method))
|
||||||
(loop (cdr ms) (cdr bs))))))
|
(or (mentions? (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)
|
;;; (pub fn name args ret ...) -> (pub fn name args ret)
|
||||||
(define (fn-prototype form)
|
(define (sex-fn-prototype form)
|
||||||
(let ((header (strip-fn-header-comments form)))
|
(let ((header (strip-fn-header-comments form)))
|
||||||
(copy-form-source! form (take header (fn-header-length header)))))
|
(copy-form-source! form (take header (fn-header-length header)))))
|
||||||
|
|
||||||
@@ -492,25 +485,6 @@
|
|||||||
(set! (hash-table-ref env :scopes) (list parameters))
|
(set! (hash-table-ref env :scopes) (list parameters))
|
||||||
env))
|
env))
|
||||||
|
|
||||||
;;; `(p (* struct point))' writes the type in one piece and
|
|
||||||
;;; `(p * struct point)' spreads it over the rest; both are
|
|
||||||
;;; `(* struct point)'
|
|
||||||
(define (named-arg-type param)
|
|
||||||
(if (null? (cddr param))
|
|
||||||
(second param)
|
|
||||||
(cdr param)))
|
|
||||||
|
|
||||||
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int), and
|
|
||||||
;;; (self * const v) is ((* const v)) just as (self (* const v)) is.
|
|
||||||
;;; Anything that names nothing -- a variadic tail -- goes through
|
|
||||||
;;; untouched.
|
|
||||||
(define (arglist-types arglist)
|
|
||||||
(map (lambda (param)
|
|
||||||
(if (named-arg? param)
|
|
||||||
(list (named-arg-type param))
|
|
||||||
param))
|
|
||||||
arglist))
|
|
||||||
|
|
||||||
(define (fn-type-of fn-form)
|
(define (fn-type-of fn-form)
|
||||||
`(fn ,(arglist-types (sex-fn-arglist fn-form))
|
`(fn ,(arglist-types (sex-fn-arglist fn-form))
|
||||||
,(sex-fn-return-type fn-form)))
|
,(sex-fn-return-type fn-form)))
|
||||||
@@ -791,21 +765,12 @@
|
|||||||
(unresolve-closure-types (expression-type queried env)))))
|
(unresolve-closure-types (expression-type queried env)))))
|
||||||
(macroexpand form (list))))
|
(macroexpand form (list))))
|
||||||
|
|
||||||
;;; `#(v : (.get a))' is `(v){vːget(&a)}': the values are expressions
|
|
||||||
;;; like any other, and the type before `:' is not one
|
|
||||||
(define (walk-literal form env)
|
|
||||||
(let* ((parts (vector->list form))
|
|
||||||
(elements (or (and (memq ': parts) (cdr (memq ': parts))) parts)))
|
|
||||||
(list->vector
|
|
||||||
(append (drop-right parts (length elements))
|
|
||||||
(map (lambda (element) (walk-operand element env)) elements)))))
|
|
||||||
|
|
||||||
;;; #(struct point : (. p x) 2) -- the values are expressions, and the
|
;;; #(struct point : (. p x) 2) -- the values are expressions, and the
|
||||||
;;; words before a `:' a type that is left as written
|
;;; words before a `:' a type that is left as written
|
||||||
(define (walk-initializer form env)
|
(define (walk-initializer form env)
|
||||||
(let-values (((prefix rest) (break (lambda (e) (eq? e ':))
|
(let-values (((prefix rest) (break (lambda (e) (eq? e ':))
|
||||||
(vector->list form))))
|
(vector->list form))))
|
||||||
(let ((walk (lambda (es) (map (lambda (e) (walk-statement e env)) es))))
|
(let ((walk (lambda (es) (map (lambda (e) (walk-operand e env)) es))))
|
||||||
(list->vector
|
(list->vector
|
||||||
(if (null? rest)
|
(if (null? rest)
|
||||||
(walk prefix)
|
(walk prefix)
|
||||||
@@ -888,9 +853,11 @@
|
|||||||
|
|
||||||
(define (wildcard-type? type) (mentions? type '_))
|
(define (wildcard-type? type) (mentions? type '_))
|
||||||
|
|
||||||
(define (mentions? type word)
|
;;; WORD anywhere in FORM, a literal's contents included
|
||||||
(cond ((eq? type word) #t)
|
(define (mentions? form word)
|
||||||
((list? type) (any (lambda (part) (mentions? part word)) type))
|
(cond ((eq? form word) #t)
|
||||||
|
((pair? form) (or (mentions? (car form) word) (mentions? (cdr form) word)))
|
||||||
|
((vector? form) (mentions? (vector->list form) word))
|
||||||
(else #f)))
|
(else #f)))
|
||||||
|
|
||||||
;;; `(each xs compare)' where `each' takes a closure: the argument is
|
;;; `(each xs compare)' where `each' takes a closure: the argument is
|
||||||
@@ -1122,20 +1089,17 @@
|
|||||||
(let ((base (second form)))
|
(let ((base (second form)))
|
||||||
(let step ((expr (walk-operand base env))
|
(let step ((expr (walk-operand base env))
|
||||||
(type (expression-type base env))
|
(type (expression-type base env))
|
||||||
(read (list)) ; the fields walked, reversed
|
|
||||||
(fields (cddr form))
|
(fields (cddr form))
|
||||||
(told? #f))
|
(told? #f))
|
||||||
(if (null? fields)
|
(if (null? fields)
|
||||||
(copy-form-source! form expr)
|
(copy-form-source! form expr)
|
||||||
(let ((target (pointer-target type)))
|
(let ((target (pointer-target type)))
|
||||||
(unless (or type told?)
|
(unless (or type told?)
|
||||||
(sex-warning form "cannot type this; `.' reads it as a value"
|
(let ((walked (drop-right (cddr form) (length fields))))
|
||||||
(if (null? read)
|
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||||
base
|
(if (null? walked) base `(|.| ,base ,@walked)))))
|
||||||
`(|.| ,base ,@(reverse read)))))
|
|
||||||
(step (list (if target '%-> 'dot-access) expr (car fields))
|
(step (list (if target '%-> 'dot-access) expr (car fields))
|
||||||
(field-type (or target type) (car fields))
|
(field-type (or target type) (car fields))
|
||||||
(cons (car fields) read)
|
|
||||||
(cdr fields)
|
(cdr fields)
|
||||||
(or told? (not type))))))))
|
(or told? (not type))))))))
|
||||||
|
|
||||||
@@ -1961,12 +1925,6 @@ Returns #f if the form is not a function, returns the form otherwise"
|
|||||||
(fmt #f "Form " fn-form " is not a function"))
|
(fmt #f "Form " fn-form " is not a function"))
|
||||||
(fourth (fn-core fn-form)))
|
(fourth (fn-core fn-form)))
|
||||||
|
|
||||||
(define (sex-fn-prototype fn-form)
|
|
||||||
"Returns all except body"
|
|
||||||
(assert (sex-fn? fn-form)
|
|
||||||
(fmt #f "Form " fn-form " is not a function"))
|
|
||||||
(take fn-form (fn-header-length fn-form)))
|
|
||||||
|
|
||||||
(define (sex-fn-body fn-form)
|
(define (sex-fn-body fn-form)
|
||||||
(assert (sex-fn? fn-form)
|
(assert (sex-fn? fn-form)
|
||||||
(fmt #f "Form " fn-form " is not a function"))
|
(fmt #f "Form " fn-form " is not a function"))
|
||||||
|
|||||||
@@ -909,24 +909,23 @@
|
|||||||
;; It groups to the right, so its else arm is written at
|
;; It groups to the right, so its else arm is written at
|
||||||
;; `conditional-else', just looser: a chain there stays bare,
|
;; `conditional-else', just looser: a chain there stays bare,
|
||||||
;; `a ? x : b ? y : z', and an assignment does not.
|
;; `a ? x : b ? y : z', and an assignment does not.
|
||||||
(define (c-if/expr . args)
|
(define (c-if/expr c p . rest)
|
||||||
(c-maybe-paren '? (apply c-if/expr/aux args)))
|
(c-maybe-paren
|
||||||
|
'?
|
||||||
(define (c-if/expr/aux c p . rest)
|
(let lp ((c c) (p p) (ls rest))
|
||||||
(let lp ((c c) (p p) (ls rest))
|
(cond
|
||||||
(cond
|
((or (eq? c 'else) (eq? c #t))
|
||||||
((or (eq? c 'else) (eq? c #t))
|
(if (not (null? ls))
|
||||||
(if (not (null? ls))
|
(error "forms after else clause in IF" c p ls)
|
||||||
(error "forms after else clause in IF" c p ls)
|
(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
|
||||||
(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))))))
|
|
||||||
|
|
||||||
(define (c-if . args)
|
(define (c-if . args)
|
||||||
(fmt-if fmt-expression?
|
(fmt-if fmt-expression?
|
||||||
|
|||||||
@@ -78,16 +78,18 @@
|
|||||||
(file-readable? module-path)
|
(file-readable? module-path)
|
||||||
module-path))))
|
module-path))))
|
||||||
|
|
||||||
;;; Every module file read for this unit, first read first: what its
|
;;; Every module file read for this unit, with its forms, last read
|
||||||
;;; output depends on, besides the unit itself
|
;;; first: what its output depends on, besides the unit itself. A module
|
||||||
(define +module-dependencies+ (list))
|
;;; that an import chain and the interface check both reach is read once
|
||||||
|
(define +modules-read+ (list))
|
||||||
|
|
||||||
(define (module-dependencies) (reverse +module-dependencies+))
|
(define (module-dependencies) (reverse (map car +modules-read+)))
|
||||||
|
|
||||||
(define (read-module path)
|
(define (read-module path)
|
||||||
(unless (member path +module-dependencies+)
|
(cond ((assoc path +modules-read+) => cdr)
|
||||||
(set! +module-dependencies+ (cons path +module-dependencies+)))
|
(else (let ((forms (read-from-file path)))
|
||||||
(read-from-file path))
|
(set! +modules-read+ (cons (cons path forms) +modules-read+))
|
||||||
|
forms))))
|
||||||
|
|
||||||
(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.
|
||||||
@@ -134,7 +136,8 @@
|
|||||||
(define (check-type-use form use visible hidden acc)
|
(define (check-type-use form use visible hidden acc)
|
||||||
(let* ((name (car use))
|
(let* ((name (car use))
|
||||||
(seen (assq name visible))
|
(seen (assq name visible))
|
||||||
(kept (assq name hidden)))
|
(kept (assq name hidden))
|
||||||
|
(remedy (if (and kept (fourth kept)) "import its module with pub" "make it pub")))
|
||||||
(cond
|
(cond
|
||||||
((and seen (third seen)) acc) ; pub with its body
|
((and seen (third seen)) acc) ; pub with its body
|
||||||
((and seen (cdr use)) acc) ; a pub handle, held by one
|
((and seen (cdr use)) acc) ; a pub handle, held by one
|
||||||
@@ -146,16 +149,11 @@
|
|||||||
(sex-error form (string-append "importers cannot see this "
|
(sex-error form (string-append "importers cannot see this "
|
||||||
(symbol->string (second kept))
|
(symbol->string (second kept))
|
||||||
", and C declares one only by defining it; "
|
", and C declares one only by defining it; "
|
||||||
(if (fourth kept)
|
remedy)
|
||||||
"import its module with pub"
|
|
||||||
"make it pub"))
|
|
||||||
name))
|
name))
|
||||||
((not (cdr use))
|
((not (cdr use))
|
||||||
(sex-error form (string-append "importers cannot see this type, held by value; "
|
(sex-error form (string-append "importers cannot see this type, held by value; "
|
||||||
(if (fourth kept)
|
remedy ", or hold it through a pointer")
|
||||||
"import its module with pub"
|
|
||||||
"make it pub")
|
|
||||||
", or hold it through a pointer")
|
|
||||||
name))
|
name))
|
||||||
((any (lambda (f) (eq? name (cadr f))) acc) acc)
|
((any (lambda (f) (eq? name (cadr f))) acc) acc)
|
||||||
(else
|
(else
|
||||||
@@ -198,13 +196,7 @@
|
|||||||
(if (list? fields) (map second (normalize-fields fields)) (list)))
|
(if (list? fields) (map second (normalize-fields fields)) (list)))
|
||||||
|
|
||||||
(define (signature-types args ret)
|
(define (signature-types args ret)
|
||||||
(cons ret (map arg-type (remove comment-form? args))))
|
(cons ret (arglist-types (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:
|
;;; Each name TYPE mentions, and whether it is behind a pointer:
|
||||||
;;;
|
;;;
|
||||||
@@ -247,7 +239,7 @@
|
|||||||
(filter-map
|
(filter-map
|
||||||
(lambda (form)
|
(lambda (form)
|
||||||
(and (pair? form)
|
(and (pair? form)
|
||||||
(let* ((core (if (pub-form? form) (cdr form) form))
|
(let* ((core (strip-pub form))
|
||||||
(name (and (pair? core) (pair? (cdr core))
|
(name (and (pair? core) (pair? (cdr core))
|
||||||
(if (and (eq? (car core) 'define) (pair? (second core)))
|
(if (and (eq? (car core) 'define) (pair? (second core)))
|
||||||
(car (second core))
|
(car (second core))
|
||||||
@@ -263,14 +255,11 @@
|
|||||||
(define (pub-form? form) (and (pair? form) (eq? (car form) 'pub)))
|
(define (pub-form? form) (and (pair? form) (eq? (car form) 'pub)))
|
||||||
|
|
||||||
;;; Pub here or through a pub import. A type declared pub both bodyless
|
;;; Pub here or through a pub import. A type declared pub both bodyless
|
||||||
;;; and whole is whole
|
;;; and whole is whole: its whole entries come first, for `assq'
|
||||||
(define (visible-types forms)
|
(define (visible-types forms)
|
||||||
(let ((all (append (declared-types (filter pub-form? forms))
|
(let ((all (append (declared-types (filter pub-form? forms))
|
||||||
(append-map exported-types (imported-modules forms #t)))))
|
(append-map exported-types (imported-modules forms #t)))))
|
||||||
(map (lambda (entry)
|
(append (filter third all) (remove third all))))
|
||||||
(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
|
;;; Declared here without pub, or brought by an import without it, as
|
||||||
;;; (name kind body? imported?)
|
;;; (name kind body? imported?)
|
||||||
@@ -292,16 +281,16 @@
|
|||||||
;;; The types a module gives its importers, its pub imports' included.
|
;;; The types a module gives its importers, its pub imports' included.
|
||||||
;;; Two modules may import each other, so each is read once
|
;;; Two modules may import each other, so each is read once
|
||||||
(define (exported-types module)
|
(define (exported-types module)
|
||||||
(let read ((modules (list module)) (seen (list)) (acc (list)))
|
(let next ((modules (list module)) (seen (list)) (acc (list)))
|
||||||
(if (null? modules)
|
(if (null? modules)
|
||||||
acc
|
(concatenate (reverse acc))
|
||||||
(let ((path (locate-module (symbol->string (car modules)))))
|
(let ((path (locate-module (symbol->string (car modules)))))
|
||||||
(if (or (not path) (member path seen))
|
(if (or (not path) (member path seen))
|
||||||
(read (cdr modules) seen acc)
|
(next (cdr modules) seen acc)
|
||||||
(let ((forms (interface-forms (read-module path))))
|
(let ((forms (interface-forms (read-module path))))
|
||||||
(read (append (cdr modules) (imported-modules forms #f))
|
(next (append (cdr modules) (imported-modules forms #f))
|
||||||
(cons path seen)
|
(cons path seen)
|
||||||
(append acc (declared-types forms)))))))))
|
(cons (declared-types forms) acc))))))))
|
||||||
|
|
||||||
(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
|
||||||
|
|||||||
7
sexc.scm
7
sexc.scm
@@ -142,8 +142,11 @@
|
|||||||
'stdin
|
'stdin
|
||||||
(car rest-args))))
|
(car rest-args))))
|
||||||
|
|
||||||
|
(define (stdout-output? output)
|
||||||
|
(or (eq? output 'default) (equal? output "-")))
|
||||||
|
|
||||||
(define (write-to-file-or-stdout output what)
|
(define (write-to-file-or-stdout output what)
|
||||||
(if (or (eq? output 'default) (equal? output "-"))
|
(if (stdout-output? output)
|
||||||
(what)
|
(what)
|
||||||
(with-output-to-file output
|
(with-output-to-file output
|
||||||
(fn (what)))))
|
(fn (what)))))
|
||||||
@@ -169,7 +172,7 @@
|
|||||||
modules)))))
|
modules)))))
|
||||||
|
|
||||||
(define (write-dependencies depfile output input)
|
(define (write-dependencies depfile output input)
|
||||||
(when (or (eq? output 'default) (equal? output "-"))
|
(when (stdout-output? output)
|
||||||
(error "--depfile needs -o, to name what depends on the modules"))
|
(error "--depfile needs -o, to name what depends on the modules"))
|
||||||
(with-output-to-file depfile
|
(with-output-to-file depfile
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
|||||||
@@ -15,12 +15,15 @@
|
|||||||
semen
|
semen
|
||||||
utils)
|
utils)
|
||||||
|
|
||||||
|
(define (read-source source)
|
||||||
|
(with-input-from-string source
|
||||||
|
(lambda ()
|
||||||
|
(parameterize ((current-source-file "codegen.sex"))
|
||||||
|
(parse-all (current-input-port))))))
|
||||||
|
|
||||||
(define (sex->c source)
|
(define (sex->c source)
|
||||||
"Compile SOURCE, a string of Sex, and return the generated C."
|
"Compile SOURCE, a string of Sex, and return the generated C."
|
||||||
(let ((forms (with-input-from-string source
|
(let ((forms (read-source source)))
|
||||||
(lambda ()
|
|
||||||
(parameterize ((current-source-file "codegen.sex"))
|
|
||||||
(parse-all (current-input-port)))))))
|
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(parameterize ((sex-line-directives 'none))
|
(parameterize ((sex-line-directives 'none))
|
||||||
@@ -37,17 +40,20 @@
|
|||||||
(define (emits? source fragment)
|
(define (emits? source fragment)
|
||||||
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
|
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
|
||||||
|
|
||||||
(define (error-message source)
|
(define (failure-message thunk)
|
||||||
"Compile SOURCE and return the error text as the user sees it --
|
"The error THUNK raises as the user sees it -- message plus arguments,
|
||||||
message plus arguments, the way CHICKEN prints it -- or #f if SOURCE
|
the way CHICKEN prints it -- or #f if it raises none."
|
||||||
compiles."
|
|
||||||
(handle-exceptions e
|
(handle-exceptions e
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(display ((condition-property-accessor 'exn 'message) e))
|
(display ((condition-property-accessor 'exn 'message) e))
|
||||||
(for-each (lambda (a) (display " ") (write a))
|
(for-each (lambda (a) (display " ") (write a))
|
||||||
((condition-property-accessor 'exn 'arguments) e))))
|
((condition-property-accessor 'exn 'arguments) e))))
|
||||||
(begin (sex->c source) #f)))
|
(begin (thunk) #f)))
|
||||||
|
|
||||||
|
(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)
|
(define (reports? source fragment)
|
||||||
(let ((m (error-message source)))
|
(let ((m (error-message source)))
|
||||||
@@ -338,6 +344,9 @@ whitespace aside, THEN?"
|
|||||||
(test-assert "a grouped header is named"
|
(test-assert "a grouped header is named"
|
||||||
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i))")
|
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i))")
|
||||||
"header is not grouped"))
|
"header is not grouped"))
|
||||||
|
(test-assert "whatever its init is"
|
||||||
|
(reports? (in-fn "(for ((= a 0) (< a 4) (++ a)) (g a))")
|
||||||
|
"header is not grouped"))
|
||||||
(test-assert "even when the body fills the slots it left"
|
(test-assert "even when the body fills the slots it left"
|
||||||
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i) (g i) (g i))")
|
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i) (g i) (g i))")
|
||||||
"header is not grouped"))
|
"header is not grouped"))
|
||||||
|
|||||||
@@ -5,23 +5,18 @@
|
|||||||
;;; is checked here is what the compiler refuses, and that it refuses
|
;;; is checked here is what the compiler refuses, and that it refuses
|
||||||
;;; it with the name of whatever is wrong.
|
;;; it with the name of whatever is wrong.
|
||||||
;;;
|
;;;
|
||||||
;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm,
|
;;; `sex->c', `read-source', `failure-message', `emits?', `reports?'
|
||||||
;;; which run.scm includes first.
|
;;; and `in-fn' come from codegen.scm, which run.scm includes first.
|
||||||
|
|
||||||
(import sex-modules)
|
(import sex-modules)
|
||||||
|
|
||||||
(define (interface-of source)
|
(define (interface-of source)
|
||||||
"The declarations SOURCE, compiled as a module, hands its importers
|
"The declarations SOURCE, compiled as a module, hands its importers
|
||||||
ahead of its pub forms."
|
ahead of its pub forms."
|
||||||
(check-public-interface
|
(check-public-interface (read-source source)))
|
||||||
(with-input-from-string source
|
|
||||||
(lambda ()
|
|
||||||
(parameterize ((current-source-file "codegen.sex"))
|
|
||||||
(parse-all (current-input-port)))))))
|
|
||||||
|
|
||||||
(define (interface-reports? source fragment)
|
(define (interface-reports? source fragment)
|
||||||
(let ((m (handle-exceptions e ((condition-property-accessor 'exn 'message) e)
|
(let ((m (failure-message (lambda () (interface-of source)))))
|
||||||
(begin (interface-of source) #f))))
|
|
||||||
(and m (string-contains m fragment) #t)))
|
(and m (string-contains m fragment) #t)))
|
||||||
|
|
||||||
(define (ops-src . body)
|
(define (ops-src . body)
|
||||||
|
|||||||
@@ -42,6 +42,9 @@
|
|||||||
|
|
||||||
type-head?
|
type-head?
|
||||||
named-arg?
|
named-arg?
|
||||||
|
named-arg-type
|
||||||
|
arglist-types
|
||||||
|
strip-pub
|
||||||
array-bound?
|
array-bound?
|
||||||
array-element-type)
|
array-element-type)
|
||||||
"../types.scm")
|
"../types.scm")
|
||||||
|
|||||||
@@ -42,6 +42,9 @@
|
|||||||
|
|
||||||
type-head?
|
type-head?
|
||||||
named-arg?
|
named-arg?
|
||||||
|
named-arg-type
|
||||||
|
arglist-types
|
||||||
|
strip-pub
|
||||||
array-bound?
|
array-bound?
|
||||||
array-element-type)
|
array-element-type)
|
||||||
"types.scm")
|
"types.scm")
|
||||||
|
|||||||
19
types.scm
19
types.scm
@@ -389,6 +389,25 @@
|
|||||||
(pair? (cdr arg)) ; 1 element args are always type
|
(pair? (cdr arg)) ; 1 element args are always type
|
||||||
(not (type-head? arg))))
|
(not (type-head? arg))))
|
||||||
|
|
||||||
|
;;; `(p (* struct point))' writes the type in one piece and
|
||||||
|
;;; `(p * struct point)' spreads it over the rest; both are
|
||||||
|
;;; `(* struct point)'
|
||||||
|
(define (named-arg-type param)
|
||||||
|
(if (null? (cddr param))
|
||||||
|
(second param)
|
||||||
|
(cdr param)))
|
||||||
|
|
||||||
|
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int), and
|
||||||
|
;;; (self * const v) is ((* const v)) just as (self (* const v)) is.
|
||||||
|
;;; Anything that names nothing -- a variadic tail -- goes through
|
||||||
|
;;; untouched.
|
||||||
|
(define (arglist-types arglist)
|
||||||
|
(map (lambda (param)
|
||||||
|
(if (named-arg? param)
|
||||||
|
(list (named-arg-type param))
|
||||||
|
param))
|
||||||
|
arglist))
|
||||||
|
|
||||||
;;; The last element is a bound unless what precedes it is still
|
;;; The last element is a bound unless what precedes it is still
|
||||||
;;; waiting for a type name -- a `*', a qualifier, a tag's keyword. C
|
;;; waiting for a type name -- a `*', a qualifier, a tag's keyword. C
|
||||||
;;; spells a type with one name at most, so a name after a name, or
|
;;; spells a type with one name at most, so a name after a name, or
|
||||||
|
|||||||
Reference in New Issue
Block a user