authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-23 02:14:01-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-23 02:14:01-07:00
logc0ea9290c4576f2111c8fc6b2d448f278effd80e
treec4b789b675b0083b3d9c2e32550a0299f2546cf5
parent91d911007b8a12405c084cff53237ac26b1f2c5f

main returns %void


8 files changed, 254 insertions(+), 223 deletions(-)

example/cat/main.zig+1-4
......@@ -4,14 +4,11 @@ import "std.zig";
44
55// Things to do to make this work:
66// * var args printing
7// * %void type
7// * update std API
88// * defer
99// * %return
1010// * %% operator
11// * make main return %void
12// * how to reference error values %.Invalid
1311// * cast err type to string
14// * update std API
1512
1613pub %.Invalid;
1714
example/hello_world/hello.zig+1-2
......@@ -2,8 +2,7 @@ export executable "hello";
22
33import "std.zig";
44
5pub fn main(args: [][]u8) i32 => {
5pub fn main(args: [][]u8) %void => {
66 //stderr.print_str("Hello, world!\n");
77 print_str("Hello, world!\n");
8 return 0;
98}
example/hello_world/hello_libc.zig+1-1
......@@ -5,7 +5,7 @@ extern {
55 fn printf(__format: &const u8, ...) i32;
66}
77
8export fn main(argc: i32, argv: &&u8, env: &&u8) i32 => {
8export fn main(argc: i32, argv: &&u8) i32 => {
99 printf(c"Hello, world!\n");
1010 return 0;
1111}
src/all_types.hpp+7-1
......@@ -60,6 +60,11 @@ struct ConstPtrValue {
6060 uint64_t len;
6161};
6262
63struct ConstErrValue {
64 ErrorTableEntry *err;
65 ConstExprValue *payload;
66};
67
6368struct ConstExprValue {
6469 bool ok; // true if constant expression evalution worked
6570 bool depends_on_compile_var;
......@@ -70,8 +75,8 @@ struct ConstExprValue {
7075 bool x_bool;
7176 FnTableEntry *x_fn;
7277 TypeTableEntry *x_type;
73 ErrorTableEntry *x_err;
7478 ConstExprValue *x_maybe;
79 ConstErrValue x_err;
7580 ConstEnumValue x_enum;
7681 ConstStructValue x_struct;
7782 ConstArrayValue x_array;
......@@ -309,6 +314,7 @@ enum CastOp {
309314 CastOpIntWidenOrShorten,
310315 CastOpToUnknownSizeArray,
311316 CastOpMaybeWrap,
317 CastOpErrorWrap,
312318 CastOpPointerReinterpret,
313319 CastOpErrToInt,
314320};
src/analyze.cpp+150-95
......@@ -1130,6 +1130,61 @@ static bool num_lit_fits_in_other_type(CodeGen *g, AstNode *literal_node, TypeTa
11301130 return false;
11311131}
11321132
1133static bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *actual_type) {
1134 if (expected_type == actual_type)
1135 return true;
1136
1137 // pointer const
1138 if (expected_type->id == TypeTableEntryIdPointer &&
1139 actual_type->id == TypeTableEntryIdPointer &&
1140 (!actual_type->data.pointer.is_const || expected_type->data.pointer.is_const))
1141 {
1142 return types_match_const_cast_only(expected_type->data.pointer.child_type,
1143 actual_type->data.pointer.child_type);
1144 }
1145
1146 // unknown size array const
1147 if (expected_type->id == TypeTableEntryIdStruct &&
1148 actual_type->id == TypeTableEntryIdStruct &&
1149 expected_type->data.structure.is_unknown_size_array &&
1150 actual_type->data.structure.is_unknown_size_array &&
1151 (!actual_type->data.structure.fields[0].type_entry->data.pointer.is_const ||
1152 expected_type->data.structure.fields[0].type_entry->data.pointer.is_const))
1153 {
1154 return types_match_const_cast_only(
1155 expected_type->data.structure.fields[0].type_entry->data.pointer.child_type,
1156 actual_type->data.structure.fields[0].type_entry->data.pointer.child_type);
1157 }
1158
1159 // maybe
1160 if (expected_type->id == TypeTableEntryIdMaybe &&
1161 actual_type->id == TypeTableEntryIdMaybe)
1162 {
1163 return types_match_const_cast_only(
1164 expected_type->data.maybe.child_type,
1165 actual_type->data.maybe.child_type);
1166 }
1167
1168 // error
1169 if (expected_type->id == TypeTableEntryIdError &&
1170 actual_type->id == TypeTableEntryIdError)
1171 {
1172 return types_match_const_cast_only(
1173 expected_type->data.error.child_type,
1174 actual_type->data.error.child_type);
1175 }
1176
1177 // fn
1178 if (expected_type->id == TypeTableEntryIdFn &&
1179 actual_type->id == TypeTableEntryIdFn)
1180 {
1181 zig_panic("TODO types_match_const_cast_only for fns");
1182 }
1183
1184
1185 return false;
1186}
1187
11331188static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *parent_source_node,
11341189 AstNode **child_nodes, TypeTableEntry **child_types, int child_count)
11351190{
......@@ -1143,6 +1198,12 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa
11431198 AstNode *cur_node = child_nodes[i];
11441199 if (cur_type->id == TypeTableEntryIdInvalid) {
11451200 return cur_type;
1201 } else if (types_match_const_cast_only(prev_type, cur_type)) {
1202 continue;
1203 } else if (types_match_const_cast_only(cur_type, prev_type)) {
1204 prev_type = cur_type;
1205 prev_node = cur_node;
1206 continue;
11461207 } else if (prev_type->id == TypeTableEntryIdUnreachable) {
11471208 prev_type = cur_type;
11481209 prev_node = cur_node;
......@@ -1163,6 +1224,16 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa
11631224 prev_type = cur_type;
11641225 prev_node = cur_node;
11651226 }
1227 } else if (prev_type->id == TypeTableEntryIdError &&
1228 types_match_const_cast_only(prev_type->data.error.child_type, cur_type))
1229 {
1230 continue;
1231 } else if (cur_type->id == TypeTableEntryIdError &&
1232 types_match_const_cast_only(cur_type->data.error.child_type, prev_type))
1233 {
1234 prev_type = cur_type;
1235 prev_node = cur_node;
1236 continue;
11661237 } else if (prev_type->id == TypeTableEntryIdNumLitFloat &&
11671238 cur_type->id == TypeTableEntryIdNumLitFloat)
11681239 {
......@@ -1189,8 +1260,6 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa
11891260 } else {
11901261 return g->builtin_types.entry_invalid;
11911262 }
1192 } else if (prev_type == cur_type) {
1193 continue;
11941263 } else {
11951264 add_node_error(g, parent_source_node,
11961265 buf_sprintf("incompatible types: '%s' and '%s'",
......@@ -1202,61 +1271,6 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa
12021271 return prev_type;
12031272}
12041273
1205static bool types_match_const_cast_only(TypeTableEntry *expected_type, TypeTableEntry *actual_type) {
1206 if (expected_type == actual_type)
1207 return true;
1208
1209 // pointer const
1210 if (expected_type->id == TypeTableEntryIdPointer &&
1211 actual_type->id == TypeTableEntryIdPointer &&
1212 (!actual_type->data.pointer.is_const || expected_type->data.pointer.is_const))
1213 {
1214 return types_match_const_cast_only(expected_type->data.pointer.child_type,
1215 actual_type->data.pointer.child_type);
1216 }
1217
1218 // unknown size array const
1219 if (expected_type->id == TypeTableEntryIdStruct &&
1220 actual_type->id == TypeTableEntryIdStruct &&
1221 expected_type->data.structure.is_unknown_size_array &&
1222 actual_type->data.structure.is_unknown_size_array &&
1223 (!actual_type->data.structure.fields[0].type_entry->data.pointer.is_const ||
1224 expected_type->data.structure.fields[0].type_entry->data.pointer.is_const))
1225 {
1226 return types_match_const_cast_only(
1227 expected_type->data.structure.fields[0].type_entry->data.pointer.child_type,
1228 actual_type->data.structure.fields[0].type_entry->data.pointer.child_type);
1229 }
1230
1231 // maybe
1232 if (expected_type->id == TypeTableEntryIdMaybe &&
1233 actual_type->id == TypeTableEntryIdMaybe)
1234 {
1235 return types_match_const_cast_only(
1236 expected_type->data.maybe.child_type,
1237 actual_type->data.maybe.child_type);
1238 }
1239
1240 // error
1241 if (expected_type->id == TypeTableEntryIdError &&
1242 actual_type->id == TypeTableEntryIdError)
1243 {
1244 return types_match_const_cast_only(
1245 expected_type->data.error.child_type,
1246 actual_type->data.error.child_type);
1247 }
1248
1249 // fn
1250 if (expected_type->id == TypeTableEntryIdFn &&
1251 actual_type->id == TypeTableEntryIdFn)
1252 {
1253 zig_panic("TODO types_match_const_cast_only for fns");
1254 }
1255
1256
1257 return false;
1258}
1259
12601274static bool types_match_with_implicit_cast(CodeGen *g, TypeTableEntry *expected_type,
12611275 TypeTableEntry *actual_type, AstNode *literal_node, bool *reported_err)
12621276{
......@@ -1272,6 +1286,14 @@ static bool types_match_with_implicit_cast(CodeGen *g, TypeTableEntry *expected_
12721286 return true;
12731287 }
12741288
1289 // implicit conversion from error child type to error type
1290 if (expected_type->id == TypeTableEntryIdError &&
1291 types_match_with_implicit_cast(g, expected_type->data.error.child_type, actual_type,
1292 literal_node, reported_err))
1293 {
1294 return true;
1295 }
1296
12751297 // implicit widening conversion
12761298 if (expected_type->id == TypeTableEntryIdInt &&
12771299 actual_type->id == TypeTableEntryIdInt &&
......@@ -1381,9 +1403,10 @@ static TypeTableEntry *resolve_peer_type_compatibility(CodeGen *g, ImportTableEn
13811403 if (!child_nodes[i]) {
13821404 continue;
13831405 }
1384 Expr *expr = get_resolved_expr(child_nodes[i]);
1406 AstNode **child_node = child_nodes[i]->parent_field;
13851407 TypeTableEntry *resolved_type = resolve_type_compatibility(g, import, block_context,
1386 child_nodes[i], expected_type, child_types[i]);
1408 *child_node, expected_type, child_types[i]);
1409 Expr *expr = get_resolved_expr(*child_node);
13871410 expr->type_entry = resolved_type;
13881411 add_global_const_expr(g, expr);
13891412 }
......@@ -1812,7 +1835,7 @@ static TypeTableEntry *resolve_expr_const_val_as_fn(CodeGen *g, AstNode *node, F
18121835static TypeTableEntry *resolve_expr_const_val_as_err(CodeGen *g, AstNode *node, ErrorTableEntry *err) {
18131836 Expr *expr = get_resolved_expr(node);
18141837 expr->const_val.ok = true;
1815 expr->const_val.data.x_err = err;
1838 expr->const_val.data.x_err.err = err;
18161839 return get_error_type(g, g->builtin_types.entry_void);
18171840}
18181841
......@@ -2868,10 +2891,18 @@ static void eval_const_expr_implicit_cast(CodeGen *g, AstNode *node, AstNode *ex
28682891 const_val->data.x_maybe = other_val;
28692892 const_val->ok = true;
28702893 break;
2871 case CastOpErrToInt:
2872 bignum_init_unsigned(&const_val->data.x_bignum, other_val->data.x_err->value);
2894 case CastOpErrorWrap:
2895 const_val->data.x_err.err = nullptr;
2896 const_val->data.x_err.payload = other_val;
28732897 const_val->ok = true;
28742898 break;
2899 case CastOpErrToInt:
2900 {
2901 uint64_t value = other_val->data.x_err.err ? other_val->data.x_err.err->value : 0;
2902 bignum_init_unsigned(&const_val->data.x_bignum, value);
2903 const_val->ok = true;
2904 break;
2905 }
28752906 }
28762907}
28772908
......@@ -2965,6 +2996,25 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B
29652996 }
29662997 }
29672998
2999 // explicit cast from child type of error type to error type
3000 if (wanted_type->id == TypeTableEntryIdError) {
3001 if (types_match_const_cast_only(wanted_type->data.error.child_type, actual_type)) {
3002 node->data.fn_call_expr.cast_op = CastOpErrorWrap;
3003 eval_const_expr_implicit_cast(g, node, expr_node);
3004 return wanted_type;
3005 } else if (actual_type->id == TypeTableEntryIdNumLitInt ||
3006 actual_type->id == TypeTableEntryIdNumLitFloat)
3007 {
3008 if (num_lit_fits_in_other_type(g, expr_node, wanted_type->data.error.child_type)) {
3009 node->data.fn_call_expr.cast_op = CastOpErrorWrap;
3010 eval_const_expr_implicit_cast(g, node, expr_node);
3011 return wanted_type;
3012 } else {
3013 return g->builtin_types.entry_invalid;
3014 }
3015 }
3016 }
3017
29683018 // explicit cast from number literal to another type
29693019 if (actual_type->id == TypeTableEntryIdNumLitFloat ||
29703020 actual_type->id == TypeTableEntryIdNumLitInt)
......@@ -3579,6 +3629,42 @@ static TypeTableEntry *analyze_string_literal_expr(CodeGen *g, ImportTableEntry
35793629 }
35803630}
35813631
3632static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
3633 TypeTableEntry *expected_type, AstNode *node)
3634{
3635 BlockContext *child_context = new_block_context(node, context);
3636 node->data.block.block_context = child_context;
3637 TypeTableEntry *return_type = g->builtin_types.entry_void;
3638
3639 for (int i = 0; i < node->data.block.statements.length; i += 1) {
3640 AstNode *child = node->data.block.statements.at(i);
3641 if (child->type == NodeTypeLabel) {
3642 LabelTableEntry *label_entry = child->data.label.label_entry;
3643 assert(label_entry);
3644 label_entry->entered_from_fallthrough = (return_type->id != TypeTableEntryIdUnreachable);
3645 return_type = g->builtin_types.entry_void;
3646 continue;
3647 }
3648 if (return_type->id == TypeTableEntryIdUnreachable) {
3649 if (is_node_void_expr(child)) {
3650 // {unreachable;void;void} is allowed.
3651 // ignore void statements once we enter unreachable land.
3652 analyze_expression(g, import, context, g->builtin_types.entry_void, child);
3653 continue;
3654 }
3655 add_node_error(g, first_executing_node(child), buf_sprintf("unreachable code"));
3656 break;
3657 }
3658 bool is_last = (i == node->data.block.statements.length - 1);
3659 TypeTableEntry *passed_expected_type = is_last ? expected_type : nullptr;
3660 return_type = analyze_expression(g, import, child_context, passed_expected_type, child);
3661 if (!is_last && return_type->id == TypeTableEntryIdMetaType) {
3662 add_node_error(g, child, buf_sprintf("expected expression, found type"));
3663 }
3664 }
3665 return return_type;
3666}
3667
35823668// When you call analyze_expression, the node you pass might no longer be the child node
35833669// you thought it was due to implicit casting rewriting the AST.
35843670static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import, BlockContext *context,
......@@ -3587,39 +3673,8 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import,
35873673 TypeTableEntry *return_type = nullptr;
35883674 switch (node->type) {
35893675 case NodeTypeBlock:
3590 {
3591 BlockContext *child_context = new_block_context(node, context);
3592 node->data.block.block_context = child_context;
3593 return_type = g->builtin_types.entry_void;
3594
3595 for (int i = 0; i < node->data.block.statements.length; i += 1) {
3596 AstNode *child = node->data.block.statements.at(i);
3597 if (child->type == NodeTypeLabel) {
3598 LabelTableEntry *label_entry = child->data.label.label_entry;
3599 assert(label_entry);
3600 label_entry->entered_from_fallthrough = (return_type->id != TypeTableEntryIdUnreachable);
3601 return_type = g->builtin_types.entry_void;
3602 continue;
3603 }
3604 if (return_type->id == TypeTableEntryIdUnreachable) {
3605 if (is_node_void_expr(child)) {
3606 // {unreachable;void;void} is allowed.
3607 // ignore void statements once we enter unreachable land.
3608 analyze_expression(g, import, context, g->builtin_types.entry_void, child);
3609 continue;
3610 }
3611 add_node_error(g, first_executing_node(child), buf_sprintf("unreachable code"));
3612 break;
3613 }
3614 bool is_last = (i == node->data.block.statements.length - 1);
3615 TypeTableEntry *passed_expected_type = is_last ? expected_type : nullptr;
3616 return_type = analyze_expression(g, import, child_context, passed_expected_type, child);
3617 if (!is_last && return_type->id == TypeTableEntryIdMetaType) {
3618 add_node_error(g, child, buf_sprintf("expected expression, found type"));
3619 }
3620 }
3621 break;
3622 }
3676 return_type = analyze_block_expr(g, import, context, expected_type, node);
3677 break;
36233678
36243679 case NodeTypeReturnExpr:
36253680 return_type = analyze_return_expr(g, import, context, expected_type, node);
src/codegen.cpp+38-19
......@@ -268,6 +268,26 @@ static LLVMValueRef gen_enum_value_expr(CodeGen *g, AstNode *node, TypeTableEntr
268268 }
269269}
270270
271static LLVMValueRef gen_widen_or_shorten(CodeGen *g, AstNode *source_node, TypeTableEntry *actual_type,
272 TypeTableEntry *wanted_type, LLVMValueRef expr_val)
273{
274 if (actual_type->size_in_bits == wanted_type->size_in_bits) {
275 return expr_val;
276 } else if (actual_type->size_in_bits < wanted_type->size_in_bits) {
277 if (actual_type->data.integral.is_signed) {
278 add_debug_source_node(g, source_node);
279 return LLVMBuildSExt(g->builder, expr_val, wanted_type->type_ref, "");
280 } else {
281 add_debug_source_node(g, source_node);
282 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
283 }
284 } else {
285 assert(actual_type->size_in_bits > wanted_type->size_in_bits);
286 add_debug_source_node(g, source_node);
287 return LLVMBuildTrunc(g->builder, expr_val, wanted_type->type_ref, "");
288 }
289}
290
271291static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
272292 assert(node->type == NodeTypeFnCallExpr);
273293
......@@ -288,7 +308,7 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
288308 case CastOpErrToInt:
289309 assert(actual_type->id == TypeTableEntryIdError);
290310 if (actual_type->data.error.child_type->size_in_bits == 0) {
291 return expr_val;
311 return gen_widen_or_shorten(g, node, g->err_tag_type, wanted_type, expr_val);
292312 } else {
293313 zig_panic("TODO");
294314 }
......@@ -309,6 +329,13 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
309329
310330 return cast_expr->tmp_ptr;
311331 }
332 case CastOpErrorWrap:
333 assert(wanted_type->id == TypeTableEntryIdError);
334 if (wanted_type->data.error.child_type->size_in_bits == 0) {
335 return LLVMConstNull(g->err_tag_type->type_ref);
336 } else {
337 zig_panic("TODO");
338 }
312339 case CastOpPtrToInt:
313340 add_debug_source_node(g, node);
314341 return LLVMBuildPtrToInt(g->builder, expr_val, wanted_type->type_ref, "");
......@@ -316,21 +343,7 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
316343 add_debug_source_node(g, node);
317344 return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, "");
318345 case CastOpIntWidenOrShorten:
319 if (actual_type->size_in_bits == wanted_type->size_in_bits) {
320 return expr_val;
321 } else if (actual_type->size_in_bits < wanted_type->size_in_bits) {
322 if (actual_type->data.integral.is_signed) {
323 add_debug_source_node(g, node);
324 return LLVMBuildSExt(g->builder, expr_val, wanted_type->type_ref, "");
325 } else {
326 add_debug_source_node(g, node);
327 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
328 }
329 } else {
330 assert(actual_type->size_in_bits > wanted_type->size_in_bits);
331 add_debug_source_node(g, node);
332 return LLVMBuildTrunc(g->builder, expr_val, wanted_type->type_ref, "");
333 }
346 return gen_widen_or_shorten(g, node, actual_type, wanted_type, expr_val);
334347 case CastOpToUnknownSizeArray:
335348 {
336349 assert(cast_expr->tmp_ptr);
......@@ -1279,7 +1292,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV
12791292 return nullptr;
12801293 }
12811294
1282 assert(!use_expr_value);
1295 assert(!use_expr_value || then_type->id == TypeTableEntryIdError);
12831296
12841297 LLVMBasicBlockRef then_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Then");
12851298 LLVMBasicBlockRef endif_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "EndIf");
......@@ -1292,7 +1305,12 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV
12921305 LLVMBuildBr(g->builder, endif_block);
12931306
12941307 LLVMPositionBuilderAtEnd(g->builder, endif_block);
1295 return nullptr;
1308
1309 if (use_expr_value) {
1310 return LLVMConstNull(g->err_tag_type->type_ref);
1311 } else {
1312 return nullptr;
1313 }
12961314}
12971315
12981316static LLVMValueRef gen_if_bool_expr(CodeGen *g, AstNode *node) {
......@@ -2132,7 +2150,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE
21322150 }
21332151 } else if (type_entry->id == TypeTableEntryIdError) {
21342152 if (type_entry->data.error.child_type->size_in_bits == 0) {
2135 return LLVMConstInt(g->err_tag_type->type_ref, const_val->data.x_err->value, false);
2153 uint64_t value = const_val->data.x_err.err ? const_val->data.x_err.err->value : 0;
2154 return LLVMConstInt(g->err_tag_type->type_ref, value, false);
21362155 } else {
21372156 zig_panic("TODO");
21382157 }
std/bootstrap.zig+4-1
......@@ -29,5 +29,8 @@ fn call_main() unreachable => {
2929 const ptr = argv[i];
3030 args[i] = ptr[0...strlen(ptr)];
3131 }
32 exit(main(args))
32 // TODO: replace the i32 cast with:
33 // main(args) %% exit(1)
34 // exit(0)
35 exit(i32(main(args)))
3336}
test/run_tests.cpp+52-100
......@@ -114,7 +114,7 @@ import "syscall.zig";
114114fn empty_function_1() => {}
115115fn empty_function_2() => { return; }
116116
117pub fn main(args: [][]u8) i32 => {
117pub fn main(args: [][]u8) %void => {
118118 empty_function_1();
119119 empty_function_2();
120120 this_is_a_function();
......@@ -136,9 +136,8 @@ fn another_function() => {}
136136
137137/// this is a documentation comment
138138/// doc comment line 2
139pub fn main(args: [][]u8) i32 => {
139pub fn main(args: [][]u8) %void => {
140140 print_str(/* mid-line comment /* nested */ */ "OK\n");
141 return 0;
142141}
143142 )SOURCE", "OK\n");
144143
......@@ -147,10 +146,9 @@ pub fn main(args: [][]u8) i32 => {
147146import "std.zig";
148147import "foo.zig";
149148
150pub fn main(args: [][]u8) i32 => {
149pub fn main(args: [][]u8) %void => {
151150 private_function();
152151 print_str("OK 2\n");
153 return 0;
154152}
155153
156154fn private_function() => {
......@@ -178,10 +176,9 @@ pub fn print_text() => {
178176import "foo.zig";
179177import "bar.zig";
180178
181pub fn main(args: [][]u8) i32 => {
179pub fn main(args: [][]u8) %void => {
182180 foo_function();
183181 bar_function();
184 return 0;
185182}
186183 )SOURCE", "OK\nOK\n");
187184
......@@ -214,7 +211,7 @@ pub fn foo_function() bool => {
214211 add_simple_case("if statements", R"SOURCE(
215212import "std.zig";
216213
217pub fn main(args: [][]u8) i32 => {
214pub fn main(args: [][]u8) %void => {
218215 if (1 != 0) {
219216 print_str("1 is true\n");
220217 } else {
......@@ -228,7 +225,6 @@ pub fn main(args: [][]u8) i32 => {
228225 if (!(0 != 0)) {
229226 print_str("!0 is true\n");
230227 }
231 return 0;
232228}
233229 )SOURCE", "1 is true\n!0 is true\n");
234230
......@@ -239,11 +235,10 @@ fn add(a: i32, b: i32) i32 => {
239235 a + b
240236}
241237
242pub fn main(args: [][]u8) i32 => {
238pub fn main(args: [][]u8) %void => {
243239 if (add(22, 11) == 33) {
244240 print_str("pass\n");
245241 }
246 return 0;
247242}
248243 )SOURCE", "pass\n");
249244
......@@ -261,41 +256,38 @@ done:
261256 return;
262257}
263258
264pub fn main(args: [][]u8) i32 => {
259pub fn main(args: [][]u8) %void => {
265260 loop(3);
266 return 0;
267261}
268262 )SOURCE", "loop\nloop\nloop\n");
269263
270264 add_simple_case("local variables", R"SOURCE(
271265import "std.zig";
272266
273pub fn main(args: [][]u8) i32 => {
267pub fn main(args: [][]u8) %void => {
274268 const a : i32 = 1;
275269 const b = i32(2);
276270 if (a + b == 3) {
277271 print_str("OK\n");
278272 }
279 return 0;
280273}
281274 )SOURCE", "OK\n");
282275
283276 add_simple_case("bool literals", R"SOURCE(
284277import "std.zig";
285278
286pub fn main(args: [][]u8) i32 => {
279pub fn main(args: [][]u8) %void => {
287280 if (true) { print_str("OK 1\n"); }
288281 if (false) { print_str("BAD 1\n"); }
289282 if (!true) { print_str("BAD 2\n"); }
290283 if (!false) { print_str("OK 2\n"); }
291 return 0;
292284}
293285 )SOURCE", "OK 1\nOK 2\n");
294286
295287 add_simple_case("separate block scopes", R"SOURCE(
296288import "std.zig";
297289
298pub fn main(args: [][]u8) i32 => {
290pub fn main(args: [][]u8) %void => {
299291 if (true) {
300292 const no_conflict : i32 = 5;
301293 if (no_conflict == 5) { print_str("OK 1\n"); }
......@@ -306,16 +298,14 @@ pub fn main(args: [][]u8) i32 => {
306298 no_conflict
307299 };
308300 if (c == 10) { print_str("OK 2\n"); }
309 return 0;
310301}
311302 )SOURCE", "OK 1\nOK 2\n");
312303
313304 add_simple_case("void parameters", R"SOURCE(
314305import "std.zig";
315306
316pub fn main(args: [][]u8) i32 => {
307pub fn main(args: [][]u8) %void => {
317308 void_fun(1, void{}, 2);
318 return 0;
319309}
320310
321311fn void_fun(a : i32, b : void, c : i32) => {
......@@ -333,7 +323,7 @@ struct Foo {
333323 b : i32,
334324 c : void,
335325}
336pub fn main(args: [][]u8) i32 => {
326pub fn main(args: [][]u8) %void => {
337327 const foo = Foo {
338328 .a = void{},
339329 .b = 1,
......@@ -346,7 +336,6 @@ pub fn main(args: [][]u8) i32 => {
346336 print_str("BAD\n");
347337 }
348338 print_str("OK\n");
349 return 0;
350339}
351340
352341 )SOURCE", "OK\n");
......@@ -354,7 +343,7 @@ pub fn main(args: [][]u8) i32 => {
354343 add_simple_case("void arrays", R"SOURCE(
355344import "std.zig";
356345
357pub fn main(args: [][]u8) i32 => {
346pub fn main(args: [][]u8) %void => {
358347 var array: [4]void;
359348 array[0] = void{};
360349 array[1] = array[2];
......@@ -365,7 +354,6 @@ pub fn main(args: [][]u8) i32 => {
365354 print_str("BAD\n");
366355 }
367356 print_str("OK\n");
368 return 0;
369357}
370358 )SOURCE", "OK\n");
371359
......@@ -373,27 +361,22 @@ pub fn main(args: [][]u8) i32 => {
373361 add_simple_case("mutable local variables", R"SOURCE(
374362import "std.zig";
375363
376pub fn main(args: [][]u8) i32 => {
364pub fn main(args: [][]u8) %void => {
377365 var zero : i32 = 0;
378366 if (zero == 0) { print_str("zero\n"); }
379367
380368 var i = i32(0);
381loop_start:
382 if (i == 3) {
383 goto done;
369 while (i != 3) {
370 print_str("loop\n");
371 i += 1;
384372 }
385 print_str("loop\n");
386 i = i + 1;
387 goto loop_start;
388done:
389 return 0;
390373}
391374 )SOURCE", "zero\nloop\nloop\nloop\n");
392375
393376 add_simple_case("arrays", R"SOURCE(
394377import "std.zig";
395378
396pub fn main(args: [][]u8) i32 => {
379pub fn main(args: [][]u8) %void => {
397380 var array : [5]i32;
398381
399382 var i : i32 = 0;
......@@ -417,8 +400,6 @@ pub fn main(args: [][]u8) i32 => {
417400 if (get_array_len(array) != 5) {
418401 print_str("BAD\n");
419402 }
420
421 return 0;
422403}
423404fn get_array_len(a: []i32) isize => {
424405 a.len
......@@ -429,9 +410,8 @@ fn get_array_len(a: []i32) isize => {
429410 add_simple_case("hello world without libc", R"SOURCE(
430411import "std.zig";
431412
432pub fn main(args: [][]u8) i32 => {
413pub fn main(args: [][]u8) %void => {
433414 print_str("Hello, world!\n");
434 return 0;
435415}
436416 )SOURCE", "Hello, world!\n");
437417
......@@ -439,7 +419,7 @@ pub fn main(args: [][]u8) i32 => {
439419 add_simple_case("a + b + c", R"SOURCE(
440420import "std.zig";
441421
442pub fn main(args: [][]u8) i32 => {
422pub fn main(args: [][]u8) %void => {
443423 if (false || false || false) { print_str("BAD 1\n"); }
444424 if (true && true && false) { print_str("BAD 2\n"); }
445425 if (1 | 2 | 4 != 7) { print_str("BAD 3\n"); }
......@@ -454,14 +434,13 @@ pub fn main(args: [][]u8) i32 => {
454434 if (i32(7) != --(i32(7))) { print_str("BAD 12\n"); }
455435
456436 print_str("OK\n");
457 return 0;
458437}
459438 )SOURCE", "OK\n");
460439
461440 add_simple_case("short circuit", R"SOURCE(
462441import "std.zig";
463442
464pub fn main(args: [][]u8) i32 => {
443pub fn main(args: [][]u8) %void => {
465444 if (true || { print_str("BAD 1\n"); false }) {
466445 print_str("OK 1\n");
467446 }
......@@ -476,15 +455,13 @@ pub fn main(args: [][]u8) i32 => {
476455 } else {
477456 print_str("OK 4\n");
478457 }
479
480 return 0;
481458}
482459 )SOURCE", "OK 1\nOK 2\nOK 3\nOK 4\n");
483460
484461 add_simple_case("modify operators", R"SOURCE(
485462import "std.zig";
486463
487pub fn main(args: [][]u8) i32 => {
464pub fn main(args: [][]u8) %void => {
488465 var i : i32 = 0;
489466 i += 5; if (i != 5) { print_str("BAD +=\n"); }
490467 i -= 2; if (i != 3) { print_str("BAD -=\n"); }
......@@ -500,7 +477,6 @@ pub fn main(args: [][]u8) i32 => {
500477 i |= 3; if (i != 7) { print_str("BAD |=\n"); }
501478
502479 print_str("OK\n");
503 return 0;
504480}
505481 )SOURCE", "OK\n");
506482
......@@ -636,7 +612,7 @@ export fn main(argc: i32, argv: &&u8) i32 => {
636612 add_simple_case("structs", R"SOURCE(
637613import "std.zig";
638614
639pub fn main(args: [][]u8) i32 => {
615pub fn main(args: [][]u8) %void => {
640616 var foo : Foo;
641617 @memset(&foo, 0, @sizeof(Foo));
642618 foo.a += 1;
......@@ -650,7 +626,6 @@ pub fn main(args: [][]u8) i32 => {
650626 test_byval_assign();
651627 test_initializer();
652628 print_str("OK\n");
653 return 0;
654629}
655630struct Foo {
656631 a : i32,
......@@ -711,23 +686,25 @@ import "std.zig";
711686const g1 : i32 = 1233 + 1;
712687var g2 : i32 = 0;
713688
714pub fn main(args: [][]u8) i32 => {
689pub fn main(args: [][]u8) %void => {
715690 if (g2 != 0) { print_str("BAD\n"); }
716691 g2 = g1;
717692 if (g2 != 1234) { print_str("BAD\n"); }
718693 print_str("OK\n");
719 return 0;
720694}
721695 )SOURCE", "OK\n");
722696
723697 add_simple_case("while loop", R"SOURCE(
724698import "std.zig";
725pub fn main(args: [][]u8) i32 => {
699pub fn main(args: [][]u8) %void => {
726700 var i : i32 = 0;
727701 while (i < 4) {
728702 print_str("loop\n");
729703 i += 1;
730704 }
705 g();
706}
707fn g() i32 => {
731708 return f();
732709}
733710fn f() i32 => {
......@@ -739,7 +716,7 @@ fn f() i32 => {
739716
740717 add_simple_case("continue and break", R"SOURCE(
741718import "std.zig";
742pub fn main(args: [][]u8) i32 => {
719pub fn main(args: [][]u8) %void => {
743720 var i : i32 = 0;
744721 while (true) {
745722 print_str("loop\n");
......@@ -749,13 +726,12 @@ pub fn main(args: [][]u8) i32 => {
749726 }
750727 break;
751728 }
752 return 0;
753729}
754730 )SOURCE", "loop\nloop\nloop\nloop\n");
755731
756732 add_simple_case("maybe type", R"SOURCE(
757733import "std.zig";
758pub fn main(args: [][]u8) i32 => {
734pub fn main(args: [][]u8) %void => {
759735 const x : ?bool = true;
760736
761737 if (const y ?= x) {
......@@ -783,19 +759,16 @@ pub fn main(args: [][]u8) i32 => {
783759 if (num != 13) {
784760 print_str("BAD\n");
785761 }
786
787 return 0;
788762}
789763 )SOURCE", "x is true\n");
790764
791765 add_simple_case("implicit cast after unreachable", R"SOURCE(
792766import "std.zig";
793pub fn main(args: [][]u8) i32 => {
767pub fn main(args: [][]u8) %void => {
794768 const x = outer();
795769 if (x == 1234) {
796770 print_str("OK\n");
797771 }
798 return 0;
799772}
800773fn inner() i32 => { 1234 }
801774fn outer() isize => {
......@@ -807,11 +780,10 @@ fn outer() isize => {
807780import "std.zig";
808781const x: u16 = 13;
809782const z: @typeof(x) = 19;
810pub fn main(args: [][]u8) i32 => {
783pub fn main(args: [][]u8) %void => {
811784 const y: @typeof(x) = 120;
812785 print_u64(@sizeof(@typeof(y)));
813786 print_str("\n");
814 return 0;
815787}
816788 )SOURCE", "2\n");
817789
......@@ -823,20 +795,19 @@ struct Rand {
823795 r.seed
824796 }
825797}
826pub fn main(args: [][]u8) i32 => {
798pub fn main(args: [][]u8) %void => {
827799 const r = Rand {.seed = 1234};
828800 if (r.get_seed() != 1234) {
829801 print_str("BAD seed\n");
830802 }
831803 print_str("OK\n");
832 return 0;
833804}
834805 )SOURCE", "OK\n");
835806
836807 add_simple_case("pointer dereferencing", R"SOURCE(
837808import "std.zig";
838809
839pub fn main(args: [][]u8) i32 => {
810pub fn main(args: [][]u8) %void => {
840811 var x = i32(3);
841812 const y = &x;
842813
......@@ -849,7 +820,6 @@ pub fn main(args: [][]u8) i32 => {
849820 print_str("BAD\n");
850821 }
851822 print_str("OK\n");
852 return 0;
853823}
854824 )SOURCE", "OK\n");
855825
......@@ -858,17 +828,16 @@ import "std.zig";
858828
859829const ARRAY_SIZE : i8 = 20;
860830
861pub fn main(args: [][]u8) i32 => {
831pub fn main(args: [][]u8) %void => {
862832 var array : [ARRAY_SIZE]u8;
863833 print_u64(@sizeof(@typeof(array)));
864834 print_str("\n");
865 return 0;
866835}
867836 )SOURCE", "20\n");
868837
869838 add_simple_case("@min_value() and @max_value()", R"SOURCE(
870839import "std.zig";
871pub fn main(args: [][]u8) i32 => {
840pub fn main(args: [][]u8) %void => {
872841 print_str("max u8: ");
873842 print_u64(@max_value(u8));
874843 print_str("\n");
......@@ -932,8 +901,6 @@ pub fn main(args: [][]u8) i32 => {
932901 print_str("min i64: ");
933902 print_i64(@min_value(i64));
934903 print_str("\n");
935
936 return 0;
937904}
938905 )SOURCE",
939906 "max u8: 255\n"
......@@ -956,7 +923,7 @@ pub fn main(args: [][]u8) i32 => {
956923
957924 add_simple_case("slicing", R"SOURCE(
958925import "std.zig";
959pub fn main(args: [][]u8) i32 => {
926pub fn main(args: [][]u8) %void => {
960927 var array : [20]i32;
961928
962929 array[5] = 1234;
......@@ -977,18 +944,16 @@ pub fn main(args: [][]u8) i32 => {
977944 }
978945
979946 print_str("OK\n");
980 return 0;
981947}
982948 )SOURCE", "OK\n");
983949
984950
985951 add_simple_case("else if expression", R"SOURCE(
986952import "std.zig";
987pub fn main(args: [][]u8) i32 => {
953pub fn main(args: [][]u8) %void => {
988954 if (f(1) == 1) {
989955 print_str("OK\n");
990956 }
991 return 0;
992957}
993958fn f(c: u8) u8 => {
994959 if (c == 0) {
......@@ -1003,7 +968,7 @@ fn f(c: u8) u8 => {
1003968
1004969 add_simple_case("overflow intrinsics", R"SOURCE(
1005970import "std.zig";
1006pub fn main(args: [][]u8) i32 => {
971pub fn main(args: [][]u8) %void => {
1007972 var result: u8;
1008973 if (!@add_with_overflow(u8, 250, 100, &result)) {
1009974 print_str("BAD\n");
......@@ -1015,13 +980,12 @@ pub fn main(args: [][]u8) i32 => {
1015980 print_str("BAD\n");
1016981 }
1017982 print_str("OK\n");
1018 return 0;
1019983}
1020984 )SOURCE", "OK\n");
1021985
1022986 add_simple_case("memcpy and memset intrinsics", R"SOURCE(
1023987import "std.zig";
1024pub fn main(args: [][]u8) i32 => {
988pub fn main(args: [][]u8) %void => {
1025989 var foo : [20]u8;
1026990 var bar : [20]u8;
1027991
......@@ -1033,7 +997,6 @@ pub fn main(args: [][]u8) i32 => {
1033997 }
1034998
1035999 print_str("OK\n");
1036 return 0;
10371000}
10381001 )SOURCE", "OK\n");
10391002
......@@ -1042,8 +1005,8 @@ import "std.zig";
10421005const z : @typeof(stdin_fileno) = 0;
10431006const x : @typeof(y) = 1234;
10441007const y : u16 = 5678;
1045pub fn main(args: [][]u8) i32 => {
1046 print_ok(x)
1008pub fn main(args: [][]u8) %void => {
1009 var x : i32 = print_ok(x);
10471010}
10481011fn print_ok(val: @typeof(x)) @typeof(foo) => {
10491012 print_str("OK\n");
......@@ -1073,7 +1036,7 @@ enum Bar {
10731036 D,
10741037}
10751038
1076pub fn main(args: [][]u8) i32 => {
1039pub fn main(args: [][]u8) %void => {
10771040 const foo1 = Foo.One(13);
10781041 const foo2 = Foo.Two(Point { .x = 1234, .y = 5678, });
10791042 const bar = Bar.B;
......@@ -1098,15 +1061,13 @@ pub fn main(args: [][]u8) i32 => {
10981061 }
10991062
11001063 print_str("OK\n");
1101
1102 return 0;
11031064}
11041065 )SOURCE", "OK\n");
11051066
11061067 add_simple_case("array literal", R"SOURCE(
11071068import "std.zig";
11081069
1109pub fn main(args: [][]u8) i32 => {
1070pub fn main(args: [][]u8) %void => {
11101071 const HEX_MULT = []u16{4096, 256, 16, 1};
11111072
11121073 if (HEX_MULT.len != 4) {
......@@ -1118,14 +1079,13 @@ pub fn main(args: [][]u8) i32 => {
11181079 }
11191080
11201081 print_str("OK\n");
1121 return 0;
11221082}
11231083 )SOURCE", "OK\n");
11241084
11251085 add_simple_case("nested arrays", R"SOURCE(
11261086import "std.zig";
11271087
1128pub fn main(args: [][]u8) i32 => {
1088pub fn main(args: [][]u8) %void => {
11291089 const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
11301090 var i: @typeof(array_of_strings.len) = 0;
11311091 while (i < array_of_strings.len) {
......@@ -1133,14 +1093,13 @@ pub fn main(args: [][]u8) i32 => {
11331093 print_str("\n");
11341094 i += 1;
11351095 }
1136 return 0;
11371096}
11381097 )SOURCE", "hello\nthis\nis\nmy\nthing\n");
11391098
11401099 add_simple_case("for loops", R"SOURCE(
11411100import "std.zig";
11421101
1143pub fn main(args: [][]u8) i32 => {
1102pub fn main(args: [][]u8) %void => {
11441103 const array = []u8 {9, 8, 7, 6};
11451104 for (item, array) {
11461105 print_u64(item);
......@@ -1159,20 +1118,18 @@ pub fn main(args: [][]u8) i32 => {
11591118 print_i64(index);
11601119 print_str("\n");
11611120 }
1162 return 0;
11631121}
11641122 )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n");
11651123
11661124 add_simple_case("function pointers", R"SOURCE(
11671125import "std.zig";
11681126
1169pub fn main(args: [][]u8) i32 => {
1127pub fn main(args: [][]u8) %void => {
11701128 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
11711129 for (f, fns) {
11721130 print_u64(f());
11731131 print_str("\n");
11741132 }
1175 return 0;
11761133}
11771134
11781135fn fn1() u32 => {5}
......@@ -1191,7 +1148,7 @@ enum Foo {
11911148 D,
11921149}
11931150
1194pub fn main(args: [][]u8) i32 => {
1151pub fn main(args: [][]u8) %void => {
11951152 const foo = Foo.C;
11961153 const val: i32 = switch (foo) {
11971154 Foo.A => 1,
......@@ -1204,7 +1161,6 @@ pub fn main(args: [][]u8) i32 => {
12041161 }
12051162
12061163 print_str("OK\n");
1207 return 0;
12081164}
12091165 )SOURCE", "OK\n");
12101166
......@@ -1213,7 +1169,7 @@ import "std.zig";
12131169
12141170const ten = 10;
12151171
1216pub fn main(args: [][]u8) i32 => {
1172pub fn main(args: [][]u8) %void => {
12171173 const one = 1;
12181174 const eleven = ten + one;
12191175
......@@ -1222,7 +1178,6 @@ pub fn main(args: [][]u8) i32 => {
12221178 }
12231179
12241180 print_str("OK\n");
1225 return 0;
12261181}
12271182 )SOURCE", "OK\n");
12281183
......@@ -1233,28 +1188,26 @@ struct Foo {
12331188 y: bool,
12341189}
12351190var foo = Foo { .x = 13, .y = true, };
1236pub fn main(args: [][]u8) i32 => {
1191pub fn main(args: [][]u8) %void => {
12371192 foo.x += 1;
12381193 if (foo.x != 14) {
12391194 print_str("BAD\n");
12401195 }
12411196
12421197 print_str("OK\n");
1243 return 0;
12441198}
12451199 )SOURCE", "OK\n");
12461200
12471201 add_simple_case("statically initialized array literal", R"SOURCE(
12481202import "std.zig";
12491203const x = []u8{1,2,3,4};
1250pub fn main(args: [][]u8) i32 => {
1204pub fn main(args: [][]u8) %void => {
12511205 const y : [4]u8 = x;
12521206 if (y[3] != 4) {
12531207 print_str("BAD\n");
12541208 }
12551209
12561210 print_str("OK\n");
1257 return 0;
12581211}
12591212 )SOURCE", "OK\n");
12601213
......@@ -1262,7 +1215,7 @@ pub fn main(args: [][]u8) i32 => {
12621215import "std.zig";
12631216%.err1;
12641217%.err2;
1265pub fn main(args: [][]u8) i32 => {
1218pub fn main(args: [][]u8) %void => {
12661219 const a = i32(%.err1);
12671220 const b = i32(%.err2);
12681221 if (a == b) {
......@@ -1270,7 +1223,6 @@ pub fn main(args: [][]u8) i32 => {
12701223 }
12711224
12721225 print_str("OK\n");
1273 return 0;
12741226}
12751227 )SOURCE", "OK\n");
12761228