♻️ refactor: split up the VM from the CLI program

main
_ 2023-10-01 17:56:06 -05:00
parent 51b04be1ab
commit ffb1950f80
35 changed files with 63 additions and 92 deletions

54
Cargo.lock generated
View File

@ -3,60 +3,12 @@
version = 3 version = 3
[[package]] [[package]]
name = "arrayref" name = "lunar_wave_cli"
version = "0.3.7" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545"
[[package]]
name = "arrayvec"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]]
name = "blake3"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0231f06152bf547e9c2b5194f247cd97aacf6dcd8b15d8e5ec0663f64580da87"
dependencies = [ dependencies = [
"arrayref", "lunar_wave_vm",
"arrayvec",
"cc",
"cfg-if",
"constant_time_eq",
] ]
[[package]]
name = "cc"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"libc",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "constant_time_eq"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2"
[[package]]
name = "libc"
version = "0.2.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
[[package]] [[package]]
name = "lunar_wave_vm" name = "lunar_wave_vm"
version = "0.1.0" version = "0.1.0"
dependencies = [
"blake3",
]

View File

@ -1,21 +1,5 @@
[package] [workspace]
name = "lunar_wave_vm" members = [
description = "A Lua virtual machine implementation" "lunar_wave_cli",
version = "0.1.0" "lunar_wave_vm",
edition = "2021" ]
[dependencies]
[dev-dependencies]
# blake3, used to hash test vectors
blake3 = "1.5.0"
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
# Recommended for flamegraph
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Clink-arg=-Wl,--no-rosegment"]
[profile.release]
# Recommended for profiling, e.g. flamegraph
debug = true

5
examples/embedding.rs Normal file
View File

@ -0,0 +1,5 @@
fn main () -> Result <(), ()> {
println! ("Embedding");
Ok (())
}

18
lunar_wave_cli/Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "lunar_wave_cli"
description = "A Lua CLI implementation"
version = "0.1.0"
edition = "2021"
author = "ReactorScram"
[dependencies]
lunar_wave_vm = { path = "../lunar_wave_vm" }
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
# Recommended for flamegraph
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Clink-arg=-Wl,--no-rosegment"]
[profile.release]
# Recommended for profiling, e.g. flamegraph
debug = true

View File

@ -1,16 +1,8 @@
// cargo run -- --script test_vectors/fizz_buzz.lua // cargo run -- --script lunar_wave_vm/test_vectors/fizz_buzz.lua
mod instruction; use lunar_wave_vm as lwvm;
mod loader;
mod state;
mod value;
#[cfg (test)]
mod tests;
fn main () -> Result <(), state::StepError> {
use state::State;
fn main () -> Result <(), lwvm::StepError> {
let mut list_bytecode = false; let mut list_bytecode = false;
let mut pipe_bytecode = false; let mut pipe_bytecode = false;
let mut script = None; let mut script = None;
@ -27,7 +19,7 @@ fn main () -> Result <(), state::StepError> {
let block_idx = str::parse (block_idx).unwrap (); let block_idx = str::parse (block_idx).unwrap ();
let program_counter = str::parse (program_counter).unwrap (); let program_counter = str::parse (program_counter).unwrap ();
breakpoints.push (state::Breakpoint { breakpoints.push (lwvm::Breakpoint {
block_idx, block_idx,
program_counter, program_counter,
}); });
@ -41,13 +33,13 @@ fn main () -> Result <(), state::StepError> {
} }
let chunk = if let Some (script) = script { let chunk = if let Some (script) = script {
let bytecode = loader::compile_bytecode_from_file (&script); let bytecode = lwvm::compile_bytecode_from_file (&script);
let mut rdr = std::io::Cursor::new (bytecode); let mut rdr = std::io::Cursor::new (bytecode);
loader::parse_chunk (&mut rdr).unwrap () lwvm::parse_chunk (&mut rdr).unwrap ()
} }
else if pipe_bytecode { else if pipe_bytecode {
let mut stdin = std::io::stdin ().lock (); let mut stdin = std::io::stdin ().lock ();
loader::parse_chunk (&mut stdin).unwrap () lwvm::parse_chunk (&mut stdin).unwrap ()
} }
else { else {
unimplemented!(); unimplemented!();
@ -57,9 +49,9 @@ fn main () -> Result <(), state::StepError> {
dbg! (&chunk); dbg! (&chunk);
} }
let upvalues = State::upvalues_from_args ([exe_name].into_iter ().chain (args)); let upvalues = lwvm::State::upvalues_from_args ([exe_name].into_iter ().chain (args));
let mut vm = State::new (&chunk, &upvalues); let mut vm = lwvm::State::new (&chunk, &upvalues);
if std::env::var("LWVM_DEBUG").is_ok() { if std::env::var("LWVM_DEBUG").is_ok() {
vm.debug_print = true; vm.debug_print = true;
} }
@ -93,7 +85,7 @@ fn main () -> Result <(), state::StepError> {
"s" => { "s" => {
match vm.step ()? { match vm.step ()? {
None => (), None => (),
Some (state::StepOutput::ChunkReturned (x)) => { Some (lwvm::StepOutput::ChunkReturned (x)) => {
dbg! (x); dbg! (x);
return Ok (()); return Ok (());
}, },
@ -106,7 +98,7 @@ fn main () -> Result <(), state::StepError> {
match vm.step ()? { match vm.step ()? {
None => (), None => (),
Some (state::StepOutput::ChunkReturned (x)) => { Some (lwvm::StepOutput::ChunkReturned (x)) => {
dbg! (x); dbg! (x);
return Ok (()); return Ok (());
}, },

6
lunar_wave_vm/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "lunar_wave_vm"
description = "A Lua virtual machine implementation"
version = "0.1.0"
edition = "2021"
author = "ReactorScram"

14
lunar_wave_vm/src/lib.rs Normal file
View File

@ -0,0 +1,14 @@
mod instruction;
mod loader;
mod state;
mod value;
pub use loader::compile_bytecode_from_file as compile_bytecode_from_file;
pub use loader::parse_chunk as parse_chunk;
pub use state::Breakpoint as Breakpoint;
pub use state::State as State;
pub use state::StepError as StepError;
pub use state::StepOutput as StepOutput;
#[cfg (test)]
mod tests;

View File

@ -8,7 +8,7 @@ use crate::{
} }
}; };
pub (crate) fn compile_bytecode_from_file (path: &str) -> Vec <u8> { pub fn compile_bytecode_from_file (path: &str) -> Vec <u8> {
use std::{ use std::{
process::{ process::{
Command, Command,