main now runs a luac file directly. No more hard-coded instructions.

main
_ 2023-09-24 22:14:08 -05:00
parent c718ba67cf
commit 44d065d6dd
4 changed files with 15 additions and 53 deletions

View File

@ -2,6 +2,7 @@ use std::io::Read;
use crate::state::{
Block,
Chunk,
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.
// 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);
}
Some (File {
Some (Chunk {
file_name,
blocks,
})

View File

@ -5,52 +5,12 @@ mod state;
mod tests;
fn main() {
use state::{
Block,
Chunk,
Instruction as Inst,
State,
};
use state::State;
let chunk = Chunk {
blocks: vec! [
Block {
instructions: vec! [
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 lua_file = {
let data = std::fs::read ("test_vectors/closure.luac").unwrap ();
let mut rdr = std::io::Cursor::new (data);
loader::parse_chunk (&mut rdr).unwrap ()
};
let mut vm = State::default ();
@ -59,5 +19,5 @@ fn main() {
}
let upvalues = State::upvalues_from_args (std::env::args ());
println! ("Returned: {:?}", vm.execute_chunk (&chunk, &upvalues));
println! ("Returned: {:?}", vm.execute_chunk (&lua_file, &upvalues));
}

View File

@ -127,6 +127,7 @@ pub struct Block {
}
pub struct Chunk {
pub file_name: String,
pub blocks: Vec <Block>,
}

View File

@ -23,6 +23,7 @@ fn bools () {
*/
let chunk = Chunk {
file_name: "".to_string (),
blocks: vec! [
Block {
instructions: vec! [
@ -116,6 +117,7 @@ fn floats () {
upvalue_count: 1,
};
let chunk = Chunk {
file_name: "".to_string (),
blocks: vec! [block],
};
@ -138,6 +140,7 @@ fn fma () {
*/
let chunk = Chunk {
file_name: "".to_string (),
blocks: vec! [
Block {
instructions: vec! [
@ -254,6 +257,7 @@ fn is_93 () {
};
let chunk = Chunk {
blocks: vec! [block],
file_name: "".to_string (),
};
for (arg, expected) in [
@ -273,7 +277,7 @@ fn is_93 () {
fn loader () {
let bytecode = include_bytes! ("../test_vectors/closure.luac");
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.blocks.len (), 3);