xplshn
·
2025-08-13
switch.b
B
1assert_equal(actual, expected, message) {
2 extrn printf, abort;
3 printf("%s: ", message);
4 if (actual != expected) {
5 printf("FAIL\n");
6 abort();
7 } else {
8 printf("OK\n");
9 }
10}
11
12test_lookup(a, b) {
13 switch a {
14 case 69:
15 return (690);
16
17 case 420:
18 switch b {
19 case 420:
20 return (42);
21 case 1337:
22 return (7331);
23 }
24 // default:
25 return (-2);
26
27 }
28 // default:
29 return (-1);
30}
31
32/* TODO: maybe this should be a part of the libb at some point?
33 * iirc snake example also uses a similar function
34 */
35unreachable(message) {
36 extrn printf, abort;
37 printf("UNREACHABLE: %s\n", message);
38 abort();
39}
40
41test_fallthrough(x) {
42 extrn printf;
43 switch (x) {
44 unreachable("test_fallthrough");
45 case 0: printf("0\n");
46 case 1: printf("1\n");
47 case 2: printf("2\n");
48 case 3: printf("3\n");
49 case 4: printf("4\n");
50 }
51}
52
53main() {
54 extrn printf, assert_equal;
55
56 assert_equal(test_lookup(69,69), 690, "(69,69) => 690");
57 assert_equal(test_lookup(420,420), 42, "(420,420) => 42");
58 assert_equal(test_lookup(420,1337), 7331, "(420,1337) => 7331");
59 assert_equal(test_lookup(420,69), -2, "(420,69) => -2");
60 assert_equal(test_lookup(34,35), -1, "(34,35) => -1");
61
62 printf("------------------------------\n");
63 test_fallthrough(0);
64 printf("------------------------------\n");
65 test_fallthrough(3);
66
67 /* According to kbman the syntax of switch-case is `switch rvalue statement`.
68 * So bellow are valid cases.
69 */
70 switch 69 {
71 unreachable("curly");
72 }
73 switch 69 unreachable("inline");
74}