Browse Source

feat: assert function

structs
Garrit Franke 3 years ago
parent
commit
a973ede75b
  1. 3
      builtin/README.md
  2. 5
      builtin/builtin.c
  3. 4
      builtin/builtin.js
  4. 12
      lib/stdio.sb
  5. 3
      src/command/run.rs
  6. 1
      tests/function_return_type_explicit.sb
  7. 2
      tests/if_statement_basic.sb
  8. 2
      tests/if_statement_non_boolean_condition.sb
  9. 24
      tests/structs.sb

3
builtin/README.md

@ -1,3 +1,4 @@
The following function signatures need to be implemented by each backend:
\_printf(msg String)
`\_printf(msg: string)`
`\_exit(code: int)`

5
builtin/builtin.c

@ -7,4 +7,9 @@ void _printf(char *msg)
printf("%s", msg);
}
void _exit(int code)
{
exit(code);
}
/* END builtins */

4
builtin/builtin.js

@ -5,4 +5,8 @@ function _printf(msg) {
process.stdout.write(msg.toString());
}
function _exit(code) {
process.exit(code);
}
/* END builtins */

12
lib/stdio.sb

@ -9,6 +9,18 @@ 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

3
src/command/run.rs

@ -1,6 +1,7 @@
use crate::command::build;
use std::io::Write;
use std::path::PathBuf;
use std::process;
use std::process::Command;
use std::process::Stdio;
use tempfile::tempdir;
@ -51,6 +52,8 @@ pub fn run(in_file: PathBuf) -> Result<(), String> {
std::io::stderr()
.write_all(&out.stderr)
.expect("Could not write to stderr");
process::exit(out.status.code().unwrap())
}
Ok(())
}

1
tests/function_return_type_explicit.sb

@ -1,5 +1,6 @@
fn main() {
let x = add_one(2)
assert(x == 3)
println(x)
}

2
tests/if_statement_basic.sb

@ -5,5 +5,7 @@ fn main() {
println("condition was true")
} else {
println("condition was false")
// Should not be true
}
}

2
tests/if_statement_non_boolean_condition.sb

@ -3,5 +3,7 @@ fn main() {
if number {
println("number was three")
} else {
assert(false)
}
}

24
tests/structs.sb

@ -4,14 +4,36 @@ struct User {
last_name: string
}
// Creates a stub user
fn user_stub() {
let stub = new User {
username: "Foo Bar",
first_name: "Foo",
last_name: "Bar"
}
assert(stub.first_name)
assert(stub.last_name)
return stub
}
fn test_initialization() {
let foo = new User {
username: "Foo Bar",
first_name: "Bar",
last_name: "Foo Bar"
last_name: "Bar"
}
assert(foo)
}
fn test_simple_field_access() {
let user: User = user_stub()
// user.username = "Foo Bar"
// assert(user.username == "Foo Bar")
}
fn main() {
test_initialization()
test_simple_field_access()
}
Loading…
Cancel
Save