Browse Source

Compile stdlib seperately

github-actions
Garrit Franke 3 years ago
parent
commit
b34944e257
  1. 2
      Cargo.toml
  2. 2
      builtin/builtin.c
  3. 1
      examples/playground.sb
  4. 27
      src/main.rs
  5. 7
      src/parser/node_type.rs

2
Cargo.toml

@ -10,7 +10,7 @@ edition = "2018"
backend_c = []
backend_node = []
default = ["backend_node"]
default = ["backend_c"]
[dependencies]
structopt = "0.3.21"

2
builtin/builtin.c

@ -1,7 +1,7 @@
/* START builtins */
#include "stdio.h"
void _printf(msg)
void _printf(char *msg)
{
printf(msg);
}

1
examples/playground.sb

@ -1,2 +1,3 @@
fn main(n: int) {
_printf("Hello World!\n")
}

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

7
src/parser/node_type.rs

@ -22,6 +22,13 @@ pub struct Program {
pub globals: Vec<String>,
}
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,

Loading…
Cancel
Save