Browse Source

chore: refactor stdlib

modules
Garrit Franke 3 years ago
parent
commit
74f79be01b
  1. 27
      lib/array.sb
  2. 6
      lib/assert.sb
  3. 10
      lib/io.sb
  4. 4
      lib/os.sb
  5. 50
      lib/stdio.sb
  6. 4
      lib/stdlib.sb
  7. 24
      src/builder/mod.rs

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

6
lib/assert.sb

@ -0,0 +1,6 @@
fn assert(condition: bool) {
if condition == false {
println("Assertion failed")
exit(1)
}
}

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

4
lib/os.sb

@ -0,0 +1,4 @@
// Exit the program immediately
fn exit(code: int) {
_exit(code)
}

50
lib/stdio.sb

@ -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
}

4
lib/stdlib.sb

@ -0,0 +1,4 @@
import "array"
import "assert"
import "io"
import "os"

24
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);
}
}
}

Loading…
Cancel
Save