From c344ae77666f09729962dd2b8383f7842cee393f Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Sun, 21 Feb 2021 22:21:20 +0100 Subject: [PATCH] feat: append standard library --- src/builder/mod.rs | 18 ++++++++++++++++-- src/command/build.rs | 23 ++--------------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/builder/mod.rs b/src/builder/mod.rs index c2a22ad..871a16c 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -1,7 +1,7 @@ use crate::generator; use crate::lexer; use crate::parser; -use std::io::Write; +use crate::Lib; use crate::PathBuf; use parser::node_type::Module; /** @@ -21,6 +21,7 @@ use parser::node_type::Module; */ use std::fs::File; use std::io::Read; +use std::io::Write; pub struct Builder { in_file: PathBuf, @@ -38,6 +39,8 @@ impl Builder { pub fn build(&mut self) -> Result<(), String> { self.build_module(self.in_file.clone())?; + // Append standard library + self.modules.push(build_stdlib()); Ok(()) } @@ -64,10 +67,21 @@ impl Builder { let mut condensed = mod_iter.next().ok_or("No module specified")?.clone(); while let Some(module) = mod_iter.next() { condensed.merge_with(module.clone()); - }; + } let output = generator::generate(condensed); let mut file = std::fs::File::create(out_file).expect("create failed"); file.write_all(output.as_bytes()).expect("write failed"); Ok(file.flush().expect("Could not flush file")) } } + +fn build_stdlib() -> parser::node_type::Module { + let stdlib_raw = + Lib::get("stdio.sb").expect("Standard library not found. This should not occur."); + let stblib_str = + std::str::from_utf8(&stdlib_raw).expect("Could not interpret standard library."); + let stdlib_tokens = lexer::tokenize(&stblib_str); + + parser::parse(stdlib_tokens, Some(stblib_str.into()), "stdio".to_string()) + .expect("Could not parse stdlib") +} diff --git a/src/command/build.rs b/src/command/build.rs index 85b41e6..1035152 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -13,30 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -use crate::generator; -use crate::lexer; use crate::builder; -use crate::parser; -use crate::Lib; -use std::io::Write; use std::path::Path; pub fn build(in_file: &Path, out_file: &Path) -> Result<(), String> { let mut b = builder::Builder::new(in_file.to_path_buf()); - b.build(); - - b.generate(out_file.to_path_buf())?; - - - Ok(()) -} - -fn build_stdlib() -> parser::node_type::Module { - let stdlib_raw = - Lib::get("stdio.sb").expect("Standard library not found. This should not occur."); - let stblib_str = - std::str::from_utf8(&stdlib_raw).expect("Could not interpret standard library."); - let stdlib_tokens = lexer::tokenize(&stblib_str); - - parser::parse(stdlib_tokens, Some(stblib_str.into()), "stdio".to_string()).expect("Could not parse stdlib") + b.build()?; + b.generate(out_file.to_path_buf()) }