forked from maplelang/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std: Add type generic memory allocation functions
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2022-2023, Mate Kukri | ||
* SPDX-License-Identifier: GPL-2.0-only | ||
* | ||
* Description: Maybe monad | ||
*/ | ||
|
||
import prog | ||
|
||
enum Option<T>( | ||
Some (val: T), | ||
None | ||
) | ||
|
||
function some<T>(val: T) -> Option<T> { | ||
Option::Some(val) | ||
} | ||
|
||
function none<T>() -> Option<T> { | ||
Option::None | ||
} | ||
|
||
function unwrap<T>(o: Option<T>) -> T { | ||
match o { | ||
s: Some => s.val, | ||
None => prog::panic(c"Tried to unwrap None\n") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2022-2023, Mate Kukri | ||
* SPDX-License-Identifier: GPL-2.0-only | ||
* | ||
* Description: Program utilities | ||
*/ | ||
|
||
import libc | ||
|
||
// Exit the program with exit code `code` | ||
function exit<T>(code: Int32) -> T { | ||
libc::exit(code); | ||
exit(code) | ||
} | ||
|
||
// Panic the program with message `msg` | ||
function panic<T>(msg: *Int8) -> T { | ||
libc::fprintf(libc::stderr, c"%s", msg); | ||
libc::abort(); | ||
panic(msg) | ||
} |