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: 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); printf("%s", msg);
} }
void _exit(int code)
{
exit(code);
}
/* END builtins */ /* END builtins */

4
builtin/builtin.js

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

12
lib/stdio.sb

@ -9,6 +9,18 @@ fn println(msg: string) {
print(msg + "\n") 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 // Prints the size of an array
fn len(arr: int[]): int { fn len(arr: int[]): int {
let c: int = 0 let c: int = 0

3
src/command/run.rs

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

1
tests/function_return_type_explicit.sb

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

2
tests/if_statement_basic.sb

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

2
tests/if_statement_non_boolean_condition.sb

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

24
tests/structs.sb

@ -4,14 +4,36 @@ struct User {
last_name: string 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() { fn test_initialization() {
let foo = new User { let foo = new User {
username: "Foo Bar", username: "Foo Bar",
first_name: "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() { fn main() {
test_initialization() test_initialization()
test_simple_field_access()
} }
Loading…
Cancel
Save