lunar_wave/src/main.rs

85 lines
1.5 KiB
Rust
Raw Normal View History

mod state;
2023-09-24 21:34:28 +00:00
#[cfg (test)]
mod tests;
2023-09-24 21:34:28 +00:00
fn main() {
2023-09-25 00:47:17 +00:00
use state::{
Block,
Chunk,
Instruction as Inst,
State,
};
2023-09-25 00:47:17 +00:00
/*
local function bool_to_x (b)
if b then
return 99
else
return 98
end
end
local x = bool_to_x (not not arg [1])
print (x)
return x
*/
let chunk = Chunk {
blocks: vec! [
Block {
instructions: vec! [
Inst::VarArgPrep (0),
Inst::Closure (0, 0),
Inst::GetTabUp (1, 0, 0),
Inst::Move (2, 0),
Inst::LoadFalse (3),
Inst::Call (2, 2, 0),
Inst::Call (1, 0, 1),
Inst::GetTabUp (1, 0, 0),
Inst::Move (2, 0),
Inst::LoadTrue (3),
Inst::Call (2, 2, 0),
Inst::Call (1, 0, 1),
Inst::Move (1, 0),
Inst::GetTabUp (2, 0, 1),
Inst::GetI (2, 2, 1),
Inst::Not (2, 2),
Inst::Not (2, 2),
Inst::Call (1, 2, 2),
Inst::GetTabUp (2, 0, 0),
Inst::Move (3, 1),
Inst::Call (2, 2, 1),
Inst::Return (1, 2, 1),
Inst::Return (2, 1, 1),
],
constants: vec! [
"print".into (),
"arg".into (),
],
},
Block {
instructions: vec! [
Inst::Test (0, 0),
Inst::Jmp (3),
Inst::LoadI (1, 99),
Inst::Return1 (1),
Inst::Jmp (2),
Inst::LoadI (1, 98),
Inst::Return1 (1),
Inst::Return0,
],
constants: vec! [],
},
2023-09-24 22:34:38 +00:00
],
};
let mut vm = State::default ();
2023-09-25 00:47:17 +00:00
if std::env::var("LUA_DEBUG").is_ok() {
vm.debug_print = true;
}
let upvalues = State::upvalues_from_args (std::env::args ());
2023-09-24 21:57:12 +00:00
println! ("Returned: {:?}", vm.execute_chunk (&chunk, &upvalues));
}