repos / gbc

GBC - Go B Compiler
git clone https://github.com/xplshn/gbc.git

gbc / examples
xplshn  ·  2025-09-10

cal.bx

Bx
 1extrn printf;
 2
 3type struct tm {
 4    sec,
 5    min,
 6    hour,
 7    mday,
 8    mon,
 9    year,
10    wday,
11    yday,
12    isdst int32; // localtime is extrn, it   comes   from the  Musl libc
13};               // and `tm`  is the    same struct  as   the  one  there
14                 // :3
15
16// Typed external function declarations
17int extrn time;
18tm* extrn localtime;
19
20int32 days2Month(m int, y int) {
21    if (m == 1) { // feb
22        if ((y % 400 == 0) | ((y % 4 == 0) & (y % 100 != 0))) {
23            return (29);
24        } else {
25            return (28);
26        }
27    };
28    if ((m == 0) | (m == 2) | (m == 4) | (m == 6) | (m == 7) | (m == 9) | (m == 11)) {
29        return (31);
30    };
31    return (30);
32}
33
34int main() {
35    now := time(0);
36    tptr := localtime(&now);
37    tm := *tptr;
38
39    year  := tm.year + 1900;
40    month := tm.mon;
41    today := tm.mday;
42    wday  := tm.wday;
43
44    days := days2Month(month, year);
45
46    printf("    %d/%d\n", month + 1, year);
47    printf("Su Mo Tu We Th Fr Sa\n");
48
49    // Calculate the weekday of the first day of the month
50    first_wday := (wday - (today - 1) % 7 + 7) % 7;
51
52    i := 0;
53    while (i < first_wday) {
54        printf("   ");
55        i = i + 1;
56    };
57
58    d := 1;
59    while (d <= days) {
60        if (d == today) {
61            printf("\033[31m%2d\033[0m ", d);
62        } else {
63            printf("%2d ", d);
64        };
65
66        current_wday := (first_wday + d - 1) % 7;
67        if (current_wday == 6) { // It's Saturday, print \n
68            printf("\n");
69        };
70        d = d + 1;
71    };
72
73    // Month doesn't end in Saturday so add \n
74    if ((first_wday + days - 1) % 7 != 6) {
75        printf("\n");
76    };
77
78    return (0);
79}
80