Variables #8
@@ -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
|
||||
|
||||
|
||||
@@ -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_;
|
||||
|
||||
@@ -56,7 +56,7 @@ TEST(Assember, Assembler)
|
||||
1: "Hello world"
|
||||
|
||||
.func 0
|
||||
pushi 2
|
||||
pushi 2 ; this is a comment
|
||||
pushi 3
|
||||
sum
|
||||
ret
|
||||
|
||||
@@ -15,8 +15,9 @@ const std::unordered_map<std::string, Instruction> 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<std::string, size_t> 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:
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user