tests: add parser/vm bitops tests; docs: update cheatsheet and language guide for bitwise ops and shift; note Arrow(>>) removal
- Add src/tests/parser_bitops_test.rs and vm_bitops_test.rs - Update tokenizer unit test to expect SHIFT_RIGHT - Update quick-reference and language guide to document &,|,^,<<,>> and Arrow deprecation Known: one unrelated test failing (consolebox println TLV vs typebox) pre-existing.
This commit is contained in:
@ -53,6 +53,10 @@ a == b, a != b, a < b, a > b, a <= b, a >= b
|
|||||||
// 論理演算子
|
// 論理演算子
|
||||||
not condition, a and b, a or b
|
not condition, a and b, a or b
|
||||||
|
|
||||||
|
// ビット演算子(整数限定)
|
||||||
|
a & b, a | b, a ^ b
|
||||||
|
a << n, a >> n # 注意: 旧来の >>(ARROW)は廃止。パイプラインは |>
|
||||||
|
|
||||||
// Cross-type演算 (Phase 1で完全実装)
|
// Cross-type演算 (Phase 1で完全実装)
|
||||||
10 + 3.14 // → 13.14 (型変換)
|
10 + 3.14 // → 13.14 (型変換)
|
||||||
"Value: " + 42 // → "Value: 42" (文字列連結)
|
"Value: " + 42 // → "Value: 42" (文字列連結)
|
||||||
@ -271,4 +275,4 @@ while condition { } // 使用不可
|
|||||||
📚 **関連ドキュメント:**
|
📚 **関連ドキュメント:**
|
||||||
- [Getting Started](GETTING_STARTED.md) - 環境構築・最初の一歩
|
- [Getting Started](GETTING_STARTED.md) - 環境構築・最初の一歩
|
||||||
- [P2P Guide](P2P_GUIDE.md) - P2P通信システム完全ガイド
|
- [P2P Guide](P2P_GUIDE.md) - P2P通信システム完全ガイド
|
||||||
- [Built-in Boxes](reference/builtin-boxes.md) - ビルトインBox詳細リファレンス
|
- [Built-in Boxes](reference/builtin-boxes.md) - ビルトインBox詳細リファレンス
|
||||||
|
|||||||
@ -245,4 +245,14 @@ local result = await future # 結果待機
|
|||||||
- 変数は必ず宣言してから使う
|
- 変数は必ず宣言してから使う
|
||||||
- ループは`loop(condition)`のみ
|
- ループは`loop(condition)`のみ
|
||||||
- 親メソッドは`from Parent.method()`で呼ぶ
|
- 親メソッドは`from Parent.method()`で呼ぶ
|
||||||
- カンマ忘れに注意!
|
- カンマ忘れに注意!
|
||||||
|
### ビット演算子(整数限定)
|
||||||
|
```nyash
|
||||||
|
a & b # ビットAND
|
||||||
|
a | b # ビットOR
|
||||||
|
a ^ b # ビットXOR
|
||||||
|
a << n # 左シフト(n は 0..63 にマスク)
|
||||||
|
a >> n # 右シフト(現在は論理シフト相当の実装)
|
||||||
|
```
|
||||||
|
|
||||||
|
注意: 旧来の `>>`(ARROW 演算子)は廃止されました。パイプラインは `|>` を使用してください。
|
||||||
|
|||||||
20
src/tests/parser_bitops_test.rs
Normal file
20
src/tests/parser_bitops_test.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
use crate::parser::NyashParser;
|
||||||
|
use crate::ast::ASTNode;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_bitops_and_shift_precedence() {
|
||||||
|
// Expression: 1 + 2 << 3 & 7
|
||||||
|
// Precedence: shift before add, then &: (1 + (2 << 3)) & 7
|
||||||
|
let code = "return 1 + 2 << 3 & 7";
|
||||||
|
let ast = NyashParser::parse_from_string(code).expect("parse ok");
|
||||||
|
// Just ensure it parses into a Program and contains a Return; deeper tree checks are optional here
|
||||||
|
fn has_return(n: &ASTNode) -> bool {
|
||||||
|
match n {
|
||||||
|
ASTNode::Program { statements, .. } => statements.iter().any(has_return),
|
||||||
|
ASTNode::Return { .. } => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(has_return(&ast));
|
||||||
|
}
|
||||||
|
|
||||||
25
src/tests/vm_bitops_test.rs
Normal file
25
src/tests/vm_bitops_test.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
use crate::parser::NyashParser;
|
||||||
|
use crate::interpreter::NyashInterpreter;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vm_exec_bitwise_and_shift() {
|
||||||
|
let code = r#"
|
||||||
|
return (5 & 3) + (5 | 2) + (5 ^ 1) + (1 << 5) + (32 >> 3)
|
||||||
|
"#;
|
||||||
|
let ast = NyashParser::parse_from_string(code).expect("parse ok");
|
||||||
|
let mut interp = NyashInterpreter::new();
|
||||||
|
let out = interp.execute(ast).expect("exec ok");
|
||||||
|
assert_eq!(out.to_string_box().value, "48");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vm_exec_shift_masking() {
|
||||||
|
// 1 << 100 should mask to 1 << (100 & 63) = 1 << 36
|
||||||
|
let code = r#" return 1 << 100 "#;
|
||||||
|
let ast = NyashParser::parse_from_string(code).expect("parse ok");
|
||||||
|
let mut interp = NyashInterpreter::new();
|
||||||
|
let out = interp.execute(ast).expect("exec ok");
|
||||||
|
// compute expected as i64
|
||||||
|
let expected = (1_i64 as i64).wrapping_shl((100_u32) & 63);
|
||||||
|
assert_eq!(out.to_string_box().value, expected.to_string());
|
||||||
|
}
|
||||||
@ -677,7 +677,7 @@ mod tests {
|
|||||||
let mut tokenizer = NyashTokenizer::new(">> == != <= >= < >");
|
let mut tokenizer = NyashTokenizer::new(">> == != <= >= < >");
|
||||||
let tokens = tokenizer.tokenize().unwrap();
|
let tokens = tokenizer.tokenize().unwrap();
|
||||||
|
|
||||||
assert_eq!(tokens[0].token_type, TokenType::ARROW);
|
assert_eq!(tokens[0].token_type, TokenType::SHIFT_RIGHT);
|
||||||
assert_eq!(tokens[1].token_type, TokenType::EQUALS);
|
assert_eq!(tokens[1].token_type, TokenType::EQUALS);
|
||||||
assert_eq!(tokens[2].token_type, TokenType::NotEquals);
|
assert_eq!(tokens[2].token_type, TokenType::NotEquals);
|
||||||
assert_eq!(tokens[3].token_type, TokenType::LessEquals);
|
assert_eq!(tokens[3].token_type, TokenType::LessEquals);
|
||||||
|
|||||||
Reference in New Issue
Block a user