diff --git a/lib/array.sb b/lib/array.sb new file mode 100644 index 0000000..9a7e76d --- /dev/null +++ b/lib/array.sb @@ -0,0 +1,27 @@ +// Prints the size of an array +fn len(arr: int[]): int { + let c: int = 0 + while arr[c] { + c += 1 + } + + return c +} + +// Reverses an array +// TODO: fix me! +fn rev(arr: int[]): int[] { + + let l: int = len(arr) + let new_arr: int[] = [] + + let i: int = 0 + let j: int = l + while i < l { + new_arr[i] = arr[j] + i = i - 1 + j = j - 1 + } + + return new_arr +} \ No newline at end of file diff --git a/lib/assert.sb b/lib/assert.sb new file mode 100644 index 0000000..6a60191 --- /dev/null +++ b/lib/assert.sb @@ -0,0 +1,6 @@ +fn assert(condition: bool) { + if condition == false { + println("Assertion failed") + exit(1) + } +} \ No newline at end of file diff --git a/lib/io.sb b/lib/io.sb new file mode 100644 index 0000000..db32fea --- /dev/null +++ b/lib/io.sb @@ -0,0 +1,10 @@ +// Raw wrapper around _printf builtin function. +// Writes the given content to stdout +fn print(arg: string) { + _printf(arg) +} + +// Like print(), but with an extra newline ('\n') character +fn println(msg: string) { + print(msg + "\n") +} diff --git a/lib/os.sb b/lib/os.sb new file mode 100644 index 0000000..ba07443 --- /dev/null +++ b/lib/os.sb @@ -0,0 +1,4 @@ +// Exit the program immediately +fn exit(code: int) { + _exit(code) +} \ No newline at end of file diff --git a/lib/stdio.sb b/lib/stdio.sb deleted file mode 100644 index ce40fc6..0000000 --- a/lib/stdio.sb +++ /dev/null @@ -1,50 +0,0 @@ -// Raw wrapper around _printf builtin function. -// Writes the given content to stdout -fn print(arg: string) { - _printf(arg) -} - -// Like print(), but with an extra newline ('\n') character -fn println(msg: string) { - print(msg + "\n") -} - -// Exit the program immediately -fn exit(code: int) { - _exit(code) -} - -fn assert(condition: bool) { - if condition == false { - println("Assertion failed") - exit(1) - } -} - -// Prints the size of an array -fn len(arr: int[]): int { - let c: int = 0 - while arr[c] { - c += 1 - } - - return c -} - -// Reverses an array -// TODO: fix me! -fn rev(arr: int[]): int[] { - - let l: int = len(arr) - let new_arr: int[] = [] - - let i: int = 0 - let j: int = l - while i < l { - new_arr[i] = arr[j] - i = i - 1 - j = j - 1 - } - - return new_arr -} \ No newline at end of file diff --git a/lib/stdlib.sb b/lib/stdlib.sb new file mode 100644 index 0000000..7a60eea --- /dev/null +++ b/lib/stdlib.sb @@ -0,0 +1,4 @@ +import "array" +import "assert" +import "io" +import "os" \ No newline at end of file diff --git a/src/builder/mod.rs b/src/builder/mod.rs index 17fbe35..53f75d5 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -50,7 +50,7 @@ impl Builder { self.build_module(self.in_file.clone())?; // Append standard library - self.modules.push(build_stdlib()); + self.build_stdlib(); Ok(()) } @@ -83,15 +83,19 @@ impl Builder { file.write_all(output.as_bytes()).expect("write failed"); file.flush().map_err(|_| "Could not flush file".into()) } -} -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); + fn build_stdlib(&mut self) { + let assets = Lib::iter(); - parser::parse(stdlib_tokens, Some(stblib_str.into()), "stdio".to_string()) - .expect("Could not parse stdlib") + for file in assets { + let stdlib_raw = + Lib::get(&file).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); + let module = parser::parse(stdlib_tokens, Some(stblib_str.into()), file.to_string()) + .expect("Could not parse stdlib"); + self.modules.push(module); + } + } }