diff options
| author | gingerBill <ginger.bill.22@gmail.com> | 2016-08-14 12:22:31 +0100 |
|---|---|---|
| committer | gingerBill <ginger.bill.22@gmail.com> | 2016-08-14 12:22:31 +0100 |
| commit | ee002364120a773753d37cf5575baa6e86d0502c (patch) | |
| tree | d96feebc57be31d385909a0b9e4df2c0aef50a6b /examples/basic.odin | |
| parent | c10b46af9feb76a9839efa292c5288ec4684055e (diff) | |
expr as type; {N}bool is still causing problems
Diffstat (limited to 'examples/basic.odin')
| -rw-r--r-- | examples/basic.odin | 82 |
1 files changed, 67 insertions, 15 deletions
diff --git a/examples/basic.odin b/examples/basic.odin index e0863188a..ea22ca663 100644 --- a/examples/basic.odin +++ b/examples/basic.odin @@ -2,7 +2,7 @@ putchar :: proc(c: i32) -> i32 #foreign print_string :: proc(s: string) { for i := 0; i < len(s); i++ { - putchar(cast(i32)s[i]); + putchar(s[i] as i32); } } @@ -15,15 +15,15 @@ byte_reverse :: proc(b: []byte) { encode_rune :: proc(r : rune) -> ([4]byte, int) { buf : [4]byte; - i := cast(u32)r; + i := r as u32; mask : byte : 0x3f; if i <= 1<<7-1 { - buf[0] = cast(byte)r; + buf[0] = r as byte; return buf, 1; } if i <= 1<<11-1 { - buf[0] = 0xc0 | cast(byte)(r>>6); - buf[1] = 0x80 | cast(byte)(r)&mask; + buf[0] = (0xc0 | r>>6) as byte; + buf[1] = (0x80 | r) as byte & mask; return buf, 2; } @@ -34,22 +34,22 @@ encode_rune :: proc(r : rune) -> ([4]byte, int) { } if i <= 1<<16-1 { - buf[0] = 0xe0 | cast(byte)(r>>12); - buf[1] = 0x80 | cast(byte)(r>>6)&mask; - buf[2] = 0x80 | cast(byte)(r)&mask; + buf[0] = (0xe0 | r>>12) as byte ; + buf[1] = (0x80 | r>>6) as byte & mask; + buf[2] = (0x80 | r) as byte & mask; return buf, 3; } - buf[0] = 0xf0 | cast(byte)(r>>18); - buf[1] = 0x80 | cast(byte)(r>>12)&mask; - buf[2] = 0x80 | cast(byte)(r>>6)&mask; - buf[3] = 0x80 | cast(byte)(r)&mask; + buf[0] = (0xf0 | r>>18) as byte; + buf[1] = (0x80 | r>>12) as byte & mask; + buf[2] = (0x80 | r>>6) as byte & mask; + buf[3] = (0x80 | r) as byte & mask; return buf, 4; } print_rune :: proc(r : rune) { buf, n := encode_rune(r); - str := cast(string)buf[:n]; + str := buf[:n] as string; print_string(str); } @@ -59,7 +59,7 @@ print_int :: proc(i : int) { print_int_base :: proc(i, base : int) { NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"; - buf: [21]byte; + buf: [65]byte; len := 0; negative := false; if i < 0 { @@ -82,5 +82,57 @@ print_int_base :: proc(i, base : int) { } byte_reverse(buf[:len]); - print_string(cast(string)buf[:len]); + print_string(buf[:len] as string); } + +print_uint :: proc(i : uint) { + print_uint_base(i, 10); +} +print_uint_base :: proc(i, base : uint) { + NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"; + + buf: [65]byte; + len := 0; + negative := false; + if i < 0 { + negative = true; + i = -i; + } + if i == 0 { + buf[len] = '0'; + len++; + } + for i > 0 { + buf[len] = NUM_TO_CHAR_TABLE[i % base]; + len++; + i /= base; + } + + if negative { + buf[len] = '-'; + len++; + } + + byte_reverse(buf[:len]); + print_string(buf[:len] as string); +} + + +// f64 + + +print_f64 :: proc(f : f64) { + buf: [128]byte; + + if f == 0 { + value : u64; + + } else { + if f < 0 { + print_rune('-'); + } + print_rune('0'); + } + +} + |