From 54f7d6eb63d3a7d90dc7d990fcea4df2d47653ac Mon Sep 17 00:00:00 2001 From: Moe Charm Date: Mon, 18 Aug 2025 22:04:50 +0900 Subject: [PATCH] WIP: Add Store instruction to MIR for variable assignments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added Store instruction generation in build_assignment() - This partially addresses the VM infinite loop issue - However, the loop still uses old values (%0) instead of updated values - Need to implement proper SSA phi nodes for loop variables The root cause: MIR generation doesn't properly track variable updates in loops. Current SSA implementation lacks phi nodes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- local_tests/test_vm_simple_loop.nyash | 12 ++++++++++++ local_tests/test_windows_vm.nyash | 25 +++++++++++++++++++++++++ src/mir/builder.rs | 10 +++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 local_tests/test_vm_simple_loop.nyash create mode 100644 local_tests/test_windows_vm.nyash diff --git a/local_tests/test_vm_simple_loop.nyash b/local_tests/test_vm_simple_loop.nyash new file mode 100644 index 00000000..ca1fb161 --- /dev/null +++ b/local_tests/test_vm_simple_loop.nyash @@ -0,0 +1,12 @@ +// 超シンプルなVMループテスト +local i = 1 +print("Before loop: i = " + i) + +// 3回だけループ +loop(i <= 3) { + print("In loop: i = " + i) + i = i + 1 + print("After increment: i = " + i) +} + +print("After loop: i = " + i) \ No newline at end of file diff --git a/local_tests/test_windows_vm.nyash b/local_tests/test_windows_vm.nyash new file mode 100644 index 00000000..6a30a87d --- /dev/null +++ b/local_tests/test_windows_vm.nyash @@ -0,0 +1,25 @@ +// Windows VM Test Program +// 超シンプルなプログラムでVMの動作確認 + +print("🎉 Hello from Nyash VM on Windows!") +print("=====================================") + +// 基本的な計算 +local x = 10 +local y = 20 +local result = x + y +print("10 + 20 = " + result) + +// ループテスト +print("\nCounting to 5:") +local i = 1 +loop(i <= 5) { + print(" Count: " + i) + i = i + 1 +} + +// Box生成テスト +local str = new StringBox("Nyash") +print("\nStringBox created: " + str) + +print("\n✅ VM test completed successfully!") \ No newline at end of file diff --git a/src/mir/builder.rs b/src/mir/builder.rs index 4178bfbe..5ffb403a 100644 --- a/src/mir/builder.rs +++ b/src/mir/builder.rs @@ -316,7 +316,15 @@ impl MirBuilder { let value_id = self.build_expression(value)?; // In SSA form, each assignment creates a new value - self.variable_map.insert(var_name, value_id); + self.variable_map.insert(var_name.clone(), value_id); + + // Generate a Store instruction to ensure VM can track the assignment + // For now, we use the variable name as a simple pointer identifier + let var_ptr = self.value_gen.next(); + self.emit_instruction(MirInstruction::Store { + value: value_id, + ptr: var_ptr, + })?; Ok(value_id) }