expr.ha (5452B)
1 // License: MPL-2.0 2 // (c) 2021 Alexey Yerin <yyp@disroot.org> 3 // (c) 2021 Bor Grošelj Simić <bor.groseljsimic@telemach.net> 4 // (c) 2021 Drew DeVault <sir@cmpwn.com> 5 // (c) 2021 Ember Sawady <ecs@d2evs.net> 6 // (c) 2022 Sebastian <sebastian@sebsite.pw> 7 8 @test fn assignment() void = { 9 roundtrip("export fn main() void = { 10 x = y; 11 *x = *y + 10; 12 *x = *foo(); 13 *y() = bar(); 14 x += 10; 15 x -= 10; 16 x *= 10; 17 x /= 10; 18 x %= 10; 19 x &= 10; 20 x |= 10; 21 x ^= 10; 22 x >>= 10; 23 x <<= 10; 24 x &&= true; 25 x ||= true; 26 x ^^= true; 27 }; 28 "); 29 }; 30 31 @test fn binarithm() void = { 32 roundtrip("export fn main() void = *void + void * void / void;\n"); 33 }; 34 35 @test fn binding() void = { 36 roundtrip("export fn main() void = { 37 let x: int = 1337, y = 7331; 38 const z: int = 42, q: int = 24; 39 const (foo, bar): (int, bool) = (42, true); 40 const (foo, _, bar): (int, uint, bool) = (42, 12u, true); 41 static let p: int = 62893, o = 39826; 42 static const w: int = 62893, t = 39826; 43 }; 44 "); 45 }; 46 47 @test fn builtin() void = { 48 roundtrip(`export fn main() void = { 49 alloc(1234); 50 alloc(1234...); 51 alloc(4321, 1234); 52 append(x, 10); 53 append(x, 10...); 54 append(x, 10, 20, 30); 55 append(x, y, z, q...); 56 static append(x, 10); 57 abort(); 58 abort("surprize"); 59 static abort(); 60 static abort("surprize"); 61 assert(x == 12); 62 assert(x == 12, "number mismatch"); 63 static assert(x == 12); 64 static assert(x == 12, "number mismatch"); 65 delete(x[10]); 66 delete(x[10..20]); 67 delete(x[..]); 68 delete(x.y.z[..]); 69 static delete(x[10]); 70 free(x); 71 insert(x[0], foo); 72 insert(x[0], foo...); 73 insert(x[0], foo, bar...); 74 static insert(x[0], foo); 75 len([1, 2, 3, 4]); 76 offset(foo.bar); 77 size(u32); 78 vastart(); 79 vaarg(va); 80 vaend(va); 81 }; 82 `); 83 }; 84 85 @test fn call() void = { 86 roundtrip("export fn main() void = test();\n\n" 87 "export fn main() void = test(void, void, void);\n\n" 88 "export fn main() void = test(void, void, void...);\n\n" 89 "export fn main() void = test()()(void);\n"); 90 }; 91 92 @test fn cast() void = { 93 roundtrip("export fn main() void = void: int;\n\n" 94 "export fn main() void = void as int;\n\n" 95 "export fn main() void = void is int;\n\n" 96 "export fn main() void = void: int: uint: u16: u8;\n\n" 97 "export fn main() void = void: int as uint: u16 is u8;\n\n" 98 "export fn main() void = {\n\tyield void;\n}: int;\n"); 99 }; 100 101 @test fn constant() void = { 102 roundtrip(`export fn main() void = { 103 2 + (-4 + void) * true / ("hello" << '?'); 104 [1, 2, 3, 4]; 105 [1, 2, 3, 4...]; 106 (1, 2, 3); 107 struct { 108 x: int = 10, 109 y: int = 20, 110 }; 111 coords { 112 x: int = 10, 113 y: int = 20, 114 ... 115 }; 116 coords { 117 x = 10, 118 y = 20, 119 }; 120 struct { 121 x: int = 10, 122 struct { 123 y: int = 20, 124 }, 125 }; 126 struct { 127 x: int = 10, 128 coords { 129 y: int = 20, 130 }, 131 }; 132 struct { 133 x: int = 10, 134 namespace::coords { 135 y: int = 20, 136 }, 137 }; 138 coords { 139 ... 140 }; 141 13.37; 142 13.37f32; 143 13.37f64; 144 6.022e23; 145 1.616255e-35; 146 1337z; 147 1337u; 148 1337i8; 149 1337u8; 150 1337i16; 151 1337u16; 152 1337i32; 153 1337u32; 154 1337i64; 155 1337u64; 156 "backslashes\\and \"double quotes\""; 157 '\''; 158 '\\'; 159 }; 160 `); 161 }; 162 163 @test fn control() void = { 164 roundtrip("export fn main() void = { 165 break; 166 break :foo; 167 continue; 168 continue :foo; 169 return; 170 return 2 + 2; 171 }; 172 "); 173 }; 174 175 @test fn defer_expr() void = { 176 roundtrip("export fn main() void = defer foo();\n"); 177 }; 178 179 @test fn for_expr() void = { 180 roundtrip("export fn main() void = { 181 for (true) { 182 x; 183 }; 184 for (true) :label { 185 x; 186 }; 187 for (let x = 0; x < 10) { 188 x; 189 }; 190 for (x < 10; x) { 191 x; 192 }; 193 for (let x = 10; x < 10; x) { 194 x; 195 }; 196 for (static let x = 0; x < 10) { 197 x; 198 }; 199 }; 200 "); 201 }; 202 203 @test fn if_expr() void = { 204 roundtrip("export fn main() void = { 205 if (x == y) { 206 z; 207 }; 208 if (y == x) z; 209 if (z == q) r else p; 210 if (a == b) c else if (d == e) f else g; 211 }; 212 "); 213 }; 214 215 @test fn list() void = { 216 roundtrip("export fn main() void = { 217 2 + 2; 218 call(); 219 }; 220 "); 221 }; 222 223 @test fn postfix() void = { 224 roundtrip("export fn main() void = x.y;\n\n" 225 "export fn main() void = x.y.z.q;\n\n" 226 "export fn main() void = x().y;\n\n" 227 "export fn main() void = x.42;\n\n" 228 "export fn main() void = x().y.0.q;\n\n" 229 "export fn main() void = x?;\n\n" 230 "export fn main() void = x!;\n\n" 231 "export fn main() void = x()?.y;\n\n" 232 "export fn main() void = x[10];\n\n" 233 "export fn main() void = x[10 + 10][20];\n"); 234 }; 235 236 @test fn slice() void = { 237 roundtrip("export fn main() void = x[..];\n\n" 238 "export fn main() void = x[123..];\n\n" 239 "export fn main() void = x[123..321];\n\n" 240 "export fn main() void = x[..321];\n"); 241 }; 242 243 @test fn switch_expr() void = { 244 roundtrip("export fn main() void = { 245 switch (x) { 246 case 1234, 4321 => 247 return y; 248 case 1337 => 249 return z; 250 case => 251 return q; 252 }; 253 }; 254 "); 255 }; 256 257 @test fn match_expr() void = { 258 roundtrip("export fn main() void = { 259 match (x) { 260 case let i: size => 261 return y; 262 case foo => 263 return bar; 264 case let foo: int => 265 return bar; 266 case foo::bar => 267 return baz; 268 case null => void; 269 case *int => void; 270 }; 271 match (x) { 272 case let s: matchdata => 273 return y; 274 case str => 275 return z; 276 case => 277 return q; 278 }; 279 }; 280 "); 281 }; 282 283 @test fn unarithm() void = { 284 roundtrip("export fn main() void = +void;\n\n" 285 "export fn main() void = -void;\n\n" 286 "export fn main() void = *void;\n\n" 287 "export fn main() void = ~void;\n\n" 288 "export fn main() void = !void;\n\n" 289 "export fn main() void = &void;\n"); 290 }; 291 292 @test fn yield_expr() void = { 293 roundtrip("export fn main() void = yield;\n\n" 294 "export fn main() void = yield void;\n\n" 295 "export fn main() void = yield :foo;\n\n" 296 "export fn main() void = yield :foo, void;\n"); 297 };