| author | |
| committer | |
| log | f5a32818773835bdeaa8f0db1d036a1506c69982 |
| tree | cec14b3ed641e3916613e5ff24f9db7943177c18 |
| parent | f2a9b40231f982b10c4d098c60b47da890396c1f |
9 files changed, 151 insertions(+), 114 deletions(-)
README.md+2-2| ... | ... | @@ -61,7 +61,7 @@ compromises backward compatibility. |
| 61 | 61 | ``` |
| 62 | 62 | mkdir build |
| 63 | 63 | cd build |
| 64 | cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd) | |
| 64 | cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd) -DZIG_LIBC_DIR=path/to/libc/dir | |
| 65 | 65 | make |
| 66 | 66 | make install |
| 67 | 67 | ./run_tests |
| ... | ... | @@ -72,7 +72,7 @@ make install |
| 72 | 72 | ``` |
| 73 | 73 | mkdir build |
| 74 | 74 | cd build |
| 75 | cmake .. -DCMAKE_BUILD_TYPE=Release | |
| 75 | cmake .. -DCMAKE_BUILD_TYPE=Release -DZIG_LIBC_DIR=path/to/libc/dir | |
| 76 | 76 | make |
| 77 | 77 | sudo make install |
| 78 | 78 | ``` |
example/hello_world/hello_libc.zig+2-2| ... | ... | @@ -6,7 +6,7 @@ extern { |
| 6 | 6 | fn exit(__status: i32) -> unreachable; |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | export fn _start() -> unreachable { | |
| 9 | export fn main(argc: i32, argv: &&u8, env: &&u8) -> i32 { | |
| 10 | 10 | printf(c"Hello, world!\n"); |
| 11 | exit(0); | |
| 11 | return 0; | |
| 12 | 12 | } |
src/analyze.hpp+3-1| ... | ... | @@ -164,7 +164,9 @@ struct CodeGen { |
| 164 | 164 | unsigned pointer_size_bytes; |
| 165 | 165 | bool is_static; |
| 166 | 166 | bool strip_debug_symbols; |
| 167 | bool insert_bootstrap_code; | |
| 167 | bool have_exported_main; | |
| 168 | bool link_libc; | |
| 169 | Buf *libc_path; | |
| 168 | 170 | CodeGenBuildType build_type; |
| 169 | 171 | LLVMTargetMachineRef target_machine; |
| 170 | 172 | LLVMZigDIFile *dummy_di_file; |
src/codegen.cpp+76-5| ... | ... | @@ -58,6 +58,10 @@ void codegen_set_out_name(CodeGen *g, Buf *out_name) { |
| 58 | 58 | g->root_out_name = out_name; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | void codegen_set_libc_path(CodeGen *g, Buf *libc_path) { | |
| 62 | g->libc_path = libc_path; | |
| 63 | } | |
| 64 | ||
| 61 | 65 | static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node); |
| 62 | 66 | |
| 63 | 67 | |
| ... | ... | @@ -1517,6 +1521,18 @@ static void init(CodeGen *g, Buf *source_path) { |
| 1517 | 1521 | |
| 1518 | 1522 | } |
| 1519 | 1523 | |
| 1524 | static bool directives_contains_link_libc(ZigList<AstNode*> *directives) { | |
| 1525 | for (int i = 0; i < directives->length; i += 1) { | |
| 1526 | AstNode *directive_node = directives->at(i); | |
| 1527 | if (buf_eql_str(&directive_node->data.directive.name, "link") && | |
| 1528 | buf_eql_str(&directive_node->data.directive.param, "c")) | |
| 1529 | { | |
| 1530 | return true; | |
| 1531 | } | |
| 1532 | } | |
| 1533 | return false; | |
| 1534 | } | |
| 1535 | ||
| 1520 | 1536 | static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source_code) { |
| 1521 | 1537 | int err; |
| 1522 | 1538 | Buf *full_path = buf_alloc(); |
| ... | ... | @@ -1613,11 +1629,13 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *src_dirname, Buf *src |
| 1613 | 1629 | assert(proto_node->type == NodeTypeFnProto); |
| 1614 | 1630 | Buf *proto_name = &proto_node->data.fn_proto.name; |
| 1615 | 1631 | |
| 1616 | bool is_exported = (proto_node->data.fn_proto.visib_mod != FnProtoVisibModPrivate); | |
| 1632 | bool is_private = (proto_node->data.fn_proto.visib_mod == FnProtoVisibModPrivate); | |
| 1617 | 1633 | |
| 1618 | if (buf_eql_str(proto_name, "main") && is_exported) { | |
| 1619 | g->insert_bootstrap_code = true; | |
| 1634 | if (buf_eql_str(proto_name, "main") && !is_private) { | |
| 1635 | g->have_exported_main = true; | |
| 1620 | 1636 | } |
| 1637 | } else if (top_level_decl->type == NodeTypeExternBlock) { | |
| 1638 | g->link_libc = directives_contains_link_libc(top_level_decl->data.extern_block.directives); | |
| 1621 | 1639 | } |
| 1622 | 1640 | } |
| 1623 | 1641 | |
| ... | ... | @@ -1633,7 +1651,7 @@ void codegen_add_root_code(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *sou |
| 1633 | 1651 | |
| 1634 | 1652 | g->root_import = codegen_add_code(g, src_dir, src_basename, source_code); |
| 1635 | 1653 | |
| 1636 | if (g->insert_bootstrap_code) { | |
| 1654 | if (g->have_exported_main && !g->link_libc && g->out_type != OutTypeLib) { | |
| 1637 | 1655 | Buf *bootstrap_dir = buf_create_from_str(ZIG_STD_DIR); |
| 1638 | 1656 | Buf *bootstrap_basename = buf_create_from_str("bootstrap.zig"); |
| 1639 | 1657 | Buf path_to_bootstrap_src = BUF_INIT; |
| ... | ... | @@ -1788,6 +1806,22 @@ static void generate_h_file(CodeGen *g) { |
| 1788 | 1806 | zig_panic("unable to close h file: %s", strerror(errno)); |
| 1789 | 1807 | } |
| 1790 | 1808 | |
| 1809 | static void find_libc_path(CodeGen *g) { | |
| 1810 | if (g->libc_path && buf_len(g->libc_path)) | |
| 1811 | return; | |
| 1812 | g->libc_path = buf_create_from_str(ZIG_LIBC_DIR); | |
| 1813 | if (g->libc_path && buf_len(g->libc_path)) | |
| 1814 | return; | |
| 1815 | fprintf(stderr, "Unable to determine libc path. Consider using `--libc-path [path]`\n"); | |
| 1816 | exit(1); | |
| 1817 | } | |
| 1818 | ||
| 1819 | static const char *get_libc_file(CodeGen *g, const char *file) { | |
| 1820 | Buf *out_buf = buf_alloc(); | |
| 1821 | os_path_join(g->libc_path, buf_create_from_str(file), out_buf); | |
| 1822 | return buf_ptr(out_buf); | |
| 1823 | } | |
| 1824 | ||
| 1791 | 1825 | void codegen_link(CodeGen *g, const char *out_file) { |
| 1792 | 1826 | bool is_optimized = (g->build_type == CodeGenBuildTypeRelease); |
| 1793 | 1827 | if (is_optimized) { |
| ... | ... | @@ -1826,6 +1860,9 @@ void codegen_link(CodeGen *g, const char *out_file) { |
| 1826 | 1860 | } |
| 1827 | 1861 | |
| 1828 | 1862 | if (g->out_type == OutTypeObj) { |
| 1863 | if (g->verbose) { | |
| 1864 | fprintf(stderr, "OK\n"); | |
| 1865 | } | |
| 1829 | 1866 | return; |
| 1830 | 1867 | } |
| 1831 | 1868 | |
| ... | ... | @@ -1840,8 +1877,12 @@ void codegen_link(CodeGen *g, const char *out_file) { |
| 1840 | 1877 | |
| 1841 | 1878 | // invoke `ld` |
| 1842 | 1879 | ZigList<const char *> args = {0}; |
| 1880 | const char *crt1o; | |
| 1843 | 1881 | if (g->is_static) { |
| 1844 | 1882 | args.append("-static"); |
| 1883 | crt1o = "crt1.o"; | |
| 1884 | } else { | |
| 1885 | crt1o = "Scrt1.o"; | |
| 1845 | 1886 | } |
| 1846 | 1887 | |
| 1847 | 1888 | char *ZIG_NATIVE_DYNAMIC_LINKER = getenv("ZIG_NATIVE_DYNAMIC_LINKER"); |
| ... | ... | @@ -1868,8 +1909,21 @@ void codegen_link(CodeGen *g, const char *out_file) { |
| 1868 | 1909 | args.append("-o"); |
| 1869 | 1910 | args.append(out_file); |
| 1870 | 1911 | |
| 1912 | bool link_in_crt = (g->link_libc && g->out_type == OutTypeExe); | |
| 1913 | ||
| 1914 | if (link_in_crt) { | |
| 1915 | find_libc_path(g); | |
| 1916 | ||
| 1917 | args.append(get_libc_file(g, crt1o)); | |
| 1918 | args.append(get_libc_file(g, "crti.o")); | |
| 1919 | } | |
| 1920 | ||
| 1871 | 1921 | args.append((const char *)buf_ptr(&out_file_o)); |
| 1872 | 1922 | |
| 1923 | if (link_in_crt) { | |
| 1924 | args.append(get_libc_file(g, "crtn.o")); | |
| 1925 | } | |
| 1926 | ||
| 1873 | 1927 | auto it = g->link_table.entry_iterator(); |
| 1874 | 1928 | for (;;) { |
| 1875 | 1929 | auto *entry = it.next(); |
| ... | ... | @@ -1880,7 +1934,24 @@ void codegen_link(CodeGen *g, const char *out_file) { |
| 1880 | 1934 | args.append(buf_ptr(arg)); |
| 1881 | 1935 | } |
| 1882 | 1936 | |
| 1883 | os_spawn_process("ld", args, false); | |
| 1937 | if (g->verbose) { | |
| 1938 | fprintf(stderr, "ld"); | |
| 1939 | for (int i = 0; i < args.length; i += 1) { | |
| 1940 | fprintf(stderr, " %s", args.at(i)); | |
| 1941 | } | |
| 1942 | fprintf(stderr, "\n"); | |
| 1943 | } | |
| 1944 | ||
| 1945 | int return_code; | |
| 1946 | Buf ld_stderr = BUF_INIT; | |
| 1947 | Buf ld_stdout = BUF_INIT; | |
| 1948 | os_exec_process("ld", args, &return_code, &ld_stderr, &ld_stdout); | |
| 1949 | ||
| 1950 | if (return_code != 0) { | |
| 1951 | fprintf(stderr, "ld failed with return code %d\n", return_code); | |
| 1952 | fprintf(stderr, "%s\n", buf_ptr(&ld_stderr)); | |
| 1953 | exit(1); | |
| 1954 | } | |
| 1884 | 1955 | |
| 1885 | 1956 | if (g->out_type == OutTypeLib) { |
| 1886 | 1957 | generate_h_file(g); |
src/codegen.hpp+1| ... | ... | @@ -33,6 +33,7 @@ void codegen_set_verbose(CodeGen *codegen, bool verbose); |
| 33 | 33 | void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color); |
| 34 | 34 | void codegen_set_out_type(CodeGen *codegen, OutType out_type); |
| 35 | 35 | void codegen_set_out_name(CodeGen *codegen, Buf *out_name); |
| 36 | void codegen_set_libc_path(CodeGen *codegen, Buf *libc_path); | |
| 36 | 37 | |
| 37 | 38 | void codegen_add_root_code(CodeGen *g, Buf *source_dir, Buf *source_basename, Buf *source_code); |
| 38 | 39 |
src/config.h.in+1| ... | ... | @@ -8,5 +8,6 @@ |
| 8 | 8 | |
| 9 | 9 | #define ZIG_HEADERS_DIR "@CMAKE_INSTALL_PREFIX@/@C_HEADERS_DEST@" |
| 10 | 10 | #define ZIG_STD_DIR "@CMAKE_INSTALL_PREFIX@/@ZIG_STD_DEST@" |
| 11 | #define ZIG_LIBC_DIR "@ZIG_LIBC_DIR@" | |
| 11 | 12 | |
| 12 | 13 | #endif |
src/main.cpp+6| ... | ... | @@ -29,6 +29,7 @@ static int usage(const char *arg0) { |
| 29 | 29 | " --output [file] override destination path\n" |
| 30 | 30 | " --verbose turn on compiler debug output\n" |
| 31 | 31 | " --color [auto|off|on] enable or disable colored error messages\n" |
| 32 | " --libc-path [path] set the C compiler data path\n" | |
| 32 | 33 | "Command: parseh target\n" |
| 33 | 34 | " -isystem [dir] add additional search path for other .h files\n" |
| 34 | 35 | " -dirafter [dir] same as -isystem but do it last\n" |
| ... | ... | @@ -52,6 +53,7 @@ struct Build { |
| 52 | 53 | const char *out_name; |
| 53 | 54 | bool verbose; |
| 54 | 55 | ErrColor color; |
| 56 | const char *libc_path; | |
| 55 | 57 | }; |
| 56 | 58 | |
| 57 | 59 | static int build(const char *arg0, int argc, char **argv) { |
| ... | ... | @@ -99,6 +101,8 @@ static int build(const char *arg0, int argc, char **argv) { |
| 99 | 101 | } |
| 100 | 102 | } else if (strcmp(arg, "--name") == 0) { |
| 101 | 103 | b.out_name = argv[i]; |
| 104 | } else if (strcmp(arg, "--libc-path") == 0) { | |
| 105 | b.libc_path = argv[i]; | |
| 102 | 106 | } else { |
| 103 | 107 | return usage(arg0); |
| 104 | 108 | } |
| ... | ... | @@ -142,6 +146,8 @@ static int build(const char *arg0, int argc, char **argv) { |
| 142 | 146 | codegen_set_out_type(g, b.out_type); |
| 143 | 147 | if (b.out_name) |
| 144 | 148 | codegen_set_out_name(g, buf_create_from_str(b.out_name)); |
| 149 | if (b.libc_path) | |
| 150 | codegen_set_libc_path(g, buf_create_from_str(b.libc_path)); | |
| 145 | 151 | codegen_set_verbose(g, b.verbose); |
| 146 | 152 | codegen_set_errmsg_color(g, b.color); |
| 147 | 153 | codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code); |
src/os.cpp+3-1| ... | ... | @@ -77,7 +77,9 @@ void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { |
| 77 | 77 | |
| 78 | 78 | void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) { |
| 79 | 79 | buf_init_from_buf(out_full_path, dirname); |
| 80 | buf_append_char(out_full_path, '/'); | |
| 80 | uint8_t c = *(buf_ptr(out_full_path) + buf_len(out_full_path) - 1); | |
| 81 | if (c != '/') | |
| 82 | buf_append_char(out_full_path, '/'); | |
| 81 | 83 | buf_append_buf(out_full_path, basename); |
| 82 | 84 | } |
| 83 | 85 |
test/run_tests.cpp+57-103| ... | ... | @@ -100,43 +100,34 @@ static void add_compiling_test_cases(void) { |
| 100 | 100 | #link("c") |
| 101 | 101 | extern { |
| 102 | 102 | fn puts(s: &const u8) -> i32; |
| 103 | fn exit(code: i32) -> unreachable; | |
| 104 | 103 | } |
| 105 | 104 | |
| 106 | export fn _start() -> unreachable { | |
| 105 | export fn main(argc: i32, argv: &&u8, env: &&u8) -> i32 { | |
| 107 | 106 | puts(c"Hello, world!"); |
| 108 | exit(0); | |
| 107 | return 0; | |
| 109 | 108 | } |
| 110 | 109 | )SOURCE", "Hello, world!\n"); |
| 111 | 110 | |
| 112 | 111 | add_simple_case("function call", R"SOURCE( |
| 113 | #link("c") | |
| 114 | extern { | |
| 115 | fn puts(s: &const u8) -> i32; | |
| 116 | fn exit(code: i32) -> unreachable; | |
| 117 | } | |
| 112 | use "std.zig"; | |
| 118 | 113 | |
| 119 | 114 | fn empty_function_1() {} |
| 120 | 115 | fn empty_function_2() { return; } |
| 121 | 116 | |
| 122 | export fn _start() -> unreachable { | |
| 117 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 123 | 118 | empty_function_1(); |
| 124 | 119 | empty_function_2(); |
| 125 | 120 | this_is_a_function(); |
| 126 | 121 | } |
| 127 | 122 | |
| 128 | 123 | fn this_is_a_function() -> unreachable { |
| 129 | puts(c"OK"); | |
| 124 | print_str("OK\n" as string); | |
| 130 | 125 | exit(0); |
| 131 | 126 | } |
| 132 | 127 | )SOURCE", "OK\n"); |
| 133 | 128 | |
| 134 | 129 | add_simple_case("comments", R"SOURCE( |
| 135 | #link("c") | |
| 136 | extern { | |
| 137 | fn puts(s: &const u8) -> i32; | |
| 138 | fn exit(code: i32) -> unreachable; | |
| 139 | } | |
| 130 | use "std.zig"; | |
| 140 | 131 | |
| 141 | 132 | /** |
| 142 | 133 | * multi line doc comment |
| ... | ... | @@ -145,9 +136,9 @@ static void add_compiling_test_cases(void) { |
| 145 | 136 | |
| 146 | 137 | /// this is a documentation comment |
| 147 | 138 | /// doc comment line 2 |
| 148 | export fn _start() -> unreachable { | |
| 149 | puts(/* mid-line comment /* nested */ */ c"OK"); | |
| 150 | exit(0); | |
| 139 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 140 | print_str(/* mid-line comment /* nested */ */ "OK\n" as string); | |
| 141 | return 0; | |
| 151 | 142 | } |
| 152 | 143 | )SOURCE", "OK\n"); |
| 153 | 144 | |
| ... | ... | @@ -156,7 +147,7 @@ static void add_compiling_test_cases(void) { |
| 156 | 147 | use "libc.zig"; |
| 157 | 148 | use "foo.zig"; |
| 158 | 149 | |
| 159 | export fn _start() -> unreachable { | |
| 150 | export fn main(argc: i32, argv: &&u8, env: &&u8) -> i32 { | |
| 160 | 151 | private_function(); |
| 161 | 152 | } |
| 162 | 153 | |
| ... | ... | @@ -190,180 +181,144 @@ static void add_compiling_test_cases(void) { |
| 190 | 181 | } |
| 191 | 182 | |
| 192 | 183 | add_simple_case("if statements", R"SOURCE( |
| 193 | #link("c") | |
| 194 | extern { | |
| 195 | fn puts(s: &const u8) -> i32; | |
| 196 | fn exit(code: i32) -> unreachable; | |
| 197 | } | |
| 184 | use "std.zig"; | |
| 198 | 185 | |
| 199 | export fn _start() -> unreachable { | |
| 186 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 200 | 187 | if 1 != 0 { |
| 201 | puts(c"1 is true"); | |
| 188 | print_str("1 is true\n" as string); | |
| 202 | 189 | } else { |
| 203 | puts(c"1 is false"); | |
| 190 | print_str("1 is false\n" as string); | |
| 204 | 191 | } |
| 205 | 192 | if 0 != 0 { |
| 206 | puts(c"0 is true"); | |
| 193 | print_str("0 is true\n" as string); | |
| 207 | 194 | } else if 1 - 1 != 0 { |
| 208 | puts(c"1 - 1 is true"); | |
| 195 | print_str("1 - 1 is true\n" as string); | |
| 209 | 196 | } |
| 210 | 197 | if !(0 != 0) { |
| 211 | puts(c"!0 is true"); | |
| 198 | print_str("!0 is true\n" as string); | |
| 212 | 199 | } |
| 213 | exit(0); | |
| 200 | return 0; | |
| 214 | 201 | } |
| 215 | 202 | )SOURCE", "1 is true\n!0 is true\n"); |
| 216 | 203 | |
| 217 | 204 | add_simple_case("params", R"SOURCE( |
| 218 | #link("c") | |
| 219 | extern { | |
| 220 | fn puts(s: &const u8) -> i32; | |
| 221 | fn exit(code: i32) -> unreachable; | |
| 222 | } | |
| 205 | use "std.zig"; | |
| 223 | 206 | |
| 224 | 207 | fn add(a: i32, b: i32) -> i32 { |
| 225 | 208 | a + b |
| 226 | 209 | } |
| 227 | 210 | |
| 228 | export fn _start() -> unreachable { | |
| 211 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 229 | 212 | if add(22, 11) == 33 { |
| 230 | puts(c"pass"); | |
| 213 | print_str("pass\n" as string); | |
| 231 | 214 | } |
| 232 | exit(0); | |
| 215 | return 0; | |
| 233 | 216 | } |
| 234 | 217 | )SOURCE", "pass\n"); |
| 235 | 218 | |
| 236 | 219 | add_simple_case("goto", R"SOURCE( |
| 237 | #link("c") | |
| 238 | extern { | |
| 239 | fn puts(s: &const u8) -> i32; | |
| 240 | fn exit(code: i32) -> unreachable; | |
| 241 | } | |
| 220 | use "std.zig"; | |
| 242 | 221 | |
| 243 | 222 | fn loop(a : i32) { |
| 244 | 223 | if a == 0 { |
| 245 | 224 | goto done; |
| 246 | 225 | } |
| 247 | puts(c"loop"); | |
| 226 | print_str("loop\n" as string); | |
| 248 | 227 | loop(a - 1); |
| 249 | 228 | |
| 250 | 229 | done: |
| 251 | 230 | return; |
| 252 | 231 | } |
| 253 | 232 | |
| 254 | export fn _start() -> unreachable { | |
| 233 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 255 | 234 | loop(3); |
| 256 | exit(0); | |
| 235 | return 0; | |
| 257 | 236 | } |
| 258 | 237 | )SOURCE", "loop\nloop\nloop\n"); |
| 259 | 238 | |
| 260 | 239 | add_simple_case("local variables", R"SOURCE( |
| 261 | #link("c") | |
| 262 | extern { | |
| 263 | fn puts(s: &const u8) -> i32; | |
| 264 | fn exit(code: i32) -> unreachable; | |
| 265 | } | |
| 240 | use "std.zig"; | |
| 266 | 241 | |
| 267 | export fn _start() -> unreachable { | |
| 242 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 268 | 243 | const a : i32 = 1; |
| 269 | 244 | const b = 2 as i32; |
| 270 | 245 | if (a + b == 3) { |
| 271 | puts(c"OK"); | |
| 246 | print_str("OK\n" as string); | |
| 272 | 247 | } |
| 273 | exit(0); | |
| 248 | return 0; | |
| 274 | 249 | } |
| 275 | 250 | )SOURCE", "OK\n"); |
| 276 | 251 | |
| 277 | 252 | add_simple_case("bool literals", R"SOURCE( |
| 278 | #link("c") | |
| 279 | extern { | |
| 280 | fn puts(s: &const u8) -> i32; | |
| 281 | fn exit(code: i32) -> unreachable; | |
| 282 | } | |
| 253 | use "std.zig"; | |
| 283 | 254 | |
| 284 | export fn _start() -> unreachable { | |
| 285 | if (true) { puts(c"OK 1"); } | |
| 286 | if (false) { puts(c"BAD 1"); } | |
| 287 | if (!true) { puts(c"BAD 2"); } | |
| 288 | if (!false) { puts(c"OK 2"); } | |
| 289 | exit(0); | |
| 255 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 256 | if (true) { print_str("OK 1\n" as string); } | |
| 257 | if (false) { print_str("BAD 1\n" as string); } | |
| 258 | if (!true) { print_str("BAD 2\n" as string); } | |
| 259 | if (!false) { print_str("OK 2\n" as string); } | |
| 260 | return 0; | |
| 290 | 261 | } |
| 291 | 262 | )SOURCE", "OK 1\nOK 2\n"); |
| 292 | 263 | |
| 293 | 264 | add_simple_case("separate block scopes", R"SOURCE( |
| 294 | #link("c") | |
| 295 | extern { | |
| 296 | fn puts(s: &const u8) -> i32; | |
| 297 | fn exit(code: i32) -> unreachable; | |
| 298 | } | |
| 265 | use "std.zig"; | |
| 299 | 266 | |
| 300 | export fn _start() -> unreachable { | |
| 267 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 301 | 268 | if (true) { |
| 302 | 269 | const no_conflict : i32 = 5; |
| 303 | if (no_conflict == 5) { puts(c"OK 1"); } | |
| 270 | if (no_conflict == 5) { print_str("OK 1\n" as string); } | |
| 304 | 271 | } |
| 305 | 272 | |
| 306 | 273 | const c = { |
| 307 | 274 | const no_conflict = 10 as i32; |
| 308 | 275 | no_conflict |
| 309 | 276 | }; |
| 310 | if (c == 10) { puts(c"OK 2"); } | |
| 311 | exit(0); | |
| 277 | if (c == 10) { print_str("OK 2\n" as string); } | |
| 278 | return 0; | |
| 312 | 279 | } |
| 313 | 280 | )SOURCE", "OK 1\nOK 2\n"); |
| 314 | 281 | |
| 315 | 282 | add_simple_case("void parameters", R"SOURCE( |
| 316 | #link("c") | |
| 317 | extern { | |
| 318 | fn puts(s: &const u8) -> i32; | |
| 319 | fn exit(code: i32) -> unreachable; | |
| 320 | } | |
| 283 | use "std.zig"; | |
| 321 | 284 | |
| 322 | export fn _start() -> unreachable { | |
| 285 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 323 | 286 | void_fun(1, void, 2); |
| 324 | exit(0); | |
| 287 | return 0; | |
| 325 | 288 | } |
| 326 | 289 | |
| 327 | 290 | fn void_fun(a : i32, b : void, c : i32) { |
| 328 | 291 | const v = b; |
| 329 | 292 | const vv : void = if (a == 1) {v} else {}; |
| 330 | if (a + c == 3) { puts(c"OK"); } | |
| 293 | if (a + c == 3) { print_str("OK\n" as string); } | |
| 331 | 294 | return vv; |
| 332 | 295 | } |
| 333 | 296 | )SOURCE", "OK\n"); |
| 334 | 297 | |
| 335 | 298 | add_simple_case("mutable local variables", R"SOURCE( |
| 336 | #link("c") | |
| 337 | extern { | |
| 338 | fn puts(s: &const u8) -> i32; | |
| 339 | fn exit(code: i32) -> unreachable; | |
| 340 | } | |
| 299 | use "std.zig"; | |
| 341 | 300 | |
| 342 | export fn _start() -> unreachable { | |
| 301 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 343 | 302 | var zero : i32; |
| 344 | if (zero == 0) { puts(c"zero"); } | |
| 303 | if (zero == 0) { print_str("zero\n" as string); } | |
| 345 | 304 | |
| 346 | 305 | var i = 0 as i32; |
| 347 | 306 | loop_start: |
| 348 | 307 | if i == 3 { |
| 349 | 308 | goto done; |
| 350 | 309 | } |
| 351 | puts(c"loop"); | |
| 310 | print_str("loop\n" as string); | |
| 352 | 311 | i = i + 1; |
| 353 | 312 | goto loop_start; |
| 354 | 313 | done: |
| 355 | exit(0); | |
| 314 | return 0; | |
| 356 | 315 | } |
| 357 | 316 | )SOURCE", "zero\nloop\nloop\nloop\n"); |
| 358 | 317 | |
| 359 | 318 | add_simple_case("arrays", R"SOURCE( |
| 360 | #link("c") | |
| 361 | extern { | |
| 362 | fn puts(s: &const u8) -> i32; | |
| 363 | fn exit(code: i32) -> unreachable; | |
| 364 | } | |
| 319 | use "std.zig"; | |
| 365 | 320 | |
| 366 | export fn _start() -> unreachable { | |
| 321 | pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 { | |
| 367 | 322 | var array : [i32; 5]; |
| 368 | 323 | |
| 369 | 324 | var i : i32 = 0; |
| ... | ... | @@ -391,10 +346,10 @@ loop_2_start: |
| 391 | 346 | loop_2_end: |
| 392 | 347 | |
| 393 | 348 | if accumulator == 15 { |
| 394 | puts(c"OK"); | |
| 349 | print_str("OK\n" as string); | |
| 395 | 350 | } |
| 396 | 351 | |
| 397 | exit(0); | |
| 352 | return 0; | |
| 398 | 353 | } |
| 399 | 354 | )SOURCE", "OK\n"); |
| 400 | 355 | |
| ... | ... | @@ -481,12 +436,11 @@ export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 { |
| 481 | 436 | #link("c") |
| 482 | 437 | extern { |
| 483 | 438 | fn printf(__format: &const u8, ...) -> i32; |
| 484 | fn exit(__status: i32) -> unreachable; | |
| 485 | 439 | } |
| 486 | 440 | |
| 487 | export fn _start() -> unreachable { | |
| 441 | export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 { | |
| 488 | 442 | printf(c"0=%d\n", 0 as i32); // TODO: more tests |
| 489 | exit(0); | |
| 443 | return 0; | |
| 490 | 444 | } |
| 491 | 445 | )SOURCE", "0=0\n"); |
| 492 | 446 |