feat: 配列/Mapリテラル糖衣構文の実装とネームスペース解決の改善計画
- ArrayLiteral/MapLiteralのAST定義追加
- パーサーで[...]と{...}構文をサポート
- MIR Builderでnew Box() + push/setへのdesugaring実装
- テストケースとスモークスクリプト追加
- CURRENT_TASK.mdにネームスペース解決Phase-1計画を追記
- 3段階解決順序(ローカル→エイリアス→プラグイン)の設計合意
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
# using — Imports and Namespaces (Phase 15)
|
||||
# using — Imports and Namespaces (Phase 15+)
|
||||
|
||||
Status: Accepted (Runner‑side resolution). Selfhost parser accepts using as no‑op and attaches `meta.usings` for future use.
|
||||
|
||||
@ -9,6 +9,50 @@ Policy
|
||||
- Registers modules into an internal registry for path/namespace hints.
|
||||
- Selfhost compiler (Ny→JSON v0) collects using lines and emits `meta.usings` when present. The bridge currently ignores this meta field.
|
||||
|
||||
## Namespace Resolution (Runner‑side)
|
||||
- Goal: keep IR/VM/JIT untouched. All resolution happens in Runner/Registry.
|
||||
- Default search order (3 stages, deterministic):
|
||||
1) Local/Core Boxes (nyrt)
|
||||
2) Aliases (nyash.toml [imports] / `needs … as …`)
|
||||
3) Plugins (short name if unique, otherwise qualified `pluginName.BoxName`)
|
||||
- On ambiguity: error with candidates and remediation (qualify or define alias).
|
||||
- Modes:
|
||||
- Relaxed (default): short names allowed when unique.
|
||||
- Strict: require plugin prefix (env `NYASH_PLUGIN_REQUIRE_PREFIX=1` or nyash.toml `[plugins] require_prefix=true`).
|
||||
- Aliases:
|
||||
- nyash.toml `[imports] HttpClient = "network.HttpClient"`
|
||||
- needs sugar: `needs plugin.network.HttpClient as HttpClient` (file‑scoped alias)
|
||||
|
||||
## Plugins
|
||||
- Unified namespace with Boxes. Prefer short names when unique.
|
||||
- Qualified form: `network.HttpClient`
|
||||
- Per‑plugin control (nyash.toml): `prefix`, `require_prefix`, `expose_short_names`
|
||||
|
||||
## `needs` sugar (optional)
|
||||
- Treated as a synonym to `using` on the Runner side; registers aliases only.
|
||||
- Examples: `needs utils.StringHelper`, `needs plugin.network.HttpClient as HttpClient`, `needs plugin.network.*`
|
||||
|
||||
## nyash.toml keys (MVP)
|
||||
- `[imports]`/`[aliases]`: short name → fully qualified
|
||||
- `[plugins.<name>]`: `path`, `prefix`, `require_prefix`, `expose_short_names`
|
||||
|
||||
## Index and Cache (Runner)
|
||||
- Build a Box index once per run to make resolution fast and predictable:
|
||||
```
|
||||
struct BoxIndex {
|
||||
local_boxes: HashMap<String, PathBuf>,
|
||||
plugin_boxes: HashMap<String, Vec<PluginBox>>,
|
||||
aliases: HashMap<String, String>,
|
||||
}
|
||||
```
|
||||
- Maintain a small resolve cache per thread:
|
||||
```
|
||||
thread_local! {
|
||||
static RESOLVE_CACHE: RefCell<HashMap<String, ResolvedBox>> = /* ... */;
|
||||
}
|
||||
```
|
||||
- Trace: `NYASH_RESOLVE_TRACE=1` prints resolution steps (for debugging/CI logs).
|
||||
|
||||
Syntax
|
||||
- Namespace: `using core.std` or `using core.std as Std`
|
||||
- File path: `using "apps/examples/string_p0.nyash" as Strings`
|
||||
@ -34,6 +78,28 @@ static box Main {
|
||||
}
|
||||
```
|
||||
|
||||
Qualified/Plugins/Aliases examples
|
||||
```nyash
|
||||
# nyash.toml
|
||||
[plugins.network]
|
||||
path = "plugins/network.so"
|
||||
prefix = "network"
|
||||
require_prefix = false
|
||||
|
||||
[imports]
|
||||
HttpClient = "network.HttpClient"
|
||||
|
||||
# code
|
||||
needs plugin.network.HttpClient as HttpClient
|
||||
|
||||
static box Main {
|
||||
main(args) {
|
||||
let a = new HttpClient() # alias
|
||||
let b = new network.HttpClient() # qualified
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Runner Configuration
|
||||
- Enable using pre‑processing: `NYASH_ENABLE_USING=1`
|
||||
- Selfhost pipeline keeps child stdout quiet and extracts JSON only: `NYASH_JSON_ONLY=1` (set by Runner automatically for child)
|
||||
@ -42,4 +108,3 @@ Runner Configuration
|
||||
Notes
|
||||
- Phase 15 keeps resolution in the Runner to minimize parser complexity. Future phases may leverage `meta.usings` for compiler decisions.
|
||||
- Unknown fields in the top‑level JSON (like `meta`) are ignored by the current bridge.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user