diff --git a/Cargo.toml b/Cargo.toml index 68406ca..57be909 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ edition = "2018" backend_c = [] backend_node = [] -default = ["backend_node"] +default = ["backend_c"] [dependencies] structopt = "0.3.21" diff --git a/builtin/builtin.c b/builtin/builtin.c index 14e60e7..5c9ab03 100644 --- a/builtin/builtin.c +++ b/builtin/builtin.c @@ -1,7 +1,7 @@ /* START builtins */ #include "stdio.h" -void _printf(msg) +void _printf(char *msg) { printf(msg); } diff --git a/examples/playground.sb b/examples/playground.sb index 18ec2f4..584e33e 100644 --- a/examples/playground.sb +++ b/examples/playground.sb @@ -1,2 +1,3 @@ fn main(n: int) { + _printf("Hello World!\n") } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f42c866..9c97f71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,13 +59,17 @@ fn main() -> Result<(), String> { let mut file = File::open(in_file).expect("Could not open file"); let mut contents = String::new(); - contents = append_stdlib(contents); - file.read_to_string(&mut contents) .expect("Could not read file"); - let tokens = lexer::tokenize(&contents); - let program = parser::parse(tokens, Some(contents))?; + let mut program = parser::parse(tokens, Some(contents))?; + + // C Backend currently does not support stdlib yet, since not all features are implemented + #[cfg(features = "backend_node")] + { + let stdlib = build_stdlib(); + program.merge_with(stdlib); + } let output = generator::generate(program); let mut file = std::fs::File::create(out_file).expect("create failed"); @@ -74,13 +78,12 @@ fn main() -> Result<(), String> { Ok(()) } -fn append_stdlib(mut buffer: String) -> String { - buffer += "// START stdlib\n\n"; - - let stdlib = Lib::get("stdio.sb").expect("Standard library not found. This should not occur."); - buffer += std::str::from_utf8(&stdlib).expect("Could not interpret standard library."); - - buffer += "\n// END stdlib\n\n"; +fn build_stdlib() -> parser::node_type::Program { + 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); - buffer + parser::parse(stdlib_tokens, Some(stblib_str.into())).expect("Could not parse stdlib") } diff --git a/src/parser/node_type.rs b/src/parser/node_type.rs index 8f81c1a..4548ad2 100644 --- a/src/parser/node_type.rs +++ b/src/parser/node_type.rs @@ -22,6 +22,13 @@ pub struct Program { pub globals: Vec, } +impl Program { + pub fn merge_with(&mut self, mut other: Program) { + self.func.append(&mut other.func); + self.globals.append(&mut other.globals) + } +} + #[derive(Debug)] pub struct Function { pub name: String,