zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

blob f2f3ffcc (800B) - Raw


      1 const builtin = @import("builtin");
      2 
      3 extern "c" fn write(usize, usize, usize) usize;
      4 
      5 pub fn main() void {
      6     var i: u32 = 0;
      7     while (i < 4) : (i += 1) print();
      8     assert(i == 4);
      9 }
     10 
     11 fn print() void {
     12     switch (builtin.os.tag) {
     13         .linux => {
     14             asm volatile ("syscall"
     15                 :
     16                 : [number] "{rax}" (1),
     17                   [arg1] "{rdi}" (1),
     18                   [arg2] "{rsi}" (@ptrToInt("hello\n")),
     19                   [arg3] "{rdx}" (6),
     20                 : "rcx", "r11", "memory"
     21             );
     22         },
     23         .macos => {
     24             _ = write(1, @ptrToInt("hello\n"), 6);
     25         },
     26         else => unreachable,
     27     }
     28 }
     29 
     30 pub fn assert(ok: bool) void {
     31     if (!ok) unreachable; // assertion failure
     32 }
     33 
     34 // run
     35 //
     36 // hello
     37 // hello
     38 // hello
     39 // hello
     40 //