Corrected default alignment on temp alloc. Added str_index_of. Added simple getline. Added a simple calculator. Allow [1..] to create a zero length slice. Added some initial macro contracts. Fix accessing enum functions. Support for @checked. Bump to 0.3.4

This commit is contained in:
Christoffer Lerno
2022-08-05 00:42:22 +02:00
parent 046469843c
commit 398e19d727
15 changed files with 129 additions and 35 deletions

View File

@@ -0,0 +1,41 @@
module test;
import std::io;
import libc;
fault TokenResult
{
NO_MORE_TOKENS
}
fn char[]! read_next(char[]* remaining)
{
// Skip spaces.
while (remaining.len > 0 && (*remaining)[0] == ' ') *remaining = (*remaining)[1..];
char* ptr_start = remaining.ptr;
usize len = 0;
while (remaining.len > 0 && (*remaining)[0] != ' ')
{
len++;
*remaining = (*remaining)[1..];
}
if (!len) return TokenResult.NO_MORE_TOKENS!;
return ptr_start[:len];
}
fn void main(char[][] args)
{
String s = io::stdin().getline();
defer s.destroy();
char[] numbers = s.str();
int val = 0;
bool add = true;
while (try char[] token = read_next(&numbers))
{
int i = libc::atoi(token.ptr);
val = add ? val + i : val - i;
char[]! op = read_next(&numbers);
if (catch op) break;
add = op[0] == '+';
}
io::printfln("%d", val);
}