| 1 | import time |
| 2 | // 1 line comment // 1 line comment |
| 3 | |
| 4 | /* |
| 5 | multi line comment (1) |
| 6 | multi line comment (2) |
| 7 | multi line comment (3) |
| 8 | */ |
| 9 | /* |
| 10 | multi line comment (1) |
| 11 | /* |
| 12 | nested comment |
| 13 | */ |
| 14 | /*nested comment*/ |
| 15 | /*nested comment |
| 16 | */ |
| 17 | /* nested comment */ |
| 18 | /* /* nested comment */ */ |
| 19 | multi line comment (2) |
| 20 | */ |
| 21 | type MyFn1 = fn (int) string |
| 22 | |
| 23 | type MyFn2 = fn (a int, b int) int |
| 24 | |
| 25 | type MyFn3 = fn (int, int) |
| 26 | |
| 27 | fn myfn4(string) |
| 28 | |
| 29 | fn foobar() |
| 30 | |
| 31 | fn slopediv(num u32, den u32) int |
| 32 | |
| 33 | type F1 = fn () |
| 34 | |
| 35 | type F2 = fn (voidptr) |
| 36 | |
| 37 | type F3 = fn (voidptr, voidptr) |
| 38 | |
| 39 | type F4 = fn (voidptr) int |
| 40 | |
| 41 | type F5 = fn (int, int) int |
| 42 | |
| 43 | type F6 = fn (int, int) |
| 44 | |
| 45 | type F7 = fn (time.Time, int) |
| 46 | |
| 47 | type MyTime = time.Time |
| 48 | type F8 = fn (MyTime) |
| 49 | |
| 50 | interface MyInterface {} |
| 51 | |
| 52 | type F9 = fn (MyInterface) |
| 53 | |
| 54 | fn C.atoi(const_s &char) i32 |
| 55 | fn C.freec(ptr voidptr) |
| 56 | |
| 57 | @[trusted] |
| 58 | fn C.exitc(code i32) |
| 59 | |
| 60 | // above checks attribute doesn't conflict with `freec` return type |
| 61 | |
| 62 | fn foo() { |
| 63 | } |
| 64 | |
| 65 | type ActionfV = fn () |
| 66 | |
| 67 | type ActionfP1 = fn (voidptr) |
| 68 | |
| 69 | type ActionfP2 = fn (voidptr, voidptr) |
| 70 | |
| 71 | // TODO |
| 72 | fn modify_array(mut a []int) { |
| 73 | a[0] = 10 |
| 74 | for i in 0 .. a.len { |
| 75 | a[i] = a[i] * 2 |
| 76 | } |
| 77 | // a << 888 |
| 78 | } |
| 79 | |
| 80 | fn modify_array2(mut a []int) { |
| 81 | a[0] = 10 |
| 82 | for i in 0 .. a.len { |
| 83 | a[i] = a[i] * 2 |
| 84 | } |
| 85 | // a << 888 |
| 86 | } |
| 87 | |
| 88 | fn test_mut_array() { |
| 89 | mut nums := [1, 2, 3] |
| 90 | modify_array(mut nums) |
| 91 | // assert nums.len == 4 |
| 92 | // println(nums) |
| 93 | assert nums[0] == 20 |
| 94 | assert nums[1] == 4 |
| 95 | assert nums[2] == 6 |
| 96 | // assert nums[3] == 888 |
| 97 | // workaround for // [91, 32, -33686272] windows bug |
| 98 | println(nums.clone()) |
| 99 | } |
| 100 | |
| 101 | fn mod_struct(mut user User) { |
| 102 | user.age++ |
| 103 | } |
| 104 | |
| 105 | struct User { |
| 106 | mut: |
| 107 | age int |
| 108 | } |
| 109 | |
| 110 | fn test_mut_struct() { |
| 111 | mut user := User{18} |
| 112 | mod_struct(mut user) |
| 113 | assert user.age == 19 |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | fn mod_ptr(mut buf &byte) { |
| 118 | buf[0] = 77 |
| 119 | } |
| 120 | |
| 121 | fn test_mut_ptr() { |
| 122 | buf := malloc(10) |
| 123 | mod_ptr(mut buf) |
| 124 | assert buf[0] == 77 |
| 125 | } |
| 126 | */ |
| 127 | |
| 128 | fn assert_in_bool_fn(v int) bool { |
| 129 | assert v < 3 |
| 130 | return true |
| 131 | } |
| 132 | |
| 133 | fn test_assert_in_bool_fn() { |
| 134 | assert_in_bool_fn(2) |
| 135 | } |
| 136 | |
| 137 | type MyFn = fn (int) int |
| 138 | |
| 139 | fn test(n int) int { |
| 140 | return n + 1000 |
| 141 | } |
| 142 | |
| 143 | struct MySt { |
| 144 | f MyFn |
| 145 | } |
| 146 | |
| 147 | fn test_fn_type_call() { |
| 148 | mut arr := []MyFn{} |
| 149 | arr << MyFn(test) |
| 150 | // TODO: `arr[0](10)` |
| 151 | // assert arr[0](10) == 1010 |
| 152 | x1 := arr[0] |
| 153 | x2 := x1(10) |
| 154 | assert x2 == 1010 |
| 155 | st := MySt{ |
| 156 | f: test |
| 157 | } |
| 158 | assert st.f(10) == 1010 |
| 159 | st1 := &MySt{ |
| 160 | f: test |
| 161 | } |
| 162 | assert st1.f(10) == 1010 |
| 163 | } |
| 164 | |
| 165 | fn ff() fn () int { |
| 166 | return fn () int { |
| 167 | return 22 |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | fn test_fn_return_fn() { |
| 172 | f := ff() |
| 173 | assert f() == 22 |
| 174 | } |
| 175 | |
| 176 | // Test new static methods |
| 177 | |
| 178 | struct Foo { |
| 179 | x int |
| 180 | } |
| 181 | |
| 182 | struct Foo2 { |
| 183 | x int |
| 184 | } |
| 185 | |
| 186 | fn (f Foo) normal_method() { |
| 187 | } |
| 188 | |
| 189 | fn Foo.static_method() int { |
| 190 | return 7 |
| 191 | } |
| 192 | |
| 193 | fn Foo2.static_method() int { |
| 194 | return 8 |
| 195 | } |
| 196 | |
| 197 | fn test_static_method() { |
| 198 | x := Foo.static_method() |
| 199 | assert x == 7 |
| 200 | x2 := Foo2.static_method() |
| 201 | assert x2 == 8 |
| 202 | } |
| 203 | |