| author | |
| committer | |
| log | 66ca916805efee6b35b8dd6104fb1da50dd1dc8b |
| tree | 7adacd400232e2df30b63a191c56f905bb217941 |
| parent | 4dc2b8250638efda3a9990182f46ad57c403b28b |
2 files changed, 24 insertions(+), 9 deletions(-)
example/expressions/expressions.zig+5-9| ... | ... | @@ -1,10 +1,6 @@ |
| 1 | 1 | export executable "expressions"; |
| 2 | 2 | |
| 3 | #link("c") | |
| 4 | extern { | |
| 5 | fn puts(s: &const u8) -> i32; | |
| 6 | fn exit(code: i32) -> unreachable; | |
| 7 | } | |
| 3 | use "std.zig"; | |
| 8 | 4 | |
| 9 | 5 | fn other_exit() -> unreachable { |
| 10 | 6 | if (true) { exit(0); } |
| ... | ... | @@ -12,21 +8,21 @@ fn other_exit() -> unreachable { |
| 12 | 8 | unreachable; |
| 13 | 9 | } |
| 14 | 10 | |
| 15 | export fn _start() -> unreachable { | |
| 11 | export fn main(argc: isize, argv: &&u8, env: &&u8) -> unreachable { | |
| 16 | 12 | const a : i32 = 1; |
| 17 | 13 | const b = 2 as i32; |
| 18 | 14 | // const c : i32; // not yet support for const variables |
| 19 | 15 | // const d; // parse error |
| 20 | 16 | if (a + b == 3) { |
| 21 | 17 | const no_conflict : i32 = 5; |
| 22 | if (no_conflict == 5) { puts(c"OK 1"); } | |
| 18 | if (no_conflict == 5) { print_str("OK 1\n" as string); } | |
| 23 | 19 | } |
| 24 | 20 | |
| 25 | 21 | const c = { |
| 26 | 22 | const no_conflict : i32 = 10; |
| 27 | 23 | no_conflict |
| 28 | 24 | }; |
| 29 | if (c == 10) { puts(c"OK 2"); } | |
| 25 | if (c == 10) { print_str("OK 2\n" as string); } | |
| 30 | 26 | |
| 31 | 27 | void_fun(1, void, 2); |
| 32 | 28 | |
| ... | ... | @@ -49,7 +45,7 @@ loop_start: |
| 49 | 45 | if i == 3 { |
| 50 | 46 | goto done; |
| 51 | 47 | } |
| 52 | puts(c"loop"); | |
| 48 | print_str("loop\n" as string); | |
| 53 | 49 | i = i + 1; |
| 54 | 50 | goto loop_start; |
| 55 | 51 | done: |
std/std.zig+19| ... | ... | @@ -1,6 +1,20 @@ |
| 1 | 1 | const SYS_write : isize = 1; |
| 2 | const SYS_exit : isize = 60; | |
| 2 | 3 | const stdout_fileno : isize = 1; |
| 3 | 4 | |
| 5 | fn syscall1(number: isize, arg1: isize) -> isize { | |
| 6 | var result : isize; | |
| 7 | asm volatile (" | |
| 8 | mov %[number], %%rax | |
| 9 | mov %[arg1], %%rdi | |
| 10 | syscall | |
| 11 | mov %%rax, %[ret]" | |
| 12 | : [ret] "=m" (result) | |
| 13 | : [number] "r" (number), [arg1] "r" (arg1) | |
| 14 | : "rcx", "r11", "rax", "rdi"); | |
| 15 | return result; | |
| 16 | } | |
| 17 | ||
| 4 | 18 | fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize { |
| 5 | 19 | var result : isize; |
| 6 | 20 | asm volatile (" |
| ... | ... | @@ -20,6 +34,11 @@ pub fn write(fd: isize, buf: &const u8, count: usize) -> isize { |
| 20 | 34 | return syscall3(SYS_write, fd, buf as isize, count as isize); |
| 21 | 35 | } |
| 22 | 36 | |
| 37 | pub fn exit(status: i32) -> unreachable { | |
| 38 | syscall1(SYS_exit, status as isize); | |
| 39 | unreachable; | |
| 40 | } | |
| 41 | ||
| 23 | 42 | // TODO error handling |
| 24 | 43 | // TODO handle buffering and flushing |
| 25 | 44 | pub fn print_str(str : string) -> isize { |