From a973ede75b45340a3e1861bcc0cc163b21c13ec4 Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Mon, 15 Feb 2021 23:28:45 +0100 Subject: [PATCH] feat: assert function --- builtin/README.md | 3 ++- builtin/builtin.c | 5 +++++ builtin/builtin.js | 4 ++++ lib/stdio.sb | 12 +++++++++++ src/command/run.rs | 3 +++ tests/function_return_type_explicit.sb | 1 + tests/if_statement_basic.sb | 2 ++ tests/if_statement_non_boolean_condition.sb | 2 ++ tests/structs.sb | 24 ++++++++++++++++++++- 9 files changed, 54 insertions(+), 2 deletions(-) diff --git a/builtin/README.md b/builtin/README.md index 72746e7..6da9e82 100644 --- a/builtin/README.md +++ b/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)` diff --git a/builtin/builtin.c b/builtin/builtin.c index bd882e5..49f8ac7 100644 --- a/builtin/builtin.c +++ b/builtin/builtin.c @@ -7,4 +7,9 @@ void _printf(char *msg) printf("%s", msg); } +void _exit(int code) +{ + exit(code); +} + /* END builtins */ diff --git a/builtin/builtin.js b/builtin/builtin.js index bf1145c..48938db 100644 --- a/builtin/builtin.js +++ b/builtin/builtin.js @@ -5,4 +5,8 @@ function _printf(msg) { process.stdout.write(msg.toString()); } +function _exit(code) { + process.exit(code); +} + /* END builtins */ diff --git a/lib/stdio.sb b/lib/stdio.sb index e5cb55f..ce40fc6 100644 --- a/lib/stdio.sb +++ b/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 diff --git a/src/command/run.rs b/src/command/run.rs index 2bc726c..757a2cd 100644 --- a/src/command/run.rs +++ b/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(()) } diff --git a/tests/function_return_type_explicit.sb b/tests/function_return_type_explicit.sb index 7d6dc27..88a9b1e 100644 --- a/tests/function_return_type_explicit.sb +++ b/tests/function_return_type_explicit.sb @@ -1,5 +1,6 @@ fn main() { let x = add_one(2) + assert(x == 3) println(x) } diff --git a/tests/if_statement_basic.sb b/tests/if_statement_basic.sb index 6569033..2fb39c7 100644 --- a/tests/if_statement_basic.sb +++ b/tests/if_statement_basic.sb @@ -5,5 +5,7 @@ fn main() { println("condition was true") } else { println("condition was false") + // Should not be true + } } \ No newline at end of file diff --git a/tests/if_statement_non_boolean_condition.sb b/tests/if_statement_non_boolean_condition.sb index 8fbd737..92e6cd0 100644 --- a/tests/if_statement_non_boolean_condition.sb +++ b/tests/if_statement_non_boolean_condition.sb @@ -3,5 +3,7 @@ fn main() { if number { println("number was three") + } else { + assert(false) } } \ No newline at end of file diff --git a/tests/structs.sb b/tests/structs.sb index ae4469a..389f873 100644 --- a/tests/structs.sb +++ b/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() } \ No newline at end of file