Calendar - for "REAL" programmers: Difference between revisions

m
→‎{{header|Phix}}: use pygments
imported>Arakov
m (→‎{{header|Phix}}: use pygments)
 
(3 intermediate revisions by 3 users not shown)
Line 1,077:
_date := Date.new(_year, _month, 1);
 
DayNames.forEach::(name)
{ _line.print(" ",name) }
}
Line 1,095:
_line.writePaddingLeft(_date.Day.toPrintable(), $32, 3);
 
_date := _date.addDays:(1)
}
until(_date.Month != _month || _date.DayOfWeek == 1)
Line 1,167:
var rowCount := 12 / _rowLength;
var months := Array.allocate(rowCount).populate::(i =>
Array.allocate(_rowLength)
.populate::(j =>
new CalendarMonthPrinter(_year, i * _rowLength + j + 1)));
months.forEach::(row)
{
var r := row;
row.forEach::(month)
{
month.printTitleTo:(output);
output.write:(" ")
};
output.writeLine();
ParallelEnumerator.new(row).forEach::(line)
{
line.forEach::(printer)
{
printer.printTo:(output);
 
output.write:(" ")
};
 
Line 1,202:
public program()
{
var calender := Calendar.new(console.write:("ENTER THE YEAR:").readLine().toInt());
calender.printTo:(console);
 
console.readChar()
Line 1,246:
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
=={{header|Forth}}==
<syntaxhighlight lang="forth">
: WEEKDAY ( D M Y -- U )
OVER 3 < IF SWAP 12 + SWAP 1- THEN
DUP 4 / OVER 100 / - OVER 400 / + + SWAP 1+ 13 * 5 / + + 2 - 7 MOD ;
 
: MDAYS ( M Y -- MSIZE MDAY )
OVER 12 = IF 31 1 2SWAP WEEKDAY NEGATE EXIT THEN
2>R 1 2R@ WEEKDAY 1 2R> SWAP 1+ SWAP WEEKDAY OVER -
7 + 7 MOD 28 + SWAP NEGATE ;
 
: .WEEK ( MSIZE MDAY -- MSIZE MDAY' )
7 0 DO DUP 0< IF 1+ 3 SPACES ELSE
2DUP > IF 1+ DUP 2 .R SPACE ELSE 3 SPACES THEN THEN LOOP ;
 
: .3MONTHS ( Y M -- )
3 0 DO ." MO TU WE TH FR SA SU " LOOP CR
3 OVER + SWAP DO I OVER MDAYS ROT LOOP DROP
6 0 DO 2ROT .WEEK 2 SPACES 2ROT .WEEK 2 SPACES 2ROT .WEEK CR LOOP
2DROP 2DROP 2DROP ;
 
: CAL ( Y -- )
30 SPACES ." [SNOOPY]" CR
32 SPACES DUP . CR
." JANUARY FEBRUARY MARCH" CR
DUP 1 .3MONTHS
." APRIL MAY JUNE" CR
DUP 4 .3MONTHS
." JULY AUGUST SEPTEMBER" CR
DUP 7 .3MONTHS
." OCTOBER NOVEMBER DECEMBER" CR
10 .3MONTHS ;
 
1969 CAL
</syntaxhighlight>
{{out}}
<pre>
[SNOOPY]
1969
JANUARY FEBRUARY MARCH
MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
APRIL MAY JUNE
MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
JULY AUGUST SEPTEMBER
MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
OCTOBER NOVEMBER DECEMBER
MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
</pre>
=={{header|Fortran}}==
Alas, the header "FORTRAN" is not recognised - REAL programmers were absent that day? Even upon the apperance of lower case, I have preferred to use shouting for programme source, and normal upper/lower case for commentary. Aside from petty details such as 1 and l being nowhere as distinct as 1 and L, this allows the two sorts of blather to be identifiably different without ratiocination as the hours drag past. Further, the names of variables can easily be distinguished from the same word in discussion, as in ... the text in TEXT will be printed as the subtitle to the text in TITLE ... Anyway, in the spirit of old, herewith the source without tedious commentary:
Line 2,749 ⟶ 2,819:
Although, if we are abusing the backticks and other innocent programs, why not just: <syntaxhighlight lang="perl">$_=$ARGV[0]//1969;`\143\141\154 $_ >&2`</syntaxhighlight>
=={{header|Phix}}==
I'm not going to take this too seriously and instead treat it as a good opportunity to showcase how trivial it is to make some rather fundamental changes to the compiler. So, the first task is to take a copy of [[Calendar#Phix|Calendar]] and make it output uppercase. Change the line in routine centre:
<syntaxhighlight lang="phix">
 
--return repeat(' ',left)&s&repeat(' ',right)
So, the first task is to take a copy of [[Calendar#Phix|Calendar]] and make it output uppercase. Change the line
return repeat(' ',left)&upper(s)&repeat(' ',right)
<!--<syntaxhighlight lang="phix">(phixonline)-->
</syntaxhighlight>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">left</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">s</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">right</span><span style="color: #0000FF;">)</span>
(That's that done.) Now, Phix is case-sensitive, and many of the keywords and builtins are in lower case, which presents a bit of a challenge - but we can cope. It is too tedious and not particularly helpful to present a completed solution, instead I shall outline the (relatively simple) steps this would require. I have limited my tests to INCLUDE and INTEGER. In psym.e you can find the line
<!--</syntaxhighlight>-->
<syntaxhighlight lang="phix">
in routine centre to:
initialSymEntry("integer", S_Type,"TI",opInt, E_none) -- #01 / 0b0001 integer
<!--<syntaxhighlight lang="phix">(phixonline)-->
</syntaxhighlight>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">left</span><span style="color: #0000FF;">)&</span><span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">right</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
(That's that done.) Now, Phix is case-sensitive, and many of the keywords and builtins are in lower case, which presents a bit of a challenge - but we can cope.<br>
It is too tedious and not particularly helpful to present a completed solution, instead I shall outline the (relatively simple) steps this would require. I have limited my tests to INCLUDE and INTEGER.
 
In psym.e you can find the line
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000000;">initialSymEntry</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"integer"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">S_Type</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TI"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">opInt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">E_none</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- #01 / 0b0001 integer</span>
<!--</syntaxhighlight>-->
Immediately after that (it should now be there commented out) add:
<!--<syntaxhighlight lang="phix">(phixonline)-->
Alias("INTEGER",symlimit)
<span style="color: #000000;">Alias</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"INTEGER"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">symlimit</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
Add similar lines for length, floor, repeat, upper, day_of_week, sequence, string, sprintf, append, printf, join, at the appropriate places. Keywords are handled slightly differently: In pttree.e we need a new alias routine (you should find this one commented out, from when I was testing all this):
 
<syntaxhighlight lang="phix">
Add similar lines for length, floor, repeat, upper, day_of_week, sequence, string, sprintf, append, printf, join, at the appropriate places.
procedure tt_stringA(sequence text, integer alias)
 
tt_string(text,-2)
Keywords are handled slightly differently: In pttree.e we need a new alias routine (you should find this one commented out, from when I was testing all this):
tt[pcurr] = alias
<!--<syntaxhighlight lang="phix">(phixonline)-->
end procedure
<span style="color: #008080;">procedure</span> <span style="color: #000000;">tt_stringA</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">alias</span><span style="color: #0000FF;">)</span>
</syntaxhighlight>
<span style="color: #000000;">tt_string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">tt</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pcurr</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">alias</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<!--</syntaxhighlight>-->
and you can find the line
<!--<syntaxhighlight lang="phix">(phixonline)-->
global constant T_include = 596 tt_stringF("include",T_include)
<span style="color: #008080;">global</span> <span style="color: #008080;">constant</span> <span style="color: #000000;">T_include</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">596</span> <span style="color: #000000;">tt_stringF</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"include"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">T_include</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
Sometime after that, in fact after the very last use of tt_stringF, add (likewise you should find this one commented out):
<!--<syntaxhighlight lang="phix">(phixonline)-->
tt_stringA("INCLUDE",T_include)
<span style="color: #000000;">tt_stringA</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"INCLUDE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">T_include</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
 
Add similar lines for function, end, iff, for, to, do, if, or, then, else, return, procedure, but this time all some ways away from the originals. Quickly run "p p" and make sure you get a normal prompt, without any "T_include should be 624(not 596)" or suchlike (which would happen <i>alot</i> if you put the aliases anywhere other than last).
Line 2,793 ⟶ 2,852:
Currently ptok.e recognises <code>\n</code> but not <code>\N</code>. Search for all references to <code>escchar</code> (there should be one definition and two references) and make sure the character being
checked is mapped to lower case, ie change (twice)
<!--<syntaxhighlight lang="phix">(phixonline)-->
--Ch = find(Ch,escchar)
<span style="color: #000000;">Ch</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Ch</span><span style="color: #0000FF;">,</span><span style="color: #000000;">escchar</span><span style="color: #0000FF;">)</span>
Ch = find(lower(Ch),escchar)
<!--</syntaxhighlight>-->
</syntaxhighlight>
to
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000000;">Ch</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Ch</span><span style="color: #0000FF;">),</span><span style="color: #000000;">escchar</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
 
Lastly, the calendar example includes builtins\timedate.e, which contains some lower-case definitions. So copy it to (say) TIMEDATEUPPER.E and change adjust_timedate, timedelta, days (the 2nd param to timedelta), and format_timedate to their uppercase equivalents, and of course change that include to <code>INCLUDE BUILTINS\TIMEDATEUPPER.E</code>.
 
There are probably even simpler changes to ptok.e/pttree.e that would make everything case insensitive, but locating those is left as an exercise for the reader.<br> Likewise the changes that would be needed to pwa/p2js have been left as an exercise for the reader, but are assumed similarly trivial.
Likewise the changes that would be needed to pwa/p2js have been left as an exercise for the reader, but are assumed similarly trivial.
 
You don't actually have to repackage (p -c p) to test this, just run p p test.exw - technically slower by a second or so but only really noticeable long-term.
 
Finally, you can take that copy of [[Calendar#Phix|Calendar]] and make it all uppercase. In Edita (shipped with Phix) that is just <Ctrl A><Alt U>.
 
=={{header|PHP}}==
While PHP functions are case-sensitive (aside of <code>_()</code>), this isn't the case for language constructs.
Line 4,430 ⟶ 4,486:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascripttext">IMPORT "./DATE" FOR DATE
IMPORT "./FMT" FOR FMT
IMPORT "./SEQ" FOR LST
 
VAR CALENDAR = FN.NEW { |YEAR|
Line 4,482 ⟶ 4,538:
 
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./str" for Str
import "meta" for Meta
 
Line 4,542 ⟶ 4,598:
30
</pre>
 
=={{header|X86 Assembly}}==
ASSEMBLE WITH: TASM CALENDAR; TLINK /t CALENDAR
7,796

edits