walk a global's initializer like a local's
Only a local's initializer was walked, so at file scope a path was not dispatched, a lambda was never lifted, and a constructor written in place would have been left a call to nothing.
This commit is contained in:
27
semen.scm
27
semen.scm
@@ -1716,28 +1716,45 @@
|
|||||||
|
|
||||||
;;; No function around a toplevel form, so no scope chain: what a
|
;;; No function around a toplevel form, so no scope chain: what a
|
||||||
;;; global's initializer names comes from `get-name-type' alone.
|
;;; global's initializer names comes from `get-name-type' alone.
|
||||||
(define (make-toplevel-env)
|
;;; What a global's initializer is walked in: no names but its own,
|
||||||
|
;;; and anything it lifts is named after the global
|
||||||
|
(define (make-toplevel-env name)
|
||||||
(let ((env (make-hash-table)))
|
(let ((env (make-hash-table)))
|
||||||
|
(set! (hash-table-ref env :fn-name) name)
|
||||||
|
(set! (hash-table-ref env :lambda-counter) 0)
|
||||||
|
(set! (hash-table-ref env :lambda-aux-code) (list))
|
||||||
(set! (hash-table-ref env :scopes) (list))
|
(set! (hash-table-ref env :scopes) (list))
|
||||||
env))
|
env))
|
||||||
|
|
||||||
|
;;; The writer spells neither `closure' nor `_', and an initializer is
|
||||||
|
;;; an expression like any other:
|
||||||
|
;;;
|
||||||
|
;;; (var n _ 1) -> int n = 1
|
||||||
|
;;; (var o v #(v : (.get a))) -> v o = (v){vːget(&a)}
|
||||||
(define (process-global-var sex-var acc)
|
(define (process-global-var sex-var acc)
|
||||||
;; A global is not walked for lambdas, but the writer spells neither
|
|
||||||
;; `closure' nor `_': `(var n _ 1)' has to reach it as `int n = 1'
|
|
||||||
(let* ((resolved (resolve-closure-types sex-var))
|
(let* ((resolved (resolve-closure-types sex-var))
|
||||||
(qualifier (and (memq (car resolved) '(pub extern)) (car resolved)))
|
(qualifier (and (memq (car resolved) '(pub extern)) (car resolved)))
|
||||||
(core (if qualifier (cdr resolved) resolved))
|
(core (if qualifier (cdr resolved) resolved))
|
||||||
|
(env (make-toplevel-env (and (pair? (cdr core)) (second core))))
|
||||||
;; `(extern var n int)' has no initializer to work a `_' out
|
;; `(extern var n int)' has no initializer to work a `_' out
|
||||||
;; from
|
;; from
|
||||||
(core (if (eq? 'extern qualifier)
|
(core (if (eq? 'extern qualifier)
|
||||||
core
|
core
|
||||||
(resolve-wildcard core (make-toplevel-env))))
|
(resolve-wildcard
|
||||||
|
(copy-form-source!
|
||||||
|
core
|
||||||
|
(if (> (length core) 3)
|
||||||
|
(append (take core 3)
|
||||||
|
(parameterize ((operand-position? #t))
|
||||||
|
(walk-body (drop core 3) env)))
|
||||||
|
core))
|
||||||
|
env)))
|
||||||
(form (if qualifier
|
(form (if qualifier
|
||||||
(copy-form-source! resolved (cons qualifier core))
|
(copy-form-source! resolved (cons qualifier core))
|
||||||
core)))
|
core)))
|
||||||
(when (and (pair? (cdr core)) (pair? (cddr core)) (symbol? (second core)))
|
(when (and (pair? (cdr core)) (pair? (cddr core)) (symbol? (second core)))
|
||||||
(add-name-type! (second core) (third core)))
|
(add-name-type! (second core) (third core)))
|
||||||
(cons form acc)))
|
(cons form (append (hash-table-ref env :lambda-aux-code) acc))))
|
||||||
|
|
||||||
;;; Utils
|
;;; Utils
|
||||||
(define (non-empty-list? form)
|
(define (non-empty-list? form)
|
||||||
|
|||||||
@@ -691,6 +691,22 @@ compiles."
|
|||||||
(test-assert "a bare -> is not the arrow"
|
(test-assert "a bare -> is not the arrow"
|
||||||
(emits? (in-line "(= (-> pp x) 1)") "->(pp, x) = 1")))
|
(emits? (in-line "(= (-> pp x) 1)") "->(pp, x) = 1")))
|
||||||
|
|
||||||
|
;; A global's initializer is an expression like a local's, and walked
|
||||||
|
;; the same way
|
||||||
|
(test-group "global initializers"
|
||||||
|
(test-assert "a path is dispatched on its type"
|
||||||
|
(emits? (string-append "(struct pt ((x int)))"
|
||||||
|
"(var a (struct pt) #((struct pt) : 1))"
|
||||||
|
"(var q (* int) (& (. a x)))")
|
||||||
|
"int * q = &a.x"))
|
||||||
|
(test-assert "a lambda is lifted, and named after the global"
|
||||||
|
(emits? "(var f (fn ((x int)) int) (lambda ((x int)) int (return x)))"
|
||||||
|
"int (*f)(int) = λ0_f"))
|
||||||
|
(test-assert "ahead of it"
|
||||||
|
(let ((c (sex->c "(var f (fn ((x int)) int) (lambda ((x int)) int (return x)))")))
|
||||||
|
(< (string-contains c "static int λ0_f (int x)")
|
||||||
|
(string-contains c "(*f)(int) = λ0_f")))))
|
||||||
|
|
||||||
;; Toplevel forms used to be separated by a blank line unconditionally,
|
;; Toplevel forms used to be separated by a blank line unconditionally,
|
||||||
;; which spread a block of adjacent typedefs over twice the lines its
|
;; which spread a block of adjacent typedefs over twice the lines its
|
||||||
;; author wrote
|
;; author wrote
|
||||||
|
|||||||
Reference in New Issue
Block a user