| ... | @@ -252,82 +252,6 @@ pub const b_text = a_text; | ... | @@ -252,82 +252,6 @@ pub const b_text = a_text; |
| 252 | )SOURCE"); | 252 | )SOURCE"); |
| 253 | } | 253 | } |
| 254 | | 254 | |
| 255 | add_simple_case("params", R"SOURCE( | | |
| 256 | const io = @import("std").io; | | |
| 257 | | | |
| 258 | fn add(a: i32, b: i32) -> i32 { | | |
| 259 | a + b | | |
| 260 | } | | |
| 261 | | | |
| 262 | pub fn main(args: [][]u8) -> %void { | | |
| 263 | if (add(22, 11) == 33) { | | |
| 264 | %%io.stdout.printf("pass\n"); | | |
| 265 | } | | |
| 266 | } | | |
| 267 | )SOURCE", "pass\n"); | | |
| 268 | | | |
| 269 | add_simple_case("void parameters", R"SOURCE( | | |
| 270 | const io = @import("std").io; | | |
| 271 | | | |
| 272 | pub fn main(args: [][]u8) -> %void { | | |
| 273 | void_fun(1, void{}, 2); | | |
| 274 | } | | |
| 275 | | | |
| 276 | fn void_fun(a : i32, b : void, c : i32) { | | |
| 277 | const v = b; | | |
| 278 | const vv : void = if (a == 1) {v} else {}; | | |
| 279 | if (a + c == 3) { %%io.stdout.printf("OK\n"); } | | |
| 280 | return vv; | | |
| 281 | } | | |
| 282 | )SOURCE", "OK\n"); | | |
| 283 | | | |
| 284 | add_simple_case("mutable local variables", R"SOURCE( | | |
| 285 | const io = @import("std").io; | | |
| 286 | | | |
| 287 | pub fn main(args: [][]u8) -> %void { | | |
| 288 | var zero : i32 = 0; | | |
| 289 | if (zero == 0) { %%io.stdout.printf("zero\n"); } | | |
| 290 | | | |
| 291 | var i = i32(0); | | |
| 292 | while (i != 3) { | | |
| 293 | %%io.stdout.printf("loop\n"); | | |
| 294 | i += 1; | | |
| 295 | } | | |
| 296 | } | | |
| 297 | )SOURCE", "zero\nloop\nloop\nloop\n"); | | |
| 298 | | | |
| 299 | add_simple_case("arrays", R"SOURCE( | | |
| 300 | const io = @import("std").io; | | |
| 301 | | | |
| 302 | pub fn main(args: [][]u8) -> %void { | | |
| 303 | var array : [5]i32 = undefined; | | |
| 304 | | | |
| 305 | var i : i32 = 0; | | |
| 306 | while (i < 5) { | | |
| 307 | array[i] = i + 1; | | |
| 308 | i = array[i]; | | |
| 309 | } | | |
| 310 | | | |
| 311 | i = 0; | | |
| 312 | var accumulator = i32(0); | | |
| 313 | while (i < 5) { | | |
| 314 | accumulator += array[i]; | | |
| 315 | | | |
| 316 | i += 1; | | |
| 317 | } | | |
| 318 | | | |
| 319 | if (accumulator == 15) { | | |
| 320 | %%io.stdout.printf("OK\n"); | | |
| 321 | } | | |
| 322 | | | |
| 323 | if (get_array_len(array) != 5) { | | |
| 324 | %%io.stdout.printf("BAD\n"); | | |
| 325 | } | | |
| 326 | } | | |
| 327 | fn get_array_len(a: []i32) -> isize { | | |
| 328 | a.len | | |
| 329 | } | | |
| 330 | )SOURCE", "OK\n"); | | |
| 331 | | 255 | |
| 332 | | 256 | |
| 333 | add_simple_case("hello world without libc", R"SOURCE( | 257 | add_simple_case("hello world without libc", R"SOURCE( |
| ... | @@ -339,49 +263,6 @@ pub fn main(args: [][]u8) -> %void { | ... | @@ -339,49 +263,6 @@ pub fn main(args: [][]u8) -> %void { |
| 339 | )SOURCE", "Hello, world!\n"); | 263 | )SOURCE", "Hello, world!\n"); |
| 340 | | 264 | |
| 341 | | 265 | |
| 342 | add_simple_case("short circuit", R"SOURCE( | | |
| 343 | const io = @import("std").io; | | |
| 344 | | | |
| 345 | pub fn main(args: [][]u8) -> %void { | | |
| 346 | if (true || { %%io.stdout.printf("BAD 1\n"); false }) { | | |
| 347 | %%io.stdout.printf("OK 1\n"); | | |
| 348 | } | | |
| 349 | if (false || { %%io.stdout.printf("OK 2\n"); false }) { | | |
| 350 | %%io.stdout.printf("BAD 2\n"); | | |
| 351 | } | | |
| 352 | | | |
| 353 | if (true && { %%io.stdout.printf("OK 3\n"); false }) { | | |
| 354 | %%io.stdout.printf("BAD 3\n"); | | |
| 355 | } | | |
| 356 | if (false && { %%io.stdout.printf("BAD 4\n"); false }) { | | |
| 357 | } else { | | |
| 358 | %%io.stdout.printf("OK 4\n"); | | |
| 359 | } | | |
| 360 | } | | |
| 361 | )SOURCE", "OK 1\nOK 2\nOK 3\nOK 4\n"); | | |
| 362 | | | |
| 363 | add_simple_case("modify operators", R"SOURCE( | | |
| 364 | const io = @import("std").io; | | |
| 365 | | | |
| 366 | pub fn main(args: [][]u8) -> %void { | | |
| 367 | var i : i32 = 0; | | |
| 368 | i += 5; if (i != 5) { %%io.stdout.printf("BAD +=\n"); } | | |
| 369 | i -= 2; if (i != 3) { %%io.stdout.printf("BAD -=\n"); } | | |
| 370 | i *= 20; if (i != 60) { %%io.stdout.printf("BAD *=\n"); } | | |
| 371 | i /= 3; if (i != 20) { %%io.stdout.printf("BAD /=\n"); } | | |
| 372 | i %= 11; if (i != 9) { %%io.stdout.printf("BAD %=\n"); } | | |
| 373 | i <<= 1; if (i != 18) { %%io.stdout.printf("BAD <<=\n"); } | | |
| 374 | i >>= 2; if (i != 4) { %%io.stdout.printf("BAD >>=\n"); } | | |
| 375 | i = 6; | | |
| 376 | i &= 5; if (i != 4) { %%io.stdout.printf("BAD &=\n"); } | | |
| 377 | i ^= 6; if (i != 2) { %%io.stdout.printf("BAD ^=\n"); } | | |
| 378 | i = 6; | | |
| 379 | i |= 3; if (i != 7) { %%io.stdout.printf("BAD |=\n"); } | | |
| 380 | | | |
| 381 | %%io.stdout.printf("OK\n"); | | |
| 382 | } | | |
| 383 | )SOURCE", "OK\n"); | | |
| 384 | | | |
| 385 | add_simple_case_libc("number literals", R"SOURCE( | 266 | add_simple_case_libc("number literals", R"SOURCE( |
| 386 | const c = @c_import(@c_include("stdio.h")); | 267 | const c = @c_import(@c_include("stdio.h")); |
| 387 | | 268 | |
| ... | @@ -508,110 +389,7 @@ export fn main(argc: c_int, argv: &&u8) -> c_int { | ... | @@ -508,110 +389,7 @@ export fn main(argc: c_int, argv: &&u8) -> c_int { |
| 508 | 0o10700.00010e0: 0x1.1c0001p+12 | 389 | 0o10700.00010e0: 0x1.1c0001p+12 |
| 509 | )OUTPUT"); | 390 | )OUTPUT"); |
| 510 | | 391 | |
| 511 | add_simple_case("structs", R"SOURCE( | | |
| 512 | const io = @import("std").io; | | |
| 513 | | | |
| 514 | pub fn main(args: [][]u8) -> %void { | | |
| 515 | var foo : Foo = undefined; | | |
| 516 | @memset(&foo, 0, @sizeof(Foo)); | | |
| 517 | foo.a += 1; | | |
| 518 | foo.b = foo.a == 1; | | |
| 519 | test_foo(foo); | | |
| 520 | test_mutation(&foo); | | |
| 521 | if (foo.c != 100) { | | |
| 522 | %%io.stdout.printf("BAD\n"); | | |
| 523 | } | | |
| 524 | test_point_to_self(); | | |
| 525 | test_byval_assign(); | | |
| 526 | test_initializer(); | | |
| 527 | %%io.stdout.printf("OK\n"); | | |
| 528 | } | | |
| 529 | struct Foo { | | |
| 530 | a : i32, | | |
| 531 | b : bool, | | |
| 532 | c : f32, | | |
| 533 | } | | |
| 534 | fn test_foo(foo : Foo) { | | |
| 535 | if (!foo.b) { | | |
| 536 | %%io.stdout.printf("BAD\n"); | | |
| 537 | } | | |
| 538 | } | | |
| 539 | fn test_mutation(foo : &Foo) { | | |
| 540 | foo.c = 100; | | |
| 541 | } | | |
| 542 | struct Node { | | |
| 543 | val: Val, | | |
| 544 | next: &Node, | | |
| 545 | } | | |
| 546 | | | |
| 547 | struct Val { | | |
| 548 | x: i32, | | |
| 549 | } | | |
| 550 | fn test_point_to_self() { | | |
| 551 | var root : Node = undefined; | | |
| 552 | root.val.x = 1; | | |
| 553 | | | |
| 554 | var node : Node = undefined; | | |
| 555 | node.next = &root; | | |
| 556 | node.val.x = 2; | | |
| 557 | | | |
| 558 | root.next = &node; | | |
| 559 | | | |
| 560 | if (node.next.next.next.val.x != 1) { | | |
| 561 | %%io.stdout.printf("BAD\n"); | | |
| 562 | } | | |
| 563 | } | | |
| 564 | fn test_byval_assign() { | | |
| 565 | var foo1 : Foo = undefined; | | |
| 566 | var foo2 : Foo = undefined; | | |
| 567 | | 392 | |
| 568 | foo1.a = 1234; | | |
| 569 | | | |
| 570 | if (foo2.a != 0) { %%io.stdout.printf("BAD\n"); } | | |
| 571 | | | |
| 572 | foo2 = foo1; | | |
| 573 | | | |
| 574 | if (foo2.a != 1234) { %%io.stdout.printf("BAD - byval assignment failed\n"); } | | |
| 575 | } | | |
| 576 | fn test_initializer() { | | |
| 577 | const val = Val { .x = 42 }; | | |
| 578 | if (val.x != 42) { %%io.stdout.printf("BAD\n"); } | | |
| 579 | } | | |
| 580 | )SOURCE", "OK\n"); | | |
| 581 | | | |
| 582 | add_simple_case("global variables", R"SOURCE( | | |
| 583 | const io = @import("std").io; | | |
| 584 | | | |
| 585 | const g1 : i32 = 1233 + 1; | | |
| 586 | var g2 : i32 = 0; | | |
| 587 | | | |
| 588 | pub fn main(args: [][]u8) -> %void { | | |
| 589 | if (g2 != 0) { %%io.stdout.printf("BAD\n"); } | | |
| 590 | g2 = g1; | | |
| 591 | if (g2 != 1234) { %%io.stdout.printf("BAD\n"); } | | |
| 592 | %%io.stdout.printf("OK\n"); | | |
| 593 | } | | |
| 594 | )SOURCE", "OK\n"); | | |
| 595 | | | |
| 596 | add_simple_case("while loop", R"SOURCE( | | |
| 597 | const io = @import("std").io; | | |
| 598 | pub fn main(args: [][]u8) -> %void { | | |
| 599 | var i : i32 = 0; | | |
| 600 | while (i < 4) { | | |
| 601 | %%io.stdout.printf("loop\n"); | | |
| 602 | i += 1; | | |
| 603 | } | | |
| 604 | g(); | | |
| 605 | } | | |
| 606 | fn g() -> i32 { | | |
| 607 | return f(); | | |
| 608 | } | | |
| 609 | fn f() -> i32 { | | |
| 610 | while (true) { | | |
| 611 | return 0; | | |
| 612 | } | | |
| 613 | } | | |
| 614 | )SOURCE", "loop\nloop\nloop\nloop\n"); | | |
| 615 | | 393 | |
| 616 | add_simple_case("continue and break", R"SOURCE( | 394 | add_simple_case("continue and break", R"SOURCE( |
| 617 | const io = @import("std").io; | 395 | const io = @import("std").io; |