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:
2026-10-14 17:08:42 +03:00
parent 9af9113bf5
commit e91b44dbe1
2 changed files with 38 additions and 5 deletions

View File

@@ -1716,28 +1716,45 @@
;;; No function around a toplevel form, so no scope chain: what a
;;; 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)))
(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))
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)
;; 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))
(qualifier (and (memq (car resolved) '(pub extern)) (car 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
;; from
(core (if (eq? 'extern qualifier)
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
(copy-form-source! resolved (cons qualifier core))
core)))
(when (and (pair? (cdr core)) (pair? (cddr core)) (symbol? (second core)))
(add-name-type! (second core) (third core)))
(cons form acc)))
(cons form (append (hash-table-ref env :lambda-aux-code) acc))))
;;; Utils
(define (non-empty-list? form)