# IF THIS SMALL FUNCTION IS PLACED IN THE STARTUP.JL
# FILE, IT WILL BE LOADED ON STARTUP. THE REST OF
# THIS EXAMPLE IS IN ALL UPPERCASE.
function RUNUPPERCASECODE(CO)
    COD = replace(lowercase(CO), "date" => "Date")
    for E in Meta.parse(COD, 1) eval(E) end
end


CODE = """BEGIN

USING DATES;
CENTEROBJECT(X, N) = BEGIN S = UPPERCASE(STRING(X)); RPAD(LPAD(S, DIV(N + LENGTH(S), 2)), N) END;
FUNCTION FORMATMONTH(YR, MO)
    DT = DATE(\"\$YR-\$MO-01\");
    DAYOFWEEKFIRST = DAYOFWEEK(DT);
    NUMWEEKLINES = 1;
    STR = UPPERCASE(CENTEROBJECT(MONTHNAME(DT), 20) * \"\\NMO TU WE TH FR SA SU\\N\");
    STR *= \" \" ^ (3 * (DAYOFWEEKFIRST - 1)) * LPAD(STRING(1), 2);
    FOR I = 2:DAYSINMONTH(DT)
        IF (I + DAYOFWEEKFIRST + 5) % 7 == 0
            STR *= \"\\N\" * LPAD(I, 2);
            NUMWEEKLINES += 1;
        ELSE
            STR *= LPAD(STRING(I), 3);
        END;
    END;
    STR *= NUMWEEKLINES < 6 ? \"\\N\\N\\N\" : \"\\N\\N\";
    ARR = [];
    FOR S IN SPLIT(STR, \"\\N\")
        PUSH!(ARR, RPAD(S, 20)[1:20])
    END;
    JOIN(ARR, \"\\N\");
END;

FUNCTION FORMATYEAR(DISPLAYYEAR)
    CALMONTHS = [FORMATMONTH(DISPLAYYEAR, MO) FOR MO IN 1:12];
    MONTHSPERLINE = 6;
    JOINSPACES = 2;
    STR = \"\\N\" * CENTEROBJECT(DISPLAYYEAR, 132) * \"\\N\";
    MONTHCAL = [SPLIT(FORMATMONTH(DISPLAYYEAR, I), \"\\N\") FOR I IN 1:12];
    FOR I IN 1:MONTHSPERLINE:LENGTH(CALMONTHS) - 1
        FOR J IN 1:LENGTH(MONTHCAL[1])
            MONTHLINES = MAP(X->MONTHCAL[X][J], I:I + MONTHSPERLINE - 1);
            STR *= RPAD(JOIN(MONTHLINES, \" \" ^ JOINSPACES), 132) * \"\\N\";
        END;
        STR *= \"\\N\";
    END;
    STR;
END;

PRINTLN(FORMATYEAR(1969));

END;
"""

RUNUPPERCASECODE(CODE)
