Files
hakorune/docs/quick-reference/syntax-cheatsheet.md

259 lines
5.3 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構文早見表Syntax Cheatsheet
## 📝 基本構文
### 変数宣言
```nyash
local x # 変数宣言
local x = 42 # 初期化付き宣言
local a, b, c # 複数宣言
local x = 10, y = 20, z # 混合初期化
```
### Box定義クラス
```nyash
box ClassName {
# フィールド宣言Phase 12.7形式)
field1: TypeBox # デフォルト非公開
public field2: TypeBox # 公開フィールド
private field3: TypeBox # 明示的非公開
birth(args) { # コンストラクタbirth統一
me.field1 = args
}
}
```
### Static Boxエントリーポイント
```nyash
static box Main {
console: ConsoleBox
main() {
me.console = new ConsoleBox()
me.console.log("Hello Nyash!")
return 0
}
}
```
## 🔄 制御構文
### 条件分岐
```nyash
if condition {
# 処理
} else if condition2 {
# 処理2
} else {
# else処理
}
```
### ループ(統一構文)
```nyash
# ✅ 唯一の正しい形式
loop(condition) {
if exitCondition {
break
}
}
# ❌ 使えない構文
while condition { } # エラー!
loop() { } # エラー!
```
## 🌟 デリゲーション(継承)
### 基本デリゲーション
```nyash
box Child from Parent {
birth(args) {
from Parent.birth(args) # 親のbirth呼び出し
}
method() { # メソッド定義
from Parent.method() # 親メソッド呼び出し
}
}
```
### 多重デリゲーション
```nyash
box Multi from StringBox, IntegerBox {
# 複数の親から機能を継承
}
```
## ⚡ 演算子
### 算術演算子
```nyash
a + b # 加算
a - b # 減算
a * b # 乗算
a / b # 除算(ゼロ除算チェック済)
a % b # 剰余
```
### 論理演算子
```nyash
not condition # NOT推奨
a and b # AND推奨
a or b # OR推奨
# シンボル版(互換性のため存在するが非推奨)
!condition # NOT
a && b # AND
a || b # OR
```
### 比較演算子
```nyash
a == b # 等しい
a != b # 等しくない
a < b # より小さい
a > b # より大きい
a <= b # 以下
a >= b # 以上
```
## 📦 オブジェクト生成
### new構文
```nyash
# 基本形
obj = new ClassName()
obj = new ClassName(arg1, arg2)
# 組み込みBox
console = new ConsoleBox()
array = new ArrayBox()
map = new MapBox()
```
### 特殊なコンストラクタ優先順位
```nyash
box Life {
birth(name) { } # 最優先
pack(args) { } # ビルトインBox継承用
init(args) { } # 通常コンストラクタ
Life(args) { } # Box名形式非推奨
}
# birthが定義されていればbirthが呼ばれる
obj = new Life("Alice")
```
## 🚨 よくある間違い
### ❌ カンマ忘れCPU暴走の原因
```nyash
# ❌ 間違い
init { field1 field2 } # カンマなし→CPU暴走
# ✅ 正しい
init { field1, field2 } # カンマ必須
```
### ❌ 未宣言変数
```nyash
# ❌ 間違い
x = 42 # Runtime Error: 未宣言変数
# ✅ 正しい
local x
x = 42
```
### ❌ 削除された構文
```nyash
# ❌ 使えない
while condition { } # whileは削除済み
super.method() # superは使えない
# ✅ 代わりに
loop(condition) { } # loop構文を使う
from Parent.method() # fromで親を呼ぶ
```
## 🎯 実用例
### Hello World
```nyash
static box Main {
main() {
print("Hello, Nyash!")
return 0
}
}
```
### 配列操作
```nyash
local array
array = new ArrayBox()
array.push(10)
array.push(20)
array.push(30)
local sum = 0
local i = 0
loop(i < array.length()) {
sum = sum + array.get(i)
i = i + 1
}
print("Sum: " + sum) # Sum: 60
```
### エラーハンドリング
```nyash
try {
local result = riskyOperation()
} catch (error) {
print("Error: " + error.message)
} cleanup {
cleanup()
}
```
### 非同期処理
```nyash
nowait future = longTask() # 非同期実行
# 他の処理...
local result = await future # 結果待機
```
## 📚 組み込みBox一覧抜粋
| Box名 | 用途 | 主要メソッド |
|-------|------|-------------|
| StringBox | 文字列 | split(), find(), replace(), trim() |
| IntegerBox | 整数 | to_string_box() |
| ArrayBox | 配列 | push(), pop(), get(), set(), length() |
| MapBox | 辞書 | set(), get(), has(), keys() |
| ConsoleBox | コンソール | log(), error(), read() |
| MathBox | 数学 | sin(), cos(), sqrt(), random() |
| FileBox | ファイル | read(), write(), exists() |
| JSONBox | JSON | parse(), stringify(), get(), set() |
---
**Tips**:
- すべての値はBoxEverything is Box
- 変数は必ず宣言してから使う
- ループは`loop(condition)`のみ
- 親メソッドは`from Parent.method()`で呼ぶ
- カンマ忘れに注意!
### ビット演算子(整数限定)
```nyash
a & b # ビットAND
a | b # ビットOR
a ^ b # ビットXOR
a << n # 左シフトn は 0..63 にマスク)
a >> n # 右シフト(現在は論理シフト相当の実装)
```
注意: 旧来の `>>`ARROW 演算子)は廃止されました。パイプラインは `|>` を使用してください。