| 1 | enum Hello as u64 { |
| 2 | a |
| 3 | b |
| 4 | c = 20 + 10 |
| 5 | d |
| 6 | e |
| 7 | } |
| 8 | |
| 9 | fn enums() Hello { |
| 10 | mut a := Hello.a |
| 11 | a = .c |
| 12 | return a |
| 13 | } |
| 14 | |
| 15 | struct AA { |
| 16 | a u8 |
| 17 | b i64 |
| 18 | } |
| 19 | |
| 20 | fn of() { |
| 21 | a := __offsetof(AA, b) |
| 22 | b := sizeof(AA) |
| 23 | _, _ := a, b |
| 24 | } |
| 25 | |
| 26 | fn constant() int { |
| 27 | return 100 |
| 28 | } |
| 29 | |
| 30 | const hello = 'hello\n' |
| 31 | |
| 32 | const float = 1.0 |
| 33 | |
| 34 | const integer = 888 |
| 35 | |
| 36 | const runtime_init = constant() |
| 37 | |
| 38 | // Test constants referencing other constants |
| 39 | const base_value = 53 |
| 40 | const ref_const1 = base_value |
| 41 | const ref_const2 = ref_const1 |
| 42 | const ref_const3 = ref_const2 |
| 43 | |
| 44 | struct EE { |
| 45 | a int |
| 46 | b int |
| 47 | } |
| 48 | |
| 49 | fn ptr_arith() { |
| 50 | mut a := EE{} |
| 51 | mut b := &a.b |
| 52 | |
| 53 | unsafe { |
| 54 | *b = 12 |
| 55 | } |
| 56 | println(a.b.str()) |
| 57 | unsafe { |
| 58 | *b = 14 |
| 59 | } |
| 60 | println(a.b.str()) |
| 61 | unsafe { |
| 62 | *b = 102 |
| 63 | } |
| 64 | println((*b).str()) |
| 65 | } |
| 66 | |
| 67 | fn defer_if(cond bool) { |
| 68 | if cond { |
| 69 | defer { |
| 70 | println('defer_if: defer!') |
| 71 | } |
| 72 | } |
| 73 | println('defer_if: start') |
| 74 | } |
| 75 | |
| 76 | fn run_defer() { |
| 77 | defer { |
| 78 | println('defer!') |
| 79 | } |
| 80 | println('before defer') |
| 81 | defer_if(true) |
| 82 | defer_if(false) |
| 83 | } |
| 84 | |
| 85 | struct HasPointers { |
| 86 | a int |
| 87 | msg &string |
| 88 | pub mut: |
| 89 | same &HasPointers |
| 90 | } |
| 91 | |
| 92 | fn ptr_in_struct() { |
| 93 | msg := 'pointer in struct' |
| 94 | mut s := HasPointers{1234, &msg, unsafe { nil }} |
| 95 | println(*s.msg) |
| 96 | s.same = &s |
| 97 | println(*unsafe { &int(voidptr(s.same.a)) }) // FIXME: wont work without casts |
| 98 | } |
| 99 | |
| 100 | fn main() { |
| 101 | println('ptr_in_struct') |
| 102 | ptr_in_struct() |
| 103 | println('ptr_arith') |
| 104 | ptr_arith() |
| 105 | run_defer() |
| 106 | println('constants') |
| 107 | println(runtime_init) |
| 108 | println(hello) |
| 109 | // println(float) |
| 110 | println(integer) |
| 111 | println('const refs') |
| 112 | println(ref_const1) |
| 113 | println(ref_const2) |
| 114 | println(ref_const3) |
| 115 | println('enums') |
| 116 | println(int(enums())) |
| 117 | println(sizeof(Hello)) |
| 118 | } |
| 119 | |