Browse Source

feat: append standard library

modules
Garrit Franke 3 years ago
parent
commit
c344ae7766
  1. 18
      src/builder/mod.rs
  2. 23
      src/command/build.rs

18
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")
}

23
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())
}

Loading…
Cancel
Save