Files
hakorune/docs/reference/language/quick-reference.md
tomoaki a40ee8dda5 refactor(plan): Phase 286C-4 Step 1 - plan_helpers module
Create helper functions to support plan_rewrites() extraction:
- build_local_block_map(): Build block ID mapping for a function
- sync_spans(): Synchronize instruction spans after rewriting

These pure functions will be used by both the current monolithic
merge_and_rewrite() and the new plan_rewrites() function.

Progress: Step 1/4 (helpers) complete

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-25 03:53:42 +09:00

78 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Nyash Quick Reference (MVP)
Purpose
- Onepage practical summary for writing and implementing Nyash.
- Keep grammar minimal; clarify rules that often cause confusion.
Keywords (reserved)
- control: `if`, `else`, `loop`, `match`, `case`, `break`, `continue`, `return`
- decl: `static`, `box`, `local`, `using`, `as`
- lit: `true`, `false`, `null`, `void`
Expressions and Calls
- Function call: `f(a, b)`
- Method call: `obj.m(a, b)` — internally normalized to function form: `Class.m(me: obj, a, b)`
- DefaultONP4: Known 受信者かつ関数が一意に存在する場合に正規化userbox 限定)。
- それ以外Unknown/core/userinstanceは安全に BoxCall へフォールバック(挙動不変)。
- 環境で無効化: `NYASH_REWRITE_KNOWN_DEFAULT=0`(開発時の切替用)。
- バックエンドVM/LLVM/Nyは統一形状の呼び出しを受け取る。
- Member: `obj.field` or `obj.m`
Display & Conversion
- Humanreadable display: `str(x)`(推奨)/ `x.str()`
- 既存の `toString()``str()` に正規化Builder早期リライト
- 互換: 既存の `stringify()` は当面エイリアス(内部で `str()` 相当へ誘導)。
- Debug表示構造的・安定: `repr(x)`将来導入、devのみ
- JSONシリアライズ: `toJson(x)`(文字列)/ `toJsonNode(x)`(構造)
Operators (precedence high→low)
- Unary: `! ~ - weak` (`weak <expr>` produces a `WeakRef`; `weak(expr)` is invalid)
- Multiplicative: `* / %`
- Additive: `+ -`
- Compare: `== != < <= > >=`
- Logical: `&& ||` (shortcircuit, sideeffect aware)
Semicolons and ASI (Automatic Semicolon Insertion)
- Allowed to omit semicolon at:
- End of line, before `}` or at EOF, when the statement is syntactically complete.
- Not allowed:
- Line break immediately after a binary operator (e.g., `1 +\n2`)
- Ambiguous continuations; parser must FailFast with a clear message.
Truthiness (boolean context)
- SSOT: `reference/language/types.md`runtime truthiness
- 実行上は `Bool/Integer/Float/String/Void` が中心。`BoxRef` は一部のコアBoxのみ許可され、その他は `TypeError`Fail-Fast
- `null``void` の別名(構文糖衣)。どちらも boolean context では `TypeError`
Equality and Comparison
- SSOT: `reference/language/types.md``==`/`!=``< <= > >=` の runtime 仕様)
- `==` は一部の cross-kind`Integer↔Bool`, `Integer↔Float`)を best-effort で扱う。その他は `false`
- `< <= > >=``Integer/Float/String` の **同型同士**のみ(異型は `TypeError`)。
String and Numeric `+`
- SSOT: `reference/language/types.md`runtime `+` 仕様)
- `Integer+Integer`, `Float+Float` は加算。片側が `String` なら文字列連結(相手は文字列化)。
- それ以外(例: `Integer+Bool`, `Integer+Float`)は `TypeError`Fail-Fast
Blocks and Control
- `if (cond) { ... } [else { ... }]`
- `loop (cond) { ... }` — minimal loop form
- `match (expr) { case ... }` — MVP (literals and simple type patterns)
Using / SSOT
- Dev/CI: filebased `using` allowed for convenience.
- Prod: `nyash.toml` only. Duplicate imports or alias rebinding is an error.
Errors (format)
- Always: `Error at line X, column Y: <message>`
- For tokenizer errors, add the reason and show one nearby line if possible.
Dev/Prod toggles (indicative)
- `NYASH_DEV=1` — developer defaults (diagnostics, tracing; behavior unchanged)
- `NYASH_ENABLE_USING=1` — enable using resolver
- `NYASH_ENTRY_ALLOW_TOPLEVEL_MAIN=1` — allow `main` as toplevel entry
Notes
- Keep the language small. Prefer explicit conversions (`int(x)`, `str(x)`, `bool(x)`) in standard helpers over implicit coercions.
- Builder rewrites method calls to keep runtime dispatch simple and consistent across backends.