⭐ main now runs a luac file directly. No more hard-coded instructions.
parent
c718ba67cf
commit
44d065d6dd
|
@ -2,6 +2,7 @@ use std::io::Read;
|
||||||
|
|
||||||
use crate::state::{
|
use crate::state::{
|
||||||
Block,
|
Block,
|
||||||
|
Chunk,
|
||||||
Instruction as Inst,
|
Instruction as Inst,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -132,12 +133,8 @@ pub fn parse_block <R: Read> (rdr: &mut R) -> Option <Block>
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct File {
|
|
||||||
pub file_name: String,
|
|
||||||
pub blocks: Vec <Block>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_file <R: Read> (rdr: &mut R) -> Option <File> {
|
pub fn parse_chunk <R: Read> (rdr: &mut R) -> Option <Chunk> {
|
||||||
// Discard 32 bytes from the start of the file.
|
// Discard 32 bytes from the start of the file.
|
||||||
// This is magic number, version number, etc.
|
// This is magic number, version number, etc.
|
||||||
|
|
||||||
|
@ -152,7 +149,7 @@ pub fn parse_file <R: Read> (rdr: &mut R) -> Option <File> {
|
||||||
blocks.push (block);
|
blocks.push (block);
|
||||||
}
|
}
|
||||||
|
|
||||||
Some (File {
|
Some (Chunk {
|
||||||
file_name,
|
file_name,
|
||||||
blocks,
|
blocks,
|
||||||
})
|
})
|
||||||
|
|
52
src/main.rs
52
src/main.rs
|
@ -5,52 +5,12 @@ mod state;
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use state::{
|
use state::State;
|
||||||
Block,
|
|
||||||
Chunk,
|
|
||||||
Instruction as Inst,
|
|
||||||
State,
|
|
||||||
};
|
|
||||||
|
|
||||||
let chunk = Chunk {
|
let lua_file = {
|
||||||
blocks: vec! [
|
let data = std::fs::read ("test_vectors/closure.luac").unwrap ();
|
||||||
Block {
|
let mut rdr = std::io::Cursor::new (data);
|
||||||
instructions: vec! [
|
loader::parse_chunk (&mut rdr).unwrap ()
|
||||||
Inst::VarArgPrep (0),
|
|
||||||
Inst::Closure (0, 0),
|
|
||||||
Inst::Move (1, 0),
|
|
||||||
Inst::Call (1, 1, 2),
|
|
||||||
Inst::GetTabUp (2, 0, 0),
|
|
||||||
Inst::Move (3, 1),
|
|
||||||
Inst::Call (3, 1, 0),
|
|
||||||
Inst::Call (2, 0, 1),
|
|
||||||
Inst::Return (2, 1, 1, false),
|
|
||||||
],
|
|
||||||
constants: vec! [
|
|
||||||
"print".into (),
|
|
||||||
],
|
|
||||||
upvalue_count: 1,
|
|
||||||
},
|
|
||||||
Block {
|
|
||||||
instructions: vec! [
|
|
||||||
Inst::LoadI (0, 5),
|
|
||||||
Inst::Closure (1, 0),
|
|
||||||
Inst::Return (1, 2, 0, true), // k?
|
|
||||||
Inst::Return (1, 1, 0, true), // k?
|
|
||||||
],
|
|
||||||
constants: vec! [],
|
|
||||||
upvalue_count: 0,
|
|
||||||
},
|
|
||||||
Block {
|
|
||||||
instructions: vec! [
|
|
||||||
Inst::GetUpVal (0, 0),
|
|
||||||
Inst::Return1 (0),
|
|
||||||
Inst::Return0,
|
|
||||||
],
|
|
||||||
constants: vec! [],
|
|
||||||
upvalue_count: 1,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut vm = State::default ();
|
let mut vm = State::default ();
|
||||||
|
@ -59,5 +19,5 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let upvalues = State::upvalues_from_args (std::env::args ());
|
let upvalues = State::upvalues_from_args (std::env::args ());
|
||||||
println! ("Returned: {:?}", vm.execute_chunk (&chunk, &upvalues));
|
println! ("Returned: {:?}", vm.execute_chunk (&lua_file, &upvalues));
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,6 +127,7 @@ pub struct Block {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Chunk {
|
pub struct Chunk {
|
||||||
|
pub file_name: String,
|
||||||
pub blocks: Vec <Block>,
|
pub blocks: Vec <Block>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@ fn bools () {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let chunk = Chunk {
|
let chunk = Chunk {
|
||||||
|
file_name: "".to_string (),
|
||||||
blocks: vec! [
|
blocks: vec! [
|
||||||
Block {
|
Block {
|
||||||
instructions: vec! [
|
instructions: vec! [
|
||||||
|
@ -116,6 +117,7 @@ fn floats () {
|
||||||
upvalue_count: 1,
|
upvalue_count: 1,
|
||||||
};
|
};
|
||||||
let chunk = Chunk {
|
let chunk = Chunk {
|
||||||
|
file_name: "".to_string (),
|
||||||
blocks: vec! [block],
|
blocks: vec! [block],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -138,6 +140,7 @@ fn fma () {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let chunk = Chunk {
|
let chunk = Chunk {
|
||||||
|
file_name: "".to_string (),
|
||||||
blocks: vec! [
|
blocks: vec! [
|
||||||
Block {
|
Block {
|
||||||
instructions: vec! [
|
instructions: vec! [
|
||||||
|
@ -254,6 +257,7 @@ fn is_93 () {
|
||||||
};
|
};
|
||||||
let chunk = Chunk {
|
let chunk = Chunk {
|
||||||
blocks: vec! [block],
|
blocks: vec! [block],
|
||||||
|
file_name: "".to_string (),
|
||||||
};
|
};
|
||||||
|
|
||||||
for (arg, expected) in [
|
for (arg, expected) in [
|
||||||
|
@ -273,7 +277,7 @@ fn is_93 () {
|
||||||
fn loader () {
|
fn loader () {
|
||||||
let bytecode = include_bytes! ("../test_vectors/closure.luac");
|
let bytecode = include_bytes! ("../test_vectors/closure.luac");
|
||||||
let mut rdr = std::io::Cursor::new (bytecode);
|
let mut rdr = std::io::Cursor::new (bytecode);
|
||||||
let file = crate::loader::parse_file (&mut rdr).unwrap ();
|
let file = crate::loader::parse_chunk (&mut rdr).unwrap ();
|
||||||
|
|
||||||
assert_eq! (file.file_name, "@test_vectors/closure.lua");
|
assert_eq! (file.file_name, "@test_vectors/closure.lua");
|
||||||
assert_eq! (file.blocks.len (), 3);
|
assert_eq! (file.blocks.len (), 3);
|
||||||
|
|
Loading…
Reference in New Issue