repos / gbc

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

gbc / examples
xplshn  ·  2025-09-10

cal2.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
16extrn time, localtime;
17
18int32 days2Month(m int, y int) {
19    if (m == 1) { // feb
20        if ((y % 400 == 0) | ((y % 4 == 0) & (y % 100 != 0))) {
21            return (29);
22        } else {
23            return (28);
24        }
25    };
26    if ((m == 0) | (m == 2) | (m == 4) | (m == 6) | (m == 7) | (m == 9) | (m == 11)) {
27        return (31);
28    };
29    return (30);
30}
31
32int main() {
33    now := time(0);
34    tptr := localtime(&now);
35    tm := *tptr;
36
37    year  := tm.year + 1900;
38    month := tm.mon;
39    today := tm.mday;
40    wday  := tm.wday;
41
42    days := days2Month(month, year);
43
44    printf("    %d/%d\n", month + 1, year);
45    printf("Su Mo Tu We Th Fr Sa\n");
46
47    // Calculate the weekday of the first day of the month
48    first_wday := (wday - (today - 1) % 7 + 7) % 7;
49
50    i := 0;
51    while (i < first_wday) {
52        printf("   ");
53        i = i + 1;
54    };
55
56    d := 1;
57    while (d <= days) {
58        if (d == today) {
59            printf("\033[31m%2d\033[0m ", d);
60        } else {
61            printf("%2d ", d);
62        };
63
64        current_wday := (first_wday + d - 1) % 7;
65        if (current_wday == 6) { // It's Saturday, print \n
66            printf("\n");
67        };
68        d = d + 1;
69    };
70
71    // Month doesn't end in Saturday so add \n
72    if ((first_wday + days - 1) % 7 != 6) {
73        printf("\n");
74    };
75
76    return (0);
77}
78