| author | |
| committer | |
| log | c0ea9290c4576f2111c8fc6b2d448f278effd80e |
| tree | c4b789b675b0083b3d9c2e32550a0299f2546cf5 |
| parent | 91d911007b8a12405c084cff53237ac26b1f2c5f |
8 files changed, 254 insertions(+), 223 deletions(-)
example/cat/main.zig+1-4| ... | ... | @@ -4,14 +4,11 @@ import "std.zig"; |
| 4 | 4 | |
| 5 | 5 | // Things to do to make this work: |
| 6 | 6 | // * var args printing |
| 7 | // * %void type | |
| 7 | // * update std API | |
| 8 | 8 | // * defer |
| 9 | 9 | // * %return |
| 10 | 10 | // * %% operator |
| 11 | // * make main return %void | |
| 12 | // * how to reference error values %.Invalid | |
| 13 | 11 | // * cast err type to string |
| 14 | // * update std API | |
| 15 | 12 | |
| 16 | 13 | pub %.Invalid; |
| 17 | 14 |
example/hello_world/hello.zig+1-2| ... | ... | @@ -2,8 +2,7 @@ export executable "hello"; |
| 2 | 2 | |
| 3 | 3 | import "std.zig"; |
| 4 | 4 | |
| 5 | pub fn main(args: [][]u8) i32 => { | |
| 5 | pub fn main(args: [][]u8) %void => { | |
| 6 | 6 | //stderr.print_str("Hello, world!\n"); |
| 7 | 7 | print_str("Hello, world!\n"); |
| 8 | return 0; | |
| 9 | 8 | } |
example/hello_world/hello_libc.zig+1-1| ... | ... | @@ -5,7 +5,7 @@ extern { |
| 5 | 5 | fn printf(__format: &const u8, ...) i32; |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | export fn main(argc: i32, argv: &&u8, env: &&u8) i32 => { | |
| 8 | export fn main(argc: i32, argv: &&u8) i32 => { | |
| 9 | 9 | printf(c"Hello, world!\n"); |
| 10 | 10 | return 0; |
| 11 | 11 | } |
src/all_types.hpp+7-1| ... | ... | @@ -60,6 +60,11 @@ struct ConstPtrValue { |
| 60 | 60 | uint64_t len; |
| 61 | 61 | }; |
| 62 | 62 | |
| 63 | struct ConstErrValue { | |
| 64 | ErrorTableEntry *err; | |
| 65 | ConstExprValue *payload; | |
| 66 | }; | |
| 67 | ||
| 63 | 68 | struct ConstExprValue { |
| 64 | 69 | bool ok; // true if constant expression evalution worked |
| 65 | 70 | bool depends_on_compile_var; |
| ... | ... | @@ -70,8 +75,8 @@ struct ConstExprValue { |
| 70 | 75 | bool x_bool; |
| 71 | 76 | FnTableEntry *x_fn; |
| 72 | 77 | TypeTableEntry *x_type; |
| 73 | ErrorTableEntry *x_err; | |
| 74 | 78 | ConstExprValue *x_maybe; |
| 79 | ConstErrValue x_err; | |
| 75 | 80 | ConstEnumValue x_enum; |
| 76 | 81 | ConstStructValue x_struct; |
| 77 | 82 | ConstArrayValue x_array; |
| ... | ... | @@ -309,6 +314,7 @@ enum CastOp { |
| 309 | 314 | CastOpIntWidenOrShorten, |
| 310 | 315 | CastOpToUnknownSizeArray, |
| 311 | 316 | CastOpMaybeWrap, |
| 317 | CastOpErrorWrap, | |
| 312 | 318 | CastOpPointerReinterpret, |
| 313 | 319 | CastOpErrToInt, |
| 314 | 320 | }; |
src/analyze.cpp+150-95| ... | ... | @@ -1130,6 +1130,61 @@ static bool num_lit_fits_in_other_type(CodeGen *g, AstNode *literal_node, TypeTa |
| 1130 | 1130 | return false; |
| 1131 | 1131 | } |
| 1132 | 1132 | |
| 1133 | static 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 | ||
| 1133 | 1188 | static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *parent_source_node, |
| 1134 | 1189 | AstNode **child_nodes, TypeTableEntry **child_types, int child_count) |
| 1135 | 1190 | { |
| ... | ... | @@ -1143,6 +1198,12 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa |
| 1143 | 1198 | AstNode *cur_node = child_nodes[i]; |
| 1144 | 1199 | if (cur_type->id == TypeTableEntryIdInvalid) { |
| 1145 | 1200 | 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; | |
| 1146 | 1207 | } else if (prev_type->id == TypeTableEntryIdUnreachable) { |
| 1147 | 1208 | prev_type = cur_type; |
| 1148 | 1209 | prev_node = cur_node; |
| ... | ... | @@ -1163,6 +1224,16 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa |
| 1163 | 1224 | prev_type = cur_type; |
| 1164 | 1225 | prev_node = cur_node; |
| 1165 | 1226 | } |
| 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; | |
| 1166 | 1237 | } else if (prev_type->id == TypeTableEntryIdNumLitFloat && |
| 1167 | 1238 | cur_type->id == TypeTableEntryIdNumLitFloat) |
| 1168 | 1239 | { |
| ... | ... | @@ -1189,8 +1260,6 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa |
| 1189 | 1260 | } else { |
| 1190 | 1261 | return g->builtin_types.entry_invalid; |
| 1191 | 1262 | } |
| 1192 | } else if (prev_type == cur_type) { | |
| 1193 | continue; | |
| 1194 | 1263 | } else { |
| 1195 | 1264 | add_node_error(g, parent_source_node, |
| 1196 | 1265 | buf_sprintf("incompatible types: '%s' and '%s'", |
| ... | ... | @@ -1202,61 +1271,6 @@ static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *pa |
| 1202 | 1271 | return prev_type; |
| 1203 | 1272 | } |
| 1204 | 1273 | |
| 1205 | static 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 | ||
| 1260 | 1274 | static bool types_match_with_implicit_cast(CodeGen *g, TypeTableEntry *expected_type, |
| 1261 | 1275 | TypeTableEntry *actual_type, AstNode *literal_node, bool *reported_err) |
| 1262 | 1276 | { |
| ... | ... | @@ -1272,6 +1286,14 @@ static bool types_match_with_implicit_cast(CodeGen *g, TypeTableEntry *expected_ |
| 1272 | 1286 | return true; |
| 1273 | 1287 | } |
| 1274 | 1288 | |
| 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 | ||
| 1275 | 1297 | // implicit widening conversion |
| 1276 | 1298 | if (expected_type->id == TypeTableEntryIdInt && |
| 1277 | 1299 | actual_type->id == TypeTableEntryIdInt && |
| ... | ... | @@ -1381,9 +1403,10 @@ static TypeTableEntry *resolve_peer_type_compatibility(CodeGen *g, ImportTableEn |
| 1381 | 1403 | if (!child_nodes[i]) { |
| 1382 | 1404 | continue; |
| 1383 | 1405 | } |
| 1384 | Expr *expr = get_resolved_expr(child_nodes[i]); | |
| 1406 | AstNode **child_node = child_nodes[i]->parent_field; | |
| 1385 | 1407 | 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); | |
| 1387 | 1410 | expr->type_entry = resolved_type; |
| 1388 | 1411 | add_global_const_expr(g, expr); |
| 1389 | 1412 | } |
| ... | ... | @@ -1812,7 +1835,7 @@ static TypeTableEntry *resolve_expr_const_val_as_fn(CodeGen *g, AstNode *node, F |
| 1812 | 1835 | static TypeTableEntry *resolve_expr_const_val_as_err(CodeGen *g, AstNode *node, ErrorTableEntry *err) { |
| 1813 | 1836 | Expr *expr = get_resolved_expr(node); |
| 1814 | 1837 | expr->const_val.ok = true; |
| 1815 | expr->const_val.data.x_err = err; | |
| 1838 | expr->const_val.data.x_err.err = err; | |
| 1816 | 1839 | return get_error_type(g, g->builtin_types.entry_void); |
| 1817 | 1840 | } |
| 1818 | 1841 | |
| ... | ... | @@ -2868,10 +2891,18 @@ static void eval_const_expr_implicit_cast(CodeGen *g, AstNode *node, AstNode *ex |
| 2868 | 2891 | const_val->data.x_maybe = other_val; |
| 2869 | 2892 | const_val->ok = true; |
| 2870 | 2893 | 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; | |
| 2873 | 2897 | const_val->ok = true; |
| 2874 | 2898 | 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 | } | |
| 2875 | 2906 | } |
| 2876 | 2907 | } |
| 2877 | 2908 | |
| ... | ... | @@ -2965,6 +2996,25 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B |
| 2965 | 2996 | } |
| 2966 | 2997 | } |
| 2967 | 2998 | |
| 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 | ||
| 2968 | 3018 | // explicit cast from number literal to another type |
| 2969 | 3019 | if (actual_type->id == TypeTableEntryIdNumLitFloat || |
| 2970 | 3020 | actual_type->id == TypeTableEntryIdNumLitInt) |
| ... | ... | @@ -3579,6 +3629,42 @@ static TypeTableEntry *analyze_string_literal_expr(CodeGen *g, ImportTableEntry |
| 3579 | 3629 | } |
| 3580 | 3630 | } |
| 3581 | 3631 | |
| 3632 | static 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 | ||
| 3582 | 3668 | // When you call analyze_expression, the node you pass might no longer be the child node |
| 3583 | 3669 | // you thought it was due to implicit casting rewriting the AST. |
| 3584 | 3670 | static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import, BlockContext *context, |
| ... | ... | @@ -3587,39 +3673,8 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import, |
| 3587 | 3673 | TypeTableEntry *return_type = nullptr; |
| 3588 | 3674 | switch (node->type) { |
| 3589 | 3675 | 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; | |
| 3623 | 3678 | |
| 3624 | 3679 | case NodeTypeReturnExpr: |
| 3625 | 3680 | 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 |
| 268 | 268 | } |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | static 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 | ||
| 271 | 291 | static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) { |
| 272 | 292 | assert(node->type == NodeTypeFnCallExpr); |
| 273 | 293 | |
| ... | ... | @@ -288,7 +308,7 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) { |
| 288 | 308 | case CastOpErrToInt: |
| 289 | 309 | assert(actual_type->id == TypeTableEntryIdError); |
| 290 | 310 | 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); | |
| 292 | 312 | } else { |
| 293 | 313 | zig_panic("TODO"); |
| 294 | 314 | } |
| ... | ... | @@ -309,6 +329,13 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) { |
| 309 | 329 | |
| 310 | 330 | return cast_expr->tmp_ptr; |
| 311 | 331 | } |
| 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 | } | |
| 312 | 339 | case CastOpPtrToInt: |
| 313 | 340 | add_debug_source_node(g, node); |
| 314 | 341 | return LLVMBuildPtrToInt(g->builder, expr_val, wanted_type->type_ref, ""); |
| ... | ... | @@ -316,21 +343,7 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) { |
| 316 | 343 | add_debug_source_node(g, node); |
| 317 | 344 | return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, ""); |
| 318 | 345 | 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); | |
| 334 | 347 | case CastOpToUnknownSizeArray: |
| 335 | 348 | { |
| 336 | 349 | assert(cast_expr->tmp_ptr); |
| ... | ... | @@ -1279,7 +1292,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV |
| 1279 | 1292 | return nullptr; |
| 1280 | 1293 | } |
| 1281 | 1294 | |
| 1282 | assert(!use_expr_value); | |
| 1295 | assert(!use_expr_value || then_type->id == TypeTableEntryIdError); | |
| 1283 | 1296 | |
| 1284 | 1297 | LLVMBasicBlockRef then_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "Then"); |
| 1285 | 1298 | 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 |
| 1292 | 1305 | LLVMBuildBr(g->builder, endif_block); |
| 1293 | 1306 | |
| 1294 | 1307 | 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 | } | |
| 1296 | 1314 | } |
| 1297 | 1315 | |
| 1298 | 1316 | static LLVMValueRef gen_if_bool_expr(CodeGen *g, AstNode *node) { |
| ... | ... | @@ -2132,7 +2150,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE |
| 2132 | 2150 | } |
| 2133 | 2151 | } else if (type_entry->id == TypeTableEntryIdError) { |
| 2134 | 2152 | 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); | |
| 2136 | 2155 | } else { |
| 2137 | 2156 | zig_panic("TODO"); |
| 2138 | 2157 | } |
std/bootstrap.zig+4-1| ... | ... | @@ -29,5 +29,8 @@ fn call_main() unreachable => { |
| 29 | 29 | const ptr = argv[i]; |
| 30 | 30 | args[i] = ptr[0...strlen(ptr)]; |
| 31 | 31 | } |
| 32 | exit(main(args)) | |
| 32 | // TODO: replace the i32 cast with: | |
| 33 | // main(args) %% exit(1) | |
| 34 | // exit(0) | |
| 35 | exit(i32(main(args))) | |
| 33 | 36 | } |
test/run_tests.cpp+52-100| ... | ... | @@ -114,7 +114,7 @@ import "syscall.zig"; |
| 114 | 114 | fn empty_function_1() => {} |
| 115 | 115 | fn empty_function_2() => { return; } |
| 116 | 116 | |
| 117 | pub fn main(args: [][]u8) i32 => { | |
| 117 | pub fn main(args: [][]u8) %void => { | |
| 118 | 118 | empty_function_1(); |
| 119 | 119 | empty_function_2(); |
| 120 | 120 | this_is_a_function(); |
| ... | ... | @@ -136,9 +136,8 @@ fn another_function() => {} |
| 136 | 136 | |
| 137 | 137 | /// this is a documentation comment |
| 138 | 138 | /// doc comment line 2 |
| 139 | pub fn main(args: [][]u8) i32 => { | |
| 139 | pub fn main(args: [][]u8) %void => { | |
| 140 | 140 | print_str(/* mid-line comment /* nested */ */ "OK\n"); |
| 141 | return 0; | |
| 142 | 141 | } |
| 143 | 142 | )SOURCE", "OK\n"); |
| 144 | 143 | |
| ... | ... | @@ -147,10 +146,9 @@ pub fn main(args: [][]u8) i32 => { |
| 147 | 146 | import "std.zig"; |
| 148 | 147 | import "foo.zig"; |
| 149 | 148 | |
| 150 | pub fn main(args: [][]u8) i32 => { | |
| 149 | pub fn main(args: [][]u8) %void => { | |
| 151 | 150 | private_function(); |
| 152 | 151 | print_str("OK 2\n"); |
| 153 | return 0; | |
| 154 | 152 | } |
| 155 | 153 | |
| 156 | 154 | fn private_function() => { |
| ... | ... | @@ -178,10 +176,9 @@ pub fn print_text() => { |
| 178 | 176 | import "foo.zig"; |
| 179 | 177 | import "bar.zig"; |
| 180 | 178 | |
| 181 | pub fn main(args: [][]u8) i32 => { | |
| 179 | pub fn main(args: [][]u8) %void => { | |
| 182 | 180 | foo_function(); |
| 183 | 181 | bar_function(); |
| 184 | return 0; | |
| 185 | 182 | } |
| 186 | 183 | )SOURCE", "OK\nOK\n"); |
| 187 | 184 | |
| ... | ... | @@ -214,7 +211,7 @@ pub fn foo_function() bool => { |
| 214 | 211 | add_simple_case("if statements", R"SOURCE( |
| 215 | 212 | import "std.zig"; |
| 216 | 213 | |
| 217 | pub fn main(args: [][]u8) i32 => { | |
| 214 | pub fn main(args: [][]u8) %void => { | |
| 218 | 215 | if (1 != 0) { |
| 219 | 216 | print_str("1 is true\n"); |
| 220 | 217 | } else { |
| ... | ... | @@ -228,7 +225,6 @@ pub fn main(args: [][]u8) i32 => { |
| 228 | 225 | if (!(0 != 0)) { |
| 229 | 226 | print_str("!0 is true\n"); |
| 230 | 227 | } |
| 231 | return 0; | |
| 232 | 228 | } |
| 233 | 229 | )SOURCE", "1 is true\n!0 is true\n"); |
| 234 | 230 | |
| ... | ... | @@ -239,11 +235,10 @@ fn add(a: i32, b: i32) i32 => { |
| 239 | 235 | a + b |
| 240 | 236 | } |
| 241 | 237 | |
| 242 | pub fn main(args: [][]u8) i32 => { | |
| 238 | pub fn main(args: [][]u8) %void => { | |
| 243 | 239 | if (add(22, 11) == 33) { |
| 244 | 240 | print_str("pass\n"); |
| 245 | 241 | } |
| 246 | return 0; | |
| 247 | 242 | } |
| 248 | 243 | )SOURCE", "pass\n"); |
| 249 | 244 | |
| ... | ... | @@ -261,41 +256,38 @@ done: |
| 261 | 256 | return; |
| 262 | 257 | } |
| 263 | 258 | |
| 264 | pub fn main(args: [][]u8) i32 => { | |
| 259 | pub fn main(args: [][]u8) %void => { | |
| 265 | 260 | loop(3); |
| 266 | return 0; | |
| 267 | 261 | } |
| 268 | 262 | )SOURCE", "loop\nloop\nloop\n"); |
| 269 | 263 | |
| 270 | 264 | add_simple_case("local variables", R"SOURCE( |
| 271 | 265 | import "std.zig"; |
| 272 | 266 | |
| 273 | pub fn main(args: [][]u8) i32 => { | |
| 267 | pub fn main(args: [][]u8) %void => { | |
| 274 | 268 | const a : i32 = 1; |
| 275 | 269 | const b = i32(2); |
| 276 | 270 | if (a + b == 3) { |
| 277 | 271 | print_str("OK\n"); |
| 278 | 272 | } |
| 279 | return 0; | |
| 280 | 273 | } |
| 281 | 274 | )SOURCE", "OK\n"); |
| 282 | 275 | |
| 283 | 276 | add_simple_case("bool literals", R"SOURCE( |
| 284 | 277 | import "std.zig"; |
| 285 | 278 | |
| 286 | pub fn main(args: [][]u8) i32 => { | |
| 279 | pub fn main(args: [][]u8) %void => { | |
| 287 | 280 | if (true) { print_str("OK 1\n"); } |
| 288 | 281 | if (false) { print_str("BAD 1\n"); } |
| 289 | 282 | if (!true) { print_str("BAD 2\n"); } |
| 290 | 283 | if (!false) { print_str("OK 2\n"); } |
| 291 | return 0; | |
| 292 | 284 | } |
| 293 | 285 | )SOURCE", "OK 1\nOK 2\n"); |
| 294 | 286 | |
| 295 | 287 | add_simple_case("separate block scopes", R"SOURCE( |
| 296 | 288 | import "std.zig"; |
| 297 | 289 | |
| 298 | pub fn main(args: [][]u8) i32 => { | |
| 290 | pub fn main(args: [][]u8) %void => { | |
| 299 | 291 | if (true) { |
| 300 | 292 | const no_conflict : i32 = 5; |
| 301 | 293 | if (no_conflict == 5) { print_str("OK 1\n"); } |
| ... | ... | @@ -306,16 +298,14 @@ pub fn main(args: [][]u8) i32 => { |
| 306 | 298 | no_conflict |
| 307 | 299 | }; |
| 308 | 300 | if (c == 10) { print_str("OK 2\n"); } |
| 309 | return 0; | |
| 310 | 301 | } |
| 311 | 302 | )SOURCE", "OK 1\nOK 2\n"); |
| 312 | 303 | |
| 313 | 304 | add_simple_case("void parameters", R"SOURCE( |
| 314 | 305 | import "std.zig"; |
| 315 | 306 | |
| 316 | pub fn main(args: [][]u8) i32 => { | |
| 307 | pub fn main(args: [][]u8) %void => { | |
| 317 | 308 | void_fun(1, void{}, 2); |
| 318 | return 0; | |
| 319 | 309 | } |
| 320 | 310 | |
| 321 | 311 | fn void_fun(a : i32, b : void, c : i32) => { |
| ... | ... | @@ -333,7 +323,7 @@ struct Foo { |
| 333 | 323 | b : i32, |
| 334 | 324 | c : void, |
| 335 | 325 | } |
| 336 | pub fn main(args: [][]u8) i32 => { | |
| 326 | pub fn main(args: [][]u8) %void => { | |
| 337 | 327 | const foo = Foo { |
| 338 | 328 | .a = void{}, |
| 339 | 329 | .b = 1, |
| ... | ... | @@ -346,7 +336,6 @@ pub fn main(args: [][]u8) i32 => { |
| 346 | 336 | print_str("BAD\n"); |
| 347 | 337 | } |
| 348 | 338 | print_str("OK\n"); |
| 349 | return 0; | |
| 350 | 339 | } |
| 351 | 340 | |
| 352 | 341 | )SOURCE", "OK\n"); |
| ... | ... | @@ -354,7 +343,7 @@ pub fn main(args: [][]u8) i32 => { |
| 354 | 343 | add_simple_case("void arrays", R"SOURCE( |
| 355 | 344 | import "std.zig"; |
| 356 | 345 | |
| 357 | pub fn main(args: [][]u8) i32 => { | |
| 346 | pub fn main(args: [][]u8) %void => { | |
| 358 | 347 | var array: [4]void; |
| 359 | 348 | array[0] = void{}; |
| 360 | 349 | array[1] = array[2]; |
| ... | ... | @@ -365,7 +354,6 @@ pub fn main(args: [][]u8) i32 => { |
| 365 | 354 | print_str("BAD\n"); |
| 366 | 355 | } |
| 367 | 356 | print_str("OK\n"); |
| 368 | return 0; | |
| 369 | 357 | } |
| 370 | 358 | )SOURCE", "OK\n"); |
| 371 | 359 | |
| ... | ... | @@ -373,27 +361,22 @@ pub fn main(args: [][]u8) i32 => { |
| 373 | 361 | add_simple_case("mutable local variables", R"SOURCE( |
| 374 | 362 | import "std.zig"; |
| 375 | 363 | |
| 376 | pub fn main(args: [][]u8) i32 => { | |
| 364 | pub fn main(args: [][]u8) %void => { | |
| 377 | 365 | var zero : i32 = 0; |
| 378 | 366 | if (zero == 0) { print_str("zero\n"); } |
| 379 | 367 | |
| 380 | 368 | var i = i32(0); |
| 381 | loop_start: | |
| 382 | if (i == 3) { | |
| 383 | goto done; | |
| 369 | while (i != 3) { | |
| 370 | print_str("loop\n"); | |
| 371 | i += 1; | |
| 384 | 372 | } |
| 385 | print_str("loop\n"); | |
| 386 | i = i + 1; | |
| 387 | goto loop_start; | |
| 388 | done: | |
| 389 | return 0; | |
| 390 | 373 | } |
| 391 | 374 | )SOURCE", "zero\nloop\nloop\nloop\n"); |
| 392 | 375 | |
| 393 | 376 | add_simple_case("arrays", R"SOURCE( |
| 394 | 377 | import "std.zig"; |
| 395 | 378 | |
| 396 | pub fn main(args: [][]u8) i32 => { | |
| 379 | pub fn main(args: [][]u8) %void => { | |
| 397 | 380 | var array : [5]i32; |
| 398 | 381 | |
| 399 | 382 | var i : i32 = 0; |
| ... | ... | @@ -417,8 +400,6 @@ pub fn main(args: [][]u8) i32 => { |
| 417 | 400 | if (get_array_len(array) != 5) { |
| 418 | 401 | print_str("BAD\n"); |
| 419 | 402 | } |
| 420 | ||
| 421 | return 0; | |
| 422 | 403 | } |
| 423 | 404 | fn get_array_len(a: []i32) isize => { |
| 424 | 405 | a.len |
| ... | ... | @@ -429,9 +410,8 @@ fn get_array_len(a: []i32) isize => { |
| 429 | 410 | add_simple_case("hello world without libc", R"SOURCE( |
| 430 | 411 | import "std.zig"; |
| 431 | 412 | |
| 432 | pub fn main(args: [][]u8) i32 => { | |
| 413 | pub fn main(args: [][]u8) %void => { | |
| 433 | 414 | print_str("Hello, world!\n"); |
| 434 | return 0; | |
| 435 | 415 | } |
| 436 | 416 | )SOURCE", "Hello, world!\n"); |
| 437 | 417 | |
| ... | ... | @@ -439,7 +419,7 @@ pub fn main(args: [][]u8) i32 => { |
| 439 | 419 | add_simple_case("a + b + c", R"SOURCE( |
| 440 | 420 | import "std.zig"; |
| 441 | 421 | |
| 442 | pub fn main(args: [][]u8) i32 => { | |
| 422 | pub fn main(args: [][]u8) %void => { | |
| 443 | 423 | if (false || false || false) { print_str("BAD 1\n"); } |
| 444 | 424 | if (true && true && false) { print_str("BAD 2\n"); } |
| 445 | 425 | if (1 | 2 | 4 != 7) { print_str("BAD 3\n"); } |
| ... | ... | @@ -454,14 +434,13 @@ pub fn main(args: [][]u8) i32 => { |
| 454 | 434 | if (i32(7) != --(i32(7))) { print_str("BAD 12\n"); } |
| 455 | 435 | |
| 456 | 436 | print_str("OK\n"); |
| 457 | return 0; | |
| 458 | 437 | } |
| 459 | 438 | )SOURCE", "OK\n"); |
| 460 | 439 | |
| 461 | 440 | add_simple_case("short circuit", R"SOURCE( |
| 462 | 441 | import "std.zig"; |
| 463 | 442 | |
| 464 | pub fn main(args: [][]u8) i32 => { | |
| 443 | pub fn main(args: [][]u8) %void => { | |
| 465 | 444 | if (true || { print_str("BAD 1\n"); false }) { |
| 466 | 445 | print_str("OK 1\n"); |
| 467 | 446 | } |
| ... | ... | @@ -476,15 +455,13 @@ pub fn main(args: [][]u8) i32 => { |
| 476 | 455 | } else { |
| 477 | 456 | print_str("OK 4\n"); |
| 478 | 457 | } |
| 479 | ||
| 480 | return 0; | |
| 481 | 458 | } |
| 482 | 459 | )SOURCE", "OK 1\nOK 2\nOK 3\nOK 4\n"); |
| 483 | 460 | |
| 484 | 461 | add_simple_case("modify operators", R"SOURCE( |
| 485 | 462 | import "std.zig"; |
| 486 | 463 | |
| 487 | pub fn main(args: [][]u8) i32 => { | |
| 464 | pub fn main(args: [][]u8) %void => { | |
| 488 | 465 | var i : i32 = 0; |
| 489 | 466 | i += 5; if (i != 5) { print_str("BAD +=\n"); } |
| 490 | 467 | i -= 2; if (i != 3) { print_str("BAD -=\n"); } |
| ... | ... | @@ -500,7 +477,6 @@ pub fn main(args: [][]u8) i32 => { |
| 500 | 477 | i |= 3; if (i != 7) { print_str("BAD |=\n"); } |
| 501 | 478 | |
| 502 | 479 | print_str("OK\n"); |
| 503 | return 0; | |
| 504 | 480 | } |
| 505 | 481 | )SOURCE", "OK\n"); |
| 506 | 482 | |
| ... | ... | @@ -636,7 +612,7 @@ export fn main(argc: i32, argv: &&u8) i32 => { |
| 636 | 612 | add_simple_case("structs", R"SOURCE( |
| 637 | 613 | import "std.zig"; |
| 638 | 614 | |
| 639 | pub fn main(args: [][]u8) i32 => { | |
| 615 | pub fn main(args: [][]u8) %void => { | |
| 640 | 616 | var foo : Foo; |
| 641 | 617 | @memset(&foo, 0, @sizeof(Foo)); |
| 642 | 618 | foo.a += 1; |
| ... | ... | @@ -650,7 +626,6 @@ pub fn main(args: [][]u8) i32 => { |
| 650 | 626 | test_byval_assign(); |
| 651 | 627 | test_initializer(); |
| 652 | 628 | print_str("OK\n"); |
| 653 | return 0; | |
| 654 | 629 | } |
| 655 | 630 | struct Foo { |
| 656 | 631 | a : i32, |
| ... | ... | @@ -711,23 +686,25 @@ import "std.zig"; |
| 711 | 686 | const g1 : i32 = 1233 + 1; |
| 712 | 687 | var g2 : i32 = 0; |
| 713 | 688 | |
| 714 | pub fn main(args: [][]u8) i32 => { | |
| 689 | pub fn main(args: [][]u8) %void => { | |
| 715 | 690 | if (g2 != 0) { print_str("BAD\n"); } |
| 716 | 691 | g2 = g1; |
| 717 | 692 | if (g2 != 1234) { print_str("BAD\n"); } |
| 718 | 693 | print_str("OK\n"); |
| 719 | return 0; | |
| 720 | 694 | } |
| 721 | 695 | )SOURCE", "OK\n"); |
| 722 | 696 | |
| 723 | 697 | add_simple_case("while loop", R"SOURCE( |
| 724 | 698 | import "std.zig"; |
| 725 | pub fn main(args: [][]u8) i32 => { | |
| 699 | pub fn main(args: [][]u8) %void => { | |
| 726 | 700 | var i : i32 = 0; |
| 727 | 701 | while (i < 4) { |
| 728 | 702 | print_str("loop\n"); |
| 729 | 703 | i += 1; |
| 730 | 704 | } |
| 705 | g(); | |
| 706 | } | |
| 707 | fn g() i32 => { | |
| 731 | 708 | return f(); |
| 732 | 709 | } |
| 733 | 710 | fn f() i32 => { |
| ... | ... | @@ -739,7 +716,7 @@ fn f() i32 => { |
| 739 | 716 | |
| 740 | 717 | add_simple_case("continue and break", R"SOURCE( |
| 741 | 718 | import "std.zig"; |
| 742 | pub fn main(args: [][]u8) i32 => { | |
| 719 | pub fn main(args: [][]u8) %void => { | |
| 743 | 720 | var i : i32 = 0; |
| 744 | 721 | while (true) { |
| 745 | 722 | print_str("loop\n"); |
| ... | ... | @@ -749,13 +726,12 @@ pub fn main(args: [][]u8) i32 => { |
| 749 | 726 | } |
| 750 | 727 | break; |
| 751 | 728 | } |
| 752 | return 0; | |
| 753 | 729 | } |
| 754 | 730 | )SOURCE", "loop\nloop\nloop\nloop\n"); |
| 755 | 731 | |
| 756 | 732 | add_simple_case("maybe type", R"SOURCE( |
| 757 | 733 | import "std.zig"; |
| 758 | pub fn main(args: [][]u8) i32 => { | |
| 734 | pub fn main(args: [][]u8) %void => { | |
| 759 | 735 | const x : ?bool = true; |
| 760 | 736 | |
| 761 | 737 | if (const y ?= x) { |
| ... | ... | @@ -783,19 +759,16 @@ pub fn main(args: [][]u8) i32 => { |
| 783 | 759 | if (num != 13) { |
| 784 | 760 | print_str("BAD\n"); |
| 785 | 761 | } |
| 786 | ||
| 787 | return 0; | |
| 788 | 762 | } |
| 789 | 763 | )SOURCE", "x is true\n"); |
| 790 | 764 | |
| 791 | 765 | add_simple_case("implicit cast after unreachable", R"SOURCE( |
| 792 | 766 | import "std.zig"; |
| 793 | pub fn main(args: [][]u8) i32 => { | |
| 767 | pub fn main(args: [][]u8) %void => { | |
| 794 | 768 | const x = outer(); |
| 795 | 769 | if (x == 1234) { |
| 796 | 770 | print_str("OK\n"); |
| 797 | 771 | } |
| 798 | return 0; | |
| 799 | 772 | } |
| 800 | 773 | fn inner() i32 => { 1234 } |
| 801 | 774 | fn outer() isize => { |
| ... | ... | @@ -807,11 +780,10 @@ fn outer() isize => { |
| 807 | 780 | import "std.zig"; |
| 808 | 781 | const x: u16 = 13; |
| 809 | 782 | const z: @typeof(x) = 19; |
| 810 | pub fn main(args: [][]u8) i32 => { | |
| 783 | pub fn main(args: [][]u8) %void => { | |
| 811 | 784 | const y: @typeof(x) = 120; |
| 812 | 785 | print_u64(@sizeof(@typeof(y))); |
| 813 | 786 | print_str("\n"); |
| 814 | return 0; | |
| 815 | 787 | } |
| 816 | 788 | )SOURCE", "2\n"); |
| 817 | 789 | |
| ... | ... | @@ -823,20 +795,19 @@ struct Rand { |
| 823 | 795 | r.seed |
| 824 | 796 | } |
| 825 | 797 | } |
| 826 | pub fn main(args: [][]u8) i32 => { | |
| 798 | pub fn main(args: [][]u8) %void => { | |
| 827 | 799 | const r = Rand {.seed = 1234}; |
| 828 | 800 | if (r.get_seed() != 1234) { |
| 829 | 801 | print_str("BAD seed\n"); |
| 830 | 802 | } |
| 831 | 803 | print_str("OK\n"); |
| 832 | return 0; | |
| 833 | 804 | } |
| 834 | 805 | )SOURCE", "OK\n"); |
| 835 | 806 | |
| 836 | 807 | add_simple_case("pointer dereferencing", R"SOURCE( |
| 837 | 808 | import "std.zig"; |
| 838 | 809 | |
| 839 | pub fn main(args: [][]u8) i32 => { | |
| 810 | pub fn main(args: [][]u8) %void => { | |
| 840 | 811 | var x = i32(3); |
| 841 | 812 | const y = &x; |
| 842 | 813 | |
| ... | ... | @@ -849,7 +820,6 @@ pub fn main(args: [][]u8) i32 => { |
| 849 | 820 | print_str("BAD\n"); |
| 850 | 821 | } |
| 851 | 822 | print_str("OK\n"); |
| 852 | return 0; | |
| 853 | 823 | } |
| 854 | 824 | )SOURCE", "OK\n"); |
| 855 | 825 | |
| ... | ... | @@ -858,17 +828,16 @@ import "std.zig"; |
| 858 | 828 | |
| 859 | 829 | const ARRAY_SIZE : i8 = 20; |
| 860 | 830 | |
| 861 | pub fn main(args: [][]u8) i32 => { | |
| 831 | pub fn main(args: [][]u8) %void => { | |
| 862 | 832 | var array : [ARRAY_SIZE]u8; |
| 863 | 833 | print_u64(@sizeof(@typeof(array))); |
| 864 | 834 | print_str("\n"); |
| 865 | return 0; | |
| 866 | 835 | } |
| 867 | 836 | )SOURCE", "20\n"); |
| 868 | 837 | |
| 869 | 838 | add_simple_case("@min_value() and @max_value()", R"SOURCE( |
| 870 | 839 | import "std.zig"; |
| 871 | pub fn main(args: [][]u8) i32 => { | |
| 840 | pub fn main(args: [][]u8) %void => { | |
| 872 | 841 | print_str("max u8: "); |
| 873 | 842 | print_u64(@max_value(u8)); |
| 874 | 843 | print_str("\n"); |
| ... | ... | @@ -932,8 +901,6 @@ pub fn main(args: [][]u8) i32 => { |
| 932 | 901 | print_str("min i64: "); |
| 933 | 902 | print_i64(@min_value(i64)); |
| 934 | 903 | print_str("\n"); |
| 935 | ||
| 936 | return 0; | |
| 937 | 904 | } |
| 938 | 905 | )SOURCE", |
| 939 | 906 | "max u8: 255\n" |
| ... | ... | @@ -956,7 +923,7 @@ pub fn main(args: [][]u8) i32 => { |
| 956 | 923 | |
| 957 | 924 | add_simple_case("slicing", R"SOURCE( |
| 958 | 925 | import "std.zig"; |
| 959 | pub fn main(args: [][]u8) i32 => { | |
| 926 | pub fn main(args: [][]u8) %void => { | |
| 960 | 927 | var array : [20]i32; |
| 961 | 928 | |
| 962 | 929 | array[5] = 1234; |
| ... | ... | @@ -977,18 +944,16 @@ pub fn main(args: [][]u8) i32 => { |
| 977 | 944 | } |
| 978 | 945 | |
| 979 | 946 | print_str("OK\n"); |
| 980 | return 0; | |
| 981 | 947 | } |
| 982 | 948 | )SOURCE", "OK\n"); |
| 983 | 949 | |
| 984 | 950 | |
| 985 | 951 | add_simple_case("else if expression", R"SOURCE( |
| 986 | 952 | import "std.zig"; |
| 987 | pub fn main(args: [][]u8) i32 => { | |
| 953 | pub fn main(args: [][]u8) %void => { | |
| 988 | 954 | if (f(1) == 1) { |
| 989 | 955 | print_str("OK\n"); |
| 990 | 956 | } |
| 991 | return 0; | |
| 992 | 957 | } |
| 993 | 958 | fn f(c: u8) u8 => { |
| 994 | 959 | if (c == 0) { |
| ... | ... | @@ -1003,7 +968,7 @@ fn f(c: u8) u8 => { |
| 1003 | 968 | |
| 1004 | 969 | add_simple_case("overflow intrinsics", R"SOURCE( |
| 1005 | 970 | import "std.zig"; |
| 1006 | pub fn main(args: [][]u8) i32 => { | |
| 971 | pub fn main(args: [][]u8) %void => { | |
| 1007 | 972 | var result: u8; |
| 1008 | 973 | if (!@add_with_overflow(u8, 250, 100, &result)) { |
| 1009 | 974 | print_str("BAD\n"); |
| ... | ... | @@ -1015,13 +980,12 @@ pub fn main(args: [][]u8) i32 => { |
| 1015 | 980 | print_str("BAD\n"); |
| 1016 | 981 | } |
| 1017 | 982 | print_str("OK\n"); |
| 1018 | return 0; | |
| 1019 | 983 | } |
| 1020 | 984 | )SOURCE", "OK\n"); |
| 1021 | 985 | |
| 1022 | 986 | add_simple_case("memcpy and memset intrinsics", R"SOURCE( |
| 1023 | 987 | import "std.zig"; |
| 1024 | pub fn main(args: [][]u8) i32 => { | |
| 988 | pub fn main(args: [][]u8) %void => { | |
| 1025 | 989 | var foo : [20]u8; |
| 1026 | 990 | var bar : [20]u8; |
| 1027 | 991 | |
| ... | ... | @@ -1033,7 +997,6 @@ pub fn main(args: [][]u8) i32 => { |
| 1033 | 997 | } |
| 1034 | 998 | |
| 1035 | 999 | print_str("OK\n"); |
| 1036 | return 0; | |
| 1037 | 1000 | } |
| 1038 | 1001 | )SOURCE", "OK\n"); |
| 1039 | 1002 | |
| ... | ... | @@ -1042,8 +1005,8 @@ import "std.zig"; |
| 1042 | 1005 | const z : @typeof(stdin_fileno) = 0; |
| 1043 | 1006 | const x : @typeof(y) = 1234; |
| 1044 | 1007 | const y : u16 = 5678; |
| 1045 | pub fn main(args: [][]u8) i32 => { | |
| 1046 | print_ok(x) | |
| 1008 | pub fn main(args: [][]u8) %void => { | |
| 1009 | var x : i32 = print_ok(x); | |
| 1047 | 1010 | } |
| 1048 | 1011 | fn print_ok(val: @typeof(x)) @typeof(foo) => { |
| 1049 | 1012 | print_str("OK\n"); |
| ... | ... | @@ -1073,7 +1036,7 @@ enum Bar { |
| 1073 | 1036 | D, |
| 1074 | 1037 | } |
| 1075 | 1038 | |
| 1076 | pub fn main(args: [][]u8) i32 => { | |
| 1039 | pub fn main(args: [][]u8) %void => { | |
| 1077 | 1040 | const foo1 = Foo.One(13); |
| 1078 | 1041 | const foo2 = Foo.Two(Point { .x = 1234, .y = 5678, }); |
| 1079 | 1042 | const bar = Bar.B; |
| ... | ... | @@ -1098,15 +1061,13 @@ pub fn main(args: [][]u8) i32 => { |
| 1098 | 1061 | } |
| 1099 | 1062 | |
| 1100 | 1063 | print_str("OK\n"); |
| 1101 | ||
| 1102 | return 0; | |
| 1103 | 1064 | } |
| 1104 | 1065 | )SOURCE", "OK\n"); |
| 1105 | 1066 | |
| 1106 | 1067 | add_simple_case("array literal", R"SOURCE( |
| 1107 | 1068 | import "std.zig"; |
| 1108 | 1069 | |
| 1109 | pub fn main(args: [][]u8) i32 => { | |
| 1070 | pub fn main(args: [][]u8) %void => { | |
| 1110 | 1071 | const HEX_MULT = []u16{4096, 256, 16, 1}; |
| 1111 | 1072 | |
| 1112 | 1073 | if (HEX_MULT.len != 4) { |
| ... | ... | @@ -1118,14 +1079,13 @@ pub fn main(args: [][]u8) i32 => { |
| 1118 | 1079 | } |
| 1119 | 1080 | |
| 1120 | 1081 | print_str("OK\n"); |
| 1121 | return 0; | |
| 1122 | 1082 | } |
| 1123 | 1083 | )SOURCE", "OK\n"); |
| 1124 | 1084 | |
| 1125 | 1085 | add_simple_case("nested arrays", R"SOURCE( |
| 1126 | 1086 | import "std.zig"; |
| 1127 | 1087 | |
| 1128 | pub fn main(args: [][]u8) i32 => { | |
| 1088 | pub fn main(args: [][]u8) %void => { | |
| 1129 | 1089 | const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"}; |
| 1130 | 1090 | var i: @typeof(array_of_strings.len) = 0; |
| 1131 | 1091 | while (i < array_of_strings.len) { |
| ... | ... | @@ -1133,14 +1093,13 @@ pub fn main(args: [][]u8) i32 => { |
| 1133 | 1093 | print_str("\n"); |
| 1134 | 1094 | i += 1; |
| 1135 | 1095 | } |
| 1136 | return 0; | |
| 1137 | 1096 | } |
| 1138 | 1097 | )SOURCE", "hello\nthis\nis\nmy\nthing\n"); |
| 1139 | 1098 | |
| 1140 | 1099 | add_simple_case("for loops", R"SOURCE( |
| 1141 | 1100 | import "std.zig"; |
| 1142 | 1101 | |
| 1143 | pub fn main(args: [][]u8) i32 => { | |
| 1102 | pub fn main(args: [][]u8) %void => { | |
| 1144 | 1103 | const array = []u8 {9, 8, 7, 6}; |
| 1145 | 1104 | for (item, array) { |
| 1146 | 1105 | print_u64(item); |
| ... | ... | @@ -1159,20 +1118,18 @@ pub fn main(args: [][]u8) i32 => { |
| 1159 | 1118 | print_i64(index); |
| 1160 | 1119 | print_str("\n"); |
| 1161 | 1120 | } |
| 1162 | return 0; | |
| 1163 | 1121 | } |
| 1164 | 1122 | )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n"); |
| 1165 | 1123 | |
| 1166 | 1124 | add_simple_case("function pointers", R"SOURCE( |
| 1167 | 1125 | import "std.zig"; |
| 1168 | 1126 | |
| 1169 | pub fn main(args: [][]u8) i32 => { | |
| 1127 | pub fn main(args: [][]u8) %void => { | |
| 1170 | 1128 | const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, }; |
| 1171 | 1129 | for (f, fns) { |
| 1172 | 1130 | print_u64(f()); |
| 1173 | 1131 | print_str("\n"); |
| 1174 | 1132 | } |
| 1175 | return 0; | |
| 1176 | 1133 | } |
| 1177 | 1134 | |
| 1178 | 1135 | fn fn1() u32 => {5} |
| ... | ... | @@ -1191,7 +1148,7 @@ enum Foo { |
| 1191 | 1148 | D, |
| 1192 | 1149 | } |
| 1193 | 1150 | |
| 1194 | pub fn main(args: [][]u8) i32 => { | |
| 1151 | pub fn main(args: [][]u8) %void => { | |
| 1195 | 1152 | const foo = Foo.C; |
| 1196 | 1153 | const val: i32 = switch (foo) { |
| 1197 | 1154 | Foo.A => 1, |
| ... | ... | @@ -1204,7 +1161,6 @@ pub fn main(args: [][]u8) i32 => { |
| 1204 | 1161 | } |
| 1205 | 1162 | |
| 1206 | 1163 | print_str("OK\n"); |
| 1207 | return 0; | |
| 1208 | 1164 | } |
| 1209 | 1165 | )SOURCE", "OK\n"); |
| 1210 | 1166 | |
| ... | ... | @@ -1213,7 +1169,7 @@ import "std.zig"; |
| 1213 | 1169 | |
| 1214 | 1170 | const ten = 10; |
| 1215 | 1171 | |
| 1216 | pub fn main(args: [][]u8) i32 => { | |
| 1172 | pub fn main(args: [][]u8) %void => { | |
| 1217 | 1173 | const one = 1; |
| 1218 | 1174 | const eleven = ten + one; |
| 1219 | 1175 | |
| ... | ... | @@ -1222,7 +1178,6 @@ pub fn main(args: [][]u8) i32 => { |
| 1222 | 1178 | } |
| 1223 | 1179 | |
| 1224 | 1180 | print_str("OK\n"); |
| 1225 | return 0; | |
| 1226 | 1181 | } |
| 1227 | 1182 | )SOURCE", "OK\n"); |
| 1228 | 1183 | |
| ... | ... | @@ -1233,28 +1188,26 @@ struct Foo { |
| 1233 | 1188 | y: bool, |
| 1234 | 1189 | } |
| 1235 | 1190 | var foo = Foo { .x = 13, .y = true, }; |
| 1236 | pub fn main(args: [][]u8) i32 => { | |
| 1191 | pub fn main(args: [][]u8) %void => { | |
| 1237 | 1192 | foo.x += 1; |
| 1238 | 1193 | if (foo.x != 14) { |
| 1239 | 1194 | print_str("BAD\n"); |
| 1240 | 1195 | } |
| 1241 | 1196 | |
| 1242 | 1197 | print_str("OK\n"); |
| 1243 | return 0; | |
| 1244 | 1198 | } |
| 1245 | 1199 | )SOURCE", "OK\n"); |
| 1246 | 1200 | |
| 1247 | 1201 | add_simple_case("statically initialized array literal", R"SOURCE( |
| 1248 | 1202 | import "std.zig"; |
| 1249 | 1203 | const x = []u8{1,2,3,4}; |
| 1250 | pub fn main(args: [][]u8) i32 => { | |
| 1204 | pub fn main(args: [][]u8) %void => { | |
| 1251 | 1205 | const y : [4]u8 = x; |
| 1252 | 1206 | if (y[3] != 4) { |
| 1253 | 1207 | print_str("BAD\n"); |
| 1254 | 1208 | } |
| 1255 | 1209 | |
| 1256 | 1210 | print_str("OK\n"); |
| 1257 | return 0; | |
| 1258 | 1211 | } |
| 1259 | 1212 | )SOURCE", "OK\n"); |
| 1260 | 1213 | |
| ... | ... | @@ -1262,7 +1215,7 @@ pub fn main(args: [][]u8) i32 => { |
| 1262 | 1215 | import "std.zig"; |
| 1263 | 1216 | %.err1; |
| 1264 | 1217 | %.err2; |
| 1265 | pub fn main(args: [][]u8) i32 => { | |
| 1218 | pub fn main(args: [][]u8) %void => { | |
| 1266 | 1219 | const a = i32(%.err1); |
| 1267 | 1220 | const b = i32(%.err2); |
| 1268 | 1221 | if (a == b) { |
| ... | ... | @@ -1270,7 +1223,6 @@ pub fn main(args: [][]u8) i32 => { |
| 1270 | 1223 | } |
| 1271 | 1224 | |
| 1272 | 1225 | print_str("OK\n"); |
| 1273 | return 0; | |
| 1274 | 1226 | } |
| 1275 | 1227 | )SOURCE", "OK\n"); |
| 1276 | 1228 |