| author | |
| committer | |
| log | 37d167f6e0e26e8dc57950cb0fa1bfa630036521 |
| tree | 8b947d065ef12b189cb7a4c880c0009e7a6cd2b8 |
| parent | 0ae9023832584e256aa3b6df0f0829026141d21a |
26 files changed, 707 insertions(+), 686 deletions(-)
doc/style.md+59-3| ... | ... | @@ -5,12 +5,68 @@ this documentation along with the compiler in order to provide a point of |
| 5 | 5 | reference, should anyone wish to point to an authority on agreed upon Zig |
| 6 | 6 | coding style. |
| 7 | 7 | |
| 8 | ## Whitespace | |
| 9 | ||
| 8 | 10 | * 4 space indentation |
| 9 | * `camelCaseFunctionName` | |
| 10 | * `TitleCaseTypeName` | |
| 11 | * `snake_case_variable_name` | |
| 12 | 11 | * Open braces on same line, unless you need to wrap. |
| 13 | 12 | * If a list of things is longer than 2, put each item on its own line and |
| 14 | 13 | exercise the abilty to put an extra comma at the end. |
| 14 | * Line length: aim for 100; use common sense. | |
| 15 | ||
| 16 | ## Names | |
| 17 | ||
| 18 | Roughly speaking: `camelCaseFunctionName`, `TitleCaseTypeName`, | |
| 19 | `snake_case_variable_name`. More precisely: | |
| 20 | ||
| 21 | * If `x` is a `struct` (or an alias of a `struct`), then `x` should be `TitleCase`. | |
| 22 | * If `x` otherwise identifies a type, `x` should have `snake_case`. | |
| 23 | * If `x` is callable, and `x`'s return type is `type`, then `x` should be `TitleCase`. | |
| 24 | * If `x` is otherwise callable, then `x` should be `camelCase`. | |
| 25 | * Otherwise, `x` should be `snake_case`. | |
| 26 | ||
| 27 | Acronyms, initialisms, proper nouns, or any other word that has capitalization | |
| 28 | rules in written English are subject to naming conventions just like any other | |
| 29 | word. Even acronyms that are only 2 letters long are subject to these | |
| 30 | conventions. | |
| 31 | ||
| 32 | Examples: | |
| 33 | ||
| 34 | ```zig | |
| 35 | const namespace_name = @import("dir_name/file_name.zig"); | |
| 36 | var global_var: i32; | |
| 37 | const const_name = 42; | |
| 38 | const primitive_type_alias = f32; | |
| 39 | const string_alias = []u8; | |
| 40 | ||
| 41 | struct StructName {} | |
| 42 | const StructAlias = StructName; | |
| 43 | ||
| 44 | fn functionName(param_name: TypeName) { | |
| 45 | var functionPointer = functionName; | |
| 46 | functionPointer(); | |
| 47 | functionPointer = otherFunction; | |
| 48 | functionPointer(); | |
| 49 | } | |
| 50 | const functionAlias = functionName; | |
| 51 | ||
| 52 | fn ListTemplateFunction(ChildType: type, inline fixed_size: usize) -> type { | |
| 53 | struct ShortList(T: type, n: usize) { | |
| 54 | field_name: [n]T, | |
| 55 | fn methodName() {} | |
| 56 | } | |
| 57 | return List(ChildType, fixed_size); | |
| 58 | } | |
| 59 | ||
| 60 | // The word XML loses its casing when used in Zig identifiers. | |
| 61 | const xml_document = | |
| 62 | \\<?xml version="1.0" encoding="UTF-8"?> | |
| 63 | \\<document> | |
| 64 | \\</document> | |
| 65 | ; | |
| 66 | struct XmlParser {} | |
| 67 | ||
| 68 | // The initials BE (Big Endian) are just another word in Zig identifier names. | |
| 69 | fn readU32Be() -> u32 {} | |
| 70 | ``` | |
| 15 | 71 | |
| 16 | 72 | See Zig standard library for examples. |
example/cat/main.zig+3-3| ... | ... | @@ -16,7 +16,7 @@ pub fn main(args: [][]u8) -> %void { |
| 16 | 16 | } else { |
| 17 | 17 | var is = io.InStream.open(arg) %% |err| { |
| 18 | 18 | %%io.stderr.printf("Unable to open file: "); |
| 19 | %%io.stderr.printf(@err_name(err)); | |
| 19 | %%io.stderr.printf(@errName(err)); | |
| 20 | 20 | %%io.stderr.printf("\n"); |
| 21 | 21 | return err; |
| 22 | 22 | }; |
| ... | ... | @@ -45,7 +45,7 @@ fn cat_stream(is: io.InStream) -> %void { |
| 45 | 45 | while (true) { |
| 46 | 46 | const bytes_read = is.read(buf) %% |err| { |
| 47 | 47 | %%io.stderr.printf("Unable to read from stream: "); |
| 48 | %%io.stderr.printf(@err_name(err)); | |
| 48 | %%io.stderr.printf(@errName(err)); | |
| 49 | 49 | %%io.stderr.printf("\n"); |
| 50 | 50 | return err; |
| 51 | 51 | }; |
| ... | ... | @@ -56,7 +56,7 @@ fn cat_stream(is: io.InStream) -> %void { |
| 56 | 56 | |
| 57 | 57 | io.stdout.write(buf[0...bytes_read]) %% |err| { |
| 58 | 58 | %%io.stderr.printf("Unable to write to stdout: "); |
| 59 | %%io.stderr.printf(@err_name(err)); | |
| 59 | %%io.stderr.printf(@errName(err)); | |
| 60 | 60 | %%io.stderr.printf("\n"); |
| 61 | 61 | return err; |
| 62 | 62 | }; |
example/guess_number/main.zig+5-4| ... | ... | @@ -6,11 +6,12 @@ const os = std.os; |
| 6 | 6 | pub fn main(args: [][]u8) -> %void { |
| 7 | 7 | %%io.stdout.printf("Welcome to the Guess Number Game in Zig.\n"); |
| 8 | 8 | |
| 9 | var seed: [@sizeof(usize)]u8 = undefined; | |
| 9 | var seed: [@sizeOf(usize)]u8 = undefined; | |
| 10 | 10 | %%os.get_random_bytes(seed); |
| 11 | var rand = Rand.init(([]usize)(seed)[0]); | |
| 11 | var rand: Rand = undefined; | |
| 12 | rand.init(([]usize)(seed)[0]); | |
| 12 | 13 | |
| 13 | const answer = rand.range_unsigned(u8, 0, 100) + 1; | |
| 14 | const answer = rand.rangeUnsigned(u8, 0, 100) + 1; | |
| 14 | 15 | |
| 15 | 16 | while (true) { |
| 16 | 17 | %%io.stdout.printf("\nGuess a number between 1 and 100: "); |
| ... | ... | @@ -21,7 +22,7 @@ pub fn main(args: [][]u8) -> %void { |
| 21 | 22 | return err; |
| 22 | 23 | }; |
| 23 | 24 | |
| 24 | const guess = io.parse_unsigned(u8, line_buf[0...line_len - 1], 10) %% { | |
| 25 | const guess = io.parseUnsigned(u8, line_buf[0...line_len - 1], 10) %% { | |
| 25 | 26 | %%io.stdout.printf("Invalid number.\n"); |
| 26 | 27 | continue; |
| 27 | 28 | }; |
src/analyze.cpp+1-1| ... | ... | @@ -5206,7 +5206,7 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry |
| 5206 | 5206 | case TypeTableEntryIdNamespace: |
| 5207 | 5207 | case TypeTableEntryIdGenericFn: |
| 5208 | 5208 | add_node_error(g, expr_node, |
| 5209 | buf_sprintf("type '%s' not eligible for @typeof", buf_ptr(&type_entry->name))); | |
| 5209 | buf_sprintf("type '%s' not eligible for @typeOf", buf_ptr(&type_entry->name))); | |
| 5210 | 5210 | return g->builtin_types.entry_invalid; |
| 5211 | 5211 | case TypeTableEntryIdMetaType: |
| 5212 | 5212 | case TypeTableEntryIdVoid: |
src/codegen.cpp+23-23| ... | ... | @@ -4642,7 +4642,7 @@ static void define_builtin_fns(CodeGen *g) { |
| 4642 | 4642 | } |
| 4643 | 4643 | { |
| 4644 | 4644 | BuiltinFnEntry *builtin_fn = create_builtin_fn_with_arg_count(g, BuiltinFnIdReturnAddress, |
| 4645 | "return_address", 0); | |
| 4645 | "returnAddress", 0); | |
| 4646 | 4646 | builtin_fn->return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true); |
| 4647 | 4647 | |
| 4648 | 4648 | LLVMTypeRef fn_type = LLVMFunctionType(builtin_fn->return_type->type_ref, |
| ... | ... | @@ -4652,7 +4652,7 @@ static void define_builtin_fns(CodeGen *g) { |
| 4652 | 4652 | } |
| 4653 | 4653 | { |
| 4654 | 4654 | BuiltinFnEntry *builtin_fn = create_builtin_fn_with_arg_count(g, BuiltinFnIdFrameAddress, |
| 4655 | "frame_address", 0); | |
| 4655 | "frameAddress", 0); | |
| 4656 | 4656 | builtin_fn->return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true); |
| 4657 | 4657 | |
| 4658 | 4658 | LLVMTypeRef fn_type = LLVMFunctionType(builtin_fn->return_type->type_ref, |
| ... | ... | @@ -4708,33 +4708,33 @@ static void define_builtin_fns(CodeGen *g) { |
| 4708 | 4708 | |
| 4709 | 4709 | g->memset_fn_val = builtin_fn->fn_val; |
| 4710 | 4710 | } |
| 4711 | create_builtin_fn_with_arg_count(g, BuiltinFnIdSizeof, "sizeof", 1); | |
| 4712 | create_builtin_fn_with_arg_count(g, BuiltinFnIdAlignof, "alignof", 1); | |
| 4713 | create_builtin_fn_with_arg_count(g, BuiltinFnIdMaxValue, "max_value", 1); | |
| 4714 | create_builtin_fn_with_arg_count(g, BuiltinFnIdMinValue, "min_value", 1); | |
| 4715 | create_builtin_fn_with_arg_count(g, BuiltinFnIdMemberCount, "member_count", 1); | |
| 4716 | create_builtin_fn_with_arg_count(g, BuiltinFnIdTypeof, "typeof", 1); | |
| 4717 | create_builtin_fn_with_arg_count(g, BuiltinFnIdAddWithOverflow, "add_with_overflow", 4); | |
| 4718 | create_builtin_fn_with_arg_count(g, BuiltinFnIdSubWithOverflow, "sub_with_overflow", 4); | |
| 4719 | create_builtin_fn_with_arg_count(g, BuiltinFnIdMulWithOverflow, "mul_with_overflow", 4); | |
| 4720 | create_builtin_fn_with_arg_count(g, BuiltinFnIdShlWithOverflow, "shl_with_overflow", 4); | |
| 4721 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCInclude, "c_include", 1); | |
| 4722 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCDefine, "c_define", 2); | |
| 4723 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCUndef, "c_undef", 1); | |
| 4724 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileVar, "compile_var", 1); | |
| 4725 | create_builtin_fn_with_arg_count(g, BuiltinFnIdConstEval, "const_eval", 1); | |
| 4711 | create_builtin_fn_with_arg_count(g, BuiltinFnIdSizeof, "sizeOf", 1); | |
| 4712 | create_builtin_fn_with_arg_count(g, BuiltinFnIdAlignof, "alignOf", 1); | |
| 4713 | create_builtin_fn_with_arg_count(g, BuiltinFnIdMaxValue, "maxValue", 1); | |
| 4714 | create_builtin_fn_with_arg_count(g, BuiltinFnIdMinValue, "minValue", 1); | |
| 4715 | create_builtin_fn_with_arg_count(g, BuiltinFnIdMemberCount, "memberCount", 1); | |
| 4716 | create_builtin_fn_with_arg_count(g, BuiltinFnIdTypeof, "typeOf", 1); | |
| 4717 | create_builtin_fn_with_arg_count(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4); | |
| 4718 | create_builtin_fn_with_arg_count(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4); | |
| 4719 | create_builtin_fn_with_arg_count(g, BuiltinFnIdMulWithOverflow, "mulWithOverflow", 4); | |
| 4720 | create_builtin_fn_with_arg_count(g, BuiltinFnIdShlWithOverflow, "shlWithOverflow", 4); | |
| 4721 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCInclude, "cInclude", 1); | |
| 4722 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCDefine, "cDefine", 2); | |
| 4723 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCUndef, "cUndef", 1); | |
| 4724 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileVar, "compileVar", 1); | |
| 4725 | create_builtin_fn_with_arg_count(g, BuiltinFnIdConstEval, "constEval", 1); | |
| 4726 | 4726 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCtz, "ctz", 2); |
| 4727 | 4727 | create_builtin_fn_with_arg_count(g, BuiltinFnIdClz, "clz", 2); |
| 4728 | 4728 | create_builtin_fn_with_arg_count(g, BuiltinFnIdImport, "import", 1); |
| 4729 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCImport, "c_import", 1); | |
| 4730 | create_builtin_fn_with_arg_count(g, BuiltinFnIdErrName, "err_name", 1); | |
| 4731 | create_builtin_fn_with_arg_count(g, BuiltinFnIdEmbedFile, "embed_file", 1); | |
| 4729 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCImport, "cImport", 1); | |
| 4730 | create_builtin_fn_with_arg_count(g, BuiltinFnIdErrName, "errName", 1); | |
| 4731 | create_builtin_fn_with_arg_count(g, BuiltinFnIdEmbedFile, "embedFile", 1); | |
| 4732 | 4732 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCmpExchange, "cmpxchg", 5); |
| 4733 | 4733 | create_builtin_fn_with_arg_count(g, BuiltinFnIdFence, "fence", 1); |
| 4734 | create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "div_exact", 2); | |
| 4734 | create_builtin_fn_with_arg_count(g, BuiltinFnIdDivExact, "divExact", 2); | |
| 4735 | 4735 | create_builtin_fn_with_arg_count(g, BuiltinFnIdTruncate, "truncate", 2); |
| 4736 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compile_err", 1); | |
| 4737 | create_builtin_fn_with_arg_count(g, BuiltinFnIdIntType, "int_type", 2); | |
| 4736 | create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileErr, "compileErr", 1); | |
| 4737 | create_builtin_fn_with_arg_count(g, BuiltinFnIdIntType, "intType", 2); | |
| 4738 | 4738 | } |
| 4739 | 4739 | |
| 4740 | 4740 | static void init(CodeGen *g, Buf *source_path) { |
std/bootstrap.zig+8-8| ... | ... | @@ -4,7 +4,7 @@ const root = @import("@root"); |
| 4 | 4 | const linux = @import("linux.zig"); |
| 5 | 5 | const cstr = @import("cstr.zig"); |
| 6 | 6 | |
| 7 | const want_start_symbol = switch(@compile_var("os")) { | |
| 7 | const want_start_symbol = switch(@compileVar("os")) { | |
| 8 | 8 | linux => true, |
| 9 | 9 | else => false, |
| 10 | 10 | }; |
| ... | ... | @@ -16,7 +16,7 @@ var argv: &&u8 = undefined; |
| 16 | 16 | #attribute("naked") |
| 17 | 17 | #condition(want_start_symbol) |
| 18 | 18 | export fn _start() -> unreachable { |
| 19 | switch (@compile_var("arch")) { | |
| 19 | switch (@compileVar("arch")) { | |
| 20 | 20 | x86_64 => { |
| 21 | 21 | argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize)); |
| 22 | 22 | argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8)); |
| ... | ... | @@ -25,12 +25,12 @@ export fn _start() -> unreachable { |
| 25 | 25 | argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize)); |
| 26 | 26 | argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8)); |
| 27 | 27 | }, |
| 28 | else => @compile_err("unsupported arch"), | |
| 28 | else => @compileErr("unsupported arch"), | |
| 29 | 29 | } |
| 30 | call_main_and_exit() | |
| 30 | callMainAndExit() | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | fn call_main() -> %void { | |
| 33 | fn callMain() -> %void { | |
| 34 | 34 | var args: [argc][]u8 = undefined; |
| 35 | 35 | for (args) |arg, i| { |
| 36 | 36 | const ptr = argv[i]; |
| ... | ... | @@ -39,8 +39,8 @@ fn call_main() -> %void { |
| 39 | 39 | return root.main(args); |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | fn call_main_and_exit() -> unreachable { | |
| 43 | call_main() %% linux.exit(1); | |
| 42 | fn callMainAndExit() -> unreachable { | |
| 43 | callMain() %% linux.exit(1); | |
| 44 | 44 | linux.exit(0); |
| 45 | 45 | } |
| 46 | 46 | |
| ... | ... | @@ -48,6 +48,6 @@ fn call_main_and_exit() -> unreachable { |
| 48 | 48 | export fn main(c_argc: i32, c_argv: &&u8) -> i32 { |
| 49 | 49 | argc = usize(c_argc); |
| 50 | 50 | argv = c_argv; |
| 51 | call_main() %% return 1; | |
| 51 | callMain() %% return 1; | |
| 52 | 52 | return 0; |
| 53 | 53 | } |
std/compiler_rt.zig+5-5| ... | ... | @@ -5,7 +5,7 @@ const si_int = c_int; |
| 5 | 5 | const su_int = c_uint; |
| 6 | 6 | |
| 7 | 7 | const udwords = [2]su_int; |
| 8 | const low = if (@compile_var("is_big_endian")) 1 else 0; | |
| 8 | const low = if (@compileVar("is_big_endian")) 1 else 0; | |
| 9 | 9 | const high = 1 - low; |
| 10 | 10 | |
| 11 | 11 | #debug_safety(false) |
| ... | ... | @@ -20,8 +20,8 @@ fn du_int_to_udwords(x: du_int) -> udwords { |
| 20 | 20 | |
| 21 | 21 | #debug_safety(false) |
| 22 | 22 | export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int { |
| 23 | const n_uword_bits = @sizeof(su_int) * CHAR_BIT; | |
| 24 | const n_udword_bits = @sizeof(du_int) * CHAR_BIT; | |
| 23 | const n_uword_bits = @sizeOf(su_int) * CHAR_BIT; | |
| 24 | const n_udword_bits = @sizeOf(du_int) * CHAR_BIT; | |
| 25 | 25 | var n = du_int_to_udwords(a); |
| 26 | 26 | var d = du_int_to_udwords(b); |
| 27 | 27 | var q: udwords = undefined; |
| ... | ... | @@ -79,7 +79,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int { |
| 79 | 79 | r[high] = n[high] & (d[high] - 1); |
| 80 | 80 | *rem = *(&du_int)(&r[0]); |
| 81 | 81 | } |
| 82 | return n[high] >> @ctz(@typeof(d[high]), d[high]); | |
| 82 | return n[high] >> @ctz(@typeOf(d[high]), d[high]); | |
| 83 | 83 | } |
| 84 | 84 | // K K |
| 85 | 85 | // --- |
| ... | ... | @@ -114,7 +114,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int { |
| 114 | 114 | if (d[low] == 1) { |
| 115 | 115 | return *(&du_int)(&n[0]); |
| 116 | 116 | } |
| 117 | sr = @ctz(@typeof(d[low]), d[low]); | |
| 117 | sr = @ctz(@typeOf(d[low]), d[low]); | |
| 118 | 118 | q[high] = n[high] >> sr; |
| 119 | 119 | q[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); |
| 120 | 120 | return *(&du_int)(&q[0]); |
std/cstr.zig+34-34| ... | ... | @@ -24,11 +24,11 @@ pub fn cmp(a: &const u8, b: &const u8) -> i32 { |
| 24 | 24 | return a[index] - b[index]; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | pub fn to_slice_const(str: &const u8) -> []const u8 { | |
| 27 | pub fn toSliceConst(str: &const u8) -> []const u8 { | |
| 28 | 28 | return str[0...strlen(str)]; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | pub fn to_slice(str: &u8) -> []u8 { | |
| 31 | pub fn toSlice(str: &u8) -> []u8 { | |
| 32 | 32 | return str[0...strlen(str)]; |
| 33 | 33 | } |
| 34 | 34 | |
| ... | ... | @@ -46,25 +46,25 @@ pub struct CBuf { |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | /// Must deinitialize with deinit. |
| 49 | pub fn init_from_mem(self: &CBuf, allocator: &Allocator, m: []const u8) -> %void { | |
| 49 | pub fn initFromMem(self: &CBuf, allocator: &Allocator, m: []const u8) -> %void { | |
| 50 | 50 | self.init(allocator); |
| 51 | 51 | %return self.resize(m.len); |
| 52 | 52 | mem.copy(u8, self.list.items, m); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /// Must deinitialize with deinit. |
| 56 | pub fn init_from_cstr(self: &CBuf, allocator: &Allocator, s: &const u8) -> %void { | |
| 57 | self.init_from_mem(allocator, s[0...strlen(s)]) | |
| 56 | pub fn initFromCStr(self: &CBuf, allocator: &Allocator, s: &const u8) -> %void { | |
| 57 | self.initFromMem(allocator, s[0...strlen(s)]) | |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /// Must deinitialize with deinit. |
| 61 | pub fn init_from_cbuf(self: &CBuf, cbuf: &const CBuf) -> %void { | |
| 62 | self.init_from_mem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]) | |
| 61 | pub fn initFromCBuf(self: &CBuf, cbuf: &const CBuf) -> %void { | |
| 62 | self.initFromMem(cbuf.list.allocator, cbuf.list.items[0...cbuf.len()]) | |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /// Must deinitialize with deinit. |
| 66 | pub fn init_from_slice(self: &CBuf, other: &const CBuf, start: usize, end: usize) -> %void { | |
| 67 | self.init_from_mem(other.list.allocator, other.list.items[start...end]) | |
| 66 | pub fn initFromSlice(self: &CBuf, other: &const CBuf, start: usize, end: usize) -> %void { | |
| 67 | self.initFromMem(other.list.allocator, other.list.items[start...end]) | |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | pub fn deinit(self: &CBuf) { |
| ... | ... | @@ -80,66 +80,66 @@ pub struct CBuf { |
| 80 | 80 | return self.list.len - 1; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | pub fn append_mem(self: &CBuf, m: []const u8) -> %void { | |
| 83 | pub fn appendMem(self: &CBuf, m: []const u8) -> %void { | |
| 84 | 84 | const old_len = self.len(); |
| 85 | 85 | %return self.resize(old_len + m.len); |
| 86 | 86 | mem.copy(u8, self.list.items[old_len...], m); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | pub fn append_cstr(self: &CBuf, s: &const u8) -> %void { | |
| 90 | self.append_mem(s[0...strlen(s)]) | |
| 89 | pub fn appendCStr(self: &CBuf, s: &const u8) -> %void { | |
| 90 | self.appendMem(s[0...strlen(s)]) | |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | pub fn append_char(self: &CBuf, c: u8) -> %void { | |
| 93 | pub fn appendChar(self: &CBuf, c: u8) -> %void { | |
| 94 | 94 | %return self.resize(self.len() + 1); |
| 95 | 95 | self.list.items[self.len() - 1] = c; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | pub fn eql_mem(self: &const CBuf, m: []const u8) -> bool { | |
| 98 | pub fn eqlMem(self: &const CBuf, m: []const u8) -> bool { | |
| 99 | 99 | if (self.len() != m.len) return false; |
| 100 | 100 | return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | pub fn eql_cstr(self: &const CBuf, s: &const u8) -> bool { | |
| 104 | self.eql_mem(s[0...strlen(s)]) | |
| 103 | pub fn eqlCStr(self: &const CBuf, s: &const u8) -> bool { | |
| 104 | self.eqlMem(s[0...strlen(s)]) | |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | pub fn eql_cbuf(self: &const CBuf, other: &const CBuf) -> bool { | |
| 108 | self.eql_mem(other.list.items[0...other.len()]) | |
| 107 | pub fn eqlCBuf(self: &const CBuf, other: &const CBuf) -> bool { | |
| 108 | self.eqlMem(other.list.items[0...other.len()]) | |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | pub fn starts_with_mem(self: &const CBuf, m: []const u8) -> bool { | |
| 111 | pub fn startsWithMem(self: &const CBuf, m: []const u8) -> bool { | |
| 112 | 112 | if (self.len() < m.len) return false; |
| 113 | 113 | return mem.cmp(u8, self.list.items[0...m.len], m) == mem.Cmp.Equal; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | pub fn starts_with_cbuf(self: &const CBuf, other: &const CBuf) -> bool { | |
| 117 | self.starts_with_mem(other.list.items[0...other.len()]) | |
| 116 | pub fn startsWithCBuf(self: &const CBuf, other: &const CBuf) -> bool { | |
| 117 | self.startsWithMem(other.list.items[0...other.len()]) | |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | pub fn starts_with_cstr(self: &const CBuf, s: &const u8) -> bool { | |
| 121 | self.starts_with_mem(s[0...strlen(s)]) | |
| 120 | pub fn startsWithCStr(self: &const CBuf, s: &const u8) -> bool { | |
| 121 | self.startsWithMem(s[0...strlen(s)]) | |
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | #attribute("test") |
| 126 | fn test_simple_cbuf() { | |
| 126 | fn testSimpleCBuf() { | |
| 127 | 127 | var buf: CBuf = undefined; |
| 128 | 128 | buf.init(&debug.global_allocator); |
| 129 | 129 | assert(buf.len() == 0); |
| 130 | %%buf.append_cstr(c"hello"); | |
| 131 | %%buf.append_char(' '); | |
| 132 | %%buf.append_mem("world"); | |
| 133 | assert(buf.eql_cstr(c"hello world")); | |
| 134 | assert(buf.eql_mem("hello world")); | |
| 130 | %%buf.appendCStr(c"hello"); | |
| 131 | %%buf.appendChar(' '); | |
| 132 | %%buf.appendMem("world"); | |
| 133 | assert(buf.eqlCStr(c"hello world")); | |
| 134 | assert(buf.eqlMem("hello world")); | |
| 135 | 135 | |
| 136 | 136 | var buf2: CBuf = undefined; |
| 137 | %%buf2.init_from_cbuf(&buf); | |
| 138 | assert(buf.eql_cbuf(&buf2)); | |
| 137 | %%buf2.initFromCBuf(&buf); | |
| 138 | assert(buf.eqlCBuf(&buf2)); | |
| 139 | 139 | |
| 140 | assert(buf.starts_with_mem("hell")); | |
| 141 | assert(buf.starts_with_cstr(c"hell")); | |
| 140 | assert(buf.startsWithMem("hell")); | |
| 141 | assert(buf.startsWithCStr(c"hell")); | |
| 142 | 142 | |
| 143 | 143 | %%buf2.resize(4); |
| 144 | assert(buf.starts_with_cbuf(&buf2)); | |
| 144 | assert(buf.startsWithCBuf(&buf2)); | |
| 145 | 145 | } |
std/debug.zig+5-5| ... | ... | @@ -6,10 +6,10 @@ pub fn assert(b: bool) { |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | pub fn printStackTrace() { |
| 9 | var maybe_fp: ?&const u8 = @frame_address(); | |
| 9 | var maybe_fp: ?&const u8 = @frameAddress(); | |
| 10 | 10 | while (true) { |
| 11 | 11 | const fp = maybe_fp ?? break; |
| 12 | const return_address = *(&const usize)(usize(fp) + @sizeof(usize)); | |
| 12 | const return_address = *(&const usize)(usize(fp) + @sizeOf(usize)); | |
| 13 | 13 | %%io.stderr.print_u64(return_address); |
| 14 | 14 | %%io.stderr.printf("\n"); |
| 15 | 15 | maybe_fp = *(&const ?&const u8)(fp); |
| ... | ... | @@ -17,9 +17,9 @@ pub fn printStackTrace() { |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | pub var global_allocator = Allocator { |
| 20 | .alloc_fn = globalAlloc, | |
| 21 | .realloc_fn = globalRealloc, | |
| 22 | .free_fn = globalFree, | |
| 20 | .allocFn = globalAlloc, | |
| 21 | .reallocFn = globalRealloc, | |
| 22 | .freeFn = globalFree, | |
| 23 | 23 | .context = null, |
| 24 | 24 | }; |
| 25 | 25 |
std/hash_map.zig+22-22| ... | ... | @@ -4,27 +4,27 @@ const math = @import("math.zig"); |
| 4 | 4 | const mem = @import("mem.zig"); |
| 5 | 5 | const Allocator = mem.Allocator; |
| 6 | 6 | |
| 7 | const want_modification_safety = !@compile_var("is_release"); | |
| 7 | const want_modification_safety = !@compileVar("is_release"); | |
| 8 | 8 | const debug_u32 = if (want_modification_safety) u32 else void; |
| 9 | 9 | |
| 10 | 10 | pub fn HashMap(inline K: type, inline V: type, inline hash: fn(key: K)->u32, |
| 11 | 11 | inline eql: fn(a: K, b: K)->bool) -> type |
| 12 | 12 | { |
| 13 | SmallHashMap(K, V, hash, eql, @sizeof(usize)) | |
| 13 | SmallHashMap(K, V, hash, eql, @sizeOf(usize)) | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b: K)->bool, STATIC_SIZE: usize) { | |
| 16 | pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b: K)->bool, static_size: usize) { | |
| 17 | 17 | entries: []Entry, |
| 18 | 18 | size: usize, |
| 19 | 19 | max_distance_from_start_index: usize, |
| 20 | 20 | allocator: &Allocator, |
| 21 | 21 | // if the hash map is small enough, we use linear search through these |
| 22 | 22 | // entries instead of allocating memory |
| 23 | prealloc_entries: [STATIC_SIZE]Entry, | |
| 23 | prealloc_entries: [static_size]Entry, | |
| 24 | 24 | // this is used to detect bugs where a hashtable is edited while an iterator is running. |
| 25 | 25 | modification_count: debug_u32, |
| 26 | 26 | |
| 27 | const Self = SmallHashMap(K, V, hash, eql, STATIC_SIZE); | |
| 27 | const Self = SmallHashMap(K, V, hash, eql, static_size); | |
| 28 | 28 | |
| 29 | 29 | pub struct Entry { |
| 30 | 30 | used: bool, |
| ... | ... | @@ -80,11 +80,11 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b |
| 80 | 80 | } |
| 81 | 81 | hm.size = 0; |
| 82 | 82 | hm.max_distance_from_start_index = 0; |
| 83 | hm.increment_modification_count(); | |
| 83 | hm.incrementModificationCount(); | |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | pub fn put(hm: &Self, key: K, value: V) -> %void { |
| 87 | hm.increment_modification_count(); | |
| 87 | hm.incrementModificationCount(); | |
| 88 | 88 | |
| 89 | 89 | const resize = if (hm.entries.ptr == &hm.prealloc_entries[0]) { |
| 90 | 90 | // preallocated entries table is full |
| ... | ... | @@ -95,11 +95,11 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b |
| 95 | 95 | }; |
| 96 | 96 | if (resize) { |
| 97 | 97 | const old_entries = hm.entries; |
| 98 | %return hm.init_capacity(hm.entries.len * 2); | |
| 98 | %return hm.initCapacity(hm.entries.len * 2); | |
| 99 | 99 | // dump all of the old elements into the new table |
| 100 | 100 | for (old_entries) |*old_entry| { |
| 101 | 101 | if (old_entry.used) { |
| 102 | hm.internal_put(old_entry.key, old_entry.value); | |
| 102 | hm.internalPut(old_entry.key, old_entry.value); | |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | if (old_entries.ptr != &hm.prealloc_entries[0]) { |
| ... | ... | @@ -107,16 +107,16 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b |
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | hm.internal_put(key, value); | |
| 110 | hm.internalPut(key, value); | |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | pub fn get(hm: &Self, key: K) -> ?&Entry { |
| 114 | return hm.internal_get(key); | |
| 114 | return hm.internalGet(key); | |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | pub fn remove(hm: &Self, key: K) { |
| 118 | hm.increment_modification_count(); | |
| 119 | const start_index = hm.key_to_index(key); | |
| 118 | hm.incrementModificationCount(); | |
| 119 | const start_index = hm.keyToIndex(key); | |
| 120 | 120 | {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) { |
| 121 | 121 | const index = (start_index + roll_over) % hm.entries.len; |
| 122 | 122 | var entry = &hm.entries[index]; |
| ... | ... | @@ -142,7 +142,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b |
| 142 | 142 | unreachable{} // key not found |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | pub fn entry_iterator(hm: &Self) -> Iterator { | |
| 145 | pub fn entryIterator(hm: &Self) -> Iterator { | |
| 146 | 146 | return Iterator { |
| 147 | 147 | .hm = hm, |
| 148 | 148 | .count = 0, |
| ... | ... | @@ -151,7 +151,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b |
| 151 | 151 | }; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | fn init_capacity(hm: &Self, capacity: usize) -> %void { | |
| 154 | fn initCapacity(hm: &Self, capacity: usize) -> %void { | |
| 155 | 155 | hm.entries = %return hm.allocator.alloc(Entry, capacity); |
| 156 | 156 | hm.size = 0; |
| 157 | 157 | hm.max_distance_from_start_index = 0; |
| ... | ... | @@ -160,16 +160,16 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b |
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | fn increment_modification_count(hm: &Self) { | |
| 163 | fn incrementModificationCount(hm: &Self) { | |
| 164 | 164 | if (want_modification_safety) { |
| 165 | 165 | hm.modification_count +%= 1; |
| 166 | 166 | } |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | fn internal_put(hm: &Self, orig_key: K, orig_value: V) { | |
| 169 | fn internalPut(hm: &Self, orig_key: K, orig_value: V) { | |
| 170 | 170 | var key = orig_key; |
| 171 | 171 | var value = orig_value; |
| 172 | const start_index = hm.key_to_index(key); | |
| 172 | const start_index = hm.keyToIndex(key); | |
| 173 | 173 | var roll_over: usize = 0; |
| 174 | 174 | var distance_from_start_index: usize = 0; |
| 175 | 175 | while (roll_over < hm.entries.len; {roll_over += 1; distance_from_start_index += 1}) { |
| ... | ... | @@ -214,8 +214,8 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b |
| 214 | 214 | unreachable{} // put into a full map |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | fn internal_get(hm: &Self, key: K) -> ?&Entry { | |
| 218 | const start_index = hm.key_to_index(key); | |
| 217 | fn internalGet(hm: &Self, key: K) -> ?&Entry { | |
| 218 | const start_index = hm.keyToIndex(key); | |
| 219 | 219 | {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) { |
| 220 | 220 | const index = (start_index + roll_over) % hm.entries.len; |
| 221 | 221 | const entry = &hm.entries[index]; |
| ... | ... | @@ -226,13 +226,13 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b |
| 226 | 226 | return null; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | fn key_to_index(hm: &Self, key: K) -> usize { | |
| 229 | fn keyToIndex(hm: &Self, key: K) -> usize { | |
| 230 | 230 | return usize(hash(key)) % hm.entries.len; |
| 231 | 231 | } |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | #attribute("test") |
| 235 | fn basic_hash_map_test() { | |
| 235 | fn basicHashMapTest() { | |
| 236 | 236 | var map: HashMap(i32, i32, hash_i32, eql_i32) = undefined; |
| 237 | 237 | map.init(&debug.global_allocator); |
| 238 | 238 | defer map.deinit(); |
std/index.zig+1-1| ... | ... | @@ -9,7 +9,7 @@ pub const list = @import("list.zig"); |
| 9 | 9 | pub const hash_map = @import("hash_map.zig"); |
| 10 | 10 | pub const mem = @import("mem.zig"); |
| 11 | 11 | pub const debug = @import("debug.zig"); |
| 12 | pub const linux = switch(@compile_var("os")) { | |
| 12 | pub const linux = switch(@compileVar("os")) { | |
| 13 | 13 | linux => @import("linux.zig"), |
| 14 | 14 | else => null_import, |
| 15 | 15 | }; |
std/io.zig+21-29| ... | ... | @@ -63,7 +63,7 @@ pub struct OutStream { |
| 63 | 63 | buffer: [buffer_size]u8, |
| 64 | 64 | index: usize, |
| 65 | 65 | |
| 66 | pub fn write_byte(os: &OutStream, b: u8) -> %void { | |
| 66 | pub fn writeByte(os: &OutStream, b: u8) -> %void { | |
| 67 | 67 | if (os.buffer.len == os.index) %return os.flush(); |
| 68 | 68 | os.buffer[os.index] = b; |
| 69 | 69 | os.index += 1; |
| ... | ... | @@ -71,7 +71,7 @@ pub struct OutStream { |
| 71 | 71 | |
| 72 | 72 | pub fn write(os: &OutStream, bytes: []const u8) -> %usize { |
| 73 | 73 | var src_bytes_left = bytes.len; |
| 74 | var src_index: @typeof(bytes.len) = 0; | |
| 74 | var src_index: @typeOf(bytes.len) = 0; | |
| 75 | 75 | const dest_space_left = os.buffer.len - os.index; |
| 76 | 76 | |
| 77 | 77 | while (src_bytes_left > 0) { |
| ... | ... | @@ -98,7 +98,7 @@ pub struct OutStream { |
| 98 | 98 | if (os.index + max_u64_base10_digits >= os.buffer.len) { |
| 99 | 99 | %return os.flush(); |
| 100 | 100 | } |
| 101 | const amt_printed = buf_print_u64(os.buffer[os.index...], x); | |
| 101 | const amt_printed = bufPrintUnsigned(u64, os.buffer[os.index...], x); | |
| 102 | 102 | os.index += amt_printed; |
| 103 | 103 | |
| 104 | 104 | return amt_printed; |
| ... | ... | @@ -108,7 +108,7 @@ pub struct OutStream { |
| 108 | 108 | if (os.index + max_u64_base10_digits >= os.buffer.len) { |
| 109 | 109 | %return os.flush(); |
| 110 | 110 | } |
| 111 | const amt_printed = buf_print_i64(os.buffer[os.index...], x); | |
| 111 | const amt_printed = bufPrintSigned(i64, os.buffer[os.index...], x); | |
| 112 | 112 | os.index += amt_printed; |
| 113 | 113 | |
| 114 | 114 | return amt_printed; |
| ... | ... | @@ -116,7 +116,7 @@ pub struct OutStream { |
| 116 | 116 | |
| 117 | 117 | pub fn flush(os: &OutStream) -> %void { |
| 118 | 118 | const write_ret = linux.write(os.fd, &os.buffer[0], os.index); |
| 119 | const write_err = linux.get_errno(write_ret); | |
| 119 | const write_err = linux.getErrno(write_ret); | |
| 120 | 120 | if (write_err > 0) { |
| 121 | 121 | return switch (write_err) { |
| 122 | 122 | errno.EINVAL => unreachable{}, |
| ... | ... | @@ -135,7 +135,7 @@ pub struct OutStream { |
| 135 | 135 | |
| 136 | 136 | pub fn close(os: &OutStream) -> %void { |
| 137 | 137 | const close_ret = linux.close(os.fd); |
| 138 | const close_err = linux.get_errno(close_ret); | |
| 138 | const close_err = linux.getErrno(close_ret); | |
| 139 | 139 | if (close_err > 0) { |
| 140 | 140 | return switch (close_err) { |
| 141 | 141 | errno.EIO => error.Io, |
| ... | ... | @@ -152,7 +152,7 @@ pub struct InStream { |
| 152 | 152 | |
| 153 | 153 | pub fn open(path: []u8) -> %InStream { |
| 154 | 154 | const fd = linux.open(path, linux.O_LARGEFILE|linux.O_RDONLY, 0); |
| 155 | const fd_err = linux.get_errno(fd); | |
| 155 | const fd_err = linux.getErrno(fd); | |
| 156 | 156 | if (fd_err > 0) { |
| 157 | 157 | return switch (fd_err) { |
| 158 | 158 | errno.EFAULT => unreachable{}, |
| ... | ... | @@ -180,7 +180,7 @@ pub struct InStream { |
| 180 | 180 | |
| 181 | 181 | pub fn read(is: &InStream, buf: []u8) -> %usize { |
| 182 | 182 | const amt_read = linux.read(is.fd, &buf[0], buf.len); |
| 183 | const read_err = linux.get_errno(amt_read); | |
| 183 | const read_err = linux.getErrno(amt_read); | |
| 184 | 184 | if (read_err > 0) { |
| 185 | 185 | return switch (read_err) { |
| 186 | 186 | errno.EINVAL => unreachable{}, |
| ... | ... | @@ -196,7 +196,7 @@ pub struct InStream { |
| 196 | 196 | |
| 197 | 197 | pub fn close(is: &InStream) -> %void { |
| 198 | 198 | const close_ret = linux.close(is.fd); |
| 199 | const close_err = linux.get_errno(close_ret); | |
| 199 | const close_err = linux.getErrno(close_ret); | |
| 200 | 200 | if (close_err > 0) { |
| 201 | 201 | return switch (close_err) { |
| 202 | 202 | errno.EIO => error.Io, |
| ... | ... | @@ -208,20 +208,20 @@ pub struct InStream { |
| 208 | 208 | } |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | pub fn parse_unsigned(inline T: type, buf: []u8, radix: u8) -> %T { | |
| 211 | pub fn parseUnsigned(inline T: type, buf: []u8, radix: u8) -> %T { | |
| 212 | 212 | var x: T = 0; |
| 213 | 213 | |
| 214 | 214 | for (buf) |c| { |
| 215 | const digit = %return char_to_digit(c, radix); | |
| 216 | x = %return math.mul_overflow(T, x, radix); | |
| 217 | x = %return math.add_overflow(T, x, digit); | |
| 215 | const digit = %return charToDigit(c, radix); | |
| 216 | x = %return math.mulOverflow(T, x, radix); | |
| 217 | x = %return math.addOverflow(T, x, digit); | |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | return x; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | pub error InvalidChar; |
| 224 | fn char_to_digit(c: u8, radix: u8) -> %u8 { | |
| 224 | fn charToDigit(c: u8, radix: u8) -> %u8 { | |
| 225 | 225 | const value = if ('0' <= c && c <= '9') { |
| 226 | 226 | c - '0' |
| 227 | 227 | } else if ('A' <= c && c <= 'Z') { |
| ... | ... | @@ -234,21 +234,17 @@ fn char_to_digit(c: u8, radix: u8) -> %u8 { |
| 234 | 234 | return if (value >= radix) error.InvalidChar else value; |
| 235 | 235 | } |
| 236 | 236 | |
| 237 | pub fn buf_print_signed(inline T: type, out_buf: []u8, x: T) -> usize { | |
| 238 | const uint = @int_type(false, T.bit_count); | |
| 237 | pub fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize { | |
| 238 | const uint = @intType(false, T.bit_count); | |
| 239 | 239 | if (x < 0) { |
| 240 | 240 | out_buf[0] = '-'; |
| 241 | return 1 + buf_print_unsigned(uint, out_buf[1...], uint(-(x + 1)) + 1); | |
| 241 | return 1 + bufPrintUnsigned(uint, out_buf[1...], uint(-(x + 1)) + 1); | |
| 242 | 242 | } else { |
| 243 | return buf_print_unsigned(uint, out_buf, uint(x)); | |
| 243 | return bufPrintUnsigned(uint, out_buf, uint(x)); | |
| 244 | 244 | } |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | pub fn buf_print_i64(out_buf: []u8, x: i64) -> usize { | |
| 248 | buf_print_signed(i64, out_buf, x) | |
| 249 | } | |
| 250 | ||
| 251 | pub fn buf_print_unsigned(inline T: type, out_buf: []u8, x: T) -> usize { | |
| 247 | pub fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize { | |
| 252 | 248 | var buf: [max_u64_base10_digits]u8 = undefined; |
| 253 | 249 | var a = x; |
| 254 | 250 | var index: usize = buf.len; |
| ... | ... | @@ -269,13 +265,9 @@ pub fn buf_print_unsigned(inline T: type, out_buf: []u8, x: T) -> usize { |
| 269 | 265 | return len; |
| 270 | 266 | } |
| 271 | 267 | |
| 272 | pub fn buf_print_u64(out_buf: []u8, x: u64) -> usize { | |
| 273 | buf_print_unsigned(u64, out_buf, x) | |
| 274 | } | |
| 275 | ||
| 276 | 268 | #attribute("test") |
| 277 | fn parse_u64_digit_too_big() { | |
| 278 | parse_unsigned(u64, "123a", 10) %% |err| { | |
| 269 | fn parseU64DigitTooBig() { | |
| 270 | parseUnsigned(u64, "123a", 10) %% |err| { | |
| 279 | 271 | if (err == error.InvalidChar) return; |
| 280 | 272 | unreachable{}; |
| 281 | 273 | }; |
std/linux.zig+9-9| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | const arch = switch (@compile_var("arch")) { | |
| 1 | const arch = switch (@compileVar("arch")) { | |
| 2 | 2 | x86_64 => @import("linux_x86_64.zig"), |
| 3 | 3 | i386 => @import("linux_i386.zig"), |
| 4 | 4 | else => @compile_err("unsupported arch"), |
| ... | ... | @@ -221,7 +221,7 @@ pub const AF_VSOCK = PF_VSOCK; |
| 221 | 221 | pub const AF_MAX = PF_MAX; |
| 222 | 222 | |
| 223 | 223 | /// Get the errno from a syscall return value, or 0 for no error. |
| 224 | pub fn get_errno(r: usize) -> usize { | |
| 224 | pub fn getErrno(r: usize) -> usize { | |
| 225 | 225 | const signed_r = *(&isize)(&r); |
| 226 | 226 | if (signed_r > -4096 && signed_r < 0) usize(-signed_r) else 0 |
| 227 | 227 | } |
| ... | ... | @@ -291,22 +291,22 @@ const app_mask = []u8 { 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, }; |
| 291 | 291 | |
| 292 | 292 | pub fn raise(sig: i32) -> i32 { |
| 293 | 293 | var set: sigset_t = undefined; |
| 294 | block_app_signals(&set); | |
| 294 | blockAppSignals(&set); | |
| 295 | 295 | const tid = i32(arch.syscall0(arch.SYS_gettid)); |
| 296 | 296 | const ret = i32(arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig))); |
| 297 | restore_signals(&set); | |
| 297 | restoreSignals(&set); | |
| 298 | 298 | return ret; |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | fn block_all_signals(set: &sigset_t) { | |
| 301 | fn blockAllSignals(set: &sigset_t) { | |
| 302 | 302 | arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&all_mask), usize(set), NSIG/8); |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | fn block_app_signals(set: &sigset_t) { | |
| 305 | fn blockAppSignals(set: &sigset_t) { | |
| 306 | 306 | arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&app_mask), usize(set), NSIG/8); |
| 307 | 307 | } |
| 308 | 308 | |
| 309 | fn restore_signals(set: &sigset_t) { | |
| 309 | fn restoreSignals(set: &sigset_t) { | |
| 310 | 310 | arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, usize(set), 0, NSIG/8); |
| 311 | 311 | } |
| 312 | 312 | |
| ... | ... | @@ -442,7 +442,7 @@ pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: |
| 442 | 442 | // } |
| 443 | 443 | // |
| 444 | 444 | // const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); |
| 445 | // const socket_err = get_errno(socket_ret); | |
| 445 | // const socket_err = getErrno(socket_ret); | |
| 446 | 446 | // if (socket_err > 0) { |
| 447 | 447 | // return error.SystemResources; |
| 448 | 448 | // } |
| ... | ... | @@ -451,7 +451,7 @@ pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: |
| 451 | 451 | // ifr.ifr_name[name.len] = 0; |
| 452 | 452 | // const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr); |
| 453 | 453 | // close(socket_fd); |
| 454 | // const ioctl_err = get_errno(ioctl_ret); | |
| 454 | // const ioctl_err = getErrno(ioctl_ret); | |
| 455 | 455 | // if (ioctl_err > 0) { |
| 456 | 456 | // return error.Io; |
| 457 | 457 | // } |
std/list.zig+10-10| ... | ... | @@ -4,17 +4,17 @@ const mem = @import("mem.zig"); |
| 4 | 4 | const Allocator = mem.Allocator; |
| 5 | 5 | |
| 6 | 6 | pub fn List(inline T: type) -> type { |
| 7 | SmallList(T, @sizeof(usize)) | |
| 7 | SmallList(T, @sizeOf(usize)) | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | // TODO: make sure that setting STATIC_SIZE to 0 codegens to the same code | |
| 11 | // as if this were programmed without STATIC_SIZE at all. | |
| 12 | pub struct SmallList(T: type, STATIC_SIZE: usize) { | |
| 13 | const Self = SmallList(T, STATIC_SIZE); | |
| 10 | // TODO: make sure that setting static_size to 0 codegens to the same code | |
| 11 | // as if this were programmed without static_size at all. | |
| 12 | pub struct SmallList(T: type, static_size: usize) { | |
| 13 | const Self = SmallList(T, static_size); | |
| 14 | 14 | |
| 15 | 15 | items: []T, |
| 16 | 16 | len: usize, |
| 17 | prealloc_items: [STATIC_SIZE]T, | |
| 17 | prealloc_items: [static_size]T, | |
| 18 | 18 | allocator: &Allocator, |
| 19 | 19 | |
| 20 | 20 | pub fn init(l: &Self, allocator: &Allocator) { |
| ... | ... | @@ -31,17 +31,17 @@ pub struct SmallList(T: type, STATIC_SIZE: usize) { |
| 31 | 31 | |
| 32 | 32 | pub fn append(l: &Self, item: T) -> %void { |
| 33 | 33 | const new_length = l.len + 1; |
| 34 | %return l.ensure_capacity(new_length); | |
| 34 | %return l.ensureCapacity(new_length); | |
| 35 | 35 | l.items[l.len] = item; |
| 36 | 36 | l.len = new_length; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | pub fn resize(l: &Self, new_len: usize) -> %void { |
| 40 | %return l.ensure_capacity(new_len); | |
| 40 | %return l.ensureCapacity(new_len); | |
| 41 | 41 | l.len = new_len; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | pub fn ensure_capacity(l: &Self, new_capacity: usize) -> %void { | |
| 44 | pub fn ensureCapacity(l: &Self, new_capacity: usize) -> %void { | |
| 45 | 45 | const old_capacity = l.items.len; |
| 46 | 46 | var better_capacity = old_capacity; |
| 47 | 47 | while (better_capacity < new_capacity) { |
| ... | ... | @@ -59,7 +59,7 @@ pub struct SmallList(T: type, STATIC_SIZE: usize) { |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | #attribute("test") |
| 62 | fn basic_list_test() { | |
| 62 | fn basicListTest() { | |
| 63 | 63 | var list: List(i32) = undefined; |
| 64 | 64 | list.init(&debug.global_allocator); |
| 65 | 65 | defer list.deinit(); |
std/math.zig+6-34| ... | ... | @@ -4,34 +4,6 @@ pub enum Cmp { |
| 4 | 4 | Less, |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | pub fn f64_from_bits(bits: u64) -> f64 { | |
| 8 | *(&f64)(&bits) | |
| 9 | } | |
| 10 | ||
| 11 | pub fn f64_to_bits(f: f64) -> u64 { | |
| 12 | *(&u64)(&f) | |
| 13 | } | |
| 14 | ||
| 15 | pub fn f64_get_pos_inf() -> f64 { | |
| 16 | f64_from_bits(0x7FF0000000000000) | |
| 17 | } | |
| 18 | ||
| 19 | pub fn f64_get_neg_inf() -> f64 { | |
| 20 | f64_from_bits(0xFFF0000000000000) | |
| 21 | } | |
| 22 | ||
| 23 | pub fn f64_is_nan(f: f64) -> bool { | |
| 24 | const bits = f64_to_bits(f); | |
| 25 | const exp: i64 = i64((bits >> 52) & ((1 << 11) - 1)); | |
| 26 | const sig = (bits & ((1 << 52) - 1)) | (1 << 52); | |
| 27 | ||
| 28 | sig != 0 && exp == (1 << 11) - 1 | |
| 29 | } | |
| 30 | ||
| 31 | pub fn f64_is_inf(f: f64) -> bool { | |
| 32 | f == f64_get_neg_inf() || f == f64_get_pos_inf() | |
| 33 | } | |
| 34 | ||
| 35 | 7 | pub fn min(inline T: type, x: T, y: T) -> T { |
| 36 | 8 | if (x < y) x else y |
| 37 | 9 | } |
| ... | ... | @@ -41,15 +13,15 @@ pub fn max(inline T: type, x: T, y: T) -> T { |
| 41 | 13 | } |
| 42 | 14 | |
| 43 | 15 | pub error Overflow; |
| 44 | pub fn mul_overflow(inline T: type, a: T, b: T) -> %T { | |
| 16 | pub fn mulOverflow(inline T: type, a: T, b: T) -> %T { | |
| 45 | 17 | var answer: T = undefined; |
| 46 | if (@mul_with_overflow(T, a, b, &answer)) error.Overflow else answer | |
| 18 | if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer | |
| 47 | 19 | } |
| 48 | pub fn add_overflow(inline T: type, a: T, b: T) -> %T { | |
| 20 | pub fn addOverflow(inline T: type, a: T, b: T) -> %T { | |
| 49 | 21 | var answer: T = undefined; |
| 50 | if (@add_with_overflow(T, a, b, &answer)) error.Overflow else answer | |
| 22 | if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer | |
| 51 | 23 | } |
| 52 | pub fn sub_overflow(inline T: type, a: T, b: T) -> %T { | |
| 24 | pub fn subOverflow(inline T: type, a: T, b: T) -> %T { | |
| 53 | 25 | var answer: T = undefined; |
| 54 | if (@sub_with_overflow(T, a, b, &answer)) error.Overflow else answer | |
| 26 | if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer | |
| 55 | 27 | } |
std/mem.zig+11-11| ... | ... | @@ -9,34 +9,34 @@ pub error NoMem; |
| 9 | 9 | |
| 10 | 10 | pub type Context = u8; |
| 11 | 11 | pub struct Allocator { |
| 12 | alloc_fn: fn (self: &Allocator, n: usize) -> %[]u8, | |
| 13 | realloc_fn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8, | |
| 14 | free_fn: fn (self: &Allocator, mem: []u8), | |
| 12 | allocFn: fn (self: &Allocator, n: usize) -> %[]u8, | |
| 13 | reallocFn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8, | |
| 14 | freeFn: fn (self: &Allocator, mem: []u8), | |
| 15 | 15 | context: ?&Context, |
| 16 | 16 | |
| 17 | 17 | /// Aborts the program if an allocation fails. |
| 18 | fn checked_alloc(self: &Allocator, inline T: type, n: usize) -> []T { | |
| 18 | fn checkedAlloc(self: &Allocator, inline T: type, n: usize) -> []T { | |
| 19 | 19 | alloc(self, T, n) %% |err| { |
| 20 | 20 | // TODO var args printf |
| 21 | 21 | %%io.stderr.write("allocation failure: "); |
| 22 | %%io.stderr.write(@err_name(err)); | |
| 22 | %%io.stderr.write(@errName(err)); | |
| 23 | 23 | %%io.stderr.printf("\n"); |
| 24 | 24 | os.abort() |
| 25 | 25 | } |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | fn alloc(self: &Allocator, inline T: type, n: usize) -> %[]T { |
| 29 | const byte_count = %return math.mul_overflow(usize, @sizeof(T), n); | |
| 30 | ([]T)(%return self.alloc_fn(self, byte_count)) | |
| 29 | const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n); | |
| 30 | ([]T)(%return self.allocFn(self, byte_count)) | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | fn realloc(self: &Allocator, inline T: type, old_mem: []T, n: usize) -> %[]T { |
| 34 | const byte_count = %return math.mul_overflow(usize, @sizeof(T), n); | |
| 35 | ([]T)(%return self.realloc_fn(self, ([]u8)(old_mem), byte_count)) | |
| 34 | const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n); | |
| 35 | ([]T)(%return self.reallocFn(self, ([]u8)(old_mem), byte_count)) | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | fn free(self: &Allocator, inline T: type, mem: []T) { |
| 39 | self.free_fn(self, ([]u8)(mem)); | |
| 39 | self.freeFn(self, ([]u8)(mem)); | |
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | 42 | |
| ... | ... | @@ -44,7 +44,7 @@ pub struct Allocator { |
| 44 | 44 | /// dest.len must be >= source.len. |
| 45 | 45 | pub fn copy(inline T: type, dest: []T, source: []const T) { |
| 46 | 46 | assert(dest.len >= source.len); |
| 47 | @memcpy(dest.ptr, source.ptr, @sizeof(T) * source.len); | |
| 47 | @memcpy(dest.ptr, source.ptr, @sizeOf(T) * source.len); | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than, |
std/net.zig+43-43| ... | ... | @@ -17,7 +17,7 @@ struct Connection { |
| 17 | 17 | |
| 18 | 18 | pub fn send(c: Connection, buf: []const u8) -> %usize { |
| 19 | 19 | const send_ret = linux.sendto(c.socket_fd, buf.ptr, buf.len, 0, null, 0); |
| 20 | const send_err = linux.get_errno(send_ret); | |
| 20 | const send_err = linux.getErrno(send_ret); | |
| 21 | 21 | switch (send_err) { |
| 22 | 22 | 0 => return send_ret, |
| 23 | 23 | errno.EINVAL => unreachable{}, |
| ... | ... | @@ -31,7 +31,7 @@ struct Connection { |
| 31 | 31 | |
| 32 | 32 | pub fn recv(c: Connection, buf: []u8) -> %[]u8 { |
| 33 | 33 | const recv_ret = linux.recvfrom(c.socket_fd, buf.ptr, buf.len, 0, null, null); |
| 34 | const recv_err = linux.get_errno(recv_ret); | |
| 34 | const recv_err = linux.getErrno(recv_ret); | |
| 35 | 35 | switch (recv_err) { |
| 36 | 36 | 0 => return buf[0...recv_ret], |
| 37 | 37 | errno.EINVAL => unreachable{}, |
| ... | ... | @@ -47,7 +47,7 @@ struct Connection { |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | pub fn close(c: Connection) -> %void { |
| 50 | switch (linux.get_errno(linux.close(c.socket_fd))) { | |
| 50 | switch (linux.getErrno(linux.close(c.socket_fd))) { | |
| 51 | 51 | 0 => return, |
| 52 | 52 | errno.EBADF => unreachable{}, |
| 53 | 53 | errno.EINTR => return error.SigInterrupt, |
| ... | ... | @@ -76,7 +76,7 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address { |
| 76 | 76 | unreachable{} // TODO |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | switch (parse_ip_literal(hostname)) { | |
| 79 | switch (parseIpLiteral(hostname)) { | |
| 80 | 80 | Ok => |addr| { |
| 81 | 81 | out_addrs[0] = addr; |
| 82 | 82 | return out_addrs[0...1]; |
| ... | ... | @@ -87,9 +87,9 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address { |
| 87 | 87 | unreachable{} // TODO |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | pub fn connect_addr(addr: &Address, port: u16) -> %Connection { | |
| 90 | pub fn connectAddr(addr: &Address, port: u16) -> %Connection { | |
| 91 | 91 | const socket_ret = linux.socket(addr.family, linux.SOCK_STREAM, linux.PROTO_tcp); |
| 92 | const socket_err = linux.get_errno(socket_ret); | |
| 92 | const socket_err = linux.getErrno(socket_ret); | |
| 93 | 93 | if (socket_err > 0) { |
| 94 | 94 | // TODO figure out possible errors from socket() |
| 95 | 95 | return error.Unexpected; |
| ... | ... | @@ -99,22 +99,22 @@ pub fn connect_addr(addr: &Address, port: u16) -> %Connection { |
| 99 | 99 | const connect_ret = if (addr.family == linux.AF_INET) { |
| 100 | 100 | var os_addr: linux.sockaddr_in = undefined; |
| 101 | 101 | os_addr.family = addr.family; |
| 102 | os_addr.port = swap_if_little_endian(u16, port); | |
| 102 | os_addr.port = swapIfLittleEndian(u16, port); | |
| 103 | 103 | @memcpy((&u8)(&os_addr.addr), &addr.addr[0], 4); |
| 104 | @memset(&os_addr.zero, 0, @sizeof(@typeof(os_addr.zero))); | |
| 105 | linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeof(linux.sockaddr_in)) | |
| 104 | @memset(&os_addr.zero, 0, @sizeOf(@typeOf(os_addr.zero))); | |
| 105 | linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in)) | |
| 106 | 106 | } else if (addr.family == linux.AF_INET6) { |
| 107 | 107 | var os_addr: linux.sockaddr_in6 = undefined; |
| 108 | 108 | os_addr.family = addr.family; |
| 109 | os_addr.port = swap_if_little_endian(u16, port); | |
| 109 | os_addr.port = swapIfLittleEndian(u16, port); | |
| 110 | 110 | os_addr.flowinfo = 0; |
| 111 | 111 | os_addr.scope_id = addr.scope_id; |
| 112 | 112 | @memcpy(&os_addr.addr[0], &addr.addr[0], 16); |
| 113 | linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeof(linux.sockaddr_in6)) | |
| 113 | linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in6)) | |
| 114 | 114 | } else { |
| 115 | 115 | unreachable{} |
| 116 | 116 | }; |
| 117 | const connect_err = linux.get_errno(connect_ret); | |
| 117 | const connect_err = linux.getErrno(connect_ret); | |
| 118 | 118 | if (connect_err > 0) { |
| 119 | 119 | switch (connect_err) { |
| 120 | 120 | errno.ETIMEDOUT => return error.TimedOut, |
| ... | ... | @@ -135,23 +135,23 @@ pub fn connect(hostname: []const u8, port: u16) -> %Connection { |
| 135 | 135 | const addrs_slice = %return lookup(hostname, addrs_buf); |
| 136 | 136 | const main_addr = &addrs_slice[0]; |
| 137 | 137 | |
| 138 | return connect_addr(main_addr, port); | |
| 138 | return connectAddr(main_addr, port); | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | pub error InvalidIpLiteral; |
| 142 | 142 | |
| 143 | pub fn parse_ip_literal(buf: []const u8) -> %Address { | |
| 144 | switch (parse_ip4(buf)) { | |
| 143 | pub fn parseIpLiteral(buf: []const u8) -> %Address { | |
| 144 | switch (parseIp4(buf)) { | |
| 145 | 145 | Ok => |ip4| { |
| 146 | 146 | var result: Address = undefined; |
| 147 | @memcpy(&result.addr[0], (&u8)(&ip4), @sizeof(u32)); | |
| 147 | @memcpy(&result.addr[0], (&u8)(&ip4), @sizeOf(u32)); | |
| 148 | 148 | result.family = linux.AF_INET; |
| 149 | 149 | result.scope_id = 0; |
| 150 | 150 | return result; |
| 151 | 151 | }, |
| 152 | 152 | else => {}, |
| 153 | 153 | } |
| 154 | switch (parse_ip6(buf)) { | |
| 154 | switch (parseIp6(buf)) { | |
| 155 | 155 | Ok => |addr| { |
| 156 | 156 | return addr; |
| 157 | 157 | }, |
| ... | ... | @@ -161,7 +161,7 @@ pub fn parse_ip_literal(buf: []const u8) -> %Address { |
| 161 | 161 | return error.InvalidIpLiteral; |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | fn hex_digit(c: u8) -> u8 { | |
| 164 | fn hexDigit(c: u8) -> u8 { | |
| 165 | 165 | // TODO use switch with range |
| 166 | 166 | if ('0' <= c && c <= '9') { |
| 167 | 167 | c - '0' |
| ... | ... | @@ -170,7 +170,7 @@ fn hex_digit(c: u8) -> u8 { |
| 170 | 170 | } else if ('a' <= c && c <= 'z') { |
| 171 | 171 | c - 'a' + 10 |
| 172 | 172 | } else { |
| 173 | @max_value(u8) | |
| 173 | @maxValue(u8) | |
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | |
| ... | ... | @@ -180,7 +180,7 @@ error JunkAtEnd; |
| 180 | 180 | error Incomplete; |
| 181 | 181 | |
| 182 | 182 | #static_eval_enable(false) |
| 183 | fn parse_ip6(buf: []const u8) -> %Address { | |
| 183 | fn parseIp6(buf: []const u8) -> %Address { | |
| 184 | 184 | var result: Address = undefined; |
| 185 | 185 | result.family = linux.AF_INET6; |
| 186 | 186 | result.scope_id = 0; |
| ... | ... | @@ -194,10 +194,10 @@ fn parse_ip6(buf: []const u8) -> %Address { |
| 194 | 194 | if (scope_id) { |
| 195 | 195 | if (c >= '0' && c <= '9') { |
| 196 | 196 | const digit = c - '0'; |
| 197 | if (@mul_with_overflow(u32, result.scope_id, 10, &result.scope_id)) { | |
| 197 | if (@mulWithOverflow(u32, result.scope_id, 10, &result.scope_id)) { | |
| 198 | 198 | return error.Overflow; |
| 199 | 199 | } |
| 200 | if (@add_with_overflow(u32, result.scope_id, digit, &result.scope_id)) { | |
| 200 | if (@addWithOverflow(u32, result.scope_id, digit, &result.scope_id)) { | |
| 201 | 201 | return error.Overflow; |
| 202 | 202 | } |
| 203 | 203 | } else { |
| ... | ... | @@ -230,14 +230,14 @@ fn parse_ip6(buf: []const u8) -> %Address { |
| 230 | 230 | scope_id = true; |
| 231 | 231 | saw_any_digits = false; |
| 232 | 232 | } else { |
| 233 | const digit = hex_digit(c); | |
| 234 | if (digit == @max_value(u8)) { | |
| 233 | const digit = hexDigit(c); | |
| 234 | if (digit == @maxValue(u8)) { | |
| 235 | 235 | return error.InvalidChar; |
| 236 | 236 | } |
| 237 | if (@mul_with_overflow(u16, x, 16, &x)) { | |
| 237 | if (@mulWithOverflow(u16, x, 16, &x)) { | |
| 238 | 238 | return error.Overflow; |
| 239 | 239 | } |
| 240 | if (@add_with_overflow(u16, x, digit, &x)) { | |
| 240 | if (@addWithOverflow(u16, x, digit, &x)) { | |
| 241 | 241 | return error.Overflow; |
| 242 | 242 | } |
| 243 | 243 | saw_any_digits = true; |
| ... | ... | @@ -276,7 +276,7 @@ fn parse_ip6(buf: []const u8) -> %Address { |
| 276 | 276 | return error.Incomplete; |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | fn parse_ip4(buf: []const u8) -> %u32 { | |
| 279 | fn parseIp4(buf: []const u8) -> %u32 { | |
| 280 | 280 | var result: u32 = undefined; |
| 281 | 281 | const out_ptr = ([]u8)((&result)[0...1]); |
| 282 | 282 | |
| ... | ... | @@ -298,10 +298,10 @@ fn parse_ip4(buf: []const u8) -> %u32 { |
| 298 | 298 | } else if (c >= '0' && c <= '9') { |
| 299 | 299 | saw_any_digits = true; |
| 300 | 300 | const digit = c - '0'; |
| 301 | if (@mul_with_overflow(u8, x, 10, &x)) { | |
| 301 | if (@mulWithOverflow(u8, x, 10, &x)) { | |
| 302 | 302 | return error.Overflow; |
| 303 | 303 | } |
| 304 | if (@add_with_overflow(u8, x, digit, &x)) { | |
| 304 | if (@addWithOverflow(u8, x, digit, &x)) { | |
| 305 | 305 | return error.Overflow; |
| 306 | 306 | } |
| 307 | 307 | } else { |
| ... | ... | @@ -318,19 +318,19 @@ fn parse_ip4(buf: []const u8) -> %u32 { |
| 318 | 318 | |
| 319 | 319 | |
| 320 | 320 | #attribute("test") |
| 321 | fn test_parse_ip4() { | |
| 322 | assert(%%parse_ip4("127.0.0.1") == swap_if_little_endian(u32, 0x7f000001)); | |
| 323 | switch (parse_ip4("256.0.0.1")) { Overflow => {}, else => unreachable {}, } | |
| 324 | switch (parse_ip4("x.0.0.1")) { InvalidChar => {}, else => unreachable {}, } | |
| 325 | switch (parse_ip4("127.0.0.1.1")) { JunkAtEnd => {}, else => unreachable {}, } | |
| 326 | switch (parse_ip4("127.0.0.")) { Incomplete => {}, else => unreachable {}, } | |
| 327 | switch (parse_ip4("100..0.1")) { InvalidChar => {}, else => unreachable {}, } | |
| 321 | fn testParseIp4() { | |
| 322 | assert(%%parseIp4("127.0.0.1") == swapIfLittleEndian(u32, 0x7f000001)); | |
| 323 | switch (parseIp4("256.0.0.1")) { Overflow => {}, else => unreachable {}, } | |
| 324 | switch (parseIp4("x.0.0.1")) { InvalidChar => {}, else => unreachable {}, } | |
| 325 | switch (parseIp4("127.0.0.1.1")) { JunkAtEnd => {}, else => unreachable {}, } | |
| 326 | switch (parseIp4("127.0.0.")) { Incomplete => {}, else => unreachable {}, } | |
| 327 | switch (parseIp4("100..0.1")) { InvalidChar => {}, else => unreachable {}, } | |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | #attribute("test") |
| 331 | fn test_parse_ip6() { | |
| 331 | fn testParseIp6() { | |
| 332 | 332 | { |
| 333 | const addr = %%parse_ip6("FF01:0:0:0:0:0:0:FB"); | |
| 333 | const addr = %%parseIp6("FF01:0:0:0:0:0:0:FB"); | |
| 334 | 334 | assert(addr.addr[0] == 0xff); |
| 335 | 335 | assert(addr.addr[1] == 0x01); |
| 336 | 336 | assert(addr.addr[2] == 0x00); |
| ... | ... | @@ -338,7 +338,7 @@ fn test_parse_ip6() { |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | #attribute("test") |
| 341 | fn test_lookup_simple_ip() { | |
| 341 | fn testLookupSimpleIp() { | |
| 342 | 342 | { |
| 343 | 343 | var addrs_buf: [5]Address = undefined; |
| 344 | 344 | const addrs = %%lookup("192.168.1.1", addrs_buf); |
| ... | ... | @@ -352,16 +352,16 @@ fn test_lookup_simple_ip() { |
| 352 | 352 | } |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | fn swap_if_little_endian(inline T: type, x: T) -> T { | |
| 356 | if (@compile_var("is_big_endian")) x else endian_swap(T, x) | |
| 355 | fn swapIfLittleEndian(inline T: type, x: T) -> T { | |
| 356 | if (@compileVar("is_big_endian")) x else endianSwap(T, x) | |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | fn endian_swap(inline T: type, x: T) -> T { | |
| 359 | fn endianSwap(inline T: type, x: T) -> T { | |
| 360 | 360 | const x_slice = ([]u8)((&const x)[0...1]); |
| 361 | 361 | var result: T = undefined; |
| 362 | 362 | const result_slice = ([]u8)((&result)[0...1]); |
| 363 | 363 | for (result_slice) |*b, i| { |
| 364 | *b = x_slice[@sizeof(T) - i - 1]; | |
| 364 | *b = x_slice[@sizeOf(T) - i - 1]; | |
| 365 | 365 | } |
| 366 | 366 | return result; |
| 367 | 367 | } |
std/os.zig+2-2| ... | ... | @@ -5,10 +5,10 @@ pub error SigInterrupt; |
| 5 | 5 | pub error Unexpected; |
| 6 | 6 | |
| 7 | 7 | pub fn get_random_bytes(buf: []u8) -> %void { |
| 8 | switch (@compile_var("os")) { | |
| 8 | switch (@compileVar("os")) { | |
| 9 | 9 | linux => { |
| 10 | 10 | const ret = linux.getrandom(buf.ptr, buf.len, 0); |
| 11 | const err = linux.get_errno(ret); | |
| 11 | const err = linux.getErrno(ret); | |
| 12 | 12 | if (err > 0) { |
| 13 | 13 | return switch (err) { |
| 14 | 14 | errno.EINVAL => unreachable{}, |
std/rand.zig+29-31| ... | ... | @@ -19,15 +19,13 @@ pub const MT19937_64 = MersenneTwister( |
| 19 | 19 | |
| 20 | 20 | /// Use `init` to initialize this state. |
| 21 | 21 | pub struct Rand { |
| 22 | const Rng = if (@sizeof(usize) >= 8) MT19937_64 else MT19937_32; | |
| 22 | const Rng = if (@sizeOf(usize) >= 8) MT19937_64 else MT19937_32; | |
| 23 | 23 | |
| 24 | 24 | rng: Rng, |
| 25 | 25 | |
| 26 | 26 | /// Initialize random state with the given seed. |
| 27 | pub fn init(seed: usize) -> Rand { | |
| 28 | var r: Rand = undefined; | |
| 29 | r.rng = Rng.init(seed); | |
| 30 | return r; | |
| 27 | pub fn init(r: &Rand, seed: usize) { | |
| 28 | r.rng.init(seed); | |
| 31 | 29 | } |
| 32 | 30 | |
| 33 | 31 | /// Get an integer with random bits. |
| ... | ... | @@ -35,24 +33,24 @@ pub struct Rand { |
| 35 | 33 | if (T == usize) { |
| 36 | 34 | return r.rng.get(); |
| 37 | 35 | } else { |
| 38 | var result: [@sizeof(T)]u8 = undefined; | |
| 39 | r.fill_bytes(result); | |
| 36 | var result: [@sizeOf(T)]u8 = undefined; | |
| 37 | r.fillBytes(result); | |
| 40 | 38 | return ([]T)(result)[0]; |
| 41 | 39 | } |
| 42 | 40 | } |
| 43 | 41 | |
| 44 | 42 | /// Fill `buf` with randomness. |
| 45 | pub fn fill_bytes(r: &Rand, buf: []u8) { | |
| 43 | pub fn fillBytes(r: &Rand, buf: []u8) { | |
| 46 | 44 | var bytes_left = buf.len; |
| 47 | while (bytes_left >= @sizeof(usize)) { | |
| 45 | while (bytes_left >= @sizeOf(usize)) { | |
| 48 | 46 | ([]usize)(buf[buf.len - bytes_left...])[0] = r.rng.get(); |
| 49 | bytes_left -= @sizeof(usize); | |
| 47 | bytes_left -= @sizeOf(usize); | |
| 50 | 48 | } |
| 51 | 49 | if (bytes_left > 0) { |
| 52 | var rand_val_array : [@sizeof(usize)]u8 = undefined; | |
| 50 | var rand_val_array : [@sizeOf(usize)]u8 = undefined; | |
| 53 | 51 | ([]usize)(rand_val_array)[0] = r.rng.get(); |
| 54 | 52 | while (bytes_left > 0) { |
| 55 | buf[buf.len - bytes_left] = rand_val_array[@sizeof(usize) - bytes_left]; | |
| 53 | buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left]; | |
| 56 | 54 | bytes_left -= 1; |
| 57 | 55 | } |
| 58 | 56 | } |
| ... | ... | @@ -61,14 +59,14 @@ pub struct Rand { |
| 61 | 59 | /// Get a random unsigned integer with even distribution between `start` |
| 62 | 60 | /// inclusive and `end` exclusive. |
| 63 | 61 | // TODO support signed integers and then rename to "range" |
| 64 | pub fn range_unsigned(r: &Rand, inline T: type, start: T, end: T) -> T { | |
| 62 | pub fn rangeUnsigned(r: &Rand, inline T: type, start: T, end: T) -> T { | |
| 65 | 63 | const range = end - start; |
| 66 | const leftover = @max_value(T) % range; | |
| 67 | const upper_bound = @max_value(T) - leftover; | |
| 68 | var rand_val_array : [@sizeof(T)]u8 = undefined; | |
| 64 | const leftover = @maxValue(T) % range; | |
| 65 | const upper_bound = @maxValue(T) - leftover; | |
| 66 | var rand_val_array : [@sizeOf(T)]u8 = undefined; | |
| 69 | 67 | |
| 70 | 68 | while (true) { |
| 71 | r.fill_bytes(rand_val_array); | |
| 69 | r.fillBytes(rand_val_array); | |
| 72 | 70 | const rand_val = ([]T)(rand_val_array)[0]; |
| 73 | 71 | if (rand_val < upper_bound) { |
| 74 | 72 | return start + (rand_val % range); |
| ... | ... | @@ -79,19 +77,19 @@ pub struct Rand { |
| 79 | 77 | /// Get a floating point value in the range 0.0..1.0. |
| 80 | 78 | pub fn float(r: &Rand, inline T: type) -> T { |
| 81 | 79 | // TODO Implement this way instead: |
| 82 | // const int = @int_type(false, @sizeof(T) * 8); | |
| 80 | // const int = @int_type(false, @sizeOf(T) * 8); | |
| 83 | 81 | // const mask = ((1 << @float_mantissa_bit_count(T)) - 1); |
| 84 | 82 | // const rand_bits = r.rng.scalar(int) & mask; |
| 85 | 83 | // return @float_compose(T, false, 0, rand_bits) - 1.0 |
| 86 | const int_type = @int_type(false, @sizeof(T) * 8); | |
| 84 | const int_type = @intType(false, @sizeOf(T) * 8); | |
| 87 | 85 | const precision = if (T == f32) { |
| 88 | 86 | 16777216 |
| 89 | 87 | } else if (T == f64) { |
| 90 | 88 | 9007199254740992 |
| 91 | 89 | } else { |
| 92 | @compile_err("unknown floating point type" ++ @type_name(T)) | |
| 90 | @compile_err("unknown floating point type" ++ @typeName(T)) | |
| 93 | 91 | }; |
| 94 | return T(r.range_unsigned(int_type, 0, precision)) / T(precision); | |
| 92 | return T(r.rangeUnsigned(int_type, 0, precision)) / T(precision); | |
| 95 | 93 | } |
| 96 | 94 | } |
| 97 | 95 | |
| ... | ... | @@ -110,8 +108,7 @@ struct MersenneTwister( |
| 110 | 108 | |
| 111 | 109 | // TODO improve compile time eval code and then allow this function to be executed at compile time. |
| 112 | 110 | #static_eval_enable(false) |
| 113 | pub fn init(seed: int) -> Self { | |
| 114 | var mt: Self = undefined; | |
| 111 | pub fn init(mt: &Self, seed: int) { | |
| 115 | 112 | mt.index = n; |
| 116 | 113 | |
| 117 | 114 | var prev_value = seed; |
| ... | ... | @@ -120,8 +117,6 @@ struct MersenneTwister( |
| 120 | 117 | prev_value = int(i) +% f *% (prev_value ^ (prev_value >> (int.bit_count - 2))); |
| 121 | 118 | mt.array[i] = prev_value; |
| 122 | 119 | }}; |
| 123 | ||
| 124 | return mt; | |
| 125 | 120 | } |
| 126 | 121 | |
| 127 | 122 | pub fn get(mt: &Self) -> int { |
| ... | ... | @@ -161,8 +156,9 @@ struct MersenneTwister( |
| 161 | 156 | } |
| 162 | 157 | |
| 163 | 158 | #attribute("test") |
| 164 | fn test_float32() { | |
| 165 | var r = Rand.init(42); | |
| 159 | fn testFloat32() { | |
| 160 | var r: Rand = undefined; | |
| 161 | r.init(42); | |
| 166 | 162 | |
| 167 | 163 | {var i: usize = 0; while (i < 1000; i += 1) { |
| 168 | 164 | const val = r.float(f32); |
| ... | ... | @@ -172,16 +168,18 @@ fn test_float32() { |
| 172 | 168 | } |
| 173 | 169 | |
| 174 | 170 | #attribute("test") |
| 175 | fn test_MT19937_64() { | |
| 176 | const rng = MT19937_64.init(rand_test.mt64_seed); | |
| 171 | fn testMT19937_64() { | |
| 172 | var rng: MT19937_64 = undefined; | |
| 173 | rng.init(rand_test.mt64_seed); | |
| 177 | 174 | for (rand_test.mt64_data) |value| { |
| 178 | 175 | assert(value == rng.get()); |
| 179 | 176 | } |
| 180 | 177 | } |
| 181 | 178 | |
| 182 | 179 | #attribute("test") |
| 183 | fn test_MT19937_32() { | |
| 184 | const rng = MT19937_32.init(rand_test.mt32_seed); | |
| 180 | fn testMT19937_32() { | |
| 181 | var rng: MT19937_32 = undefined; | |
| 182 | rng.init(rand_test.mt32_seed); | |
| 185 | 183 | for (rand_test.mt32_data) |value| { |
| 186 | 184 | assert(value == rng.get()); |
| 187 | 185 | } |
std/str.zig+3-3| ... | ... | @@ -1,10 +1,10 @@ |
| 1 | 1 | const assert = @import("debug.zig").assert; |
| 2 | 2 | |
| 3 | 3 | pub fn eql(a: []const u8, b: []const u8) -> bool { |
| 4 | slice_eql(u8, a, b) | |
| 4 | sliceEql(u8, a, b) | |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | pub fn slice_eql(inline T: type, a: []const T, b: []const T) -> bool { | |
| 7 | pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool { | |
| 8 | 8 | if (a.len != b.len) return false; |
| 9 | 9 | for (a) |item, index| { |
| 10 | 10 | if (b[index] != item) return false; |
| ... | ... | @@ -13,7 +13,7 @@ pub fn slice_eql(inline T: type, a: []const T, b: []const T) -> bool { |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | #attribute("test") |
| 16 | fn string_equality() { | |
| 16 | fn stringEquality() { | |
| 17 | 17 | assert(eql("abcd", "abcd")); |
| 18 | 18 | assert(!eql("abcdef", "abZdef")); |
| 19 | 19 | assert(!eql("abcdefg", "abcdef")); |
std/test_runner.zig+4-4| ... | ... | @@ -7,19 +7,19 @@ struct TestFn { |
| 7 | 7 | |
| 8 | 8 | extern var zig_test_fn_list: []TestFn; |
| 9 | 9 | |
| 10 | pub fn run_tests() -> %void { | |
| 11 | for (zig_test_fn_list) |test_fn, i| { | |
| 10 | pub fn runTests() -> %void { | |
| 11 | for (zig_test_fn_list) |testFn, i| { | |
| 12 | 12 | // TODO: print var args |
| 13 | 13 | %%io.stderr.write("Test "); |
| 14 | 14 | %%io.stderr.print_u64(i + 1); |
| 15 | 15 | %%io.stderr.write("/"); |
| 16 | 16 | %%io.stderr.print_u64(zig_test_fn_list.len); |
| 17 | 17 | %%io.stderr.write(" "); |
| 18 | %%io.stderr.write(test_fn.name); | |
| 18 | %%io.stderr.write(testFn.name); | |
| 19 | 19 | %%io.stderr.write("..."); |
| 20 | 20 | %%io.stderr.flush(); |
| 21 | 21 | |
| 22 | test_fn.func(); | |
| 22 | testFn.func(); | |
| 23 | 23 | |
| 24 | 24 | |
| 25 | 25 | %%io.stderr.write("OK\n"); |
std/test_runner_libc.zig+1-1| ... | ... | @@ -1,6 +1,6 @@ |
| 1 | 1 | const test_runner = @import("test_runner.zig"); |
| 2 | 2 | |
| 3 | 3 | export fn main(argc: c_int, argv: &&u8) -> c_int { |
| 4 | test_runner.run_tests() %% return -1; | |
| 4 | test_runner.runTests() %% return -1; | |
| 5 | 5 | return 0; |
| 6 | 6 | } |
std/test_runner_nolibc.zig+1-1| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const test_runner = @import("test_runner.zig"); |
| 2 | 2 | |
| 3 | 3 | pub fn main(args: [][]u8) -> %void { |
| 4 | return test_runner.run_tests(); | |
| 4 | return test_runner.runTests(); | |
| 5 | 5 | } |
test/cases/sizeof_and_typeof.zig created+9| ... | ... | @@ -0,0 +1,9 @@ |
| 1 | const assert = @import("std").debug.assert; | |
| 2 | ||
| 3 | #attribute("test") | |
| 4 | fn sizeofAndTypeOf() { | |
| 5 | const y: @typeOf(x) = 120; | |
| 6 | assert(@sizeOf(@typeOf(y)) == 2); | |
| 7 | } | |
| 8 | const x: u16 = 13; | |
| 9 | const z: @typeOf(x) = 19; |
test/run_tests.cpp+35-35| ... | ... | @@ -202,7 +202,7 @@ static TestCase *add_parseh_case(const char *case_name, const char *source, int |
| 202 | 202 | |
| 203 | 203 | static void add_compiling_test_cases(void) { |
| 204 | 204 | add_simple_case_libc("hello world with libc", R"SOURCE( |
| 205 | const c = @c_import(@c_include("stdio.h")); | |
| 205 | const c = @cImport(@cInclude("stdio.h")); | |
| 206 | 206 | export fn main(argc: c_int, argv: &&u8) -> c_int { |
| 207 | 207 | c.puts(c"Hello, world!"); |
| 208 | 208 | return 0; |
| ... | ... | @@ -215,12 +215,12 @@ use @import("std").io; |
| 215 | 215 | use @import("foo.zig"); |
| 216 | 216 | |
| 217 | 217 | pub fn main(args: [][]u8) -> %void { |
| 218 | private_function(); | |
| 218 | privateFunction(); | |
| 219 | 219 | %%stdout.printf("OK 2\n"); |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | fn private_function() { | |
| 223 | print_text(); | |
| 222 | fn privateFunction() { | |
| 223 | printText(); | |
| 224 | 224 | } |
| 225 | 225 | )SOURCE", "OK 1\nOK 2\n"); |
| 226 | 226 | |
| ... | ... | @@ -229,12 +229,12 @@ use @import("std").io; |
| 229 | 229 | |
| 230 | 230 | // purposefully conflicting function with main.zig |
| 231 | 231 | // but it's private so it should be OK |
| 232 | fn private_function() { | |
| 232 | fn privateFunction() { | |
| 233 | 233 | %%stdout.printf("OK 1\n"); |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | pub fn print_text() { | |
| 237 | private_function(); | |
| 236 | pub fn printText() { | |
| 237 | privateFunction(); | |
| 238 | 238 | } |
| 239 | 239 | )SOURCE"); |
| 240 | 240 | } |
| ... | ... | @@ -316,7 +316,7 @@ pub fn main(args: [][]u8) -> %void { |
| 316 | 316 | |
| 317 | 317 | |
| 318 | 318 | add_simple_case_libc("number literals", R"SOURCE( |
| 319 | const c = @c_import(@c_include("stdio.h")); | |
| 319 | const c = @cImport(@cInclude("stdio.h")); | |
| 320 | 320 | |
| 321 | 321 | export fn main(argc: c_int, argv: &&u8) -> c_int { |
| 322 | 322 | c.printf(c"\n"); |
| ... | ... | @@ -444,12 +444,12 @@ export fn main(argc: c_int, argv: &&u8) -> c_int { |
| 444 | 444 | add_simple_case("order-independent declarations", R"SOURCE( |
| 445 | 445 | const io = @import("std").io; |
| 446 | 446 | const z = io.stdin_fileno; |
| 447 | const x : @typeof(y) = 1234; | |
| 447 | const x : @typeOf(y) = 1234; | |
| 448 | 448 | const y : u16 = 5678; |
| 449 | 449 | pub fn main(args: [][]u8) -> %void { |
| 450 | 450 | var x_local : i32 = print_ok(x); |
| 451 | 451 | } |
| 452 | fn print_ok(val: @typeof(x)) -> @typeof(foo) { | |
| 452 | fn print_ok(val: @typeOf(x)) -> @typeOf(foo) { | |
| 453 | 453 | %%io.stdout.printf("OK\n"); |
| 454 | 454 | return 0; |
| 455 | 455 | } |
| ... | ... | @@ -482,7 +482,7 @@ pub fn main(args: [][]u8) -> %void { |
| 482 | 482 | )SOURCE", "9\n8\n7\n6\n0\n1\n2\n3\n9\n8\n7\n6\n0\n1\n2\n3\n"); |
| 483 | 483 | |
| 484 | 484 | add_simple_case_libc("expose function pointer to C land", R"SOURCE( |
| 485 | const c = @c_import(@c_include("stdlib.h")); | |
| 485 | const c = @cImport(@cInclude("stdlib.h")); | |
| 486 | 486 | |
| 487 | 487 | export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int { |
| 488 | 488 | const a_int = (&i32)(a ?? unreachable{}); |
| ... | ... | @@ -499,7 +499,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int { |
| 499 | 499 | export fn main(args: c_int, argv: &&u8) -> c_int { |
| 500 | 500 | var array = []u32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 }; |
| 501 | 501 | |
| 502 | c.qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn); | |
| 502 | c.qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeOf(i32), compare_fn); | |
| 503 | 503 | |
| 504 | 504 | for (array) |item, i| { |
| 505 | 505 | if (item != i) { |
| ... | ... | @@ -514,7 +514,7 @@ export fn main(args: c_int, argv: &&u8) -> c_int { |
| 514 | 514 | |
| 515 | 515 | |
| 516 | 516 | add_simple_case_libc("casting between float and integer types", R"SOURCE( |
| 517 | const c = @c_import(@c_include("stdio.h")); | |
| 517 | const c = @cImport(@cInclude("stdio.h")); | |
| 518 | 518 | export fn main(argc: c_int, argv: &&u8) -> c_int { |
| 519 | 519 | const small: f32 = 3.25; |
| 520 | 520 | const x: f64 = small; |
| ... | ... | @@ -654,8 +654,8 @@ fn its_gonna_pass() -> %void { } |
| 654 | 654 | |
| 655 | 655 | |
| 656 | 656 | { |
| 657 | TestCase *tc = add_simple_case("@embed_file", R"SOURCE( | |
| 658 | const foo_txt = @embed_file("foo.txt"); | |
| 657 | TestCase *tc = add_simple_case("@embedFile", R"SOURCE( | |
| 658 | const foo_txt = @embedFile("foo.txt"); | |
| 659 | 659 | const io = @import("std").io; |
| 660 | 660 | |
| 661 | 661 | pub fn main(args: [][]u8) -> %void { |
| ... | ... | @@ -956,8 +956,8 @@ fn f() -> @bogus(foo) { |
| 956 | 956 | )SOURCE", 1, ".tmp_source.zig:2:11: error: invalid builtin function: 'bogus'"); |
| 957 | 957 | |
| 958 | 958 | add_compile_fail_case("top level decl dependency loop", R"SOURCE( |
| 959 | const a : @typeof(b) = 0; | |
| 960 | const b : @typeof(a) = 0; | |
| 959 | const a : @typeOf(b) = 0; | |
| 960 | const b : @typeOf(a) = 0; | |
| 961 | 961 | )SOURCE", 1, ".tmp_source.zig:2:1: error: 'a' depends on itself"); |
| 962 | 962 | |
| 963 | 963 | add_compile_fail_case("noalias on non pointer param", R"SOURCE( |
| ... | ... | @@ -1012,8 +1012,8 @@ fn f(s: [10]u8) -> []u8 { |
| 1012 | 1012 | } |
| 1013 | 1013 | )SOURCE", 1, ".tmp_source.zig:3:5: error: array concatenation requires constant expression"); |
| 1014 | 1014 | |
| 1015 | add_compile_fail_case("c_import with bogus include", R"SOURCE( | |
| 1016 | const c = @c_import(@c_include("bogus.h")); | |
| 1015 | add_compile_fail_case("@cImport with bogus include", R"SOURCE( | |
| 1016 | const c = @cImport(@cInclude("bogus.h")); | |
| 1017 | 1017 | )SOURCE", 2, ".tmp_source.zig:2:11: error: C import failed", |
| 1018 | 1018 | ".h:1:10: note: 'bogus.h' file not found"); |
| 1019 | 1019 | |
| ... | ... | @@ -1022,12 +1022,12 @@ const x = 3; |
| 1022 | 1022 | const y = &x; |
| 1023 | 1023 | )SOURCE", 1, ".tmp_source.zig:3:12: error: unable to get address of type '(integer literal)'"); |
| 1024 | 1024 | |
| 1025 | add_compile_fail_case("@typeof number literal", R"SOURCE( | |
| 1025 | add_compile_fail_case("@typeOf number literal", R"SOURCE( | |
| 1026 | 1026 | const x = 3; |
| 1027 | 1027 | struct Foo { |
| 1028 | index: @typeof(x), | |
| 1028 | index: @typeOf(x), | |
| 1029 | 1029 | } |
| 1030 | )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeof"); | |
| 1030 | )SOURCE", 1, ".tmp_source.zig:4:20: error: type '(integer literal)' not eligible for @typeOf"); | |
| 1031 | 1031 | |
| 1032 | 1032 | add_compile_fail_case("integer overflow error", R"SOURCE( |
| 1033 | 1033 | const x : u8 = 300; |
| ... | ... | @@ -1050,7 +1050,7 @@ struct Foo { |
| 1050 | 1050 | } |
| 1051 | 1051 | } |
| 1052 | 1052 | |
| 1053 | const member_fn_type = @typeof(Foo.member_a); | |
| 1053 | const member_fn_type = @typeOf(Foo.member_a); | |
| 1054 | 1054 | const members = []member_fn_type { |
| 1055 | 1055 | Foo.member_a, |
| 1056 | 1056 | Foo.member_b, |
| ... | ... | @@ -1101,15 +1101,15 @@ fn func() -> bogus {} |
| 1101 | 1101 | |
| 1102 | 1102 | |
| 1103 | 1103 | add_compile_fail_case("bogus compile var", R"SOURCE( |
| 1104 | const x = @compile_var("bogus"); | |
| 1105 | )SOURCE", 1, ".tmp_source.zig:2:24: error: unrecognized compile variable: 'bogus'"); | |
| 1104 | const x = @compileVar("bogus"); | |
| 1105 | )SOURCE", 1, ".tmp_source.zig:2:23: error: unrecognized compile variable: 'bogus'"); | |
| 1106 | 1106 | |
| 1107 | 1107 | |
| 1108 | add_compile_fail_case("@const_eval", R"SOURCE( | |
| 1108 | add_compile_fail_case("@constEval", R"SOURCE( | |
| 1109 | 1109 | fn a(x: i32) { |
| 1110 | const y = @const_eval(x); | |
| 1110 | const y = @constEval(x); | |
| 1111 | 1111 | } |
| 1112 | )SOURCE", 1, ".tmp_source.zig:3:27: error: unable to evaluate constant expression"); | |
| 1112 | )SOURCE", 1, ".tmp_source.zig:3:26: error: unable to evaluate constant expression"); | |
| 1113 | 1113 | |
| 1114 | 1114 | add_compile_fail_case("non constant expression in array size outside function", R"SOURCE( |
| 1115 | 1115 | struct Foo { |
| ... | ... | @@ -1245,8 +1245,8 @@ fn fibbonaci(x: i32) -> i32 { |
| 1245 | 1245 | ".tmp_source.zig:2:37: note: called from here", |
| 1246 | 1246 | ".tmp_source.zig:4:40: note: quota exceeded here"); |
| 1247 | 1247 | |
| 1248 | add_compile_fail_case("@embed_file with bogus file", R"SOURCE( | |
| 1249 | const resource = @embed_file("bogus.txt"); | |
| 1248 | add_compile_fail_case("@embedFile with bogus file", R"SOURCE( | |
| 1249 | const resource = @embedFile("bogus.txt"); | |
| 1250 | 1250 | )SOURCE", 1, ".tmp_source.zig:2:18: error: unable to find './bogus.txt'"); |
| 1251 | 1251 | |
| 1252 | 1252 | |
| ... | ... | @@ -1555,23 +1555,23 @@ fn div0(a: i32, b: i32) -> i32 { |
| 1555 | 1555 | add_debug_safety_case("exact division failure", R"SOURCE( |
| 1556 | 1556 | error Whatever; |
| 1557 | 1557 | pub fn main(args: [][]u8) -> %void { |
| 1558 | const x = div_exact(10, 3); | |
| 1558 | const x = divExact(10, 3); | |
| 1559 | 1559 | if (x == 0) return error.Whatever; |
| 1560 | 1560 | } |
| 1561 | 1561 | #static_eval_enable(false) |
| 1562 | fn div_exact(a: i32, b: i32) -> i32 { | |
| 1563 | @div_exact(a, b) | |
| 1562 | fn divExact(a: i32, b: i32) -> i32 { | |
| 1563 | @divExact(a, b) | |
| 1564 | 1564 | } |
| 1565 | 1565 | )SOURCE"); |
| 1566 | 1566 | |
| 1567 | 1567 | add_debug_safety_case("cast []u8 to bigger slice of wrong size", R"SOURCE( |
| 1568 | 1568 | error Whatever; |
| 1569 | 1569 | pub fn main(args: [][]u8) -> %void { |
| 1570 | const x = widen_slice([]u8{1, 2, 3, 4, 5}); | |
| 1570 | const x = widenSlice([]u8{1, 2, 3, 4, 5}); | |
| 1571 | 1571 | if (x.len == 0) return error.Whatever; |
| 1572 | 1572 | } |
| 1573 | 1573 | #static_eval_enable(false) |
| 1574 | fn widen_slice(slice: []u8) -> []i32 { | |
| 1574 | fn widenSlice(slice: []u8) -> []i32 { | |
| 1575 | 1575 | ([]i32)(slice) |
| 1576 | 1576 | } |
| 1577 | 1577 | )SOURCE"); |
test/self_hosted.zig+357-364| ... | ... | @@ -3,29 +3,30 @@ const assert = std.debug.assert; |
| 3 | 3 | const str = std.str; |
| 4 | 4 | const cstr = std.cstr; |
| 5 | 5 | const other = @import("other.zig"); |
| 6 | const cases_return_type_type = @import("cases/return_type_type.zig"); | |
| 6 | const test_return_type_type = @import("cases/return_type_type.zig"); | |
| 7 | 7 | const test_zeroes = @import("cases/zeroes.zig"); |
| 8 | const test_sizeof_and_typeof = @import("cases/sizeof_and_typeof.zig"); | |
| 8 | 9 | |
| 9 | 10 | // normal comment |
| 10 | 11 | /// this is a documentation comment |
| 11 | 12 | /// doc comment line 2 |
| 12 | 13 | #attribute("test") |
| 13 | fn empty_function_with_comments() {} | |
| 14 | fn emptyFunctionWithComments() {} | |
| 14 | 15 | |
| 15 | 16 | |
| 16 | 17 | #attribute("test") |
| 17 | fn if_statements() { | |
| 18 | should_be_equal(1, 1); | |
| 19 | first_eql_third(2, 1, 2); | |
| 18 | fn ifStatements() { | |
| 19 | shouldBeEqual(1, 1); | |
| 20 | firstEqlThird(2, 1, 2); | |
| 20 | 21 | } |
| 21 | fn should_be_equal(a: i32, b: i32) { | |
| 22 | fn shouldBeEqual(a: i32, b: i32) { | |
| 22 | 23 | if (a != b) { |
| 23 | 24 | unreachable{}; |
| 24 | 25 | } else { |
| 25 | 26 | return; |
| 26 | 27 | } |
| 27 | 28 | } |
| 28 | fn first_eql_third(a: i32, b: i32, c: i32) { | |
| 29 | fn firstEqlThird(a: i32, b: i32, c: i32) { | |
| 29 | 30 | if (a == b) { |
| 30 | 31 | unreachable{}; |
| 31 | 32 | } else if (b == c) { |
| ... | ... | @@ -40,33 +41,33 @@ fn first_eql_third(a: i32, b: i32, c: i32) { |
| 40 | 41 | |
| 41 | 42 | #attribute("test") |
| 42 | 43 | fn params() { |
| 43 | assert(test_params_add(22, 11) == 33); | |
| 44 | assert(testParamsAdd(22, 11) == 33); | |
| 44 | 45 | } |
| 45 | fn test_params_add(a: i32, b: i32) -> i32 { | |
| 46 | fn testParamsAdd(a: i32, b: i32) -> i32 { | |
| 46 | 47 | a + b |
| 47 | 48 | } |
| 48 | 49 | |
| 49 | 50 | |
| 50 | 51 | #attribute("test") |
| 51 | fn local_variables() { | |
| 52 | test_loc_vars(2); | |
| 52 | fn localVariables() { | |
| 53 | testLocVars(2); | |
| 53 | 54 | } |
| 54 | fn test_loc_vars(b: i32) { | |
| 55 | fn testLocVars(b: i32) { | |
| 55 | 56 | const a: i32 = 1; |
| 56 | 57 | if (a + b != 3) unreachable{}; |
| 57 | 58 | } |
| 58 | 59 | |
| 59 | 60 | #attribute("test") |
| 60 | fn bool_literals() { | |
| 61 | fn boolLiterals() { | |
| 61 | 62 | assert(true); |
| 62 | 63 | assert(!false); |
| 63 | 64 | } |
| 64 | 65 | |
| 65 | 66 | #attribute("test") |
| 66 | fn void_parameters() { | |
| 67 | void_fun(1, void{}, 2, {}); | |
| 67 | fn voidParameters() { | |
| 68 | voidFun(1, void{}, 2, {}); | |
| 68 | 69 | } |
| 69 | fn void_fun(a : i32, b : void, c : i32, d : void) { | |
| 70 | fn voidFun(a : i32, b : void, c : i32, d : void) { | |
| 70 | 71 | const v = b; |
| 71 | 72 | const vv : void = if (a == 1) {v} else {}; |
| 72 | 73 | assert(a + c == 3); |
| ... | ... | @@ -74,7 +75,7 @@ fn void_fun(a : i32, b : void, c : i32, d : void) { |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | 77 | #attribute("test") |
| 77 | fn mutable_local_variables() { | |
| 78 | fn mutableLocalVariables() { | |
| 78 | 79 | var zero : i32 = 0; |
| 79 | 80 | assert(zero == 0); |
| 80 | 81 | |
| ... | ... | @@ -104,31 +105,31 @@ fn arrays() { |
| 104 | 105 | } |
| 105 | 106 | |
| 106 | 107 | assert(accumulator == 15); |
| 107 | assert(get_array_len(array) == 5); | |
| 108 | assert(getArrayLen(array) == 5); | |
| 108 | 109 | } |
| 109 | fn get_array_len(a: []u32) -> usize { | |
| 110 | fn getArrayLen(a: []u32) -> usize { | |
| 110 | 111 | a.len |
| 111 | 112 | } |
| 112 | 113 | |
| 113 | 114 | #attribute("test") |
| 114 | fn short_circuit() { | |
| 115 | fn shortCircuit() { | |
| 115 | 116 | var hit_1 = false; |
| 116 | 117 | var hit_2 = false; |
| 117 | 118 | var hit_3 = false; |
| 118 | 119 | var hit_4 = false; |
| 119 | 120 | |
| 120 | if (true || {assert_runtime(false); false}) { | |
| 121 | if (true || {assertRuntime(false); false}) { | |
| 121 | 122 | hit_1 = true; |
| 122 | 123 | } |
| 123 | 124 | if (false || { hit_2 = true; false }) { |
| 124 | assert_runtime(false); | |
| 125 | assertRuntime(false); | |
| 125 | 126 | } |
| 126 | 127 | |
| 127 | 128 | if (true && { hit_3 = true; false }) { |
| 128 | assert_runtime(false); | |
| 129 | assertRuntime(false); | |
| 129 | 130 | } |
| 130 | if (false && {assert_runtime(false); false}) { | |
| 131 | assert_runtime(false); | |
| 131 | if (false && {assertRuntime(false); false}) { | |
| 132 | assertRuntime(false); | |
| 132 | 133 | } else { |
| 133 | 134 | hit_4 = true; |
| 134 | 135 | } |
| ... | ... | @@ -139,12 +140,12 @@ fn short_circuit() { |
| 139 | 140 | } |
| 140 | 141 | |
| 141 | 142 | #static_eval_enable(false) |
| 142 | fn assert_runtime(b: bool) { | |
| 143 | fn assertRuntime(b: bool) { | |
| 143 | 144 | if (!b) unreachable{} |
| 144 | 145 | } |
| 145 | 146 | |
| 146 | 147 | #attribute("test") |
| 147 | fn modify_operators() { | |
| 148 | fn modifyOperators() { | |
| 148 | 149 | var i : i32 = 0; |
| 149 | 150 | i += 5; assert(i == 5); |
| 150 | 151 | i -= 2; assert(i == 3); |
| ... | ... | @@ -162,7 +163,7 @@ fn modify_operators() { |
| 162 | 163 | |
| 163 | 164 | |
| 164 | 165 | #attribute("test") |
| 165 | fn separate_block_scopes() { | |
| 166 | fn separateBlockScopes() { | |
| 166 | 167 | { |
| 167 | 168 | const no_conflict : i32 = 5; |
| 168 | 169 | assert(no_conflict == 5); |
| ... | ... | @@ -177,14 +178,14 @@ fn separate_block_scopes() { |
| 177 | 178 | |
| 178 | 179 | |
| 179 | 180 | #attribute("test") |
| 180 | fn void_struct_fields() { | |
| 181 | fn voidStructFields() { | |
| 181 | 182 | const foo = VoidStructFieldsFoo { |
| 182 | 183 | .a = void{}, |
| 183 | 184 | .b = 1, |
| 184 | 185 | .c = void{}, |
| 185 | 186 | }; |
| 186 | 187 | assert(foo.b == 1); |
| 187 | assert(@sizeof(VoidStructFieldsFoo) == 4); | |
| 188 | assert(@sizeOf(VoidStructFieldsFoo) == 4); | |
| 188 | 189 | } |
| 189 | 190 | struct VoidStructFieldsFoo { |
| 190 | 191 | a : void, |
| ... | ... | @@ -197,11 +198,11 @@ struct VoidStructFieldsFoo { |
| 197 | 198 | #attribute("test") |
| 198 | 199 | pub fn structs() { |
| 199 | 200 | var foo : StructFoo = undefined; |
| 200 | @memset(&foo, 0, @sizeof(StructFoo)); | |
| 201 | @memset(&foo, 0, @sizeOf(StructFoo)); | |
| 201 | 202 | foo.a += 1; |
| 202 | 203 | foo.b = foo.a == 1; |
| 203 | test_foo(foo); | |
| 204 | test_mutation(&foo); | |
| 204 | testFoo(foo); | |
| 205 | testMutation(&foo); | |
| 205 | 206 | assert(foo.c == 100); |
| 206 | 207 | } |
| 207 | 208 | struct StructFoo { |
| ... | ... | @@ -209,10 +210,10 @@ struct StructFoo { |
| 209 | 210 | b : bool, |
| 210 | 211 | c : f32, |
| 211 | 212 | } |
| 212 | fn test_foo(foo : StructFoo) { | |
| 213 | fn testFoo(foo : StructFoo) { | |
| 213 | 214 | assert(foo.b); |
| 214 | 215 | } |
| 215 | fn test_mutation(foo : &StructFoo) { | |
| 216 | fn testMutation(foo : &StructFoo) { | |
| 216 | 217 | foo.c = 100; |
| 217 | 218 | } |
| 218 | 219 | struct Node { |
| ... | ... | @@ -225,7 +226,7 @@ struct Val { |
| 225 | 226 | } |
| 226 | 227 | |
| 227 | 228 | #attribute("test") |
| 228 | fn struct_point_to_self() { | |
| 229 | fn structPointToSelf() { | |
| 229 | 230 | var root : Node = undefined; |
| 230 | 231 | root.val.x = 1; |
| 231 | 232 | |
| ... | ... | @@ -239,7 +240,7 @@ fn struct_point_to_self() { |
| 239 | 240 | } |
| 240 | 241 | |
| 241 | 242 | #attribute("test") |
| 242 | fn struct_byval_assign() { | |
| 243 | fn structByvalAssign() { | |
| 243 | 244 | var foo1 : StructFoo = undefined; |
| 244 | 245 | var foo2 : StructFoo = undefined; |
| 245 | 246 | |
| ... | ... | @@ -250,7 +251,7 @@ fn struct_byval_assign() { |
| 250 | 251 | assert(foo2.a == 1234); |
| 251 | 252 | } |
| 252 | 253 | |
| 253 | fn struct_initializer() { | |
| 254 | fn structInitializer() { | |
| 254 | 255 | const val = Val { .x = 42 }; |
| 255 | 256 | assert(val.x == 42); |
| 256 | 257 | } |
| ... | ... | @@ -260,7 +261,7 @@ const g1 : i32 = 1233 + 1; |
| 260 | 261 | var g2 : i32 = 0; |
| 261 | 262 | |
| 262 | 263 | #attribute("test") |
| 263 | fn global_variables() { | |
| 264 | fn globalVariables() { | |
| 264 | 265 | assert(g2 == 0); |
| 265 | 266 | g2 = g1; |
| 266 | 267 | assert(g2 == 1234); |
| ... | ... | @@ -268,55 +269,55 @@ fn global_variables() { |
| 268 | 269 | |
| 269 | 270 | |
| 270 | 271 | #attribute("test") |
| 271 | fn while_loop() { | |
| 272 | fn whileLoop() { | |
| 272 | 273 | var i : i32 = 0; |
| 273 | 274 | while (i < 4) { |
| 274 | 275 | i += 1; |
| 275 | 276 | } |
| 276 | 277 | assert(i == 4); |
| 277 | assert(while_loop_1() == 1); | |
| 278 | assert(whileLoop1() == 1); | |
| 278 | 279 | } |
| 279 | fn while_loop_1() -> i32 { | |
| 280 | return while_loop_2(); | |
| 280 | fn whileLoop1() -> i32 { | |
| 281 | return whileLoop2(); | |
| 281 | 282 | } |
| 282 | fn while_loop_2() -> i32 { | |
| 283 | fn whileLoop2() -> i32 { | |
| 283 | 284 | while (true) { |
| 284 | 285 | return 1; |
| 285 | 286 | } |
| 286 | 287 | } |
| 287 | 288 | |
| 288 | 289 | #attribute("test") |
| 289 | fn void_arrays() { | |
| 290 | fn voidArrays() { | |
| 290 | 291 | var array: [4]void = undefined; |
| 291 | 292 | array[0] = void{}; |
| 292 | 293 | array[1] = array[2]; |
| 293 | assert(@sizeof(@typeof(array)) == 0); | |
| 294 | assert(@sizeOf(@typeOf(array)) == 0); | |
| 294 | 295 | assert(array.len == 4); |
| 295 | 296 | } |
| 296 | 297 | |
| 297 | 298 | |
| 298 | 299 | #attribute("test") |
| 299 | fn three_expr_in_a_row() { | |
| 300 | assert_false(false || false || false); | |
| 301 | assert_false(true && true && false); | |
| 302 | assert_false(1 | 2 | 4 != 7); | |
| 303 | assert_false(3 ^ 6 ^ 8 != 13); | |
| 304 | assert_false(7 & 14 & 28 != 4); | |
| 305 | assert_false(9 << 1 << 2 != 9 << 3); | |
| 306 | assert_false(90 >> 1 >> 2 != 90 >> 3); | |
| 307 | assert_false(100 - 1 + 1000 != 1099); | |
| 308 | assert_false(5 * 4 / 2 % 3 != 1); | |
| 309 | assert_false(i32(i32(5)) != 5); | |
| 310 | assert_false(!!false); | |
| 311 | assert_false(i32(7) != --(i32(7))); | |
| 300 | fn threeExprInARow() { | |
| 301 | assertFalse(false || false || false); | |
| 302 | assertFalse(true && true && false); | |
| 303 | assertFalse(1 | 2 | 4 != 7); | |
| 304 | assertFalse(3 ^ 6 ^ 8 != 13); | |
| 305 | assertFalse(7 & 14 & 28 != 4); | |
| 306 | assertFalse(9 << 1 << 2 != 9 << 3); | |
| 307 | assertFalse(90 >> 1 >> 2 != 90 >> 3); | |
| 308 | assertFalse(100 - 1 + 1000 != 1099); | |
| 309 | assertFalse(5 * 4 / 2 % 3 != 1); | |
| 310 | assertFalse(i32(i32(5)) != 5); | |
| 311 | assertFalse(!!false); | |
| 312 | assertFalse(i32(7) != --(i32(7))); | |
| 312 | 313 | } |
| 313 | fn assert_false(b: bool) { | |
| 314 | fn assertFalse(b: bool) { | |
| 314 | 315 | assert(!b); |
| 315 | 316 | } |
| 316 | 317 | |
| 317 | 318 | |
| 318 | 319 | #attribute("test") |
| 319 | fn maybe_type() { | |
| 320 | fn maybeType() { | |
| 320 | 321 | const x : ?bool = true; |
| 321 | 322 | |
| 322 | 323 | if (const y ?= x) { |
| ... | ... | @@ -344,21 +345,21 @@ fn maybe_type() { |
| 344 | 345 | |
| 345 | 346 | |
| 346 | 347 | #attribute("test") |
| 347 | fn enum_type() { | |
| 348 | fn enumType() { | |
| 348 | 349 | const foo1 = EnumTypeFoo.One {13}; |
| 349 | 350 | const foo2 = EnumTypeFoo.Two {EnumType { .x = 1234, .y = 5678, }}; |
| 350 | 351 | const bar = EnumTypeBar.B; |
| 351 | 352 | |
| 352 | 353 | assert(bar == EnumTypeBar.B); |
| 353 | assert(@member_count(EnumTypeFoo) == 3); | |
| 354 | assert(@member_count(EnumTypeBar) == 4); | |
| 355 | const expected_foo_size = switch (@compile_var("arch")) { | |
| 354 | assert(@memberCount(EnumTypeFoo) == 3); | |
| 355 | assert(@memberCount(EnumTypeBar) == 4); | |
| 356 | const expected_foo_size = switch (@compileVar("arch")) { | |
| 356 | 357 | i386 => 20, |
| 357 | 358 | x86_64 => 24, |
| 358 | 359 | else => unreachable{}, |
| 359 | 360 | }; |
| 360 | assert(@sizeof(EnumTypeFoo) == expected_foo_size); | |
| 361 | assert(@sizeof(EnumTypeBar) == 1); | |
| 361 | assert(@sizeOf(EnumTypeFoo) == expected_foo_size); | |
| 362 | assert(@sizeOf(EnumTypeBar) == 1); | |
| 362 | 363 | } |
| 363 | 364 | struct EnumType { |
| 364 | 365 | x: u64, |
| ... | ... | @@ -378,16 +379,16 @@ enum EnumTypeBar { |
| 378 | 379 | |
| 379 | 380 | |
| 380 | 381 | #attribute("test") |
| 381 | fn array_literal() { | |
| 382 | const HEX_MULT = []u16{4096, 256, 16, 1}; | |
| 382 | fn arrayLiteral() { | |
| 383 | const hex_mult = []u16{4096, 256, 16, 1}; | |
| 383 | 384 | |
| 384 | assert(HEX_MULT.len == 4); | |
| 385 | assert(HEX_MULT[1] == 256); | |
| 385 | assert(hex_mult.len == 4); | |
| 386 | assert(hex_mult[1] == 256); | |
| 386 | 387 | } |
| 387 | 388 | |
| 388 | 389 | |
| 389 | 390 | #attribute("test") |
| 390 | fn const_number_literal() { | |
| 391 | fn constNumberLiteral() { | |
| 391 | 392 | const one = 1; |
| 392 | 393 | const eleven = ten + one; |
| 393 | 394 | |
| ... | ... | @@ -397,7 +398,7 @@ const ten = 10; |
| 397 | 398 | |
| 398 | 399 | |
| 399 | 400 | #attribute("test") |
| 400 | fn error_values() { | |
| 401 | fn errorValues() { | |
| 401 | 402 | const a = i32(error.err1); |
| 402 | 403 | const b = i32(error.err2); |
| 403 | 404 | assert(a != b); |
| ... | ... | @@ -408,30 +409,30 @@ error err2; |
| 408 | 409 | |
| 409 | 410 | |
| 410 | 411 | #attribute("test") |
| 411 | fn fn_call_of_struct_field() { | |
| 412 | assert(call_struct_field(Foo {.ptr = a_func,}) == 13); | |
| 412 | fn fnCallOfStructField() { | |
| 413 | assert(callStructField(Foo {.ptr = aFunc,}) == 13); | |
| 413 | 414 | } |
| 414 | 415 | |
| 415 | 416 | struct Foo { |
| 416 | 417 | ptr: fn() -> i32, |
| 417 | 418 | } |
| 418 | 419 | |
| 419 | fn a_func() -> i32 { 13 } | |
| 420 | fn aFunc() -> i32 { 13 } | |
| 420 | 421 | |
| 421 | fn call_struct_field(foo: Foo) -> i32 { | |
| 422 | fn callStructField(foo: Foo) -> i32 { | |
| 422 | 423 | return foo.ptr(); |
| 423 | 424 | } |
| 424 | 425 | |
| 425 | 426 | |
| 426 | 427 | |
| 427 | 428 | #attribute("test") |
| 428 | fn redefinition_of_error_values_allowed() { | |
| 429 | should_be_not_equal(error.AnError, error.SecondError); | |
| 429 | fn redefinitionOfErrorValuesAllowed() { | |
| 430 | shouldBeNotEqual(error.AnError, error.SecondError); | |
| 430 | 431 | } |
| 431 | 432 | error AnError; |
| 432 | 433 | error AnError; |
| 433 | 434 | error SecondError; |
| 434 | fn should_be_not_equal(a: error, b: error) { | |
| 435 | fn shouldBeNotEqual(a: error, b: error) { | |
| 435 | 436 | if (a == b) unreachable{} |
| 436 | 437 | } |
| 437 | 438 | |
| ... | ... | @@ -439,21 +440,21 @@ fn should_be_not_equal(a: error, b: error) { |
| 439 | 440 | |
| 440 | 441 | |
| 441 | 442 | #attribute("test") |
| 442 | fn constant_enum_with_payload() { | |
| 443 | fn constantEnumWithPayload() { | |
| 443 | 444 | var empty = AnEnumWithPayload.Empty; |
| 444 | 445 | var full = AnEnumWithPayload.Full {13}; |
| 445 | should_be_empty(empty); | |
| 446 | should_be_not_empty(full); | |
| 446 | shouldBeEmpty(empty); | |
| 447 | shouldBeNotEmpty(full); | |
| 447 | 448 | } |
| 448 | 449 | |
| 449 | fn should_be_empty(x: AnEnumWithPayload) { | |
| 450 | fn shouldBeEmpty(x: AnEnumWithPayload) { | |
| 450 | 451 | switch (x) { |
| 451 | 452 | Empty => {}, |
| 452 | 453 | else => unreachable{}, |
| 453 | 454 | } |
| 454 | 455 | } |
| 455 | 456 | |
| 456 | fn should_be_not_empty(x: AnEnumWithPayload) { | |
| 457 | fn shouldBeNotEmpty(x: AnEnumWithPayload) { | |
| 457 | 458 | switch (x) { |
| 458 | 459 | Empty => unreachable{}, |
| 459 | 460 | else => {}, |
| ... | ... | @@ -467,7 +468,7 @@ enum AnEnumWithPayload { |
| 467 | 468 | |
| 468 | 469 | |
| 469 | 470 | #attribute("test") |
| 470 | fn continue_in_for_loop() { | |
| 471 | fn continueInForLoop() { | |
| 471 | 472 | const array = []i32 {1, 2, 3, 4, 5}; |
| 472 | 473 | var sum : i32 = 0; |
| 473 | 474 | for (array) |x| { |
| ... | ... | @@ -482,24 +483,24 @@ fn continue_in_for_loop() { |
| 482 | 483 | |
| 483 | 484 | |
| 484 | 485 | #attribute("test") |
| 485 | fn cast_bool_to_int() { | |
| 486 | fn castBoolToInt() { | |
| 486 | 487 | const t = true; |
| 487 | 488 | const f = false; |
| 488 | 489 | assert(i32(t) == i32(1)); |
| 489 | 490 | assert(i32(f) == i32(0)); |
| 490 | non_const_cast_bool_to_int(t, f); | |
| 491 | nonConstCastBoolToInt(t, f); | |
| 491 | 492 | } |
| 492 | 493 | |
| 493 | fn non_const_cast_bool_to_int(t: bool, f: bool) { | |
| 494 | fn nonConstCastBoolToInt(t: bool, f: bool) { | |
| 494 | 495 | assert(i32(t) == i32(1)); |
| 495 | 496 | assert(i32(f) == i32(0)); |
| 496 | 497 | } |
| 497 | 498 | |
| 498 | 499 | |
| 499 | 500 | #attribute("test") |
| 500 | fn switch_on_enum() { | |
| 501 | fn switchOnEnum() { | |
| 501 | 502 | const fruit = Fruit.Orange; |
| 502 | non_const_switch_on_enum(fruit); | |
| 503 | nonConstSwitchOnEnum(fruit); | |
| 503 | 504 | } |
| 504 | 505 | enum Fruit { |
| 505 | 506 | Apple, |
| ... | ... | @@ -507,7 +508,7 @@ enum Fruit { |
| 507 | 508 | Banana, |
| 508 | 509 | } |
| 509 | 510 | #static_eval_enable(false) |
| 510 | fn non_const_switch_on_enum(fruit: Fruit) { | |
| 511 | fn nonConstSwitchOnEnum(fruit: Fruit) { | |
| 511 | 512 | switch (fruit) { |
| 512 | 513 | Apple => unreachable{}, |
| 513 | 514 | Orange => {}, |
| ... | ... | @@ -516,11 +517,11 @@ fn non_const_switch_on_enum(fruit: Fruit) { |
| 516 | 517 | } |
| 517 | 518 | |
| 518 | 519 | #attribute("test") |
| 519 | fn switch_statement() { | |
| 520 | non_const_switch(SwitchStatmentFoo.C); | |
| 520 | fn switchStatement() { | |
| 521 | nonConstSwitch(SwitchStatmentFoo.C); | |
| 521 | 522 | } |
| 522 | 523 | #static_eval_enable(false) |
| 523 | fn non_const_switch(foo: SwitchStatmentFoo) { | |
| 524 | fn nonConstSwitch(foo: SwitchStatmentFoo) { | |
| 524 | 525 | const val: i32 = switch (foo) { |
| 525 | 526 | A => 1, |
| 526 | 527 | B => 2, |
| ... | ... | @@ -538,10 +539,10 @@ enum SwitchStatmentFoo { |
| 538 | 539 | |
| 539 | 540 | |
| 540 | 541 | #attribute("test") |
| 541 | fn switch_prong_with_var() { | |
| 542 | switch_prong_with_var_fn(SwitchProngWithVarEnum.One {13}); | |
| 543 | switch_prong_with_var_fn(SwitchProngWithVarEnum.Two {13.0}); | |
| 544 | switch_prong_with_var_fn(SwitchProngWithVarEnum.Meh); | |
| 542 | fn switchProngWithVar() { | |
| 543 | switchProngWithVarFn(SwitchProngWithVarEnum.One {13}); | |
| 544 | switchProngWithVarFn(SwitchProngWithVarEnum.Two {13.0}); | |
| 545 | switchProngWithVarFn(SwitchProngWithVarEnum.Meh); | |
| 545 | 546 | } |
| 546 | 547 | enum SwitchProngWithVarEnum { |
| 547 | 548 | One: i32, |
| ... | ... | @@ -549,7 +550,7 @@ enum SwitchProngWithVarEnum { |
| 549 | 550 | Meh, |
| 550 | 551 | } |
| 551 | 552 | #static_eval_enable(false) |
| 552 | fn switch_prong_with_var_fn(a: SwitchProngWithVarEnum) { | |
| 553 | fn switchProngWithVarFn(a: SwitchProngWithVarEnum) { | |
| 553 | 554 | switch(a) { |
| 554 | 555 | One => |x| { |
| 555 | 556 | if (x != 13) unreachable{}; |
| ... | ... | @@ -565,54 +566,54 @@ fn switch_prong_with_var_fn(a: SwitchProngWithVarEnum) { |
| 565 | 566 | |
| 566 | 567 | |
| 567 | 568 | #attribute("test") |
| 568 | fn err_return_in_assignment() { | |
| 569 | %%do_err_return_in_assignment(); | |
| 569 | fn errReturnInAssignment() { | |
| 570 | %%doErrReturnInAssignment(); | |
| 570 | 571 | } |
| 571 | 572 | |
| 572 | 573 | #static_eval_enable(false) |
| 573 | fn do_err_return_in_assignment() -> %void { | |
| 574 | fn doErrReturnInAssignment() -> %void { | |
| 574 | 575 | var x : i32 = undefined; |
| 575 | x = %return make_a_non_err(); | |
| 576 | x = %return makeANonErr(); | |
| 576 | 577 | } |
| 577 | 578 | |
| 578 | fn make_a_non_err() -> %i32 { | |
| 579 | fn makeANonErr() -> %i32 { | |
| 579 | 580 | return 1; |
| 580 | 581 | } |
| 581 | 582 | |
| 582 | 583 | |
| 583 | 584 | |
| 584 | 585 | #attribute("test") |
| 585 | fn rhs_maybe_unwrap_return() { | |
| 586 | fn rhsMaybeUnwrapReturn() { | |
| 586 | 587 | const x = ?true; |
| 587 | 588 | const y = x ?? return; |
| 588 | 589 | } |
| 589 | 590 | |
| 590 | 591 | |
| 591 | 592 | #attribute("test") |
| 592 | fn implicit_cast_fn_unreachable_return() { | |
| 593 | wants_fn_with_void(fn_with_unreachable); | |
| 593 | fn implicitCastFnUnreachableReturn() { | |
| 594 | wantsFnWithVoid(fnWithUnreachable); | |
| 594 | 595 | } |
| 595 | 596 | |
| 596 | fn wants_fn_with_void(f: fn()) { } | |
| 597 | fn wantsFnWithVoid(f: fn()) { } | |
| 597 | 598 | |
| 598 | fn fn_with_unreachable() -> unreachable { | |
| 599 | fn fnWithUnreachable() -> unreachable { | |
| 599 | 600 | unreachable {} |
| 600 | 601 | } |
| 601 | 602 | |
| 602 | 603 | |
| 603 | 604 | #attribute("test") |
| 604 | fn explicit_cast_maybe_pointers() { | |
| 605 | fn explicitCastMaybePointers() { | |
| 605 | 606 | const a: ?&i32 = undefined; |
| 606 | 607 | const b: ?&f32 = (?&f32)(a); |
| 607 | 608 | } |
| 608 | 609 | |
| 609 | 610 | |
| 610 | 611 | #attribute("test") |
| 611 | fn const_expr_eval_on_single_expr_blocks() { | |
| 612 | assert(const_expr_eval_on_single_expr_blocks_fn(1, true) == 3); | |
| 612 | fn constExprEvalOnSingleExprBlocks() { | |
| 613 | assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3); | |
| 613 | 614 | } |
| 614 | 615 | |
| 615 | fn const_expr_eval_on_single_expr_blocks_fn(x: i32, b: bool) -> i32 { | |
| 616 | fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 { | |
| 616 | 617 | const literal = 3; |
| 617 | 618 | |
| 618 | 619 | const result = if (b) { |
| ... | ... | @@ -626,9 +627,9 @@ fn const_expr_eval_on_single_expr_blocks_fn(x: i32, b: bool) -> i32 { |
| 626 | 627 | |
| 627 | 628 | |
| 628 | 629 | #attribute("test") |
| 629 | fn builtin_const_eval() { | |
| 630 | const x : i32 = @const_eval(1 + 2 + 3); | |
| 631 | assert(x == @const_eval(6)); | |
| 630 | fn builtinConstEval() { | |
| 631 | const x : i32 = @constEval(1 + 2 + 3); | |
| 632 | assert(x == @constEval(6)); | |
| 632 | 633 | } |
| 633 | 634 | |
| 634 | 635 | #attribute("test") |
| ... | ... | @@ -650,7 +651,7 @@ fn slicing() { |
| 650 | 651 | |
| 651 | 652 | |
| 652 | 653 | #attribute("test") |
| 653 | fn memcpy_and_memset_intrinsics() { | |
| 654 | fn memcpyAndMemsetIntrinsics() { | |
| 654 | 655 | var foo : [20]u8 = undefined; |
| 655 | 656 | var bar : [20]u8 = undefined; |
| 656 | 657 | |
| ... | ... | @@ -662,22 +663,22 @@ fn memcpy_and_memset_intrinsics() { |
| 662 | 663 | |
| 663 | 664 | |
| 664 | 665 | #attribute("test") |
| 665 | fn array_dot_len_const_expr() { } | |
| 666 | fn arrayDotLenConstExpr() { } | |
| 666 | 667 | struct ArrayDotLenConstExpr { |
| 667 | y: [@const_eval(some_array.len)]u8, | |
| 668 | y: [@constEval(some_array.len)]u8, | |
| 668 | 669 | } |
| 669 | 670 | const some_array = []u8 {0, 1, 2, 3}; |
| 670 | 671 | |
| 671 | 672 | |
| 672 | 673 | #attribute("test") |
| 673 | fn count_leading_zeroes() { | |
| 674 | fn countLeadingZeroes() { | |
| 674 | 675 | assert(@clz(u8, 0b00001010) == 4); |
| 675 | 676 | assert(@clz(u8, 0b10001010) == 0); |
| 676 | 677 | assert(@clz(u8, 0b00000000) == 8); |
| 677 | 678 | } |
| 678 | 679 | |
| 679 | 680 | #attribute("test") |
| 680 | fn count_trailing_zeroes() { | |
| 681 | fn countTrailingZeroes() { | |
| 681 | 682 | assert(@ctz(u8, 0b10100000) == 5); |
| 682 | 683 | assert(@ctz(u8, 0b10001010) == 1); |
| 683 | 684 | assert(@ctz(u8, 0b00000000) == 8); |
| ... | ... | @@ -685,7 +686,7 @@ fn count_trailing_zeroes() { |
| 685 | 686 | |
| 686 | 687 | |
| 687 | 688 | #attribute("test") |
| 688 | fn multiline_string() { | |
| 689 | fn multilineString() { | |
| 689 | 690 | const s1 = |
| 690 | 691 | \\one |
| 691 | 692 | \\two) |
| ... | ... | @@ -696,7 +697,7 @@ fn multiline_string() { |
| 696 | 697 | } |
| 697 | 698 | |
| 698 | 699 | #attribute("test") |
| 699 | fn multiline_c_string() { | |
| 700 | fn multilineCString() { | |
| 700 | 701 | const s1 = |
| 701 | 702 | c\\one |
| 702 | 703 | c\\two) |
| ... | ... | @@ -709,7 +710,7 @@ fn multiline_c_string() { |
| 709 | 710 | |
| 710 | 711 | |
| 711 | 712 | #attribute("test") |
| 712 | fn simple_generic_fn() { | |
| 713 | fn simpleGenericFn() { | |
| 713 | 714 | assert(max(i32, 3, -1) == 3); |
| 714 | 715 | assert(max(f32, 0.123, 0.456) == 0.456); |
| 715 | 716 | assert(add(2, 3) == 5); |
| ... | ... | @@ -720,42 +721,42 @@ fn max(inline T: type, a: T, b: T) -> T { |
| 720 | 721 | } |
| 721 | 722 | |
| 722 | 723 | fn add(inline a: i32, b: i32) -> i32 { |
| 723 | return @const_eval(a) + b; | |
| 724 | return @constEval(a) + b; | |
| 724 | 725 | } |
| 725 | 726 | |
| 726 | 727 | |
| 727 | 728 | #attribute("test") |
| 728 | fn constant_equal_function_pointers() { | |
| 729 | const alias = empty_fn; | |
| 730 | assert(@const_eval(empty_fn == alias)); | |
| 729 | fn constantEqualFunctionPointers() { | |
| 730 | const alias = emptyFn; | |
| 731 | assert(@constEval(emptyFn == alias)); | |
| 731 | 732 | } |
| 732 | 733 | |
| 733 | fn empty_fn() {} | |
| 734 | fn emptyFn() {} | |
| 734 | 735 | |
| 735 | 736 | |
| 736 | 737 | #attribute("test") |
| 737 | fn generic_malloc_free() { | |
| 738 | const a = %%mem_alloc(u8, 10); | |
| 739 | mem_free(u8, a); | |
| 738 | fn genericMallocFree() { | |
| 739 | const a = %%memAlloc(u8, 10); | |
| 740 | memFree(u8, a); | |
| 740 | 741 | } |
| 741 | 742 | const some_mem : [100]u8 = undefined; |
| 742 | 743 | #static_eval_enable(false) |
| 743 | fn mem_alloc(inline T: type, n: usize) -> %[]T { | |
| 744 | fn memAlloc(inline T: type, n: usize) -> %[]T { | |
| 744 | 745 | return (&T)(&some_mem[0])[0...n]; |
| 745 | 746 | } |
| 746 | fn mem_free(inline T: type, mem: []T) { } | |
| 747 | fn memFree(inline T: type, mem: []T) { } | |
| 747 | 748 | |
| 748 | 749 | |
| 749 | 750 | #attribute("test") |
| 750 | fn call_fn_with_empty_string() { | |
| 751 | accepts_string(""); | |
| 751 | fn callFnWithEmptyString() { | |
| 752 | acceptsString(""); | |
| 752 | 753 | } |
| 753 | 754 | |
| 754 | fn accepts_string(foo: []u8) { } | |
| 755 | fn acceptsString(foo: []u8) { } | |
| 755 | 756 | |
| 756 | 757 | |
| 757 | 758 | #attribute("test") |
| 758 | fn hex_escape() { | |
| 759 | fn hexEscape() { | |
| 759 | 760 | assert(str.eql("\x68\x65\x6c\x6c\x6f", "hello")); |
| 760 | 761 | } |
| 761 | 762 | |
| ... | ... | @@ -763,18 +764,18 @@ fn hex_escape() { |
| 763 | 764 | error AnError; |
| 764 | 765 | error ALongerErrorName; |
| 765 | 766 | #attribute("test") |
| 766 | fn error_name_string() { | |
| 767 | assert(str.eql(@err_name(error.AnError), "AnError")); | |
| 768 | assert(str.eql(@err_name(error.ALongerErrorName), "ALongerErrorName")); | |
| 767 | fn errorNameString() { | |
| 768 | assert(str.eql(@errName(error.AnError), "AnError")); | |
| 769 | assert(str.eql(@errName(error.ALongerErrorName), "ALongerErrorName")); | |
| 769 | 770 | } |
| 770 | 771 | |
| 771 | 772 | |
| 772 | 773 | #attribute("test") |
| 773 | fn goto_and_labels() { | |
| 774 | goto_loop(); | |
| 774 | fn gotoAndLabels() { | |
| 775 | gotoLoop(); | |
| 775 | 776 | assert(goto_counter == 10); |
| 776 | 777 | } |
| 777 | fn goto_loop() { | |
| 778 | fn gotoLoop() { | |
| 778 | 779 | var i: i32 = 0; |
| 779 | 780 | goto cond; |
| 780 | 781 | loop: |
| ... | ... | @@ -790,11 +791,11 @@ var goto_counter: i32 = 0; |
| 790 | 791 | |
| 791 | 792 | |
| 792 | 793 | #attribute("test") |
| 793 | fn goto_leave_defer_scope() { | |
| 794 | test_goto_leave_defer_scope(true); | |
| 794 | fn gotoLeaveDeferScope() { | |
| 795 | testGotoLeaveDeferScope(true); | |
| 795 | 796 | } |
| 796 | 797 | #static_eval_enable(false) |
| 797 | fn test_goto_leave_defer_scope(b: bool) { | |
| 798 | fn testGotoLeaveDeferScope(b: bool) { | |
| 798 | 799 | var it_worked = false; |
| 799 | 800 | |
| 800 | 801 | goto entry; |
| ... | ... | @@ -810,25 +811,25 @@ entry: |
| 810 | 811 | |
| 811 | 812 | |
| 812 | 813 | #attribute("test") |
| 813 | fn cast_undefined() { | |
| 814 | fn castUndefined() { | |
| 814 | 815 | const array: [100]u8 = undefined; |
| 815 | 816 | const slice = ([]u8)(array); |
| 816 | test_cast_undefined(slice); | |
| 817 | testCastUndefined(slice); | |
| 817 | 818 | } |
| 818 | fn test_cast_undefined(x: []u8) {} | |
| 819 | fn testCastUndefined(x: []u8) {} | |
| 819 | 820 | |
| 820 | 821 | |
| 821 | 822 | #attribute("test") |
| 822 | fn cast_small_unsigned_to_larger_signed() { | |
| 823 | assert(cast_small_unsigned_to_larger_signed_1(200) == i16(200)); | |
| 824 | assert(cast_small_unsigned_to_larger_signed_2(9999) == i64(9999)); | |
| 823 | fn castSmallUnsignedToLargerSigned() { | |
| 824 | assert(castSmallUnsignedToLargerSigned1(200) == i16(200)); | |
| 825 | assert(castSmallUnsignedToLargerSigned2(9999) == i64(9999)); | |
| 825 | 826 | } |
| 826 | fn cast_small_unsigned_to_larger_signed_1(x: u8) -> i16 { x } | |
| 827 | fn cast_small_unsigned_to_larger_signed_2(x: u16) -> i64 { x } | |
| 827 | fn castSmallUnsignedToLargerSigned1(x: u8) -> i16 { x } | |
| 828 | fn castSmallUnsignedToLargerSigned2(x: u16) -> i64 { x } | |
| 828 | 829 | |
| 829 | 830 | |
| 830 | 831 | #attribute("test") |
| 831 | fn implicit_cast_after_unreachable() { | |
| 832 | fn implicitCastAfterUnreachable() { | |
| 832 | 833 | assert(outer() == 1234); |
| 833 | 834 | } |
| 834 | 835 | fn inner() -> i32 { 1234 } |
| ... | ... | @@ -838,10 +839,10 @@ fn outer() -> i64 { |
| 838 | 839 | |
| 839 | 840 | |
| 840 | 841 | #attribute("test") |
| 841 | fn else_if_expression() { | |
| 842 | assert(else_if_expression_f(1) == 1); | |
| 842 | fn elseIfExpression() { | |
| 843 | assert(elseIfExpressionF(1) == 1); | |
| 843 | 844 | } |
| 844 | fn else_if_expression_f(c: u8) -> u8 { | |
| 845 | fn elseIfExpressionF(c: u8) -> u8 { | |
| 845 | 846 | if (c == 0) { |
| 846 | 847 | 0 |
| 847 | 848 | } else if (c == 1) { |
| ... | ... | @@ -852,14 +853,14 @@ fn else_if_expression_f(c: u8) -> u8 { |
| 852 | 853 | } |
| 853 | 854 | |
| 854 | 855 | #attribute("test") |
| 855 | fn err_binary_operator() { | |
| 856 | const a = err_binary_operator_g(true) %% 3; | |
| 857 | const b = err_binary_operator_g(false) %% 3; | |
| 856 | fn errBinaryOperator() { | |
| 857 | const a = errBinaryOperatorG(true) %% 3; | |
| 858 | const b = errBinaryOperatorG(false) %% 3; | |
| 858 | 859 | assert(a == 3); |
| 859 | 860 | assert(b == 10); |
| 860 | 861 | } |
| 861 | 862 | error ItBroke; |
| 862 | fn err_binary_operator_g(x: bool) -> %isize { | |
| 863 | fn errBinaryOperatorG(x: bool) -> %isize { | |
| 863 | 864 | if (x) { |
| 864 | 865 | error.ItBroke |
| 865 | 866 | } else { |
| ... | ... | @@ -868,18 +869,18 @@ fn err_binary_operator_g(x: bool) -> %isize { |
| 868 | 869 | } |
| 869 | 870 | |
| 870 | 871 | #attribute("test") |
| 871 | fn unwrap_simple_value_from_error() { | |
| 872 | const i = %%unwrap_simple_value_from_error_do(); | |
| 872 | fn unwrapSimpleValueFromError() { | |
| 873 | const i = %%unwrapSimpleValueFromErrorDo(); | |
| 873 | 874 | assert(i == 13); |
| 874 | 875 | } |
| 875 | fn unwrap_simple_value_from_error_do() -> %isize { 13 } | |
| 876 | fn unwrapSimpleValueFromErrorDo() -> %isize { 13 } | |
| 876 | 877 | |
| 877 | 878 | |
| 878 | 879 | #attribute("test") |
| 879 | fn store_member_function_in_variable() { | |
| 880 | fn storeMemberFunctionInVariable() { | |
| 880 | 881 | const instance = MemberFnTestFoo { .x = 1234, }; |
| 881 | const member_fn = MemberFnTestFoo.member; | |
| 882 | const result = member_fn(instance); | |
| 882 | const memberFn = MemberFnTestFoo.member; | |
| 883 | const result = memberFn(instance); | |
| 883 | 884 | assert(result == 1234); |
| 884 | 885 | } |
| 885 | 886 | struct MemberFnTestFoo { |
| ... | ... | @@ -888,34 +889,34 @@ struct MemberFnTestFoo { |
| 888 | 889 | } |
| 889 | 890 | |
| 890 | 891 | #attribute("test") |
| 891 | fn call_member_function_directly() { | |
| 892 | fn callMemberFunctionDirectly() { | |
| 892 | 893 | const instance = MemberFnTestFoo { .x = 1234, }; |
| 893 | 894 | const result = MemberFnTestFoo.member(instance); |
| 894 | 895 | assert(result == 1234); |
| 895 | 896 | } |
| 896 | 897 | |
| 897 | 898 | #attribute("test") |
| 898 | fn member_functions() { | |
| 899 | fn memberFunctions() { | |
| 899 | 900 | const r = MemberFnRand {.seed = 1234}; |
| 900 | assert(r.get_seed() == 1234); | |
| 901 | assert(r.getSeed() == 1234); | |
| 901 | 902 | } |
| 902 | 903 | struct MemberFnRand { |
| 903 | 904 | seed: u32, |
| 904 | pub fn get_seed(r: MemberFnRand) -> u32 { | |
| 905 | pub fn getSeed(r: MemberFnRand) -> u32 { | |
| 905 | 906 | r.seed |
| 906 | 907 | } |
| 907 | 908 | } |
| 908 | 909 | |
| 909 | 910 | #attribute("test") |
| 910 | fn static_function_evaluation() { | |
| 911 | fn staticFunctionEvaluation() { | |
| 911 | 912 | assert(statically_added_number == 3); |
| 912 | 913 | } |
| 913 | const statically_added_number = static_add(1, 2); | |
| 914 | fn static_add(a: i32, b: i32) -> i32 { a + b } | |
| 914 | const statically_added_number = staticAdd(1, 2); | |
| 915 | fn staticAdd(a: i32, b: i32) -> i32 { a + b } | |
| 915 | 916 | |
| 916 | 917 | |
| 917 | 918 | #attribute("test") |
| 918 | fn statically_initalized_list() { | |
| 919 | fn staticallyInitalizedList() { | |
| 919 | 920 | assert(static_point_list[0].x == 1); |
| 920 | 921 | assert(static_point_list[0].y == 2); |
| 921 | 922 | assert(static_point_list[1].x == 3); |
| ... | ... | @@ -925,8 +926,8 @@ struct Point { |
| 925 | 926 | x: i32, |
| 926 | 927 | y: i32, |
| 927 | 928 | } |
| 928 | const static_point_list = []Point { make_point(1, 2), make_point(3, 4) }; | |
| 929 | fn make_point(x: i32, y: i32) -> Point { | |
| 929 | const static_point_list = []Point { makePoint(1, 2), makePoint(3, 4) }; | |
| 930 | fn makePoint(x: i32, y: i32) -> Point { | |
| 930 | 931 | return Point { |
| 931 | 932 | .x = x, |
| 932 | 933 | .y = y, |
| ... | ... | @@ -935,7 +936,7 @@ fn make_point(x: i32, y: i32) -> Point { |
| 935 | 936 | |
| 936 | 937 | |
| 937 | 938 | #attribute("test") |
| 938 | fn static_eval_recursive() { | |
| 939 | fn staticEvalRecursive() { | |
| 939 | 940 | assert(seventh_fib_number == 21); |
| 940 | 941 | } |
| 941 | 942 | const seventh_fib_number = fibbonaci(7); |
| ... | ... | @@ -945,21 +946,21 @@ fn fibbonaci(x: i32) -> i32 { |
| 945 | 946 | } |
| 946 | 947 | |
| 947 | 948 | #attribute("test") |
| 948 | fn static_eval_while() { | |
| 949 | fn staticEvalWhile() { | |
| 949 | 950 | assert(static_eval_while_number == 1); |
| 950 | 951 | } |
| 951 | const static_eval_while_number = static_while_loop_1(); | |
| 952 | fn static_while_loop_1() -> i32 { | |
| 953 | return while_loop_2(); | |
| 952 | const static_eval_while_number = staticWhileLoop1(); | |
| 953 | fn staticWhileLoop1() -> i32 { | |
| 954 | return whileLoop2(); | |
| 954 | 955 | } |
| 955 | fn static_while_loop_2() -> i32 { | |
| 956 | fn staticWhileLoop2() -> i32 { | |
| 956 | 957 | while (true) { |
| 957 | 958 | return 1; |
| 958 | 959 | } |
| 959 | 960 | } |
| 960 | 961 | |
| 961 | 962 | #attribute("test") |
| 962 | fn static_eval_list_init() { | |
| 963 | fn staticEvalListInit() { | |
| 963 | 964 | assert(static_vec3.data[2] == 1.0); |
| 964 | 965 | } |
| 965 | 966 | const static_vec3 = vec3(0.0, 0.0, 1.0); |
| ... | ... | @@ -974,22 +975,22 @@ pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 { |
| 974 | 975 | |
| 975 | 976 | |
| 976 | 977 | #attribute("test") |
| 977 | fn generic_fn_with_implicit_cast() { | |
| 978 | assert(get_first_byte(u8, []u8 {13}) == 13); | |
| 979 | assert(get_first_byte(u16, []u16 {0, 13}) == 0); | |
| 978 | fn genericFnWithImplicitCast() { | |
| 979 | assert(getFirstByte(u8, []u8 {13}) == 13); | |
| 980 | assert(getFirstByte(u16, []u16 {0, 13}) == 0); | |
| 980 | 981 | } |
| 981 | fn get_byte(ptr: ?&u8) -> u8 {*??ptr} | |
| 982 | fn get_first_byte(inline T: type, mem: []T) -> u8 { | |
| 983 | get_byte((&u8)(&mem[0])) | |
| 982 | fn getByte(ptr: ?&u8) -> u8 {*??ptr} | |
| 983 | fn getFirstByte(inline T: type, mem: []T) -> u8 { | |
| 984 | getByte((&u8)(&mem[0])) | |
| 984 | 985 | } |
| 985 | 986 | |
| 986 | 987 | #attribute("test") |
| 987 | fn continue_and_break() { | |
| 988 | run_continue_and_break_test(); | |
| 988 | fn continueAndBreak() { | |
| 989 | runContinueAndBreakTest(); | |
| 989 | 990 | assert(continue_and_break_counter == 8); |
| 990 | 991 | } |
| 991 | 992 | var continue_and_break_counter: i32 = 0; |
| 992 | fn run_continue_and_break_test() { | |
| 993 | fn runContinueAndBreakTest() { | |
| 993 | 994 | var i : i32 = 0; |
| 994 | 995 | while (true) { |
| 995 | 996 | continue_and_break_counter += 2; |
| ... | ... | @@ -1002,17 +1003,9 @@ fn run_continue_and_break_test() { |
| 1002 | 1003 | assert(i == 4); |
| 1003 | 1004 | } |
| 1004 | 1005 | |
| 1005 | #attribute("test") | |
| 1006 | fn sizeof_and_typeof() { | |
| 1007 | const y: @typeof(sizeof_and_typeof_x) = 120; | |
| 1008 | assert(@sizeof(@typeof(y)) == 2); | |
| 1009 | } | |
| 1010 | const sizeof_and_typeof_x: u16 = 13; | |
| 1011 | const sizeof_and_typeof_z: @typeof(sizeof_and_typeof_x) = 19; | |
| 1012 | ||
| 1013 | 1006 | |
| 1014 | 1007 | #attribute("test") |
| 1015 | fn pointer_dereferencing() { | |
| 1008 | fn pointerDereferencing() { | |
| 1016 | 1009 | var x = i32(3); |
| 1017 | 1010 | const y = &x; |
| 1018 | 1011 | |
| ... | ... | @@ -1023,47 +1016,47 @@ fn pointer_dereferencing() { |
| 1023 | 1016 | } |
| 1024 | 1017 | |
| 1025 | 1018 | #attribute("test") |
| 1026 | fn constant_expressions() { | |
| 1027 | var array : [ARRAY_SIZE]u8 = undefined; | |
| 1028 | assert(@sizeof(@typeof(array)) == 20); | |
| 1019 | fn constantExpressions() { | |
| 1020 | var array : [array_size]u8 = undefined; | |
| 1021 | assert(@sizeOf(@typeOf(array)) == 20); | |
| 1029 | 1022 | } |
| 1030 | const ARRAY_SIZE : u8 = 20; | |
| 1023 | const array_size : u8 = 20; | |
| 1031 | 1024 | |
| 1032 | 1025 | |
| 1033 | 1026 | #attribute("test") |
| 1034 | fn min_value_and_max_value() { | |
| 1035 | assert(@max_value(u8) == 255); | |
| 1036 | assert(@max_value(u16) == 65535); | |
| 1037 | assert(@max_value(u32) == 4294967295); | |
| 1038 | assert(@max_value(u64) == 18446744073709551615); | |
| 1027 | fn minValueAndMaxValue() { | |
| 1028 | assert(@maxValue(u8) == 255); | |
| 1029 | assert(@maxValue(u16) == 65535); | |
| 1030 | assert(@maxValue(u32) == 4294967295); | |
| 1031 | assert(@maxValue(u64) == 18446744073709551615); | |
| 1039 | 1032 | |
| 1040 | assert(@max_value(i8) == 127); | |
| 1041 | assert(@max_value(i16) == 32767); | |
| 1042 | assert(@max_value(i32) == 2147483647); | |
| 1043 | assert(@max_value(i64) == 9223372036854775807); | |
| 1033 | assert(@maxValue(i8) == 127); | |
| 1034 | assert(@maxValue(i16) == 32767); | |
| 1035 | assert(@maxValue(i32) == 2147483647); | |
| 1036 | assert(@maxValue(i64) == 9223372036854775807); | |
| 1044 | 1037 | |
| 1045 | assert(@min_value(u8) == 0); | |
| 1046 | assert(@min_value(u16) == 0); | |
| 1047 | assert(@min_value(u32) == 0); | |
| 1048 | assert(@min_value(u64) == 0); | |
| 1038 | assert(@minValue(u8) == 0); | |
| 1039 | assert(@minValue(u16) == 0); | |
| 1040 | assert(@minValue(u32) == 0); | |
| 1041 | assert(@minValue(u64) == 0); | |
| 1049 | 1042 | |
| 1050 | assert(@min_value(i8) == -128); | |
| 1051 | assert(@min_value(i16) == -32768); | |
| 1052 | assert(@min_value(i32) == -2147483648); | |
| 1053 | assert(@min_value(i64) == -9223372036854775808); | |
| 1043 | assert(@minValue(i8) == -128); | |
| 1044 | assert(@minValue(i16) == -32768); | |
| 1045 | assert(@minValue(i32) == -2147483648); | |
| 1046 | assert(@minValue(i64) == -9223372036854775808); | |
| 1054 | 1047 | } |
| 1055 | 1048 | |
| 1056 | 1049 | #attribute("test") |
| 1057 | fn overflow_intrinsics() { | |
| 1050 | fn overflowIntrinsics() { | |
| 1058 | 1051 | var result: u8 = undefined; |
| 1059 | assert(@add_with_overflow(u8, 250, 100, &result)); | |
| 1060 | assert(!@add_with_overflow(u8, 100, 150, &result)); | |
| 1052 | assert(@addWithOverflow(u8, 250, 100, &result)); | |
| 1053 | assert(!@addWithOverflow(u8, 100, 150, &result)); | |
| 1061 | 1054 | assert(result == 250); |
| 1062 | 1055 | } |
| 1063 | 1056 | |
| 1064 | 1057 | |
| 1065 | 1058 | #attribute("test") |
| 1066 | fn nested_arrays() { | |
| 1059 | fn nestedArrays() { | |
| 1067 | 1060 | const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"}; |
| 1068 | 1061 | for (array_of_strings) |s, i| { |
| 1069 | 1062 | if (i == 0) assert(str.eql(s, "hello")); |
| ... | ... | @@ -1075,7 +1068,7 @@ fn nested_arrays() { |
| 1075 | 1068 | } |
| 1076 | 1069 | |
| 1077 | 1070 | #attribute("test") |
| 1078 | fn int_to_ptr_cast() { | |
| 1071 | fn intToPtrCast() { | |
| 1079 | 1072 | const x = isize(13); |
| 1080 | 1073 | const y = (&u8)(x); |
| 1081 | 1074 | const z = usize(y); |
| ... | ... | @@ -1083,12 +1076,12 @@ fn int_to_ptr_cast() { |
| 1083 | 1076 | } |
| 1084 | 1077 | |
| 1085 | 1078 | #attribute("test") |
| 1086 | fn string_concatenation() { | |
| 1079 | fn stringConcatenation() { | |
| 1087 | 1080 | assert(str.eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED")); |
| 1088 | 1081 | } |
| 1089 | 1082 | |
| 1090 | 1083 | #attribute("test") |
| 1091 | fn constant_struct_with_negation() { | |
| 1084 | fn constantStructWithNegation() { | |
| 1092 | 1085 | assert(vertices[0].x == -0.6); |
| 1093 | 1086 | } |
| 1094 | 1087 | struct Vertex { |
| ... | ... | @@ -1106,25 +1099,25 @@ const vertices = []Vertex { |
| 1106 | 1099 | |
| 1107 | 1100 | |
| 1108 | 1101 | #attribute("test") |
| 1109 | fn return_with_implicit_cast_from_while_loop() { | |
| 1110 | %%return_with_implicit_cast_from_while_loop_test(); | |
| 1102 | fn returnWithImplicitCastFromWhileLoop() { | |
| 1103 | %%returnWithImplicitCastFromWhileLoopTest(); | |
| 1111 | 1104 | } |
| 1112 | fn return_with_implicit_cast_from_while_loop_test() -> %void { | |
| 1105 | fn returnWithImplicitCastFromWhileLoopTest() -> %void { | |
| 1113 | 1106 | while (true) { |
| 1114 | 1107 | return; |
| 1115 | 1108 | } |
| 1116 | 1109 | } |
| 1117 | 1110 | |
| 1118 | 1111 | #attribute("test") |
| 1119 | fn return_struct_byval_from_function() { | |
| 1120 | const bar = make_bar(1234, 5678); | |
| 1112 | fn returnStructByvalFromFunction() { | |
| 1113 | const bar = makeBar(1234, 5678); | |
| 1121 | 1114 | assert(bar.y == 5678); |
| 1122 | 1115 | } |
| 1123 | 1116 | struct Bar { |
| 1124 | 1117 | x: i32, |
| 1125 | 1118 | y: i32, |
| 1126 | 1119 | } |
| 1127 | fn make_bar(x: i32, y: i32) -> Bar { | |
| 1120 | fn makeBar(x: i32, y: i32) -> Bar { | |
| 1128 | 1121 | Bar { |
| 1129 | 1122 | .x = x, |
| 1130 | 1123 | .y = y, |
| ... | ... | @@ -1132,8 +1125,8 @@ fn make_bar(x: i32, y: i32) -> Bar { |
| 1132 | 1125 | } |
| 1133 | 1126 | |
| 1134 | 1127 | #attribute("test") |
| 1135 | fn function_pointers() { | |
| 1136 | const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, }; | |
| 1128 | fn functionPointers() { | |
| 1129 | const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, }; | |
| 1137 | 1130 | for (fns) |f, i| { |
| 1138 | 1131 | assert(f() == u32(i) + 5); |
| 1139 | 1132 | } |
| ... | ... | @@ -1146,7 +1139,7 @@ fn fn4() -> u32 {8} |
| 1146 | 1139 | |
| 1147 | 1140 | |
| 1148 | 1141 | #attribute("test") |
| 1149 | fn statically_initalized_struct() { | |
| 1142 | fn staticallyInitalizedStruct() { | |
| 1150 | 1143 | st_init_str_foo.x += 1; |
| 1151 | 1144 | assert(st_init_str_foo.x == 14); |
| 1152 | 1145 | } |
| ... | ... | @@ -1157,7 +1150,7 @@ struct StInitStrFoo { |
| 1157 | 1150 | var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, }; |
| 1158 | 1151 | |
| 1159 | 1152 | #attribute("test") |
| 1160 | fn statically_initialized_array_literal() { | |
| 1153 | fn staticallyInitializedArrayLiteral() { | |
| 1161 | 1154 | const y : [4]u8 = st_init_arr_lit_x; |
| 1162 | 1155 | assert(y[3] == 4); |
| 1163 | 1156 | } |
| ... | ... | @@ -1166,33 +1159,33 @@ const st_init_arr_lit_x = []u8{1,2,3,4}; |
| 1166 | 1159 | |
| 1167 | 1160 | |
| 1168 | 1161 | #attribute("test") |
| 1169 | fn pointer_to_void_return_type() { | |
| 1170 | %%test_pointer_to_void_return_type(); | |
| 1162 | fn pointerToVoidReturnType() { | |
| 1163 | %%testPointerToVoidReturnType(); | |
| 1171 | 1164 | } |
| 1172 | fn test_pointer_to_void_return_type() -> %void { | |
| 1173 | const a = test_pointer_to_void_return_type_2(); | |
| 1165 | fn testPointerToVoidReturnType() -> %void { | |
| 1166 | const a = testPointerToVoidReturnType2(); | |
| 1174 | 1167 | return *a; |
| 1175 | 1168 | } |
| 1176 | 1169 | const test_pointer_to_void_return_type_x = void{}; |
| 1177 | fn test_pointer_to_void_return_type_2() -> &void { | |
| 1170 | fn testPointerToVoidReturnType2() -> &void { | |
| 1178 | 1171 | return &test_pointer_to_void_return_type_x; |
| 1179 | 1172 | } |
| 1180 | 1173 | |
| 1181 | 1174 | |
| 1182 | 1175 | #attribute("test") |
| 1183 | fn call_result_of_if_else_expression() { | |
| 1176 | fn callResultOfIfElseExpression() { | |
| 1184 | 1177 | assert(str.eql(f2(true), "a")); |
| 1185 | 1178 | assert(str.eql(f2(false), "b")); |
| 1186 | 1179 | } |
| 1187 | 1180 | fn f2(x: bool) -> []u8 { |
| 1188 | return (if (x) f_a else f_b)(); | |
| 1181 | return (if (x) fA else fB)(); | |
| 1189 | 1182 | } |
| 1190 | fn f_a() -> []u8 { "a" } | |
| 1191 | fn f_b() -> []u8 { "b" } | |
| 1183 | fn fA() -> []u8 { "a" } | |
| 1184 | fn fB() -> []u8 { "b" } | |
| 1192 | 1185 | |
| 1193 | 1186 | |
| 1194 | 1187 | #attribute("test") |
| 1195 | fn const_expression_eval_handling_of_variables() { | |
| 1188 | fn constExpressionEvalHandlingOfVariables() { | |
| 1196 | 1189 | var x = true; |
| 1197 | 1190 | while (x) { |
| 1198 | 1191 | x = false; |
| ... | ... | @@ -1202,7 +1195,7 @@ fn const_expression_eval_handling_of_variables() { |
| 1202 | 1195 | |
| 1203 | 1196 | |
| 1204 | 1197 | #attribute("test") |
| 1205 | fn constant_enum_initialization_with_differing_sizes() { | |
| 1198 | fn constantEnumInitializationWithDifferingSizes() { | |
| 1206 | 1199 | test3_1(test3_foo); |
| 1207 | 1200 | test3_2(test3_bar); |
| 1208 | 1201 | } |
| ... | ... | @@ -1240,22 +1233,22 @@ fn test3_2(f: Test3Foo) { |
| 1240 | 1233 | |
| 1241 | 1234 | |
| 1242 | 1235 | #attribute("test") |
| 1243 | fn pub_enum() { | |
| 1244 | pub_enum_test(other.APubEnum.Two); | |
| 1236 | fn pubEnum() { | |
| 1237 | pubEnumTest(other.APubEnum.Two); | |
| 1245 | 1238 | } |
| 1246 | fn pub_enum_test(foo: other.APubEnum) { | |
| 1239 | fn pubEnumTest(foo: other.APubEnum) { | |
| 1247 | 1240 | assert(foo == other.APubEnum.Two); |
| 1248 | 1241 | } |
| 1249 | 1242 | |
| 1250 | 1243 | |
| 1251 | 1244 | #attribute("test") |
| 1252 | fn cast_with_imported_symbol() { | |
| 1245 | fn castWithImportedSymbol() { | |
| 1253 | 1246 | assert(other.size_t(42) == 42); |
| 1254 | 1247 | } |
| 1255 | 1248 | |
| 1256 | 1249 | |
| 1257 | 1250 | #attribute("test") |
| 1258 | fn while_with_continue_expr() { | |
| 1251 | fn whileWithContinueExpr() { | |
| 1259 | 1252 | var sum: i32 = 0; |
| 1260 | 1253 | {var i: i32 = 0; while (i < 10; i += 1) { |
| 1261 | 1254 | if (i == 5) continue; |
| ... | ... | @@ -1266,22 +1259,22 @@ fn while_with_continue_expr() { |
| 1266 | 1259 | |
| 1267 | 1260 | |
| 1268 | 1261 | #attribute("test") |
| 1269 | fn for_loop_with_pointer_elem_var() { | |
| 1262 | fn forLoopWithPointerElemVar() { | |
| 1270 | 1263 | const source = "abcdefg"; |
| 1271 | 1264 | var target: [source.len]u8 = undefined; |
| 1272 | 1265 | @memcpy(&target[0], &source[0], source.len); |
| 1273 | mangle_string(target); | |
| 1266 | mangleString(target); | |
| 1274 | 1267 | assert(str.eql(target, "bcdefgh")); |
| 1275 | 1268 | } |
| 1276 | 1269 | #static_eval_enable(false) |
| 1277 | fn mangle_string(s: []u8) { | |
| 1270 | fn mangleString(s: []u8) { | |
| 1278 | 1271 | for (s) |*c| { |
| 1279 | 1272 | *c += 1; |
| 1280 | 1273 | } |
| 1281 | 1274 | } |
| 1282 | 1275 | |
| 1283 | 1276 | #attribute("test") |
| 1284 | fn empty_struct_method_call() { | |
| 1277 | fn emptyStructMethodCall() { | |
| 1285 | 1278 | const es = EmptyStruct{}; |
| 1286 | 1279 | assert(es.method() == 1234); |
| 1287 | 1280 | } |
| ... | ... | @@ -1296,48 +1289,48 @@ fn @"weird function name"() { } |
| 1296 | 1289 | |
| 1297 | 1290 | |
| 1298 | 1291 | #attribute("test") |
| 1299 | fn return_empty_struct_from_fn() { | |
| 1300 | test_return_empty_struct_from_fn(); | |
| 1301 | test_return_empty_struct_from_fn_noeval(); | |
| 1292 | fn returnEmptyStructFromFn() { | |
| 1293 | testReturnEmptyStructFromFn(); | |
| 1294 | testReturnEmptyStructFromFnNoeval(); | |
| 1302 | 1295 | } |
| 1303 | 1296 | struct EmptyStruct2 {} |
| 1304 | fn test_return_empty_struct_from_fn() -> EmptyStruct2 { | |
| 1297 | fn testReturnEmptyStructFromFn() -> EmptyStruct2 { | |
| 1305 | 1298 | EmptyStruct2 {} |
| 1306 | 1299 | } |
| 1307 | 1300 | #static_eval_enable(false) |
| 1308 | fn test_return_empty_struct_from_fn_noeval() -> EmptyStruct2 { | |
| 1301 | fn testReturnEmptyStructFromFnNoeval() -> EmptyStruct2 { | |
| 1309 | 1302 | EmptyStruct2 {} |
| 1310 | 1303 | } |
| 1311 | 1304 | |
| 1312 | 1305 | #attribute("test") |
| 1313 | fn pass_slice_of_empty_struct_to_fn() { | |
| 1314 | assert(test_pass_slice_of_empty_struct_to_fn([]EmptyStruct2{ EmptyStruct2{} }) == 1); | |
| 1306 | fn passSliceOfEmptyStructToFn() { | |
| 1307 | assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1); | |
| 1315 | 1308 | } |
| 1316 | fn test_pass_slice_of_empty_struct_to_fn(slice: []EmptyStruct2) -> usize { | |
| 1309 | fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize { | |
| 1317 | 1310 | slice.len |
| 1318 | 1311 | } |
| 1319 | 1312 | |
| 1320 | 1313 | |
| 1321 | 1314 | #attribute("test") |
| 1322 | fn pointer_comparison() { | |
| 1315 | fn pointerComparison() { | |
| 1323 | 1316 | const a = ([]u8)("a"); |
| 1324 | 1317 | const b = &a; |
| 1325 | assert(ptr_eql(b, b)); | |
| 1318 | assert(ptrEql(b, b)); | |
| 1326 | 1319 | } |
| 1327 | fn ptr_eql(a: &[]u8, b: &[]u8) -> bool { | |
| 1320 | fn ptrEql(a: &[]u8, b: &[]u8) -> bool { | |
| 1328 | 1321 | a == b |
| 1329 | 1322 | } |
| 1330 | 1323 | |
| 1331 | 1324 | #attribute("test") |
| 1332 | fn character_literals() { | |
| 1325 | fn characterLiterals() { | |
| 1333 | 1326 | assert('\'' == single_quote); |
| 1334 | 1327 | } |
| 1335 | 1328 | const single_quote = '\''; |
| 1336 | 1329 | |
| 1337 | 1330 | |
| 1338 | 1331 | #attribute("test") |
| 1339 | fn switch_with_multiple_expressions() { | |
| 1340 | const x: i32 = switch (returns_five()) { | |
| 1332 | fn switchWithMultipleExpressions() { | |
| 1333 | const x: i32 = switch (returnsFive()) { | |
| 1341 | 1334 | 1, 2, 3 => 1, |
| 1342 | 1335 | 4, 5, 6 => 2, |
| 1343 | 1336 | else => 3, |
| ... | ... | @@ -1345,12 +1338,12 @@ fn switch_with_multiple_expressions() { |
| 1345 | 1338 | assert(x == 2); |
| 1346 | 1339 | } |
| 1347 | 1340 | #static_eval_enable(false) |
| 1348 | fn returns_five() -> i32 { 5 } | |
| 1341 | fn returnsFive() -> i32 { 5 } | |
| 1349 | 1342 | |
| 1350 | 1343 | |
| 1351 | 1344 | #attribute("test") |
| 1352 | fn switch_on_error_union() { | |
| 1353 | const x = switch (returns_ten()) { | |
| 1345 | fn switchOnErrorUnion() { | |
| 1346 | const x = switch (returnsTen()) { | |
| 1354 | 1347 | Ok => |val| val + 1, |
| 1355 | 1348 | ItBroke, NoMem => 1, |
| 1356 | 1349 | CrappedOut => 2, |
| ... | ... | @@ -1361,40 +1354,40 @@ error ItBroke; |
| 1361 | 1354 | error NoMem; |
| 1362 | 1355 | error CrappedOut; |
| 1363 | 1356 | #static_eval_enable(false) |
| 1364 | fn returns_ten() -> %i32 { 10 } | |
| 1357 | fn returnsTen() -> %i32 { 10 } | |
| 1365 | 1358 | |
| 1366 | 1359 | |
| 1367 | 1360 | #attribute("test") |
| 1368 | fn bool_cmp() { | |
| 1369 | assert(test_bool_cmp(true, false) == false); | |
| 1361 | fn boolCmp() { | |
| 1362 | assert(testBoolCmp(true, false) == false); | |
| 1370 | 1363 | } |
| 1371 | 1364 | #static_eval_enable(false) |
| 1372 | fn test_bool_cmp(a: bool, b: bool) -> bool { a == b } | |
| 1365 | fn testBoolCmp(a: bool, b: bool) -> bool { a == b } | |
| 1373 | 1366 | |
| 1374 | 1367 | |
| 1375 | 1368 | #attribute("test") |
| 1376 | fn take_address_of_parameter() { | |
| 1377 | test_take_address_of_parameter(12.34); | |
| 1378 | test_take_address_of_parameter_noeval(12.34); | |
| 1369 | fn takeAddressOfParameter() { | |
| 1370 | testTakeAddressOfParameter(12.34); | |
| 1371 | testTakeAddressOfParameterNoeval(12.34); | |
| 1379 | 1372 | } |
| 1380 | fn test_take_address_of_parameter(f: f32) { | |
| 1373 | fn testTakeAddressOfParameter(f: f32) { | |
| 1381 | 1374 | const f_ptr = &f; |
| 1382 | 1375 | assert(*f_ptr == 12.34); |
| 1383 | 1376 | } |
| 1384 | 1377 | #static_eval_enable(false) |
| 1385 | fn test_take_address_of_parameter_noeval(f: f32) { | |
| 1378 | fn testTakeAddressOfParameterNoeval(f: f32) { | |
| 1386 | 1379 | const f_ptr = &f; |
| 1387 | 1380 | assert(*f_ptr == 12.34); |
| 1388 | 1381 | } |
| 1389 | 1382 | |
| 1390 | 1383 | |
| 1391 | 1384 | #attribute("test") |
| 1392 | fn array_mult_operator() { | |
| 1385 | fn arrayMultOperator() { | |
| 1393 | 1386 | assert(str.eql("ab" ** 5, "ababababab")); |
| 1394 | 1387 | } |
| 1395 | 1388 | |
| 1396 | 1389 | #attribute("test") |
| 1397 | fn string_escapes() { | |
| 1390 | fn stringEscapes() { | |
| 1398 | 1391 | assert(str.eql("\"", "\x22")); |
| 1399 | 1392 | assert(str.eql("\'", "\x27")); |
| 1400 | 1393 | assert(str.eql("\n", "\x0a")); |
| ... | ... | @@ -1405,11 +1398,11 @@ fn string_escapes() { |
| 1405 | 1398 | } |
| 1406 | 1399 | |
| 1407 | 1400 | #attribute("test") |
| 1408 | fn if_var_maybe_pointer() { | |
| 1409 | assert(should_be_a_plus_1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15); | |
| 1401 | fn ifVarMaybePointer() { | |
| 1402 | assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15); | |
| 1410 | 1403 | } |
| 1411 | 1404 | #static_eval_enable(false) |
| 1412 | fn should_be_a_plus_1(p: Particle) -> u64 { | |
| 1405 | fn shouldBeAPlus1(p: Particle) -> u64 { | |
| 1413 | 1406 | var maybe_particle: ?Particle = p; |
| 1414 | 1407 | if (const *particle ?= maybe_particle) { |
| 1415 | 1408 | particle.a += 1; |
| ... | ... | @@ -1427,7 +1420,7 @@ struct Particle { |
| 1427 | 1420 | } |
| 1428 | 1421 | |
| 1429 | 1422 | #attribute("test") |
| 1430 | fn assign_to_if_var_ptr() { | |
| 1423 | fn assignToIfVarPtr() { | |
| 1431 | 1424 | var maybe_bool: ?bool = true; |
| 1432 | 1425 | |
| 1433 | 1426 | if (const *b ?= maybe_bool) { |
| ... | ... | @@ -1452,85 +1445,85 @@ fn fence() { |
| 1452 | 1445 | } |
| 1453 | 1446 | |
| 1454 | 1447 | #attribute("test") |
| 1455 | fn unsigned_wrapping() { | |
| 1456 | test_unsigned_wrapping_eval(@max_value(u32)); | |
| 1457 | test_unsigned_wrapping_noeval(@max_value(u32)); | |
| 1448 | fn unsignedWrapping() { | |
| 1449 | testUnsignedWrappingEval(@maxValue(u32)); | |
| 1450 | testUnsignedWrappingNoeval(@maxValue(u32)); | |
| 1458 | 1451 | } |
| 1459 | fn test_unsigned_wrapping_eval(x: u32) { | |
| 1452 | fn testUnsignedWrappingEval(x: u32) { | |
| 1460 | 1453 | const zero = x +% 1; |
| 1461 | 1454 | assert(zero == 0); |
| 1462 | 1455 | const orig = zero -% 1; |
| 1463 | assert(orig == @max_value(u32)); | |
| 1456 | assert(orig == @maxValue(u32)); | |
| 1464 | 1457 | } |
| 1465 | 1458 | #static_eval_enable(false) |
| 1466 | fn test_unsigned_wrapping_noeval(x: u32) { | |
| 1459 | fn testUnsignedWrappingNoeval(x: u32) { | |
| 1467 | 1460 | const zero = x +% 1; |
| 1468 | 1461 | assert(zero == 0); |
| 1469 | 1462 | const orig = zero -% 1; |
| 1470 | assert(orig == @max_value(u32)); | |
| 1463 | assert(orig == @maxValue(u32)); | |
| 1471 | 1464 | } |
| 1472 | 1465 | |
| 1473 | 1466 | #attribute("test") |
| 1474 | fn signed_wrapping() { | |
| 1475 | test_signed_wrapping_eval(@max_value(i32)); | |
| 1476 | test_signed_wrapping_noeval(@max_value(i32)); | |
| 1467 | fn signedWrapping() { | |
| 1468 | testSignedWrappingEval(@maxValue(i32)); | |
| 1469 | testSignedWrappingNoeval(@maxValue(i32)); | |
| 1477 | 1470 | } |
| 1478 | fn test_signed_wrapping_eval(x: i32) { | |
| 1471 | fn testSignedWrappingEval(x: i32) { | |
| 1479 | 1472 | const min_val = x +% 1; |
| 1480 | assert(min_val == @min_value(i32)); | |
| 1473 | assert(min_val == @minValue(i32)); | |
| 1481 | 1474 | const max_val = min_val -% 1; |
| 1482 | assert(max_val == @max_value(i32)); | |
| 1475 | assert(max_val == @maxValue(i32)); | |
| 1483 | 1476 | } |
| 1484 | 1477 | #static_eval_enable(false) |
| 1485 | fn test_signed_wrapping_noeval(x: i32) { | |
| 1478 | fn testSignedWrappingNoeval(x: i32) { | |
| 1486 | 1479 | const min_val = x +% 1; |
| 1487 | assert(min_val == @min_value(i32)); | |
| 1480 | assert(min_val == @minValue(i32)); | |
| 1488 | 1481 | const max_val = min_val -% 1; |
| 1489 | assert(max_val == @max_value(i32)); | |
| 1482 | assert(max_val == @maxValue(i32)); | |
| 1490 | 1483 | } |
| 1491 | 1484 | |
| 1492 | 1485 | #attribute("test") |
| 1493 | fn negation_wrapping() { | |
| 1494 | test_negation_wrapping_eval(@min_value(i16)); | |
| 1495 | test_negation_wrapping_noeval(@min_value(i16)); | |
| 1486 | fn negationWrapping() { | |
| 1487 | testNegationWrappingEval(@minValue(i16)); | |
| 1488 | testNegationWrappingNoeval(@minValue(i16)); | |
| 1496 | 1489 | } |
| 1497 | fn test_negation_wrapping_eval(x: i16) { | |
| 1490 | fn testNegationWrappingEval(x: i16) { | |
| 1498 | 1491 | assert(x == -32768); |
| 1499 | 1492 | const neg = -%x; |
| 1500 | 1493 | assert(neg == -32768); |
| 1501 | 1494 | } |
| 1502 | 1495 | #static_eval_enable(false) |
| 1503 | fn test_negation_wrapping_noeval(x: i16) { | |
| 1496 | fn testNegationWrappingNoeval(x: i16) { | |
| 1504 | 1497 | assert(x == -32768); |
| 1505 | 1498 | const neg = -%x; |
| 1506 | 1499 | assert(neg == -32768); |
| 1507 | 1500 | } |
| 1508 | 1501 | |
| 1509 | 1502 | #attribute("test") |
| 1510 | fn shl_wrapping() { | |
| 1511 | test_shl_wrapping_eval(@max_value(u16)); | |
| 1512 | test_shl_wrapping_noeval(@max_value(u16)); | |
| 1503 | fn shlWrapping() { | |
| 1504 | testShlWrappingEval(@maxValue(u16)); | |
| 1505 | testShlWrappingNoeval(@maxValue(u16)); | |
| 1513 | 1506 | } |
| 1514 | fn test_shl_wrapping_eval(x: u16) { | |
| 1507 | fn testShlWrappingEval(x: u16) { | |
| 1515 | 1508 | const shifted = x <<% 1; |
| 1516 | 1509 | assert(shifted == 65534); |
| 1517 | 1510 | } |
| 1518 | 1511 | #static_eval_enable(false) |
| 1519 | fn test_shl_wrapping_noeval(x: u16) { | |
| 1512 | fn testShlWrappingNoeval(x: u16) { | |
| 1520 | 1513 | const shifted = x <<% 1; |
| 1521 | 1514 | assert(shifted == 65534); |
| 1522 | 1515 | } |
| 1523 | 1516 | |
| 1524 | 1517 | #attribute("test") |
| 1525 | fn shl_with_overflow() { | |
| 1518 | fn shlWithOverflow() { | |
| 1526 | 1519 | var result: u16 = undefined; |
| 1527 | assert(@shl_with_overflow(u16, 0b0010111111111111, 3, &result)); | |
| 1528 | assert(!@shl_with_overflow(u16, 0b0010111111111111, 2, &result)); | |
| 1520 | assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result)); | |
| 1521 | assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result)); | |
| 1529 | 1522 | assert(result == 0b1011111111111100); |
| 1530 | 1523 | } |
| 1531 | 1524 | |
| 1532 | 1525 | #attribute("test") |
| 1533 | fn c_string_concatenation() { | |
| 1526 | fn cStringConcatenation() { | |
| 1534 | 1527 | const a = c"OK" ++ c" IT " ++ c"WORKED"; |
| 1535 | 1528 | const b = c"OK IT WORKED"; |
| 1536 | 1529 | |
| ... | ... | @@ -1544,22 +1537,22 @@ fn c_string_concatenation() { |
| 1544 | 1537 | } |
| 1545 | 1538 | |
| 1546 | 1539 | #attribute("test") |
| 1547 | fn generic_struct() { | |
| 1540 | fn genericStruct() { | |
| 1548 | 1541 | var a1 = GenNode(i32) {.value = 13, .next = null,}; |
| 1549 | 1542 | var b1 = GenNode(bool) {.value = true, .next = null,}; |
| 1550 | 1543 | assert(a1.value == 13); |
| 1551 | assert(a1.value == a1.get_val()); | |
| 1552 | assert(b1.get_val()); | |
| 1544 | assert(a1.value == a1.getVal()); | |
| 1545 | assert(b1.getVal()); | |
| 1553 | 1546 | } |
| 1554 | 1547 | struct GenNode(T: type) { |
| 1555 | 1548 | value: T, |
| 1556 | 1549 | next: ?&GenNode(T), |
| 1557 | fn get_val(n: &const GenNode(T)) -> T { n.value } | |
| 1550 | fn getVal(n: &const GenNode(T)) -> T { n.value } | |
| 1558 | 1551 | } |
| 1559 | 1552 | |
| 1560 | 1553 | #attribute("test") |
| 1561 | fn cast_slice_to_u8_slice() { | |
| 1562 | assert(@sizeof(i32) == 4); | |
| 1554 | fn castSliceToU8Slice() { | |
| 1555 | assert(@sizeOf(i32) == 4); | |
| 1563 | 1556 | var big_thing_array = []i32{1, 2, 3, 4}; |
| 1564 | 1557 | const big_thing_slice: []i32 = big_thing_array; |
| 1565 | 1558 | const bytes = ([]u8)(big_thing_slice); |
| ... | ... | @@ -1572,14 +1565,14 @@ fn cast_slice_to_u8_slice() { |
| 1572 | 1565 | const big_thing_again = ([]i32)(bytes); |
| 1573 | 1566 | assert(big_thing_again[2] == 3); |
| 1574 | 1567 | big_thing_again[2] = -1; |
| 1575 | assert(bytes[8] == @max_value(u8)); | |
| 1576 | assert(bytes[9] == @max_value(u8)); | |
| 1577 | assert(bytes[10] == @max_value(u8)); | |
| 1578 | assert(bytes[11] == @max_value(u8)); | |
| 1568 | assert(bytes[8] == @maxValue(u8)); | |
| 1569 | assert(bytes[9] == @maxValue(u8)); | |
| 1570 | assert(bytes[10] == @maxValue(u8)); | |
| 1571 | assert(bytes[11] == @maxValue(u8)); | |
| 1579 | 1572 | } |
| 1580 | 1573 | |
| 1581 | 1574 | #attribute("test") |
| 1582 | fn float_division() { | |
| 1575 | fn floatDivision() { | |
| 1583 | 1576 | assert(fdiv32(12.0, 3.0) == 4.0); |
| 1584 | 1577 | } |
| 1585 | 1578 | #static_eval_enable(false) |
| ... | ... | @@ -1588,16 +1581,16 @@ fn fdiv32(a: f32, b: f32) -> f32 { |
| 1588 | 1581 | } |
| 1589 | 1582 | |
| 1590 | 1583 | #attribute("test") |
| 1591 | fn exact_division() { | |
| 1592 | assert(div_exact(55, 11) == 5); | |
| 1584 | fn exactDivision() { | |
| 1585 | assert(divExact(55, 11) == 5); | |
| 1593 | 1586 | } |
| 1594 | 1587 | #static_eval_enable(false) |
| 1595 | fn div_exact(a: u32, b: u32) -> u32 { | |
| 1596 | @div_exact(a, b) | |
| 1588 | fn divExact(a: u32, b: u32) -> u32 { | |
| 1589 | @divExact(a, b) | |
| 1597 | 1590 | } |
| 1598 | 1591 | |
| 1599 | 1592 | #attribute("test") |
| 1600 | fn null_literal_outside_function() { | |
| 1593 | fn nullLiteralOutsideFunction() { | |
| 1601 | 1594 | const is_null = if (const _ ?= here_is_a_null_literal.context) false else true; |
| 1602 | 1595 | assert(is_null); |
| 1603 | 1596 | } |
| ... | ... | @@ -1610,15 +1603,15 @@ const here_is_a_null_literal = SillyStruct { |
| 1610 | 1603 | |
| 1611 | 1604 | #attribute("test") |
| 1612 | 1605 | fn truncate() { |
| 1613 | assert(test_truncate(0x10fd) == 0xfd); | |
| 1606 | assert(testTruncate(0x10fd) == 0xfd); | |
| 1614 | 1607 | } |
| 1615 | 1608 | #static_eval_enable(false) |
| 1616 | fn test_truncate(x: u32) -> u8 { | |
| 1609 | fn testTruncate(x: u32) -> u8 { | |
| 1617 | 1610 | @truncate(u8, x) |
| 1618 | 1611 | } |
| 1619 | 1612 | |
| 1620 | 1613 | #attribute("test") |
| 1621 | fn const_decls_in_struct() { | |
| 1614 | fn constDeclsInStruct() { | |
| 1622 | 1615 | assert(GenericDataThing(3).count_plus_one == 4); |
| 1623 | 1616 | } |
| 1624 | 1617 | struct GenericDataThing(count: isize) { |
| ... | ... | @@ -1626,30 +1619,30 @@ struct GenericDataThing(count: isize) { |
| 1626 | 1619 | } |
| 1627 | 1620 | |
| 1628 | 1621 | #attribute("test") |
| 1629 | fn use_generic_param_in_generic_param() { | |
| 1630 | assert(a_generic_fn(i32, 3, 4) == 7); | |
| 1622 | fn useGenericParamInGenericParam() { | |
| 1623 | assert(aGenericFn(i32, 3, 4) == 7); | |
| 1631 | 1624 | } |
| 1632 | fn a_generic_fn(inline T: type, inline a: T, b: T) -> T { | |
| 1625 | fn aGenericFn(inline T: type, inline a: T, b: T) -> T { | |
| 1633 | 1626 | return a + b; |
| 1634 | 1627 | } |
| 1635 | 1628 | |
| 1636 | 1629 | |
| 1637 | 1630 | #attribute("test") |
| 1638 | fn namespace_depends_on_compile_var() { | |
| 1631 | fn namespaceDependsOnCompileVar() { | |
| 1639 | 1632 | if (some_namespace.a_bool) { |
| 1640 | 1633 | assert(some_namespace.a_bool); |
| 1641 | 1634 | } else { |
| 1642 | 1635 | assert(!some_namespace.a_bool); |
| 1643 | 1636 | } |
| 1644 | 1637 | } |
| 1645 | const some_namespace = switch(@compile_var("os")) { | |
| 1638 | const some_namespace = switch(@compileVar("os")) { | |
| 1646 | 1639 | linux => @import("a.zig"), |
| 1647 | 1640 | else => @import("b.zig"), |
| 1648 | 1641 | }; |
| 1649 | 1642 | |
| 1650 | 1643 | |
| 1651 | 1644 | #attribute("test") |
| 1652 | fn unsigned_64_bit_division() { | |
| 1645 | fn unsigned64BitDivision() { | |
| 1653 | 1646 | const result = div(1152921504606846976, 34359738365); |
| 1654 | 1647 | assert(result.quotient == 33554432); |
| 1655 | 1648 | assert(result.remainder == 100663296); |
| ... | ... | @@ -1667,16 +1660,16 @@ struct DivResult { |
| 1667 | 1660 | } |
| 1668 | 1661 | |
| 1669 | 1662 | #attribute("test") |
| 1670 | fn int_type_builtin() { | |
| 1671 | assert(@int_type(true, 8) == i8); | |
| 1672 | assert(@int_type(true, 16) == i16); | |
| 1673 | assert(@int_type(true, 32) == i32); | |
| 1674 | assert(@int_type(true, 64) == i64); | |
| 1663 | fn intTypeBuiltin() { | |
| 1664 | assert(@intType(true, 8) == i8); | |
| 1665 | assert(@intType(true, 16) == i16); | |
| 1666 | assert(@intType(true, 32) == i32); | |
| 1667 | assert(@intType(true, 64) == i64); | |
| 1675 | 1668 | |
| 1676 | assert(@int_type(false, 8) == u8); | |
| 1677 | assert(@int_type(false, 16) == u16); | |
| 1678 | assert(@int_type(false, 32) == u32); | |
| 1679 | assert(@int_type(false, 64) == u64); | |
| 1669 | assert(@intType(false, 8) == u8); | |
| 1670 | assert(@intType(false, 16) == u16); | |
| 1671 | assert(@intType(false, 32) == u32); | |
| 1672 | assert(@intType(false, 64) == u64); | |
| 1680 | 1673 | |
| 1681 | 1674 | assert(i8.bit_count == 8); |
| 1682 | 1675 | assert(i16.bit_count == 16); |
| ... | ... | @@ -1698,15 +1691,15 @@ fn int_type_builtin() { |
| 1698 | 1691 | } |
| 1699 | 1692 | |
| 1700 | 1693 | #attribute("test") |
| 1701 | fn int_to_enum() { | |
| 1702 | test_int_to_enum_eval(3); | |
| 1703 | test_int_to_enum_noeval(3); | |
| 1694 | fn intToEnum() { | |
| 1695 | testIntToEnumEval(3); | |
| 1696 | testIntToEnumNoeval(3); | |
| 1704 | 1697 | } |
| 1705 | fn test_int_to_enum_eval(x: i32) { | |
| 1698 | fn testIntToEnumEval(x: i32) { | |
| 1706 | 1699 | assert(IntToEnumNumber(x) == IntToEnumNumber.Three); |
| 1707 | 1700 | } |
| 1708 | 1701 | #static_eval_enable(false) |
| 1709 | fn test_int_to_enum_noeval(x: i32) { | |
| 1702 | fn testIntToEnumNoeval(x: i32) { | |
| 1710 | 1703 | assert(IntToEnumNumber(x) == IntToEnumNumber.Three); |
| 1711 | 1704 | } |
| 1712 | 1705 | enum IntToEnumNumber { |