From 1f2096ff9d379beb48e57db63a1fc3db9a112fd9 Mon Sep 17 00:00:00 2001 From: Empa Date: Wed, 1 May 2024 14:50:08 +0200 Subject: [PATCH] Add native cleaner --- Cargo.toml | 13 +++++++------ crates/saft-cli/src/main.rs | 2 +- crates/saft/src/lib.rs | 21 ++++++++++++--------- tests/Cargo.toml | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2202b47..039927a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,13 @@ [workspace] resolver = "2" members = [ - "crates/saft", - "crates/saft-bytecode", "crates/saft-cli", - "crates/saft-ir", - "crates/saft-macro", - "crates/saft-syntax", - "tests", + "crates/saft", + "crates/saft-bytecode", + "crates/saft-cli", + "crates/saft-ir", + "crates/saft-macro", + "crates/saft-syntax", + "tests", ] default-members = ["crates/saft-cli"] diff --git a/crates/saft-cli/src/main.rs b/crates/saft-cli/src/main.rs index 1655a6b..1d17237 100644 --- a/crates/saft-cli/src/main.rs +++ b/crates/saft-cli/src/main.rs @@ -18,7 +18,7 @@ struct Args { fn main() { let args = Args::parse(); - let mut saft = Saft::new(); + let mut saft = Saft::new_with_std(); match args.script { Some(path) => { diff --git a/crates/saft/src/lib.rs b/crates/saft/src/lib.rs index 3952835..013d09a 100644 --- a/crates/saft/src/lib.rs +++ b/crates/saft/src/lib.rs @@ -23,16 +23,8 @@ pub struct Saft { #[allow(clippy::new_without_default)] impl Saft { pub fn new() -> Self { - let mut lowerer = Lowerer::new(); - - let mut add_native = |native: bytecode::value::NativeFunction| { - lowerer.add_item(native.name.to_string(), ir::Item::Builtin(native)); - }; - - add_native(natives::print); - Self { - lowerer, + lowerer: Lowerer::new(), compiler: bytecode::compiler::Compiler::new(), vm: Vm::new(), diagnostic_writer: codespan_reporting::term::termcolor::StandardStream::stdout( @@ -42,6 +34,17 @@ impl Saft { } } + pub fn new_with_std() -> Self { + let mut saft = Self::new(); + saft.add_native(saft_bytecode::natives::print); + saft + } + + fn add_native(&mut self, native: saft_bytecode::value::NativeFunction) { + self.lowerer + .add_item(native.name.to_string(), ir::Item::Builtin(native)); + } + fn try_parse(&mut self, fname: &str, s: &str) -> Option { let mut files = SimpleFiles::new(); let id = files.add(fname, s); diff --git a/tests/Cargo.toml b/tests/Cargo.toml index c381258..fd277e0 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -7,6 +7,6 @@ build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -assert_cmd = { version = "2.0.12", features = [ "color-auto" ] } +assert_cmd = { version = "2.0.12", features = ["color-auto"] } pretty_assertions = "1.4.0" saft-macro = { version = "0.1.0", path = "../crates/saft-macro" }