From 44d065d6ddb1c61f5e8e82b0460e9e9cc620a908 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sun, 24 Sep 2023 22:14:08 -0500 Subject: [PATCH] :star: main now runs a luac file directly. No more hard-coded instructions. --- src/loader.rs | 9 +++------ src/main.rs | 52 ++++++--------------------------------------------- src/state.rs | 1 + src/tests.rs | 6 +++++- 4 files changed, 15 insertions(+), 53 deletions(-) diff --git a/src/loader.rs b/src/loader.rs index 8703a67..0a0aa67 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -2,6 +2,7 @@ use std::io::Read; use crate::state::{ Block, + Chunk, Instruction as Inst, }; @@ -132,12 +133,8 @@ pub fn parse_block (rdr: &mut R) -> Option }) } -pub struct File { - pub file_name: String, - pub blocks: Vec , -} -pub fn parse_file (rdr: &mut R) -> Option { +pub fn parse_chunk (rdr: &mut R) -> Option { // Discard 32 bytes from the start of the file. // This is magic number, version number, etc. @@ -152,7 +149,7 @@ pub fn parse_file (rdr: &mut R) -> Option { blocks.push (block); } - Some (File { + Some (Chunk { file_name, blocks, }) diff --git a/src/main.rs b/src/main.rs index ccf896e..d18e795 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)); } diff --git a/src/state.rs b/src/state.rs index 590c420..7ff23f0 100644 --- a/src/state.rs +++ b/src/state.rs @@ -127,6 +127,7 @@ pub struct Block { } pub struct Chunk { + pub file_name: String, pub blocks: Vec , } diff --git a/src/tests.rs b/src/tests.rs index a27ec15..76db919 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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);