Implement module loading and imports

This commit is contained in:
2026-06-16 10:01:31 +09:00
parent fead194ba6
commit bd3da1aeee
13 changed files with 586 additions and 124 deletions
+10 -3
View File
@@ -54,23 +54,30 @@ desugar は、意味論を単純にするための表層構文変換を行う。
## module registry
module registry は、読み込んだ module を canonical path で管理する。
module registry は、読み込んだ module を loader が返す安定 key で管理する。
CLI では canonical path を key とする。
組み込み利用では、resource name や static source table の key を使える。
```text
ModuleRegistry:
CanonicalPath -> ModuleId
ModuleKey -> ModuleId
```
処理系は、まず root module を parse / desugar して registry に登録する。
import 先 module は、この段階で全て読み込む必要はない。
import expression が評価されたとき、module registry は path を解決し、未登録なら対象 module を parse / desugar して登録する
import expression が評価されたとき、処理系は `SourceLoader` に現在の module key と import specifier を渡す
loader は module key、表示名、source text を返す。
module registry は key が未登録なら対象 module を parse / desugar して登録する。
登録された module は module root thunk を持つ。
同じ module が複数回 import された場合は、同じ `ModuleId` を返す。
つまり import は module を即時評価しない。
module を読み込み、module root を thunk として登録するだけにする。
AST の `ExprId` は module-local である。
そのため runtime が保持する式参照は `ExprRef { module, expr }` として module-qualified にする。
## demand-driven evaluation
評価器は、必要になった thunk だけを force する。