update std lib to use error type and global variables
This commit is contained in:
@@ -3,45 +3,33 @@ export executable "guess_number";
|
||||
import "std.zig";
|
||||
import "rand.zig";
|
||||
|
||||
error GetRandomFail;
|
||||
error ReadInputFail;
|
||||
|
||||
pub fn main(args: [][]u8) %void => {
|
||||
print_str("Welcome to the Guess Number Game in Zig.\n");
|
||||
stderr.print_str("Welcome to the Guess Number Game in Zig.\n");
|
||||
|
||||
var seed : u32;
|
||||
const seed_bytes = (&u8)(&seed)[0...@sizeof(u32)];
|
||||
const err = os_get_random_bytes(seed_bytes);
|
||||
if (err != @sizeof(u32)) {
|
||||
// TODO full error message
|
||||
fprint_str(stderr_fileno, "unable to get random bytes\n");
|
||||
return error.GetRandomFail;
|
||||
}
|
||||
const seed_bytes = (&u8)(&seed)[0...4];
|
||||
os_get_random_bytes(seed_bytes);
|
||||
|
||||
var rand = rand_new(seed);
|
||||
|
||||
const answer = rand.range_u64(0, 100) + 1;
|
||||
|
||||
while (true) {
|
||||
print_str("\nGuess a number between 1 and 100: ");
|
||||
stderr.print_str("\nGuess a number between 1 and 100: ");
|
||||
var line_buf : [20]u8;
|
||||
var line_len : isize;
|
||||
// TODO fix this awkward error handling
|
||||
if (readline(line_buf, &line_len) || line_len == line_buf.len) {
|
||||
// TODO full error message
|
||||
fprint_str(stderr_fileno, "unable to read input\n");
|
||||
return error.ReadInputFail;
|
||||
}
|
||||
|
||||
// TODO print error message instead of returning
|
||||
const line_len = %return stdin.readline(line_buf);
|
||||
|
||||
var guess : u64;
|
||||
if (parse_u64(line_buf[0...line_len - 1], 10, &guess)) {
|
||||
print_str("Invalid number format.\n");
|
||||
stderr.print_str("Invalid number format.\n");
|
||||
} else if (guess > answer) {
|
||||
print_str("Guess lower.\n");
|
||||
stderr.print_str("Guess lower.\n");
|
||||
} else if (guess < answer) {
|
||||
print_str("Guess higher.\n");
|
||||
stderr.print_str("Guess higher.\n");
|
||||
} else {
|
||||
print_str("You win!\n");
|
||||
stderr.print_str("You win!\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,5 @@ export executable "hello";
|
||||
import "std.zig";
|
||||
|
||||
pub fn main(args: [][]u8) %void => {
|
||||
//stderr.print_str("Hello, world!\n");
|
||||
print_str("Hello, world!\n");
|
||||
stdout.printf("Hello, world!\n");
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import "std.zig";
|
||||
// purposefully conflicting function with main.zig
|
||||
// but it's private so it should be OK
|
||||
fn private_function() => {
|
||||
print_str("OK 1\n");
|
||||
stdout.printf("OK 1\n");
|
||||
}
|
||||
|
||||
pub fn print_text() => {
|
||||
|
||||
@@ -5,7 +5,7 @@ import "foo.zig";
|
||||
|
||||
pub fn main(args: [][]u8) i32 => {
|
||||
private_function();
|
||||
print_str("OK 2\n");
|
||||
stdout.printf("OK 2\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#version("2.0.0")
|
||||
export library "mathtest";
|
||||
|
||||
export fn add(a: i32, b: i32) -> i32 {
|
||||
export fn add(a: i32, b: i32) i32 => {
|
||||
a + b
|
||||
}
|
||||
|
||||
export fn hang() -> unreachable {
|
||||
entry: goto entry;
|
||||
export fn hang() unreachable => {
|
||||
while (true) { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user