lunar_wave/src/main.rs

203 lines
4.6 KiB
Rust
Raw Normal View History

2023-09-24 21:34:28 +00:00
use std::collections::BTreeMap;
enum Instruction {
VarArgPrep (i32),
GetTabUp (u8, u8, u8),
GetI (u8, u8, u8),
EqK (u8, u8, u8),
Jmp (i32),
LoadK (u8, i32),
Call (u8, u8, u8),
Closure (u8, i32),
Return (u8, u8, u8),
}
#[derive (Clone, Debug, PartialEq)]
enum Value {
Nil,
False,
True,
Float (f64),
String (String),
// These are all bogus, I haven't figured out how to implement
// tables and function pointers yet
2023-09-24 21:34:28 +00:00
BogusArg (Vec <String>),
BogusEnv (BTreeMap <String, Value>),
BogusPrint,
}
impl Default for Value {
fn default () -> Self {
Self::Nil
}
}
impl From <String> for Value {
fn from (x: String) -> Self {
Self::String (x)
}
}
impl From <&str> for Value {
fn from (x: &str) -> Self {
Self::from (String::from (x))
}
}
struct Chunk {
instructions: Vec <Instruction>,
constants: Vec <Value>,
}
2023-09-24 21:34:28 +00:00
struct VirtualMachine {
registers: Vec <Value>,
// i32 makes it a little easier to implement jumps
program_counter: i32,
}
impl Default for VirtualMachine {
fn default () -> Self {
Self {
registers: vec! [Value::Nil; 256],
program_counter: 0,
}
}
}
impl VirtualMachine {
fn execute_chunk (&mut self, chunk: &Chunk, upvalues: &Vec <Value>) {
let max_iters = 2000;
for _ in 0..max_iters {
let instruction = chunk.instructions.get (usize::try_from (self.program_counter).unwrap ()).unwrap ();
let r = &mut self.registers;
let k = &chunk.constants;
match instruction {
Instruction::Call (a, b, c) => {
// Take arguments from registers [a + 1, a + b)
// Call the function in register [a]
// Return values in registers [a, a + c - 1)
//
// That is, call a with b - 1 arguments and expect c returns
//
// e.g. CALL 0 2 1 mean "Call 0 with 1 argument, return 1 value", like for printing a constant
// TODO: Only implement printing constants for now
let a = usize::try_from (*a).unwrap ();
assert_eq! (*b, 2);
assert_eq! (*c, 1);
println! ("{:?}", r [a + 1]);
},
Instruction::EqK (a, b, c_k) => {
let a = usize::try_from (*a).unwrap ();
let b = usize::try_from (*b).unwrap ();
let equal = r [a] == k [b];
match (equal, c_k) {
(true, 0) => self.program_counter += 1,
(false, 1) => self.program_counter += 1,
_ => (),
}
},
Instruction::GetTabUp (a, b, c) => {
let a = usize::try_from (*a).unwrap ();
let b = usize::try_from (*b).unwrap ();
let c = usize::try_from (*c).unwrap ();
let env = match upvalues.get (b).unwrap () {
Value::BogusEnv (x) => x,
_ => panic! ("Only allowed upvalue is BogusEnv"),
};
let key = match k.get (c).unwrap () {
Value::String (s) => s,
_ => panic! ("GetTabUp only supports string keys"),
};
let value = env.get (key).unwrap ();
r [a] = value.clone ();
},
Instruction::GetI (a, b, c) => {
let a = usize::try_from (*a).unwrap ();
let b = usize::try_from (*b).unwrap ();
let c = usize::try_from (*c).unwrap ();
let table = r.get (b).unwrap ();
let value = match table {
Value::BogusArg (arg) => arg.get (c).unwrap ().as_str().into (),
_ => unimplemented!(),
};
r [a] = value;
},
Instruction::Jmp (sJ) => self.program_counter += sJ,
Instruction::LoadK (a, bx) => {
let a = usize::try_from (*a).unwrap ();
let bx = usize::try_from (*bx).unwrap ();
r [a] = k [bx].clone ();
},
Instruction::Return (_a, _b, _c) => {
break;
},
Instruction::VarArgPrep (_) => (),
_ => (),
}
self.program_counter += 1;
}
}
}
fn main() {
let arg: Vec <_> = std::env::args ().collect ();
let chunk = Chunk {
instructions: vec! [
Instruction::VarArgPrep (0),
Instruction::GetTabUp (0, 0, 0),
Instruction::GetI (0, 0, 1),
Instruction::EqK (0, 1, 0),
Instruction::Jmp (4),
Instruction::GetTabUp (0, 0, 2),
Instruction::LoadK (1, 3),
Instruction::Call (0, 2, 1),
Instruction::Jmp (3),
Instruction::GetTabUp (0, 0, 2),
Instruction::LoadK (1, 4),
Instruction::Call (0, 2, 1),
Instruction::Return (1, 1, 1),
],
constants: vec! [
"arg",
"93",
"print",
"it's 93",
"it's not 93",
].into_iter ().map (|s| Value::from (s)).collect (),
};
2023-09-24 21:34:28 +00:00
let env = BTreeMap::from_iter ([
("arg", Value::BogusArg (arg.clone ())),
("print", Value::BogusPrint),
].map (|(k, v)| (k.to_string (), v)).into_iter ());
2023-09-24 21:34:28 +00:00
let upvalues = vec! [
Value::BogusEnv (env),
];
2023-09-24 21:34:28 +00:00
let mut vm = VirtualMachine::default ();
vm.execute_chunk (&chunk, &upvalues);
}