Calendar - for "REAL" programmers: Difference between revisions

Line 2,415:
* * X** X XXXX X
* * X** XX X X
=={{header|M2000 Interpreter}}== * ** X** X XX X
* ** X* XXX X X
* ** XX XXXX XXX
Line 2,518:
RETURN
</lang>
 
=={{header|Nim}}==
Nim is a peculiar language regarding case sensitivity. First, keywords are all in lowercase. Second, Nim is case sensitive regarding the first character only. That means that ''Thing'' is different of ''thing'' but identical to ''THING''. There are several conventions regarding the style. For instance, the first character of a type should be in uppercase while the first character of a procedure or a variable should be in lowercase. Nim is also style-insensitive which means that "myvar", "myVar" and "my_var" are equivalent identifiers.
 
This makes impossible to write a valid program with only uppercase characters. So, we have built a program which reads the text written in uppercase, transforms it to valid Nim code, writes it into a file and includes this file. That means that in only one step, the uppercase text is read, transformed and compiled.
 
Instead of using the command <code>nim c file.txt</code> which would not work, we use the command <code>nim c file.nim</code> which, in fact, compiles “nim.txt” after is has been transformed.
 
Here is the helper program:
 
<lang>import strutils
 
const progUpper = staticRead("calendar_upper.txt")
 
proc transformed(s: string): string {.compileTime.} =
## Return a transformed version (which can compile) of a program in uppercase.
 
let upper = s.toLower() # Convert all to lowercase.
var inString = false # Text in string should be in uppercase…
var inBraces = false # … except if this is in an expression to interpolate.
for ch in upper:
case ch
of '"':
inString = not inString
of '{':
if inString: inBraces = true
of '}':
if inString: inBraces = false
of 'a'..'z':
if inString and not inBraces:
result.add(ch.toUpperAscii()) # Restore content of strings to uppercase.
continue
else:
discard
result.add(ch)
 
const prog = progUpper.transformed()
 
static: writeFile("calendar_transformed.nim", prog)
 
include "calendar_transformed.nim"</lang>
 
Here is the actual program in uppercase.
 
<lang>IMPORT TIMES
IMPORT STRFORMAT
PROC PRINTCALENDAR(YEAR, NCOLS: INT) =
VAR ROWS = 12 DIV NCOLS
VAR DATE = INITDATETIME(1, MJAN, YEAR, 0, 0, 0, UTC())
IF ROWS MOD NCOLS != 0:
INC ROWS
VAR OFFS = GETDAYOFWEEK(DATE.MONTHDAY, DATE.MONTH, DATE.YEAR).INT
VAR MONS: ARRAY[12, ARRAY[8, STRING]]
FOR M IN 0..11:
MONS[M][0] = &"{$DATE.MONTH:^21}"
MONS[M][1] = " SU MO TU WE TH FR SA"
VAR DIM = GETDAYSINMONTH(DATE.MONTH, DATE.YEAR)
FOR D IN 1..42:
VAR DAY = D > OFFS AND D <= OFFS + DIM
VAR STR = IF DAY: &" {D-OFFS:2}" ELSE: " "
MONS[M][2 + (D - 1) DIV 7] &= STR
OFFS = (OFFS + DIM) MOD 7
DATE = DATE + MONTHS(1)
VAR SNOOPYSTRING, YEARSTRING: STRING
FORMATVALUE(SNOOPYSTRING, "[SNOOPY PICTURE]", "^" & $(NCOLS * 24 + 4))
FORMATVALUE(YEARSTRING, $YEAR, "^" & $(NCOLS * 24 + 4))
ECHO SNOOPYSTRING, "\N" , YEARSTRING, "\N"
FOR R IN 0..<ROWS:
VAR S: ARRAY[8, STRING]
FOR C IN 0..<NCOLS:
IF R * NCOLS + C > 11:
BREAK
FOR I, LINE IN MONS[R * NCOLS + C]:
S[I] &= &" {LINE}"
FOR LINE IN S:
IF LINE == "":
BREAK
ECHO LINE
ECHO ""
PRINTCALENDAR(1969, 5)</lang>
 
{{out}}
The program allows to choose the number of columns. Five is the maximum which can fit with 132 columns.
 
<pre> [SNOOPY PICTURE]
1969
 
January February March April May
SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA
1 2 3 4 5 1 2 1 2 1 2 3 4 5 6 1 2 3 4
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31
31
 
June July August September October
SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA SU MO TU WE TH FR SA
1 1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30 31 29 30 27 28 29 30 31
30
 
November December
SU MO TU WE TH FR SA SU MO TU WE TH FR SA
1 2 1 2 3 4 5 6 7
3 4 5 6 7 8 9 8 9 10 11 12 13 14
10 11 12 13 14 15 16 15 16 17 18 19 20 21
17 18 19 20 21 22 23 22 23 24 25 26 27 28
24 25 26 27 28 29 30 29 30 31
</pre>
 
=={{header|Perl}}==
Anonymous user