authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-24 22:53:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-24 22:53:00-07:00
logbcb18338cd15dfd7a4e3e80e74d1fe395870fa48
treed44e1166bc509300c1dc39817aa9b9362732945f
parent29a83f648b8f0b6f757e07e3b1a6e15e8764b670

update std lib to use error type and global variables


10 files changed, 296 insertions(+), 303 deletions(-)

example/guess_number/main.zig+11-23
...@@ -3,45 +3,33 @@ export executable "guess_number";...@@ -3,45 +3,33 @@ export executable "guess_number";
3import "std.zig";3import "std.zig";
4import "rand.zig";4import "rand.zig";
55
6error GetRandomFail;
7error ReadInputFail;
8
9pub fn main(args: [][]u8) %void => {6pub fn main(args: [][]u8) %void => {
10 print_str("Welcome to the Guess Number Game in Zig.\n");7 stderr.print_str("Welcome to the Guess Number Game in Zig.\n");
118
12 var seed : u32;9 var seed : u32;
13 const seed_bytes = (&u8)(&seed)[0...@sizeof(u32)];10 const seed_bytes = (&u8)(&seed)[0...4];
14 const err = os_get_random_bytes(seed_bytes);11 os_get_random_bytes(seed_bytes);
15 if (err != @sizeof(u32)) {
16 // TODO full error message
17 fprint_str(stderr_fileno, "unable to get random bytes\n");
18 return error.GetRandomFail;
19 }
2012
21 var rand = rand_new(seed);13 var rand = rand_new(seed);
2214
23 const answer = rand.range_u64(0, 100) + 1;15 const answer = rand.range_u64(0, 100) + 1;
2416
25 while (true) {17 while (true) {
26 print_str("\nGuess a number between 1 and 100: ");18 stderr.print_str("\nGuess a number between 1 and 100: ");
27 var line_buf : [20]u8;19 var line_buf : [20]u8;
28 var line_len : isize;20
29 // TODO fix this awkward error handling21 // TODO print error message instead of returning
30 if (readline(line_buf, &line_len) || line_len == line_buf.len) {22 const line_len = %return stdin.readline(line_buf);
31 // TODO full error message
32 fprint_str(stderr_fileno, "unable to read input\n");
33 return error.ReadInputFail;
34 }
3523
36 var guess : u64;24 var guess : u64;
37 if (parse_u64(line_buf[0...line_len - 1], 10, &guess)) {25 if (parse_u64(line_buf[0...line_len - 1], 10, &guess)) {
38 print_str("Invalid number format.\n");26 stderr.print_str("Invalid number format.\n");
39 } else if (guess > answer) {27 } else if (guess > answer) {
40 print_str("Guess lower.\n");28 stderr.print_str("Guess lower.\n");
41 } else if (guess < answer) {29 } else if (guess < answer) {
42 print_str("Guess higher.\n");30 stderr.print_str("Guess higher.\n");
43 } else {31 } else {
44 print_str("You win!\n");32 stderr.print_str("You win!\n");
45 return;33 return;
46 }34 }
47 }35 }
example/hello_world/hello.zig+1-2
...@@ -3,6 +3,5 @@ export executable "hello";...@@ -3,6 +3,5 @@ export executable "hello";
3import "std.zig";3import "std.zig";
44
5pub fn main(args: [][]u8) %void => {5pub fn main(args: [][]u8) %void => {
6 //stderr.print_str("Hello, world!\n");6 stdout.printf("Hello, world!\n");
7 print_str("Hello, world!\n");
8}7}
example/multiple_files/foo.zig+1-1
...@@ -3,7 +3,7 @@ import "std.zig";...@@ -3,7 +3,7 @@ import "std.zig";
3// purposefully conflicting function with main.zig3// purposefully conflicting function with main.zig
4// but it's private so it should be OK4// but it's private so it should be OK
5fn private_function() => {5fn private_function() => {
6 print_str("OK 1\n");6 stdout.printf("OK 1\n");
7}7}
88
9pub fn print_text() => {9pub fn print_text() => {
example/multiple_files/main.zig+1-1
...@@ -5,7 +5,7 @@ import "foo.zig";...@@ -5,7 +5,7 @@ import "foo.zig";
55
6pub fn main(args: [][]u8) i32 => {6pub fn main(args: [][]u8) i32 => {
7 private_function();7 private_function();
8 print_str("OK 2\n");8 stdout.printf("OK 2\n");
9 return 0;9 return 0;
10}10}
1111
example/shared_library/mathtest.zig+3-3
...@@ -1,10 +1,10 @@...@@ -1,10 +1,10 @@
1#version("2.0.0")1#version("2.0.0")
2export library "mathtest";2export library "mathtest";
33
4export fn add(a: i32, b: i32) -> i32 {4export fn add(a: i32, b: i32) i32 => {
5 a + b5 a + b
6}6}
77
8export fn hang() -> unreachable {8export fn hang() unreachable => {
9entry: goto entry;9 while (true) { }
10}10}
src/analyze.cpp+4
...@@ -3058,6 +3058,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B...@@ -3058,6 +3058,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B
3058 if (wanted_type->id == TypeTableEntryIdMaybe) {3058 if (wanted_type->id == TypeTableEntryIdMaybe) {
3059 if (types_match_const_cast_only(wanted_type->data.maybe.child_type, actual_type)) {3059 if (types_match_const_cast_only(wanted_type->data.maybe.child_type, actual_type)) {
3060 node->data.fn_call_expr.cast_op = CastOpMaybeWrap;3060 node->data.fn_call_expr.cast_op = CastOpMaybeWrap;
3061 context->cast_alloca_list.append(node);
3061 eval_const_expr_implicit_cast(g, node, expr_node);3062 eval_const_expr_implicit_cast(g, node, expr_node);
3062 return wanted_type;3063 return wanted_type;
3063 } else if (actual_type->id == TypeTableEntryIdNumLitInt ||3064 } else if (actual_type->id == TypeTableEntryIdNumLitInt ||
...@@ -3065,6 +3066,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B...@@ -3065,6 +3066,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B
3065 {3066 {
3066 if (num_lit_fits_in_other_type(g, expr_node, wanted_type->data.maybe.child_type)) {3067 if (num_lit_fits_in_other_type(g, expr_node, wanted_type->data.maybe.child_type)) {
3067 node->data.fn_call_expr.cast_op = CastOpMaybeWrap;3068 node->data.fn_call_expr.cast_op = CastOpMaybeWrap;
3069 context->cast_alloca_list.append(node);
3068 eval_const_expr_implicit_cast(g, node, expr_node);3070 eval_const_expr_implicit_cast(g, node, expr_node);
3069 return wanted_type;3071 return wanted_type;
3070 } else {3072 } else {
...@@ -3077,6 +3079,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B...@@ -3077,6 +3079,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B
3077 if (wanted_type->id == TypeTableEntryIdErrorUnion) {3079 if (wanted_type->id == TypeTableEntryIdErrorUnion) {
3078 if (types_match_const_cast_only(wanted_type->data.error.child_type, actual_type)) {3080 if (types_match_const_cast_only(wanted_type->data.error.child_type, actual_type)) {
3079 node->data.fn_call_expr.cast_op = CastOpErrorWrap;3081 node->data.fn_call_expr.cast_op = CastOpErrorWrap;
3082 context->cast_alloca_list.append(node);
3080 eval_const_expr_implicit_cast(g, node, expr_node);3083 eval_const_expr_implicit_cast(g, node, expr_node);
3081 return wanted_type;3084 return wanted_type;
3082 } else if (actual_type->id == TypeTableEntryIdNumLitInt ||3085 } else if (actual_type->id == TypeTableEntryIdNumLitInt ||
...@@ -3084,6 +3087,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B...@@ -3084,6 +3087,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B
3084 {3087 {
3085 if (num_lit_fits_in_other_type(g, expr_node, wanted_type->data.error.child_type)) {3088 if (num_lit_fits_in_other_type(g, expr_node, wanted_type->data.error.child_type)) {
3086 node->data.fn_call_expr.cast_op = CastOpErrorWrap;3089 node->data.fn_call_expr.cast_op = CastOpErrorWrap;
3090 context->cast_alloca_list.append(node);
3087 eval_const_expr_implicit_cast(g, node, expr_node);3091 eval_const_expr_implicit_cast(g, node, expr_node);
3088 return wanted_type;3092 return wanted_type;
3089 } else {3093 } else {
src/codegen.cpp+32-11
...@@ -325,11 +325,28 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {...@@ -325,11 +325,28 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
325 return cast_expr->tmp_ptr;325 return cast_expr->tmp_ptr;
326 }326 }
327 case CastOpErrorWrap:327 case CastOpErrorWrap:
328 assert(wanted_type->id == TypeTableEntryIdErrorUnion);328 {
329 if (wanted_type->data.error.child_type->size_in_bits == 0) {329 assert(wanted_type->id == TypeTableEntryIdErrorUnion);
330 return LLVMConstNull(g->err_tag_type->type_ref);330 TypeTableEntry *child_type = wanted_type->data.error.child_type;
331 } else {331 LLVMValueRef ok_err_val = LLVMConstNull(g->err_tag_type->type_ref);
332 zig_panic("TODO");332
333 if (child_type->size_in_bits == 0) {
334 return ok_err_val;
335 } else {
336 assert(cast_expr->tmp_ptr);
337 assert(wanted_type->id == TypeTableEntryIdErrorUnion);
338 assert(actual_type);
339
340 add_debug_source_node(g, node);
341 LLVMValueRef err_tag_ptr = LLVMBuildStructGEP(g->builder, cast_expr->tmp_ptr, 0, "");
342 LLVMBuildStore(g->builder, ok_err_val, err_tag_ptr);
343
344 LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, cast_expr->tmp_ptr, 1, "");
345 gen_assign_raw(g, node, BinOpTypeAssign,
346 payload_ptr, expr_val, child_type, actual_type);
347
348 return cast_expr->tmp_ptr;
349 }
333 }350 }
334 case CastOpPureErrorWrap:351 case CastOpPureErrorWrap:
335 assert(wanted_type->id == TypeTableEntryIdErrorUnion);352 assert(wanted_type->id == TypeTableEntryIdErrorUnion);
...@@ -1286,12 +1303,16 @@ static LLVMValueRef gen_return_expr(CodeGen *g, AstNode *node) {...@@ -1286,12 +1303,16 @@ static LLVMValueRef gen_return_expr(CodeGen *g, AstNode *node) {
1286 if (return_type->id == TypeTableEntryIdPureError) {1303 if (return_type->id == TypeTableEntryIdPureError) {
1287 gen_return(g, node, err_val);1304 gen_return(g, node, err_val);
1288 } else if (return_type->id == TypeTableEntryIdErrorUnion) {1305 } else if (return_type->id == TypeTableEntryIdErrorUnion) {
1289 assert(g->cur_ret_ptr);1306 if (return_type->data.error.child_type->size_in_bits > 0) {
1307 assert(g->cur_ret_ptr);
12901308
1291 add_debug_source_node(g, node);1309 add_debug_source_node(g, node);
1292 LLVMValueRef tag_ptr = LLVMBuildStructGEP(g->builder, g->cur_ret_ptr, 0, "");1310 LLVMValueRef tag_ptr = LLVMBuildStructGEP(g->builder, g->cur_ret_ptr, 0, "");
1293 LLVMBuildStore(g->builder, err_val, tag_ptr);1311 LLVMBuildStore(g->builder, err_val, tag_ptr);
1294 LLVMBuildRetVoid(g->builder);1312 LLVMBuildRetVoid(g->builder);
1313 } else {
1314 gen_return(g, node, err_val);
1315 }
1295 } else {1316 } else {
1296 zig_unreachable();1317 zig_unreachable();
1297 }1318 }
...@@ -2011,7 +2032,7 @@ static LLVMValueRef gen_switch_expr(CodeGen *g, AstNode *node) {...@@ -2011,7 +2032,7 @@ static LLVMValueRef gen_switch_expr(CodeGen *g, AstNode *node) {
2011 LLVMPositionBuilderAtEnd(g->builder, end_block);2032 LLVMPositionBuilderAtEnd(g->builder, end_block);
20122033
2013 add_debug_source_node(g, node);2034 add_debug_source_node(g, node);
2014 LLVMValueRef phi = LLVMBuildPhi(g->builder, get_expr_type(node)->type_ref, "");2035 LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMTypeOf(incoming_values.at(0)), "");
2015 LLVMAddIncoming(phi, incoming_values.items, incoming_blocks.items, incoming_values.length);2036 LLVMAddIncoming(phi, incoming_values.items, incoming_blocks.items, incoming_values.length);
20162037
2017 return phi;2038 return phi;
std/std.zig+50-69
...@@ -5,7 +5,6 @@ pub const stdin_fileno = 0;...@@ -5,7 +5,6 @@ pub const stdin_fileno = 0;
5pub const stdout_fileno = 1;5pub const stdout_fileno = 1;
6pub const stderr_fileno = 2;6pub const stderr_fileno = 2;
77
8/*
9pub var stdin = InStream {8pub var stdin = InStream {
10 .fd = stdin_fileno,9 .fd = stdin_fileno,
11};10};
...@@ -23,9 +22,16 @@ pub var stderr = OutStream {...@@ -23,9 +22,16 @@ pub var stderr = OutStream {
23 .index = 0,22 .index = 0,
24 .buffered = false,23 .buffered = false,
25};24};
26*/
2725
26/// The function received invalid input at runtime. An Invalid error means a
27/// bug in the program that called the function.
28pub error Invalid;
29
30/// When an Unexpected error occurs, code that emitted the error likely needs
31/// a patch to recognize the unexpected case so that it can handle it and emit
32/// a more specific error.
28pub error Unexpected;33pub error Unexpected;
34
29pub error DiskQuota;35pub error DiskQuota;
30pub error FileTooBig;36pub error FileTooBig;
31pub error SigInterrupt;37pub error SigInterrupt;
...@@ -33,26 +39,25 @@ pub error Io;...@@ -33,26 +39,25 @@ pub error Io;
33pub error NoSpaceLeft;39pub error NoSpaceLeft;
34pub error BadPerm;40pub error BadPerm;
35pub error PipeFail;41pub error PipeFail;
36pub error Invalid;42pub error BadFd;
3743
38const buffer_size = 4 * 1024;44const buffer_size = 4 * 1024;
39const max_u64_base10_digits = 20;45const max_u64_base10_digits = 20;
4046
41/*
42pub struct OutStream {47pub struct OutStream {
43 fd: isize,48 fd: isize,
44 buffer: [buffer_size]u8,49 buffer: [buffer_size]u8,
45 index: @typeof(buffer_size),50 index: isize,
46 buffered: bool,51 buffered: bool,
4752
48 pub fn print_str(os: &OutStream, str: []const u8) %isize => {53 pub fn print_str(os: &OutStream, str: []const u8) %isize => {
49 var src_bytes_left = str.len;54 var src_bytes_left = str.len;
50 var src_index: @typeof(str.len) = 0;55 var src_index: @typeof(str.len) = 0;
51 const dest_space_left = os.buffer.len - index;56 const dest_space_left = os.buffer.len - os.index;
5257
53 while (src_bytes_left > 0) {58 while (src_bytes_left > 0) {
54 const copy_amt = min_isize(dest_space_left, src_bytes_left);59 const copy_amt = min_isize(dest_space_left, src_bytes_left);
55 @memcpy(&buffer[os.index], &str[src_index], copy_amt);60 @memcpy(&os.buffer[os.index], &str[src_index], copy_amt);
56 os.index += copy_amt;61 os.index += copy_amt;
57 if (os.index == os.buffer.len) {62 if (os.index == os.buffer.len) {
58 %return os.flush();63 %return os.flush();
...@@ -65,11 +70,19 @@ pub struct OutStream {...@@ -65,11 +70,19 @@ pub struct OutStream {
65 return str.len;70 return str.len;
66 }71 }
6772
73 /// Prints a byte buffer, flushes the buffer, then returns the number of
74 /// bytes printed. The "f" is for "flush".
75 pub fn printf(os: &OutStream, str: []const u8) %isize => {
76 const byte_count = %return os.print_str(str);
77 %return os.flush();
78 return byte_count;
79 }
80
68 pub fn print_u64(os: &OutStream, x: u64) %isize => {81 pub fn print_u64(os: &OutStream, x: u64) %isize => {
69 if (os.index + max_u64_base10_digits >= os.buffer.len) {82 if (os.index + max_u64_base10_digits >= os.buffer.len) {
70 %return os.flush();83 %return os.flush();
71 }84 }
72 const amt_printed = buf_print_u64(buf[os.index...], x);85 const amt_printed = buf_print_u64(os.buffer[os.index...], x);
73 os.index += amt_printed;86 os.index += amt_printed;
7487
75 if (!os.buffered) {88 if (!os.buffered) {
...@@ -84,7 +97,7 @@ pub struct OutStream {...@@ -84,7 +97,7 @@ pub struct OutStream {
84 if (os.index + max_u64_base10_digits >= os.buffer.len) {97 if (os.index + max_u64_base10_digits >= os.buffer.len) {
85 %return os.flush();98 %return os.flush();
86 }99 }
87 const amt_printed = buf_print_i64(buf[os.index...], x);100 const amt_printed = buf_print_i64(os.buffer[os.index...], x);
88 os.index += amt_printed;101 os.index += amt_printed;
89102
90 if (!os.buffered) {103 if (!os.buffered) {
...@@ -96,18 +109,20 @@ pub struct OutStream {...@@ -96,18 +109,20 @@ pub struct OutStream {
96109
97110
98 pub fn flush(os: &OutStream) %void => {111 pub fn flush(os: &OutStream) %void => {
99 const amt_to_write = os.index;112 const amt_written = write(os.fd, os.buffer.ptr, os.index);
100 os.index = 0;113 os.index = 0;
101 switch (write(os.fd, os.buffer.ptr, amt_to_write)) {114 if (amt_written < 0) {
102 EINVAL => unreachable{},115 return switch (-amt_written) {
103 EDQUOT => error.DiskQuota,116 EINVAL => unreachable{},
104 EFBIG => error.FileTooBig,117 EDQUOT => error.DiskQuota,
105 EINTR => error.SigInterrupt,118 EFBIG => error.FileTooBig,
106 EIO => error.Io,119 EINTR => error.SigInterrupt,
107 ENOSPC => error.NoSpaceLeft,120 EIO => error.Io,
108 EPERM => error.BadPerm,121 ENOSPC => error.NoSpaceLeft,
109 EPIPE => error.PipeFail,122 EPERM => error.BadPerm,
110 else => error.Unexpected,123 EPIPE => error.PipeFail,
124 else => error.Unexpected,
125 }
111 }126 }
112 }127 }
113}128}
...@@ -115,10 +130,10 @@ pub struct OutStream {...@@ -115,10 +130,10 @@ pub struct OutStream {
115pub struct InStream {130pub struct InStream {
116 fd: isize,131 fd: isize,
117132
118 pub fn readline(buf: []u8) %isize => {133 pub fn readline(is: &InStream, buf: []u8) %isize => {
119 const amt_read = read(stdin_fileno, buf.ptr, buf.len);134 const amt_read = read(is.fd, buf.ptr, buf.len);
120 if (amt_read < 0) {135 if (amt_read < 0) {
121 switch (-amt_read) {136 return switch (-amt_read) {
122 EINVAL => unreachable{},137 EINVAL => unreachable{},
123 EFAULT => unreachable{},138 EFAULT => unreachable{},
124 EBADF => error.BadFd,139 EBADF => error.BadFd,
...@@ -133,53 +148,15 @@ pub struct InStream {...@@ -133,53 +148,15 @@ pub struct InStream {
133}148}
134149
135pub fn os_get_random_bytes(buf: []u8) %void => {150pub fn os_get_random_bytes(buf: []u8) %void => {
136 switch (getrandom(buf.ptr, buf.len, 0)) {151 const amt_got = getrandom(buf.ptr, buf.len, 0);
137 EINVAL => unreachable{},152 if (amt_got < 0) {
138 EFAULT => unreachable{},153 return switch (-amt_got) {
139 EINTR => error.SigInterrupt,154 EINVAL => unreachable{},
140 else => error.Unexpected,155 EFAULT => unreachable{},
141 }156 EINTR => error.SigInterrupt,
142}157 else => error.Unexpected,
143*/158 }
144
145
146// TODO remove this
147pub fn print_str(str: []const u8) isize => {
148 fprint_str(stdout_fileno, str)
149}
150
151// TODO remove this
152pub fn fprint_str(fd: isize, str: []const u8) isize => {
153 write(fd, str.ptr, str.len)
154}
155
156// TODO remove this
157pub fn os_get_random_bytes(buf: []u8) isize => {
158 getrandom(buf.ptr, buf.len, 0)
159}
160
161// TODO remove this
162pub fn print_u64(x: u64) isize => {
163 var buf: [max_u64_base10_digits]u8;
164 const len = buf_print_u64(buf, x);
165 return write(stdout_fileno, buf.ptr, len);
166}
167
168// TODO remove this
169pub fn print_i64(x: i64) isize => {
170 var buf: [max_u64_base10_digits]u8;
171 const len = buf_print_i64(buf, x);
172 return write(stdout_fileno, buf.ptr, len);
173}
174
175// TODO remove this
176pub fn readline(buf: []u8, out_len: &isize) bool => {
177 const amt_read = read(stdin_fileno, buf.ptr, buf.len);
178 if (amt_read < 0) {
179 return true;
180 }159 }
181 *out_len = isize(amt_read);
182 return false;
183}160}
184161
185162
...@@ -251,3 +228,7 @@ fn buf_print_u64(out_buf: []u8, x: u64) isize => {...@@ -251,3 +228,7 @@ fn buf_print_u64(out_buf: []u8, x: u64) isize => {
251228
252 return len;229 return len;
253}230}
231
232fn min_isize(x: isize, y: isize) isize => {
233 if (x < y) x else y
234}
std/syscall.zig+7-7
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const SYS_read : isize = 0;1const SYS_read = 0;
2const SYS_write : isize = 1;2const SYS_write = 1;
3const SYS_exit : isize = 60;3const SYS_exit = 60;
4const SYS_getrandom : isize = 318;4const SYS_getrandom = 318;
55
6fn syscall1(number: isize, arg1: isize) isize => {6fn syscall1(number: isize, arg1: isize) isize => {
7 asm volatile ("syscall"7 asm volatile ("syscall"
...@@ -18,11 +18,11 @@ fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) isize => {...@@ -18,11 +18,11 @@ fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) isize => {
18}18}
1919
20pub fn read(fd: isize, buf: &u8, count: isize) isize => {20pub fn read(fd: isize, buf: &u8, count: isize) isize => {
21 isize(syscall3(SYS_read, isize(fd), isize(buf), count))21 syscall3(SYS_read, isize(fd), isize(buf), count)
22}22}
2323
24pub fn write(fd: isize, buf: &const u8, count: isize) isize => {24pub fn write(fd: isize, buf: &const u8, count: isize) isize => {
25 isize(syscall3(SYS_write, isize(fd), isize(buf), count))25 syscall3(SYS_write, isize(fd), isize(buf), count)
26}26}
2727
28pub fn exit(status: i32) unreachable => {28pub fn exit(status: i32) unreachable => {
...@@ -31,5 +31,5 @@ pub fn exit(status: i32) unreachable => {...@@ -31,5 +31,5 @@ pub fn exit(status: i32) unreachable => {
31}31}
3232
33pub fn getrandom(buf: &u8, count: isize, flags: u32) isize => {33pub fn getrandom(buf: &u8, count: isize, flags: u32) isize => {
34 isize(syscall3(SYS_getrandom, isize(buf), count, isize(flags)))34 syscall3(SYS_getrandom, isize(buf), count, isize(flags))
35}35}
test/run_tests.cpp+186-186
...@@ -121,7 +121,7 @@ pub fn main(args: [][]u8) %void => {...@@ -121,7 +121,7 @@ pub fn main(args: [][]u8) %void => {
121}121}
122122
123fn this_is_a_function() unreachable => {123fn this_is_a_function() unreachable => {
124 print_str("OK\n");124 stdout.printf("OK\n");
125 exit(0);125 exit(0);
126}126}
127 )SOURCE", "OK\n");127 )SOURCE", "OK\n");
...@@ -137,7 +137,7 @@ fn another_function() => {}...@@ -137,7 +137,7 @@ fn another_function() => {}
137/// this is a documentation comment137/// this is a documentation comment
138/// doc comment line 2138/// doc comment line 2
139pub fn main(args: [][]u8) %void => {139pub fn main(args: [][]u8) %void => {
140 print_str(/* mid-line comment /* nested */ */ "OK\n");140 stdout.printf(/* mid-line comment /* nested */ */ "OK\n");
141}141}
142 )SOURCE", "OK\n");142 )SOURCE", "OK\n");
143143
...@@ -148,7 +148,7 @@ import "foo.zig";...@@ -148,7 +148,7 @@ import "foo.zig";
148148
149pub fn main(args: [][]u8) %void => {149pub fn main(args: [][]u8) %void => {
150 private_function();150 private_function();
151 print_str("OK 2\n");151 stdout.printf("OK 2\n");
152}152}
153153
154fn private_function() => {154fn private_function() => {
...@@ -162,7 +162,7 @@ import "std.zig";...@@ -162,7 +162,7 @@ import "std.zig";
162// purposefully conflicting function with main.zig162// purposefully conflicting function with main.zig
163// but it's private so it should be OK163// but it's private so it should be OK
164fn private_function() => {164fn private_function() => {
165 print_str("OK 1\n");165 stdout.printf("OK 1\n");
166}166}
167167
168pub fn print_text() => {168pub fn print_text() => {
...@@ -185,7 +185,7 @@ pub fn main(args: [][]u8) %void => {...@@ -185,7 +185,7 @@ pub fn main(args: [][]u8) %void => {
185 add_source_file(tc, "foo.zig", R"SOURCE(185 add_source_file(tc, "foo.zig", R"SOURCE(
186import "std.zig";186import "std.zig";
187pub fn foo_function() => {187pub fn foo_function() => {
188 print_str("OK\n");188 stdout.printf("OK\n");
189}189}
190 )SOURCE");190 )SOURCE");
191191
...@@ -195,7 +195,7 @@ import "std.zig";...@@ -195,7 +195,7 @@ import "std.zig";
195195
196pub fn bar_function() => {196pub fn bar_function() => {
197 if (foo_function()) {197 if (foo_function()) {
198 print_str("OK\n");198 stdout.printf("OK\n");
199 }199 }
200}200}
201 )SOURCE");201 )SOURCE");
...@@ -213,17 +213,17 @@ import "std.zig";...@@ -213,17 +213,17 @@ import "std.zig";
213213
214pub fn main(args: [][]u8) %void => {214pub fn main(args: [][]u8) %void => {
215 if (1 != 0) {215 if (1 != 0) {
216 print_str("1 is true\n");216 stdout.printf("1 is true\n");
217 } else {217 } else {
218 print_str("1 is false\n");218 stdout.printf("1 is false\n");
219 }219 }
220 if (0 != 0) {220 if (0 != 0) {
221 print_str("0 is true\n");221 stdout.printf("0 is true\n");
222 } else if (1 - 1 != 0) {222 } else if (1 - 1 != 0) {
223 print_str("1 - 1 is true\n");223 stdout.printf("1 - 1 is true\n");
224 }224 }
225 if (!(0 != 0)) {225 if (!(0 != 0)) {
226 print_str("!0 is true\n");226 stdout.printf("!0 is true\n");
227 }227 }
228}228}
229 )SOURCE", "1 is true\n!0 is true\n");229 )SOURCE", "1 is true\n!0 is true\n");
...@@ -237,7 +237,7 @@ fn add(a: i32, b: i32) i32 => {...@@ -237,7 +237,7 @@ fn add(a: i32, b: i32) i32 => {
237237
238pub fn main(args: [][]u8) %void => {238pub fn main(args: [][]u8) %void => {
239 if (add(22, 11) == 33) {239 if (add(22, 11) == 33) {
240 print_str("pass\n");240 stdout.printf("pass\n");
241 }241 }
242}242}
243 )SOURCE", "pass\n");243 )SOURCE", "pass\n");
...@@ -249,7 +249,7 @@ fn loop(a : i32) => {...@@ -249,7 +249,7 @@ fn loop(a : i32) => {
249 if (a == 0) {249 if (a == 0) {
250 goto done;250 goto done;
251 }251 }
252 print_str("loop\n");252 stdout.printf("loop\n");
253 loop(a - 1);253 loop(a - 1);
254254
255done:255done:
...@@ -268,7 +268,7 @@ pub fn main(args: [][]u8) %void => {...@@ -268,7 +268,7 @@ pub fn main(args: [][]u8) %void => {
268 const a : i32 = 1;268 const a : i32 = 1;
269 const b = i32(2);269 const b = i32(2);
270 if (a + b == 3) {270 if (a + b == 3) {
271 print_str("OK\n");271 stdout.printf("OK\n");
272 }272 }
273}273}
274 )SOURCE", "OK\n");274 )SOURCE", "OK\n");
...@@ -277,10 +277,10 @@ pub fn main(args: [][]u8) %void => {...@@ -277,10 +277,10 @@ pub fn main(args: [][]u8) %void => {
277import "std.zig";277import "std.zig";
278278
279pub fn main(args: [][]u8) %void => {279pub fn main(args: [][]u8) %void => {
280 if (true) { print_str("OK 1\n"); }280 if (true) { stdout.printf("OK 1\n"); }
281 if (false) { print_str("BAD 1\n"); }281 if (false) { stdout.printf("BAD 1\n"); }
282 if (!true) { print_str("BAD 2\n"); }282 if (!true) { stdout.printf("BAD 2\n"); }
283 if (!false) { print_str("OK 2\n"); }283 if (!false) { stdout.printf("OK 2\n"); }
284}284}
285 )SOURCE", "OK 1\nOK 2\n");285 )SOURCE", "OK 1\nOK 2\n");
286286
...@@ -290,14 +290,14 @@ import "std.zig";...@@ -290,14 +290,14 @@ import "std.zig";
290pub fn main(args: [][]u8) %void => {290pub fn main(args: [][]u8) %void => {
291 if (true) {291 if (true) {
292 const no_conflict : i32 = 5;292 const no_conflict : i32 = 5;
293 if (no_conflict == 5) { print_str("OK 1\n"); }293 if (no_conflict == 5) { stdout.printf("OK 1\n"); }
294 }294 }
295295
296 const c = {296 const c = {
297 const no_conflict = i32(10);297 const no_conflict = i32(10);
298 no_conflict298 no_conflict
299 };299 };
300 if (c == 10) { print_str("OK 2\n"); }300 if (c == 10) { stdout.printf("OK 2\n"); }
301}301}
302 )SOURCE", "OK 1\nOK 2\n");302 )SOURCE", "OK 1\nOK 2\n");
303303
...@@ -311,7 +311,7 @@ pub fn main(args: [][]u8) %void => {...@@ -311,7 +311,7 @@ pub fn main(args: [][]u8) %void => {
311fn void_fun(a : i32, b : void, c : i32) => {311fn void_fun(a : i32, b : void, c : i32) => {
312 const v = b;312 const v = b;
313 const vv : void = if (a == 1) {v} else {};313 const vv : void = if (a == 1) {v} else {};
314 if (a + c == 3) { print_str("OK\n"); }314 if (a + c == 3) { stdout.printf("OK\n"); }
315 return vv;315 return vv;
316}316}
317 )SOURCE", "OK\n");317 )SOURCE", "OK\n");
...@@ -330,12 +330,12 @@ pub fn main(args: [][]u8) %void => {...@@ -330,12 +330,12 @@ pub fn main(args: [][]u8) %void => {
330 .c = void{},330 .c = void{},
331 };331 };
332 if (foo.b != 1) {332 if (foo.b != 1) {
333 print_str("BAD\n");333 stdout.printf("BAD\n");
334 }334 }
335 if (@sizeof(Foo) != 4) {335 if (@sizeof(Foo) != 4) {
336 print_str("BAD\n");336 stdout.printf("BAD\n");
337 }337 }
338 print_str("OK\n");338 stdout.printf("OK\n");
339}339}
340340
341 )SOURCE", "OK\n");341 )SOURCE", "OK\n");
...@@ -348,12 +348,12 @@ pub fn main(args: [][]u8) %void => {...@@ -348,12 +348,12 @@ pub fn main(args: [][]u8) %void => {
348 array[0] = void{};348 array[0] = void{};
349 array[1] = array[2];349 array[1] = array[2];
350 if (@sizeof(@typeof(array)) != 0) {350 if (@sizeof(@typeof(array)) != 0) {
351 print_str("BAD\n");351 stdout.printf("BAD\n");
352 }352 }
353 if (array.len != 4) {353 if (array.len != 4) {
354 print_str("BAD\n");354 stdout.printf("BAD\n");
355 }355 }
356 print_str("OK\n");356 stdout.printf("OK\n");
357}357}
358 )SOURCE", "OK\n");358 )SOURCE", "OK\n");
359359
...@@ -363,11 +363,11 @@ import "std.zig";...@@ -363,11 +363,11 @@ import "std.zig";
363363
364pub fn main(args: [][]u8) %void => {364pub fn main(args: [][]u8) %void => {
365 var zero : i32 = 0;365 var zero : i32 = 0;
366 if (zero == 0) { print_str("zero\n"); }366 if (zero == 0) { stdout.printf("zero\n"); }
367367
368 var i = i32(0);368 var i = i32(0);
369 while (i != 3) {369 while (i != 3) {
370 print_str("loop\n");370 stdout.printf("loop\n");
371 i += 1;371 i += 1;
372 }372 }
373}373}
...@@ -394,11 +394,11 @@ pub fn main(args: [][]u8) %void => {...@@ -394,11 +394,11 @@ pub fn main(args: [][]u8) %void => {
394 }394 }
395395
396 if (accumulator == 15) {396 if (accumulator == 15) {
397 print_str("OK\n");397 stdout.printf("OK\n");
398 }398 }
399399
400 if (get_array_len(array) != 5) {400 if (get_array_len(array) != 5) {
401 print_str("BAD\n");401 stdout.printf("BAD\n");
402 }402 }
403}403}
404fn get_array_len(a: []i32) isize => {404fn get_array_len(a: []i32) isize => {
...@@ -411,7 +411,7 @@ fn get_array_len(a: []i32) isize => {...@@ -411,7 +411,7 @@ fn get_array_len(a: []i32) isize => {
411import "std.zig";411import "std.zig";
412412
413pub fn main(args: [][]u8) %void => {413pub fn main(args: [][]u8) %void => {
414 print_str("Hello, world!\n");414 stdout.printf("Hello, world!\n");
415}415}
416 )SOURCE", "Hello, world!\n");416 )SOURCE", "Hello, world!\n");
417417
...@@ -420,20 +420,20 @@ pub fn main(args: [][]u8) %void => {...@@ -420,20 +420,20 @@ pub fn main(args: [][]u8) %void => {
420import "std.zig";420import "std.zig";
421421
422pub fn main(args: [][]u8) %void => {422pub fn main(args: [][]u8) %void => {
423 if (false || false || false) { print_str("BAD 1\n"); }423 if (false || false || false) { stdout.printf("BAD 1\n"); }
424 if (true && true && false) { print_str("BAD 2\n"); }424 if (true && true && false) { stdout.printf("BAD 2\n"); }
425 if (1 | 2 | 4 != 7) { print_str("BAD 3\n"); }425 if (1 | 2 | 4 != 7) { stdout.printf("BAD 3\n"); }
426 if (3 ^ 6 ^ 8 != 13) { print_str("BAD 4\n"); }426 if (3 ^ 6 ^ 8 != 13) { stdout.printf("BAD 4\n"); }
427 if (7 & 14 & 28 != 4) { print_str("BAD 5\n"); }427 if (7 & 14 & 28 != 4) { stdout.printf("BAD 5\n"); }
428 if (9 << 1 << 2 != 9 << 3) { print_str("BAD 6\n"); }428 if (9 << 1 << 2 != 9 << 3) { stdout.printf("BAD 6\n"); }
429 if (90 >> 1 >> 2 != 90 >> 3) { print_str("BAD 7\n"); }429 if (90 >> 1 >> 2 != 90 >> 3) { stdout.printf("BAD 7\n"); }
430 if (100 - 1 + 1000 != 1099) { print_str("BAD 8\n"); }430 if (100 - 1 + 1000 != 1099) { stdout.printf("BAD 8\n"); }
431 if (5 * 4 / 2 % 3 != 1) { print_str("BAD 9\n"); }431 if (5 * 4 / 2 % 3 != 1) { stdout.printf("BAD 9\n"); }
432 if (i32(i32(5)) != 5) { print_str("BAD 10\n"); }432 if (i32(i32(5)) != 5) { stdout.printf("BAD 10\n"); }
433 if (!!false) { print_str("BAD 11\n"); }433 if (!!false) { stdout.printf("BAD 11\n"); }
434 if (i32(7) != --(i32(7))) { print_str("BAD 12\n"); }434 if (i32(7) != --(i32(7))) { stdout.printf("BAD 12\n"); }
435435
436 print_str("OK\n");436 stdout.printf("OK\n");
437}437}
438 )SOURCE", "OK\n");438 )SOURCE", "OK\n");
439439
...@@ -441,19 +441,19 @@ pub fn main(args: [][]u8) %void => {...@@ -441,19 +441,19 @@ pub fn main(args: [][]u8) %void => {
441import "std.zig";441import "std.zig";
442442
443pub fn main(args: [][]u8) %void => {443pub fn main(args: [][]u8) %void => {
444 if (true || { print_str("BAD 1\n"); false }) {444 if (true || { stdout.printf("BAD 1\n"); false }) {
445 print_str("OK 1\n");445 stdout.printf("OK 1\n");
446 }446 }
447 if (false || { print_str("OK 2\n"); false }) {447 if (false || { stdout.printf("OK 2\n"); false }) {
448 print_str("BAD 2\n");448 stdout.printf("BAD 2\n");
449 }449 }
450450
451 if (true && { print_str("OK 3\n"); false }) {451 if (true && { stdout.printf("OK 3\n"); false }) {
452 print_str("BAD 3\n");452 stdout.printf("BAD 3\n");
453 }453 }
454 if (false && { print_str("BAD 4\n"); false }) {454 if (false && { stdout.printf("BAD 4\n"); false }) {
455 } else {455 } else {
456 print_str("OK 4\n");456 stdout.printf("OK 4\n");
457 }457 }
458}458}
459 )SOURCE", "OK 1\nOK 2\nOK 3\nOK 4\n");459 )SOURCE", "OK 1\nOK 2\nOK 3\nOK 4\n");
...@@ -463,20 +463,20 @@ import "std.zig";...@@ -463,20 +463,20 @@ import "std.zig";
463463
464pub fn main(args: [][]u8) %void => {464pub fn main(args: [][]u8) %void => {
465 var i : i32 = 0;465 var i : i32 = 0;
466 i += 5; if (i != 5) { print_str("BAD +=\n"); }466 i += 5; if (i != 5) { stdout.printf("BAD +=\n"); }
467 i -= 2; if (i != 3) { print_str("BAD -=\n"); }467 i -= 2; if (i != 3) { stdout.printf("BAD -=\n"); }
468 i *= 20; if (i != 60) { print_str("BAD *=\n"); }468 i *= 20; if (i != 60) { stdout.printf("BAD *=\n"); }
469 i /= 3; if (i != 20) { print_str("BAD /=\n"); }469 i /= 3; if (i != 20) { stdout.printf("BAD /=\n"); }
470 i %= 11; if (i != 9) { print_str("BAD %=\n"); }470 i %= 11; if (i != 9) { stdout.printf("BAD %=\n"); }
471 i <<= 1; if (i != 18) { print_str("BAD <<=\n"); }471 i <<= 1; if (i != 18) { stdout.printf("BAD <<=\n"); }
472 i >>= 2; if (i != 4) { print_str("BAD >>=\n"); }472 i >>= 2; if (i != 4) { stdout.printf("BAD >>=\n"); }
473 i = 6;473 i = 6;
474 i &= 5; if (i != 4) { print_str("BAD &=\n"); }474 i &= 5; if (i != 4) { stdout.printf("BAD &=\n"); }
475 i ^= 6; if (i != 2) { print_str("BAD ^=\n"); }475 i ^= 6; if (i != 2) { stdout.printf("BAD ^=\n"); }
476 i = 6;476 i = 6;
477 i |= 3; if (i != 7) { print_str("BAD |=\n"); }477 i |= 3; if (i != 7) { stdout.printf("BAD |=\n"); }
478478
479 print_str("OK\n");479 stdout.printf("OK\n");
480}480}
481 )SOURCE", "OK\n");481 )SOURCE", "OK\n");
482482
...@@ -620,12 +620,12 @@ pub fn main(args: [][]u8) %void => {...@@ -620,12 +620,12 @@ pub fn main(args: [][]u8) %void => {
620 test_foo(foo);620 test_foo(foo);
621 test_mutation(&foo);621 test_mutation(&foo);
622 if (foo.c != 100) {622 if (foo.c != 100) {
623 print_str("BAD\n");623 stdout.printf("BAD\n");
624 }624 }
625 test_point_to_self();625 test_point_to_self();
626 test_byval_assign();626 test_byval_assign();
627 test_initializer();627 test_initializer();
628 print_str("OK\n");628 stdout.printf("OK\n");
629}629}
630struct Foo {630struct Foo {
631 a : i32,631 a : i32,
...@@ -634,7 +634,7 @@ struct Foo {...@@ -634,7 +634,7 @@ struct Foo {
634}634}
635fn test_foo(foo : Foo) => {635fn test_foo(foo : Foo) => {
636 if (!foo.b) {636 if (!foo.b) {
637 print_str("BAD\n");637 stdout.printf("BAD\n");
638 }638 }
639}639}
640fn test_mutation(foo : &Foo) => {640fn test_mutation(foo : &Foo) => {
...@@ -659,7 +659,7 @@ fn test_point_to_self() => {...@@ -659,7 +659,7 @@ fn test_point_to_self() => {
659 root.next = &node;659 root.next = &node;
660660
661 if (node.next.next.next.val.x != 1) {661 if (node.next.next.next.val.x != 1) {
662 print_str("BAD\n");662 stdout.printf("BAD\n");
663 }663 }
664}664}
665fn test_byval_assign() => {665fn test_byval_assign() => {
...@@ -668,15 +668,15 @@ fn test_byval_assign() => {...@@ -668,15 +668,15 @@ fn test_byval_assign() => {
668668
669 foo1.a = 1234;669 foo1.a = 1234;
670670
671 if (foo2.a != 0) { print_str("BAD\n"); }671 if (foo2.a != 0) { stdout.printf("BAD\n"); }
672672
673 foo2 = foo1;673 foo2 = foo1;
674674
675 if (foo2.a != 1234) { print_str("BAD - byval assignment failed\n"); }675 if (foo2.a != 1234) { stdout.printf("BAD - byval assignment failed\n"); }
676}676}
677fn test_initializer() => {677fn test_initializer() => {
678 const val = Val { .x = 42 };678 const val = Val { .x = 42 };
679 if (val.x != 42) { print_str("BAD\n"); }679 if (val.x != 42) { stdout.printf("BAD\n"); }
680}680}
681 )SOURCE", "OK\n");681 )SOURCE", "OK\n");
682682
...@@ -687,10 +687,10 @@ const g1 : i32 = 1233 + 1;...@@ -687,10 +687,10 @@ const g1 : i32 = 1233 + 1;
687var g2 : i32 = 0;687var g2 : i32 = 0;
688688
689pub fn main(args: [][]u8) %void => {689pub fn main(args: [][]u8) %void => {
690 if (g2 != 0) { print_str("BAD\n"); }690 if (g2 != 0) { stdout.printf("BAD\n"); }
691 g2 = g1;691 g2 = g1;
692 if (g2 != 1234) { print_str("BAD\n"); }692 if (g2 != 1234) { stdout.printf("BAD\n"); }
693 print_str("OK\n");693 stdout.printf("OK\n");
694}694}
695 )SOURCE", "OK\n");695 )SOURCE", "OK\n");
696696
...@@ -699,7 +699,7 @@ import "std.zig";...@@ -699,7 +699,7 @@ import "std.zig";
699pub fn main(args: [][]u8) %void => {699pub fn main(args: [][]u8) %void => {
700 var i : i32 = 0;700 var i : i32 = 0;
701 while (i < 4) {701 while (i < 4) {
702 print_str("loop\n");702 stdout.printf("loop\n");
703 i += 1;703 i += 1;
704 }704 }
705 g();705 g();
...@@ -719,7 +719,7 @@ import "std.zig";...@@ -719,7 +719,7 @@ import "std.zig";
719pub fn main(args: [][]u8) %void => {719pub fn main(args: [][]u8) %void => {
720 var i : i32 = 0;720 var i : i32 = 0;
721 while (true) {721 while (true) {
722 print_str("loop\n");722 stdout.printf("loop\n");
723 i += 1;723 i += 1;
724 if (i < 4) {724 if (i < 4) {
725 continue;725 continue;
...@@ -736,12 +736,12 @@ pub fn main(args: [][]u8) %void => {...@@ -736,12 +736,12 @@ pub fn main(args: [][]u8) %void => {
736736
737 if (const y ?= x) {737 if (const y ?= x) {
738 if (y) {738 if (y) {
739 print_str("x is true\n");739 stdout.printf("x is true\n");
740 } else {740 } else {
741 print_str("x is false\n");741 stdout.printf("x is false\n");
742 }742 }
743 } else {743 } else {
744 print_str("x is none\n");744 stdout.printf("x is none\n");
745 }745 }
746746
747 const next_x : ?i32 = null;747 const next_x : ?i32 = null;
...@@ -749,7 +749,7 @@ pub fn main(args: [][]u8) %void => {...@@ -749,7 +749,7 @@ pub fn main(args: [][]u8) %void => {
749 const z = next_x ?? 1234;749 const z = next_x ?? 1234;
750750
751 if (z != 1234) {751 if (z != 1234) {
752 print_str("BAD\n");752 stdout.printf("BAD\n");
753 }753 }
754754
755 const final_x : ?i32 = 13;755 const final_x : ?i32 = 13;
...@@ -757,7 +757,7 @@ pub fn main(args: [][]u8) %void => {...@@ -757,7 +757,7 @@ pub fn main(args: [][]u8) %void => {
757 const num = final_x ?? unreachable{};757 const num = final_x ?? unreachable{};
758758
759 if (num != 13) {759 if (num != 13) {
760 print_str("BAD\n");760 stdout.printf("BAD\n");
761 }761 }
762}762}
763 )SOURCE", "x is true\n");763 )SOURCE", "x is true\n");
...@@ -767,7 +767,7 @@ import "std.zig";...@@ -767,7 +767,7 @@ import "std.zig";
767pub fn main(args: [][]u8) %void => {767pub fn main(args: [][]u8) %void => {
768 const x = outer();768 const x = outer();
769 if (x == 1234) {769 if (x == 1234) {
770 print_str("OK\n");770 stdout.printf("OK\n");
771 }771 }
772}772}
773fn inner() i32 => { 1234 }773fn inner() i32 => { 1234 }
...@@ -782,8 +782,8 @@ const x: u16 = 13;...@@ -782,8 +782,8 @@ const x: u16 = 13;
782const z: @typeof(x) = 19;782const z: @typeof(x) = 19;
783pub fn main(args: [][]u8) %void => {783pub fn main(args: [][]u8) %void => {
784 const y: @typeof(x) = 120;784 const y: @typeof(x) = 120;
785 print_u64(@sizeof(@typeof(y)));785 stdout.print_u64(@sizeof(@typeof(y)));
786 print_str("\n");786 stdout.printf("\n");
787}787}
788 )SOURCE", "2\n");788 )SOURCE", "2\n");
789789
...@@ -798,9 +798,9 @@ struct Rand {...@@ -798,9 +798,9 @@ struct Rand {
798pub fn main(args: [][]u8) %void => {798pub fn main(args: [][]u8) %void => {
799 const r = Rand {.seed = 1234};799 const r = Rand {.seed = 1234};
800 if (r.get_seed() != 1234) {800 if (r.get_seed() != 1234) {
801 print_str("BAD seed\n");801 stdout.printf("BAD seed\n");
802 }802 }
803 print_str("OK\n");803 stdout.printf("OK\n");
804}804}
805 )SOURCE", "OK\n");805 )SOURCE", "OK\n");
806806
...@@ -814,12 +814,12 @@ pub fn main(args: [][]u8) %void => {...@@ -814,12 +814,12 @@ pub fn main(args: [][]u8) %void => {
814 *y += 1;814 *y += 1;
815815
816 if (x != 4) {816 if (x != 4) {
817 print_str("BAD\n");817 stdout.printf("BAD\n");
818 }818 }
819 if (*y != 4) {819 if (*y != 4) {
820 print_str("BAD\n");820 stdout.printf("BAD\n");
821 }821 }
822 print_str("OK\n");822 stdout.printf("OK\n");
823}823}
824 )SOURCE", "OK\n");824 )SOURCE", "OK\n");
825825
...@@ -830,77 +830,77 @@ const ARRAY_SIZE : i8 = 20;...@@ -830,77 +830,77 @@ const ARRAY_SIZE : i8 = 20;
830830
831pub fn main(args: [][]u8) %void => {831pub fn main(args: [][]u8) %void => {
832 var array : [ARRAY_SIZE]u8;832 var array : [ARRAY_SIZE]u8;
833 print_u64(@sizeof(@typeof(array)));833 stdout.print_u64(@sizeof(@typeof(array)));
834 print_str("\n");834 stdout.printf("\n");
835}835}
836 )SOURCE", "20\n");836 )SOURCE", "20\n");
837837
838 add_simple_case("@min_value() and @max_value()", R"SOURCE(838 add_simple_case("@min_value() and @max_value()", R"SOURCE(
839import "std.zig";839import "std.zig";
840pub fn main(args: [][]u8) %void => {840pub fn main(args: [][]u8) %void => {
841 print_str("max u8: ");841 stdout.printf("max u8: ");
842 print_u64(@max_value(u8));842 stdout.print_u64(@max_value(u8));
843 print_str("\n");843 stdout.printf("\n");
844844
845 print_str("max u16: ");845 stdout.printf("max u16: ");
846 print_u64(@max_value(u16));846 stdout.print_u64(@max_value(u16));
847 print_str("\n");847 stdout.printf("\n");
848848
849 print_str("max u32: ");849 stdout.printf("max u32: ");
850 print_u64(@max_value(u32));850 stdout.print_u64(@max_value(u32));
851 print_str("\n");851 stdout.printf("\n");
852852
853 print_str("max u64: ");853 stdout.printf("max u64: ");
854 print_u64(@max_value(u64));854 stdout.print_u64(@max_value(u64));
855 print_str("\n");855 stdout.printf("\n");
856856
857 print_str("max i8: ");857 stdout.printf("max i8: ");
858 print_i64(@max_value(i8));858 stdout.print_i64(@max_value(i8));
859 print_str("\n");859 stdout.printf("\n");
860860
861 print_str("max i16: ");861 stdout.printf("max i16: ");
862 print_i64(@max_value(i16));862 stdout.print_i64(@max_value(i16));
863 print_str("\n");863 stdout.printf("\n");
864864
865 print_str("max i32: ");865 stdout.printf("max i32: ");
866 print_i64(@max_value(i32));866 stdout.print_i64(@max_value(i32));
867 print_str("\n");867 stdout.printf("\n");
868868
869 print_str("max i64: ");869 stdout.printf("max i64: ");
870 print_i64(@max_value(i64));870 stdout.print_i64(@max_value(i64));
871 print_str("\n");871 stdout.printf("\n");
872872
873 print_str("min u8: ");873 stdout.printf("min u8: ");
874 print_u64(@min_value(u8));874 stdout.print_u64(@min_value(u8));
875 print_str("\n");875 stdout.printf("\n");
876876
877 print_str("min u16: ");877 stdout.printf("min u16: ");
878 print_u64(@min_value(u16));878 stdout.print_u64(@min_value(u16));
879 print_str("\n");879 stdout.printf("\n");
880880
881 print_str("min u32: ");881 stdout.printf("min u32: ");
882 print_u64(@min_value(u32));882 stdout.print_u64(@min_value(u32));
883 print_str("\n");883 stdout.printf("\n");
884884
885 print_str("min u64: ");885 stdout.printf("min u64: ");
886 print_u64(@min_value(u64));886 stdout.print_u64(@min_value(u64));
887 print_str("\n");887 stdout.printf("\n");
888888
889 print_str("min i8: ");889 stdout.printf("min i8: ");
890 print_i64(@min_value(i8));890 stdout.print_i64(@min_value(i8));
891 print_str("\n");891 stdout.printf("\n");
892892
893 print_str("min i16: ");893 stdout.printf("min i16: ");
894 print_i64(@min_value(i16));894 stdout.print_i64(@min_value(i16));
895 print_str("\n");895 stdout.printf("\n");
896896
897 print_str("min i32: ");897 stdout.printf("min i32: ");
898 print_i64(@min_value(i32));898 stdout.print_i64(@min_value(i32));
899 print_str("\n");899 stdout.printf("\n");
900900
901 print_str("min i64: ");901 stdout.printf("min i64: ");
902 print_i64(@min_value(i64));902 stdout.print_i64(@min_value(i64));
903 print_str("\n");903 stdout.printf("\n");
904}904}
905 )SOURCE",905 )SOURCE",
906 "max u8: 255\n"906 "max u8: 255\n"
...@@ -931,19 +931,19 @@ pub fn main(args: [][]u8) %void => {...@@ -931,19 +931,19 @@ pub fn main(args: [][]u8) %void => {
931 var slice = array[5...10];931 var slice = array[5...10];
932932
933 if (slice.len != 5) {933 if (slice.len != 5) {
934 print_str("BAD\n");934 stdout.printf("BAD\n");
935 }935 }
936936
937 if (slice.ptr[0] != 1234) {937 if (slice.ptr[0] != 1234) {
938 print_str("BAD\n");938 stdout.printf("BAD\n");
939 }939 }
940940
941 var slice_rest = array[10...];941 var slice_rest = array[10...];
942 if (slice_rest.len != 10) {942 if (slice_rest.len != 10) {
943 print_str("BAD\n");943 stdout.printf("BAD\n");
944 }944 }
945945
946 print_str("OK\n");946 stdout.printf("OK\n");
947}947}
948 )SOURCE", "OK\n");948 )SOURCE", "OK\n");
949949
...@@ -952,7 +952,7 @@ pub fn main(args: [][]u8) %void => {...@@ -952,7 +952,7 @@ pub fn main(args: [][]u8) %void => {
952import "std.zig";952import "std.zig";
953pub fn main(args: [][]u8) %void => {953pub fn main(args: [][]u8) %void => {
954 if (f(1) == 1) {954 if (f(1) == 1) {
955 print_str("OK\n");955 stdout.printf("OK\n");
956 }956 }
957}957}
958fn f(c: u8) u8 => {958fn f(c: u8) u8 => {
...@@ -971,15 +971,15 @@ import "std.zig";...@@ -971,15 +971,15 @@ import "std.zig";
971pub fn main(args: [][]u8) %void => {971pub fn main(args: [][]u8) %void => {
972 var result: u8;972 var result: u8;
973 if (!@add_with_overflow(u8, 250, 100, &result)) {973 if (!@add_with_overflow(u8, 250, 100, &result)) {
974 print_str("BAD\n");974 stdout.printf("BAD\n");
975 }975 }
976 if (@add_with_overflow(u8, 100, 150, &result)) {976 if (@add_with_overflow(u8, 100, 150, &result)) {
977 print_str("BAD\n");977 stdout.printf("BAD\n");
978 }978 }
979 if (result != 250) {979 if (result != 250) {
980 print_str("BAD\n");980 stdout.printf("BAD\n");
981 }981 }
982 print_str("OK\n");982 stdout.printf("OK\n");
983}983}
984 )SOURCE", "OK\n");984 )SOURCE", "OK\n");
985985
...@@ -993,10 +993,10 @@ pub fn main(args: [][]u8) %void => {...@@ -993,10 +993,10 @@ pub fn main(args: [][]u8) %void => {
993 @memcpy(bar.ptr, foo.ptr, bar.len);993 @memcpy(bar.ptr, foo.ptr, bar.len);
994994
995 if (bar[11] != 'A') {995 if (bar[11] != 'A') {
996 print_str("BAD\n");996 stdout.printf("BAD\n");
997 }997 }
998998
999 print_str("OK\n");999 stdout.printf("OK\n");
1000}1000}
1001 )SOURCE", "OK\n");1001 )SOURCE", "OK\n");
10021002
...@@ -1009,7 +1009,7 @@ pub fn main(args: [][]u8) %void => {...@@ -1009,7 +1009,7 @@ pub fn main(args: [][]u8) %void => {
1009 var x : i32 = print_ok(x);1009 var x : i32 = print_ok(x);
1010}1010}
1011fn print_ok(val: @typeof(x)) @typeof(foo) => {1011fn print_ok(val: @typeof(x)) @typeof(foo) => {
1012 print_str("OK\n");1012 stdout.printf("OK\n");
1013 return 0;1013 return 0;
1014}1014}
1015const foo : i32 = 0;1015const foo : i32 = 0;
...@@ -1042,25 +1042,25 @@ pub fn main(args: [][]u8) %void => {...@@ -1042,25 +1042,25 @@ pub fn main(args: [][]u8) %void => {
1042 const bar = Bar.B;1042 const bar = Bar.B;
10431043
1044 if (bar != Bar.B) {1044 if (bar != Bar.B) {
1045 print_str("BAD\n");1045 stdout.printf("BAD\n");
1046 }1046 }
10471047
1048 if (@member_count(Foo) != 3) {1048 if (@member_count(Foo) != 3) {
1049 print_str("BAD\n");1049 stdout.printf("BAD\n");
1050 }1050 }
10511051
1052 if (@member_count(Bar) != 4) {1052 if (@member_count(Bar) != 4) {
1053 print_str("BAD\n");1053 stdout.printf("BAD\n");
1054 }1054 }
10551055
1056 if (@sizeof(Foo) != 17) {1056 if (@sizeof(Foo) != 17) {
1057 print_str("BAD\n");1057 stdout.printf("BAD\n");
1058 }1058 }
1059 if (@sizeof(Bar) != 1) {1059 if (@sizeof(Bar) != 1) {
1060 print_str("BAD\n");1060 stdout.printf("BAD\n");
1061 }1061 }
10621062
1063 print_str("OK\n");1063 stdout.printf("OK\n");
1064}1064}
1065 )SOURCE", "OK\n");1065 )SOURCE", "OK\n");
10661066
...@@ -1071,14 +1071,14 @@ pub fn main(args: [][]u8) %void => {...@@ -1071,14 +1071,14 @@ pub fn main(args: [][]u8) %void => {
1071 const HEX_MULT = []u16{4096, 256, 16, 1};1071 const HEX_MULT = []u16{4096, 256, 16, 1};
10721072
1073 if (HEX_MULT.len != 4) {1073 if (HEX_MULT.len != 4) {
1074 print_str("BAD\n");1074 stdout.printf("BAD\n");
1075 }1075 }
10761076
1077 if (HEX_MULT[1] != 256) {1077 if (HEX_MULT[1] != 256) {
1078 print_str("BAD\n");1078 stdout.printf("BAD\n");
1079 }1079 }
10801080
1081 print_str("OK\n");1081 stdout.printf("OK\n");
1082}1082}
1083 )SOURCE", "OK\n");1083 )SOURCE", "OK\n");
10841084
...@@ -1089,8 +1089,8 @@ pub fn main(args: [][]u8) %void => {...@@ -1089,8 +1089,8 @@ pub fn main(args: [][]u8) %void => {
1089 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};1089 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
1090 var i: @typeof(array_of_strings.len) = 0;1090 var i: @typeof(array_of_strings.len) = 0;
1091 while (i < array_of_strings.len) {1091 while (i < array_of_strings.len) {
1092 print_str(array_of_strings[i]);1092 stdout.printf(array_of_strings[i]);
1093 print_str("\n");1093 stdout.printf("\n");
1094 i += 1;1094 i += 1;
1095 }1095 }
1096}1096}
...@@ -1102,21 +1102,21 @@ import "std.zig";...@@ -1102,21 +1102,21 @@ import "std.zig";
1102pub fn main(args: [][]u8) %void => {1102pub fn main(args: [][]u8) %void => {
1103 const array = []u8 {9, 8, 7, 6};1103 const array = []u8 {9, 8, 7, 6};
1104 for (item, array) {1104 for (item, array) {
1105 print_u64(item);1105 stdout.print_u64(item);
1106 print_str("\n");1106 stdout.printf("\n");
1107 }1107 }
1108 for (item, array, index) {1108 for (item, array, index) {
1109 print_i64(index);1109 stdout.print_i64(index);
1110 print_str("\n");1110 stdout.printf("\n");
1111 }1111 }
1112 const unknown_size: []u8 = array;1112 const unknown_size: []u8 = array;
1113 for (item, unknown_size) {1113 for (item, unknown_size) {
1114 print_u64(item);1114 stdout.print_u64(item);
1115 print_str("\n");1115 stdout.printf("\n");
1116 }1116 }
1117 for (item, unknown_size, index) {1117 for (item, unknown_size, index) {
1118 print_i64(index);1118 stdout.print_i64(index);
1119 print_str("\n");1119 stdout.printf("\n");
1120 }1120 }
1121}1121}
1122 )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n");1122 )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n");
...@@ -1127,8 +1127,8 @@ import "std.zig";...@@ -1127,8 +1127,8 @@ import "std.zig";
1127pub fn main(args: [][]u8) %void => {1127pub fn main(args: [][]u8) %void => {
1128 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };1128 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
1129 for (f, fns) {1129 for (f, fns) {
1130 print_u64(f());1130 stdout.print_u64(f());
1131 print_str("\n");1131 stdout.printf("\n");
1132 }1132 }
1133}1133}
11341134
...@@ -1157,10 +1157,10 @@ pub fn main(args: [][]u8) %void => {...@@ -1157,10 +1157,10 @@ pub fn main(args: [][]u8) %void => {
1157 Foo.D => 4,1157 Foo.D => 4,
1158 };1158 };
1159 if (val != 3) {1159 if (val != 3) {
1160 print_str("BAD\n");1160 stdout.printf("BAD\n");
1161 }1161 }
11621162
1163 print_str("OK\n");1163 stdout.printf("OK\n");
1164}1164}
1165 )SOURCE", "OK\n");1165 )SOURCE", "OK\n");
11661166
...@@ -1174,10 +1174,10 @@ pub fn main(args: [][]u8) %void => {...@@ -1174,10 +1174,10 @@ pub fn main(args: [][]u8) %void => {
1174 const eleven = ten + one;1174 const eleven = ten + one;
11751175
1176 if (eleven != 11) {1176 if (eleven != 11) {
1177 print_str("BAD\n");1177 stdout.printf("BAD\n");
1178 }1178 }
11791179
1180 print_str("OK\n");1180 stdout.printf("OK\n");
1181}1181}
1182 )SOURCE", "OK\n");1182 )SOURCE", "OK\n");
11831183
...@@ -1191,10 +1191,10 @@ var foo = Foo { .x = 13, .y = true, };...@@ -1191,10 +1191,10 @@ var foo = Foo { .x = 13, .y = true, };
1191pub fn main(args: [][]u8) %void => {1191pub fn main(args: [][]u8) %void => {
1192 foo.x += 1;1192 foo.x += 1;
1193 if (foo.x != 14) {1193 if (foo.x != 14) {
1194 print_str("BAD\n");1194 stdout.printf("BAD\n");
1195 }1195 }
11961196
1197 print_str("OK\n");1197 stdout.printf("OK\n");
1198}1198}
1199 )SOURCE", "OK\n");1199 )SOURCE", "OK\n");
12001200
...@@ -1204,10 +1204,10 @@ const x = []u8{1,2,3,4};...@@ -1204,10 +1204,10 @@ const x = []u8{1,2,3,4};
1204pub fn main(args: [][]u8) %void => {1204pub fn main(args: [][]u8) %void => {
1205 const y : [4]u8 = x;1205 const y : [4]u8 = x;
1206 if (y[3] != 4) {1206 if (y[3] != 4) {
1207 print_str("BAD\n");1207 stdout.printf("BAD\n");
1208 }1208 }
12091209
1210 print_str("OK\n");1210 stdout.printf("OK\n");
1211}1211}
1212 )SOURCE", "OK\n");1212 )SOURCE", "OK\n");
12131213
...@@ -1219,10 +1219,10 @@ pub fn main(args: [][]u8) %void => {...@@ -1219,10 +1219,10 @@ pub fn main(args: [][]u8) %void => {
1219 const a = i32(error.err1);1219 const a = i32(error.err1);
1220 const b = i32(error.err2);1220 const b = i32(error.err2);
1221 if (a == b) {1221 if (a == b) {
1222 print_str("BAD\n");1222 stdout.printf("BAD\n");
1223 }1223 }
12241224
1225 print_str("OK\n");1225 stdout.printf("OK\n");
1226}1226}
1227 )SOURCE", "OK\n");1227 )SOURCE", "OK\n");
12281228
...@@ -1230,7 +1230,7 @@ pub fn main(args: [][]u8) %void => {...@@ -1230,7 +1230,7 @@ pub fn main(args: [][]u8) %void => {
1230import "std.zig";1230import "std.zig";
1231pub fn main(args: [][]u8) %void => {1231pub fn main(args: [][]u8) %void => {
1232 while (true) {1232 while (true) {
1233 print_str("OK\n");1233 stdout.printf("OK\n");
1234 return;1234 return;
1235 }1235 }
1236}1236}
...@@ -1251,9 +1251,9 @@ fn make_foo(x: i32, y: i32) Foo => {...@@ -1251,9 +1251,9 @@ fn make_foo(x: i32, y: i32) Foo => {
1251pub fn main(args: [][]u8) %void => {1251pub fn main(args: [][]u8) %void => {
1252 const foo = make_foo(1234, 5678);1252 const foo = make_foo(1234, 5678);
1253 if (foo.y != 5678) {1253 if (foo.y != 5678) {
1254 print_str("BAD\n");1254 stdout.printf("BAD\n");
1255 }1255 }
1256 print_str("OK\n");1256 stdout.printf("OK\n");
1257}1257}
1258 )SOURCE", "OK\n");1258 )SOURCE", "OK\n");
1259}1259}