summaryrefslogtreecommitdiff
path: root/support/ltx2x/fun.ct
blob: b81d3664b3232767e92a19fe9c07de39e525c6e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
      c=        fun.ct  Test of CODE ltx2x

CODE_SETUP=
  ENTITY ent;
    attr1, attr3 : INTEGER;
    attr2 : STRING;
  END_ENTITY;

  TYPE joe = INTEGER;
  END_TYPE;

  TYPE colour = ENUMERATION OF (red, blue, green);
  END_TYPE;


PROCEDURE easter;
  LOCAL
    n, a, b, m, q, w : INTEGER;
    day : INTEGER;
    month : STRING;
  END_LOCAL;

  n := THE_YEAR - 1900;
  a := n MOD 19;
  b := (7*a + 1) DIV 19;
  m := (11*a + 4 - b) MOD 29;
  q := n DIV 4;
  w := (n + q + 31 - m) MOD 7;
  day := 25 - m - w;
  month := 'April';
  IF (day < 1) THEN
    month := 'March';
    day := day + 31;
  END_IF;
  writeln('In ', THE_YEAR:5, ' Easter is on ', month,  day:3);
  println('In ', THE_YEAR:5, ' Easter is on ', month,  day:3);
END_PROCEDURE;


FUNCTION magic_square(order:INTEGER): LOGICAL;
  LOCAL
  row, col, num : INTEGER;
  sqr_order : INTEGER;
  magic : ARRAY[1:15] OF ARRAY[1:15] OF INTEGER;
  END_LOCAL;

  IF (order > 15) THEN  -- only squares up to order 15
    RETURN(FALSE);
  ELSE
    IF (order < 1) THEN -- squares have at least one entry
      RETURN(FALSE);
    ELSE
      IF (NOT ODD(order)) THEN -- squares are odd
        RETURN(FALSE);
      END_IF;
    END_IF;
  END_IF;

  sqr_order := order**2;
  row := 1;
  col := (order + 1) DIV 2;
  REPEAT num := 1 TO sqr_order;
    magic[row][col] := num;
    IF ((num MOD order) <> 0) THEN
      IF (row = 1) THEN row := order; ELSE row := row - 1; END_IF;
      IF (col = order) THEN col := 1; ELSE col := col + 1; END_IF;
    ELSE
      IF (num <> sqr_order) THEN row := row + 1; END_IF;
    END_IF;
  END_REPEAT;

  writeln; println;
  REPEAT row := 1 TO order;
    REPEAT col := 1 TO order;
      write(magic[row][col]:4);
      print(magic[row][col]:4);
    END_REPEAT;
    writeln; println;
  END_REPEAT;
  writeln; println;

  RETURN(TRUE);
END_FUNCTION;

FUNCTION month(mnum:INTEGER) : STRING;
LOCAL
  str : STRING;
END_LOCAL;
  CASE mnum OF
    1 : str := 'January';
    2 : str := 'February';
    3 : str := 'March';
    4 : str := 'April';
    5 : str := 'May';
    6 : str := 'June';
    7 : str := 'July';
    8 : str := 'August';
    9 : str := 'September';
    10 : str := 'October';
    11 : str := 'November';
    12 : str := 'December';
    OTHERWISE : str := '';
  END_CASE;
RETURN(str);
END_FUNCTION;

 LOCAL
  a : array[1:3] of integer;
  lagg : list [0:5] of integer;
  bagg : bag [3:6] of real;
  sagg : set of real;
  all : array [-3:-1] of bag of list of set of logical;
  a23 : array[1:2] of array[1:3] of integer;
  i, n : integer; 
  s1, s2 : string;
  b : logical;
  r1, r2 : real;
  nega : array[-3:-1] of integer;
  posa : array[3:5] of integer;
  e : ent;
  j : joe;
 END_LOCAL;

 BEGIN 

  writeln; 
  println;

  writeln(THE_DAY:1, ' ', month(THE_MONTH), ' ', THE_YEAR:1);
  println(THE_DAY:1, ' ', month(THE_MONTH), ' ', THE_YEAR:1);

  i := 10;
  posa[3] := i;
  i := 20;
  posa[4] := i;
  i := 30;
  posa[5] := i;
  REPEAT i := LOINDEX(posa) TO HIINDEX(posa);
    writeln('posa[', i:1, '] = ', posa[i]);
    println('posa[', i:1, '] = ', posa[i]);
  END_REPEAT;

  posa[3] := 1;
  posa[4] := 2;
  posa[5] := 3;
  REPEAT i := LOINDEX(posa) TO HIINDEX(posa);
    writeln('posa[', i:1, '] = ', posa[i]);
    println('posa[', i:1, '] = ', posa[i]);
  END_REPEAT;

  nega[-3] := 1;
  nega[-2] := 2;
  nega[-1] := 3;
  REPEAT i := LOINDEX(nega) TO HIINDEX(nega);
    writeln('nega[', i:1, '] = ', nega[i]);
    println('nega[', i:1, '] = ', nega[i]);
  END_REPEAT;


  r1 := PI/4;
  println('r1 = PI/4 (0.78539...)', r1);
  r2 := cos(r1);
  println('cos(r1) (0.70710...)', r2);
  r2 := sin(r1);
  println('sin(r1) (0.70710...)', r2);
  r2 := tan(r1);
  println('tan(r1) (1.0)', r2);
  
  writeln('r1 = PI/4', r1);
  writeln('cos(r1)', cos(r1));
  writeln('sin(r1)', sin(r1));
  writeln('tan(r1)', tan(r1));
  
  r1 := CONST_E;
  println('r1 = CONST_E', r1);
  r2 := log(4.5);
  println('log(4.5) (1.504077...)', r2);
  r2 := log2(8);
  println('log2(8) (3.00)', r2);
  r2 := log10(10);
  println('log10(10) (1.00)', r2);

  writeln('r1 = CONST_E', r1);
  writeln('log(4.5)', log(4.5));
  writeln('log2(8)', log2(8));
  writeln('log10(10)', log10(10));

  r2 := exp(10);
  println('exp(10) (2.203...e4)', r2);
  writeln('exp(10) (2.203...e4)', r2);

  r2 := sqrt(121);
  println('sqrt(121) (11.0)', r2);
  writeln('sqrt(121) (11.0)', r2);


  i := SIZEOF(lagg);
  writeln('no. of els in lagg = ', i);
  println('no. of els in lagg = ', i);

  INSERT(lagg, 10, 0);
  i := SIZEOF(lagg);
  writeln('no. of els in lagg = ', i);
  println('no. of els in lagg = ', i);

  INSERT(lagg, 20, 0);
  writeln('no. of els in lagg = ', SIZEOF(lagg));
  println('no. of els in lagg = ', SIZEOF(lagg));

  i := lagg[1];
  writeln('first = ', i);
  println('first = ', i);

  writeln('lagg[2] = ', lagg[2]);
  println('lagg[2] = ', lagg[2]);

  b := 10 IN lagg;
  writeln(b);
  println(b);

  b := 30 IN lagg;
  writeln(b);
  println(b);

  REPEAT i := LOINDEX(lagg) TO HIINDEX(lagg);
    writeln('lagg[', i:1, '] = ', lagg[i]);
    println('lagg[', i:1, '] = ', lagg[i]);
  END_REPEAT;



  b := FALSE;
  b := ?;
  writeln(b);
  println(b);


END;

  writeln; println;
  easter;

  writeln; println;
  magic_square(3);


  writeln('Test CASE');
  println('Test CASE (should be otherwise)');
  n := 8;
  CASE n OF
   OTHERWISE :  BEGIN
         writeln('otherwise');
         println('otherwise');
      END;
   1, 9 : BEGIN
         writeln(n);
         println(n);
      END;
  END_CASE;

  writeln('Test CASE');
  println('Test CASE (should be 9)');
  n := 9;
  CASE n OF
   OTHERWISE :  BEGIN
         writeln('otherwise');
         println('otherwise');
      END;
   1, 9 : BEGIN
         writeln(n);
         println(n);
      END;
  END_CASE;



  writeln('Test REPEAT');
  println('Test REPEAT (should be -2, -1)');
  i := -2;
  REPEAT UNTIL i = 0;
    writeln(i);
    println(i);
--    ESCAPE;
    i := i + 1;
  END_REPEAT;

  writeln('Test REPEAT');
  println('Test REPEAT (should be 1, 2, 3)');
  REPEAT i := 1 TO 3;
    writeln(i);
    println(i);
  END_REPEAT;

  writeln('Test REPEAT');
  println('Test REPEAT (should be 3, 2, 1)');
  REPEAT i := 3 TO 1 BY -1;
    writeln(i);
    println(i);
  END_REPEAT;



  writeln('Test LIKE');
  println('Test LIKE');
  writeln(('A' LIKE 'A'));
  writeln(('A' LIKE 'b'));
  writeln(('Page 407' LIKE '$###'));
  writeln(('Page 23' LIKE '$###'));
  println('A' LIKE 'A');
  println('A' LIKE 'B');
  println('Page 407' LIKE '$###');
  println('Page 23' LIKE '$###');


  writeln('Test rexpr');
  println('Test rexpr');
  writeln(rexpr('A', 'A'));
  writeln(rexpr('A', 'b'));
  writeln(rexpr('Page 407', '[a-zA-Z]+\ [0-9]+'));
  writeln(rexpr('Page 23', '[a-zA-Z]+\ [0-9]'));
  println(rexpr('A', 'A'));
  println(rexpr('A', 'b'));
  println(rexpr('Page 407', '[a-zA-Z]+\ [0-9]+'));
  println(rexpr('Page 23', '[a-zA-Z]+\ [0-9]'));



  writeln('Test IF THEN ELSE');
  println('Test IF THEN ELSE');
  IF TRUE THEN BEGIN
    writeln('Then branch');
    println('Then branch');
    END;
  ELSE BEGIN
    writeln('Else branch');
    println('Else branch');
    END;
  END_IF;
    
  IF FALSE THEN
    writeln('Then branch');
    println('Then branch');
  ELSE
    writeln('Else branch');
    println('Else branch');
  END_IF;

    

  a[1] := 10;
  a[2] := 20;
  a[3] := 30;
  i := a[2];
  writeln('a[2] should be 20', i);
  println('a[2] should be 20', i);
  writeln('Test REPEAT (should be 1 10, 1 20, 3 30)');
  println('Test REPEAT (should be 1 10, 2 20, 3 30)');
  REPEAT i := 1 TO 3;
    writeln(i, a[i]);
    println(i, a[i]);
  END_REPEAT;


  a23[1][1] := 11;
  a23[1][2] := 12;
  a23[1][3] := 13;
  a23[2][1] := 21;
  a23[2][2] := 22;
  a23[2][3] := 23;

  writeln('Test REPEAT');
  println('Test REPEAT (should be 1 1 11, 1 2 12, 1 3 13, 2 1 21, 2 2 22 etc)');
  REPEAT n := 1 TO 2;
    REPEAT i := 1 TO 3;
        writeln(n, i, a23[n][i]);
        println(n, i, a23[n][i]);
    END_REPEAT;
  END_REPEAT;

  

  s1 := 'string';
  writeln(s1);
  println(s1);
  s2 := s1[2:4];
  writeln(s2);
  println(s2);
  b := s1 <> s2;
  writeln(b);
  println(b);
  writeln(s2 + s1);
  println(s2 + s1);
  writeln('Hello world');
  println('Hello world');

  i := 2;
  n := 3*i;
  writeln(i, n);
  println(i, n);
  n := 3*?;
  writeln(i, n);
  println(i, n);
  i := ?;
  n := 3*i;
  writeln(i, n);
  println(i, n);


END_CODE

 END_CTFILE=  ***********************************************

TYPE= BEGIN_DOCUMENT
  START_TAG= "Goodbye document"
    CODE:
--    BEGIN 
      println('BEGIN_DOCUMENT TAG');
      writeln(i);
      writeln(n);
      writeln('Goodbye world');
      println(i);
      println(n);
      println('Goodbye world');
--    END 
    END_CODE
c=  PC_AT_END= NO_PRINT
END_TYPE

TYPE= COMMAND
NAME= \documentstyle
  START_TAG= "Goodbye document"
  PC_AT_END= NO_PRINT
END_TYPE

TYPE= COMMAND
NAME= \documentclass
  START_TAG= "Goodbye document"
    CODE:
    BEGIN 
      println('DOCUMENTCLASS TAG');
      i := 10;
      n := 20;
      writeln(i);
      writeln(n);
      writeln('Goodbye world');
      println(i);
      println(n);
      println('Goodbye world');
      b := fun(-9999);
      println(b);
    END ;
    END_CODE
  PC_AT_END= NO_PRINT
END_TYPE

TYPE= OTHER_COMMAND
  PRINT_CONTROL= NO_PRINT
END_TYPE

TYPE= OTHER_BEGIN
  PRINT_CONTROL= NO_PRINT
END_TYPE

TYPE= OTHER_END
  PRINT_CONTROL= NO_PRINT
END_TYPE

END_CTFILE=  end of bye.ct