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)))
|
||||
|
||||
;;; (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)
|
||||
(cond ((and (pair? slots) (pair? (car slots)) (pair? (caar slots))
|
||||
(eq? 'var (car (caar slots))))
|
||||
(cond ((and (pair? slots) (list? (car slots)) (= 3 (length (car slots)))
|
||||
(every pair? (car slots)))
|
||||
(sex-error form "a for header is not grouped: write (for init test step body ...)"))
|
||||
((< (length slots) 3)
|
||||
(sex-error form "malformed for: write (for init test step body ...)"))))
|
||||
@@ -133,16 +133,18 @@ forms, and what remains."
|
||||
;;; exclamation mark, both letters. Not the retroflex click ǃ, which
|
||||
;;; looks more like `!' -- enough that clang warns at every use.
|
||||
(define +name-spellings+
|
||||
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ")))
|
||||
(map (lambda (spelling) (cons (irregex (car spelling)) (cdr spelling)))
|
||||
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ"))))
|
||||
|
||||
(define (unkebabify sym)
|
||||
(case sym
|
||||
((- -- -> -= / /= ! != ?) sym) ; bare operators, not names
|
||||
(else
|
||||
(let ((name (symbol->string sym)))
|
||||
(if (or (memq sym '(- -- -> -= / /= ! != ?)) ; bare operators, not names
|
||||
(not (string-index name (lambda (c) (memv c '(#\- #\/ #\? #\!))))))
|
||||
sym
|
||||
(string->symbol
|
||||
(fold (lambda (spelling name)
|
||||
(irregex-replace/all (car spelling) name (cdr spelling)))
|
||||
(symbol->string sym)
|
||||
name
|
||||
+name-spellings+)))))
|
||||
|
||||
(define (atom-to-fmt-c atom)
|
||||
|
||||
80
semen.scm
80
semen.scm
@@ -428,11 +428,9 @@
|
||||
(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)))
|
||||
(early (filter (lambda (m) (called-before? m methods bodies)) fns)))
|
||||
(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
|
||||
;;; under `length'; a comment is passed along
|
||||
@@ -446,28 +444,23 @@
|
||||
(let ((name (second core)))
|
||||
(when (get-method 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)
|
||||
;; its signature too, which a call's receiver is fitted to
|
||||
(process-fn (fn-prototype (rename-fn form qualified)) (list))
|
||||
(rename-fn form qualified))))))
|
||||
(process-fn (sex-fn-prototype renamed) (list))
|
||||
renamed)))))
|
||||
|
||||
;;; Does a body written above METHOD call it?
|
||||
(define (called-before? name method methods bodies)
|
||||
(define (called-before? method methods bodies)
|
||||
(let ((name (sex-fn-name method)))
|
||||
(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)))
|
||||
(or (mentions? (car bs) name)
|
||||
(loop (cdr ms) (cdr bs)))))))
|
||||
|
||||
;;; (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)))
|
||||
(copy-form-source! form (take header (fn-header-length header)))))
|
||||
|
||||
@@ -492,25 +485,6 @@
|
||||
(set! (hash-table-ref env :scopes) (list parameters))
|
||||
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)
|
||||
`(fn ,(arglist-types (sex-fn-arglist fn-form))
|
||||
,(sex-fn-return-type fn-form)))
|
||||
@@ -791,21 +765,12 @@
|
||||
(unresolve-closure-types (expression-type queried env)))))
|
||||
(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
|
||||
;;; words before a `:' a type that is left as written
|
||||
(define (walk-initializer form env)
|
||||
(let-values (((prefix rest) (break (lambda (e) (eq? e ':))
|
||||
(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
|
||||
(if (null? rest)
|
||||
(walk prefix)
|
||||
@@ -888,9 +853,11 @@
|
||||
|
||||
(define (wildcard-type? type) (mentions? type '_))
|
||||
|
||||
(define (mentions? type word)
|
||||
(cond ((eq? type word) #t)
|
||||
((list? type) (any (lambda (part) (mentions? part word)) type))
|
||||
;;; WORD anywhere in FORM, a literal's contents included
|
||||
(define (mentions? form word)
|
||||
(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)))
|
||||
|
||||
;;; `(each xs compare)' where `each' takes a closure: the argument is
|
||||
@@ -1122,20 +1089,17 @@
|
||||
(let ((base (second form)))
|
||||
(let step ((expr (walk-operand base env))
|
||||
(type (expression-type base env))
|
||||
(read (list)) ; the fields walked, reversed
|
||||
(fields (cddr form))
|
||||
(told? #f))
|
||||
(if (null? fields)
|
||||
(copy-form-source! form expr)
|
||||
(let ((target (pointer-target type)))
|
||||
(unless (or type told?)
|
||||
(let ((walked (drop-right (cddr form) (length fields))))
|
||||
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||
(if (null? read)
|
||||
base
|
||||
`(|.| ,base ,@(reverse read)))))
|
||||
(if (null? walked) base `(|.| ,base ,@walked)))))
|
||||
(step (list (if target '%-> 'dot-access) expr (car fields))
|
||||
(field-type (or target type) (car fields))
|
||||
(cons (car fields) read)
|
||||
(cdr fields)
|
||||
(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"))
|
||||
(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)
|
||||
(assert (sex-fn? fn-form)
|
||||
(fmt #f "Form " fn-form " is not a function"))
|
||||
|
||||
@@ -909,10 +909,9 @@
|
||||
;; 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)
|
||||
(c-maybe-paren '? (apply c-if/expr/aux args)))
|
||||
|
||||
(define (c-if/expr/aux c p . rest)
|
||||
(define (c-if/expr c p . rest)
|
||||
(c-maybe-paren
|
||||
'?
|
||||
(let lp ((c c) (p p) (ls rest))
|
||||
(cond
|
||||
((or (eq? c 'else) (eq? c #t))
|
||||
@@ -926,7 +925,7 @@
|
||||
(lp (car ls) (cadr ls) (cddr ls))
|
||||
(lp 'else (car ls) '())))))
|
||||
(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)
|
||||
(fmt-if fmt-expression?
|
||||
|
||||
@@ -78,16 +78,18 @@
|
||||
(file-readable? 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))
|
||||
;;; Every module file read for this unit, with its forms, last read
|
||||
;;; first: what its output depends on, besides the unit itself. A module
|
||||
;;; 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)
|
||||
(unless (member path +module-dependencies+)
|
||||
(set! +module-dependencies+ (cons path +module-dependencies+)))
|
||||
(read-from-file path))
|
||||
(cond ((assoc path +modules-read+) => cdr)
|
||||
(else (let ((forms (read-from-file path)))
|
||||
(set! +modules-read+ (cons (cons path forms) +modules-read+))
|
||||
forms))))
|
||||
|
||||
(define (read-public-interface module-path)
|
||||
;; 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)
|
||||
(let* ((name (car use))
|
||||
(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
|
||||
((and seen (third seen)) acc) ; pub with its body
|
||||
((and seen (cdr use)) acc) ; a pub handle, held by one
|
||||
@@ -146,16 +149,11 @@
|
||||
(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"))
|
||||
remedy)
|
||||
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")
|
||||
remedy ", or hold it through a pointer")
|
||||
name))
|
||||
((any (lambda (f) (eq? name (cadr f))) acc) acc)
|
||||
(else
|
||||
@@ -198,13 +196,7 @@
|
||||
(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))))
|
||||
(cons ret (arglist-types (remove comment-form? args))))
|
||||
|
||||
;;; Each name TYPE mentions, and whether it is behind a pointer:
|
||||
;;;
|
||||
@@ -247,7 +239,7 @@
|
||||
(filter-map
|
||||
(lambda (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))
|
||||
(if (and (eq? (car core) 'define) (pair? (second core)))
|
||||
(car (second core))
|
||||
@@ -263,14 +255,11 @@
|
||||
(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
|
||||
;;; and whole is whole: its whole entries come first, for `assq'
|
||||
(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)))
|
||||
(append (filter third all) (remove third all))))
|
||||
|
||||
;;; Declared here without pub, or brought by an import without it, as
|
||||
;;; (name kind body? imported?)
|
||||
@@ -292,16 +281,16 @@
|
||||
;;; 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)))
|
||||
(let next ((modules (list module)) (seen (list)) (acc (list)))
|
||||
(if (null? modules)
|
||||
acc
|
||||
(concatenate (reverse acc))
|
||||
(let ((path (locate-module (symbol->string (car modules)))))
|
||||
(if (or (not path) (member path seen))
|
||||
(read (cdr modules) seen acc)
|
||||
(next (cdr modules) seen acc)
|
||||
(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)
|
||||
(append acc (declared-types forms)))))))))
|
||||
(cons (declared-types forms) acc))))))))
|
||||
|
||||
(define (public-fn-interface raw-form)
|
||||
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
|
||||
|
||||
7
sexc.scm
7
sexc.scm
@@ -142,8 +142,11 @@
|
||||
'stdin
|
||||
(car rest-args))))
|
||||
|
||||
(define (stdout-output? output)
|
||||
(or (eq? output 'default) (equal? output "-")))
|
||||
|
||||
(define (write-to-file-or-stdout output what)
|
||||
(if (or (eq? output 'default) (equal? output "-"))
|
||||
(if (stdout-output? output)
|
||||
(what)
|
||||
(with-output-to-file output
|
||||
(fn (what)))))
|
||||
@@ -169,7 +172,7 @@
|
||||
modules)))))
|
||||
|
||||
(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"))
|
||||
(with-output-to-file depfile
|
||||
(lambda ()
|
||||
|
||||
@@ -15,12 +15,15 @@
|
||||
semen
|
||||
utils)
|
||||
|
||||
(define (sex->c source)
|
||||
"Compile SOURCE, a string of Sex, and return the generated C."
|
||||
(let ((forms (with-input-from-string source
|
||||
(define (read-source source)
|
||||
(with-input-from-string source
|
||||
(lambda ()
|
||||
(parameterize ((current-source-file "codegen.sex"))
|
||||
(parse-all (current-input-port)))))))
|
||||
(parse-all (current-input-port))))))
|
||||
|
||||
(define (sex->c source)
|
||||
"Compile SOURCE, a string of Sex, and return the generated C."
|
||||
(let ((forms (read-source source)))
|
||||
(with-output-to-string
|
||||
(lambda ()
|
||||
(parameterize ((sex-line-directives 'none))
|
||||
@@ -37,17 +40,20 @@
|
||||
(define (emits? source fragment)
|
||||
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
|
||||
|
||||
(define (error-message source)
|
||||
"Compile SOURCE and return the error text as the user sees it --
|
||||
message plus arguments, the way CHICKEN prints it -- or #f if SOURCE
|
||||
compiles."
|
||||
(define (failure-message thunk)
|
||||
"The error THUNK raises as the user sees it -- message plus arguments,
|
||||
the way CHICKEN prints it -- or #f if it raises none."
|
||||
(handle-exceptions e
|
||||
(with-output-to-string
|
||||
(lambda ()
|
||||
(display ((condition-property-accessor 'exn 'message) e))
|
||||
(for-each (lambda (a) (display " ") (write a))
|
||||
((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)
|
||||
(let ((m (error-message source)))
|
||||
@@ -338,6 +344,9 @@ whitespace aside, THEN?"
|
||||
(test-assert "a grouped header is named"
|
||||
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i))")
|
||||
"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"
|
||||
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i) (g i) (g i))")
|
||||
"header is not grouped"))
|
||||
|
||||
@@ -5,23 +5,18 @@
|
||||
;;; is checked here is what the compiler refuses, and that it refuses
|
||||
;;; it with the name of whatever is wrong.
|
||||
;;;
|
||||
;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm,
|
||||
;;; which run.scm includes first.
|
||||
;;; `sex->c', `read-source', `failure-message', `emits?', `reports?'
|
||||
;;; and `in-fn' come from codegen.scm, 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)))))))
|
||||
(check-public-interface (read-source source)))
|
||||
|
||||
(define (interface-reports? source fragment)
|
||||
(let ((m (handle-exceptions e ((condition-property-accessor 'exn 'message) e)
|
||||
(begin (interface-of source) #f))))
|
||||
(let ((m (failure-message (lambda () (interface-of source)))))
|
||||
(and m (string-contains m fragment) #t)))
|
||||
|
||||
(define (ops-src . body)
|
||||
|
||||
@@ -42,6 +42,9 @@
|
||||
|
||||
type-head?
|
||||
named-arg?
|
||||
named-arg-type
|
||||
arglist-types
|
||||
strip-pub
|
||||
array-bound?
|
||||
array-element-type)
|
||||
"../types.scm")
|
||||
|
||||
@@ -42,6 +42,9 @@
|
||||
|
||||
type-head?
|
||||
named-arg?
|
||||
named-arg-type
|
||||
arglist-types
|
||||
strip-pub
|
||||
array-bound?
|
||||
array-element-type)
|
||||
"types.scm")
|
||||
|
||||
19
types.scm
19
types.scm
@@ -389,6 +389,25 @@
|
||||
(pair? (cdr arg)) ; 1 element args are always type
|
||||
(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
|
||||
;;; 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
|
||||
|
||||
Reference in New Issue
Block a user