From 1d24570f1bb5455c69ec9857cbf55a6733ab3833 Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Sat, 2 May 2026 15:25:09 -0500 Subject: [PATCH] . --- doc/OPCODES | 5 +++-- src/assembler/lexer.cc | 4 +++- src/assembler/tests.cc | 2 +- src/vm/instruction.cc | 26 ++++++++++++++++---------- src/vm/instruction.hh | 27 +++++++++++++++------------ src/vm/tests.cc | 13 +++++++++++++ 6 files changed, 51 insertions(+), 26 deletions(-) diff --git a/doc/OPCODES b/doc/OPCODES index 06676fb..764758b 100644 --- a/doc/OPCODES +++ b/doc/OPCODES @@ -30,8 +30,9 @@ Stack operations: 05 dup Local variables: - a3 c3 e3 setl [int] Set stack top as indexed local variable - a4 c4 e4 getl [int] Get indexed local variable and place on stack + a3 c3 e3 pushv [int] Push n nil values into the stack (used to init local vars) + ab cb eb set [index] Set value in stack position (set local variable) + a4 c4 e4 dupv [index] Duplicate stack value (load local variable) a5 c5 e5 setg [int] Set global variable a6 c6 e6 getg [int] Get global variable diff --git a/src/assembler/lexer.cc b/src/assembler/lexer.cc index 03b3836..ab10cf2 100644 --- a/src/assembler/lexer.cc +++ b/src/assembler/lexer.cc @@ -100,7 +100,9 @@ void Lexer::ingest_next_token() } else if (c == ':') { type = TokenType::Colon; ++pos_; - } else if (c == '\n') { + } else if (c == '\n' || c == ';') { + while (pos_ < source_.size() && source_.at(pos_) != '\n') + ++pos_; type = TokenType::Enter; value = "\n"; ++pos_; diff --git a/src/assembler/tests.cc b/src/assembler/tests.cc index 06b446a..d897b8a 100644 --- a/src/assembler/tests.cc +++ b/src/assembler/tests.cc @@ -56,7 +56,7 @@ TEST(Assember, Assembler) 1: "Hello world" .func 0 - pushi 2 + pushi 2 ; this is a comment pushi 3 sum ret diff --git a/src/vm/instruction.cc b/src/vm/instruction.cc index 90fa77a..b246919 100644 --- a/src/vm/instruction.cc +++ b/src/vm/instruction.cc @@ -15,8 +15,9 @@ const std::unordered_map instruction_names = { { "newt", Instruction::NewTable }, { "pop", Instruction::Pop }, { "dup", Instruction::Duplicate }, - { "setl", Instruction::SetLocal8 }, - { "getl", Instruction::GetLocal8 }, + { "pushv", Instruction::PushValues8 }, + { "set", Instruction::SetValue8 }, + { "dupv", Instruction::DuplicateValue8 }, { "setg", Instruction::SetGlobal8 }, { "getl", Instruction::GetGlobal8 }, { "call8", Instruction::Call8 }, @@ -88,15 +89,20 @@ std::pair debug_instruction(Instruction inst, int oper) case Instruction::NewTable: out = "newt"; break; case Instruction::Pop: out = "pop"; break; case Instruction::Duplicate: out = "dup"; break; - case Instruction::SetLocal8: - case Instruction::SetLocal16: - case Instruction::SetLocal32: - out = "setl"; + case Instruction::PushValues8: + case Instruction::PushValues16: + case Instruction::PushValues32: + out = "pushv"; break; - case Instruction::GetLocal8: - case Instruction::GetLocal16: - case Instruction::GetLocal32: - out = "getl"; + case Instruction::SetValue8: + case Instruction::SetValue16: + case Instruction::SetValue32: + out = "set"; + break; + case Instruction::DuplicateValue8: + case Instruction::DuplicateValue16: + case Instruction::DuplicateValue32: + out = "dupv"; break; case Instruction::SetGlobal8: case Instruction::SetGlobal16: diff --git a/src/vm/instruction.hh b/src/vm/instruction.hh index 78023ad..f53bc2e 100644 --- a/src/vm/instruction.hh +++ b/src/vm/instruction.hh @@ -32,18 +32,21 @@ enum class Instruction : uint8_t { Duplicate = 0x05, // local variables - SetLocal8 = 0xa3, - SetLocal16 = 0xc3, - SetLocal32 = 0xe3, - GetLocal8 = 0xa4, - GetLocal16 = 0xc4, - GetLocal32 = 0xe4, - SetGlobal8 = 0xa5, - SetGlobal16 = 0xc5, - SetGlobal32 = 0xe5, - GetGlobal8 = 0xa6, - GetGlobal16 = 0xc6, - GetGlobal32 = 0xe6, + PushValues8 = 0xa3, + PushValues16 = 0xc3, + PushValues32 = 0xe3, + SetValue8 = 0xab, + SetValue16 = 0xcb, + SetValue32 = 0xeb, + DuplicateValue8 = 0xa4, + DuplicateValue16 = 0xc4, + DuplicateValue32 = 0xe4, + SetGlobal8 = 0xa5, + SetGlobal16 = 0xc5, + SetGlobal32 = 0xe5, + GetGlobal8 = 0xa6, + GetGlobal16 = 0xc6, + GetGlobal32 = 0xe6, // function operations Call8 = 0xa7, diff --git a/src/vm/tests.cc b/src/vm/tests.cc index b147cc7..8ef8dd7 100644 --- a/src/vm/tests.cc +++ b/src/vm/tests.cc @@ -268,6 +268,19 @@ TEST(VM, StringString) )").to_integer(-1), 0); } +TEST(VM, LocalVariables) +{ + /* + ASSERT_EQ(run(R"( + .func 0 + pushv 2 + pushi 3 + setv 0 + pushi + )").to_integer(-1), 4); + */ +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv);