summaryrefslogtreecommitdiff
path: root/support/ltx2x/l2xistd.c
blob: 49c3dbc9cb9f5a62c032c5559962cfb9ed7a0d4c (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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* l2xistd.c  LTX2X interpreter  Parsing for calls to standard functions */
/*  Written by: Peter Wilson, CUA  pwilson@cme.nist.gov                */
/*  This code is partly based on algorithms presented by Ronald Mak in */
/*  "Writing Compilers & Interpreters", John Wiley & Sons, 1991        */

#include <stdio.h>
#include "l2xicmon.h"
#include "l2xierr.h"
#include "l2xiscan.h"
#include "l2xisymt.h"
#include "l2xiprse.h"
#include "l2xiidbg.h"


#define DEFAULT_NUMERIC_FIELD_WIDTH 10
#define DEFAULT_PRECISION 2

/* EXTERNALS */

extern TOKEN_CODE token;
extern char word_string[];
extern SYMTAB_NODE_PTR symtab_display[];
extern int level;
extern TOKEN_CODE follow_parm_list[];
extern TOKEN_CODE statement_end_list[];

/* FORWARDS */

TYPE_STRUCT_PTR eof_eoln(), abs_sqr(), arctan_cos_exp_ln_sin_sqrt(),
                pred_succ(), odd(), ord(), round_trunc();
TYPE_STRUCT_PTR atan(), exists_etc(), nvl_etc();
TYPE_STRUCT_PTR rexpr_etc(), hibound_etc(), length_etc();

/***************************************************************************/
/* standard_routine_call (rtn_idp) Process call to standard function       */
/* return pointer to type structure of the call                            */

TYPE_STRUCT_PTR standard_routine_call(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;           /* routine id */
{

  switch (rtn_idp->defn.info.routine.key) {
    case READ:
    case READLN: {
      read_readln(rtn_idp);
      return(NULL);
    }
    case WRITE:
    case WRITELN: {
      write_writeln(rtn_idp);
      return(NULL);
    }
    case EOFF: 
    case EOLN: {
      return(eof_eoln(rtn_idp));
    }
    case ABS:          /* real or int arg -> real or int */
              {
      return(abs_sqr());
    }
    case COS:          /* real or int arg -> real */ 
    case EXP: 
    case SIN: 
    case SQRT: 
    case XACOS: 
    case XASIN:
    case XLOG:
    case XLOG2:
    case XLOG10:
    case XTAN: {
      return(arctan_cos_exp_ln_sin_sqrt());
    }
    case XATAN: {  
      return(atan());
    }
    case ODD:  {       /* int arg -> boolean */
      return(odd());
    }
    case ROUND:        /* real arg -> int */
    case TRUNC: {
      return(round_trunc());
    }
    case L2XPRINT:
    case L2XPRINTLN: {               /* extra for ltx2x */
      print_println(rtn_idp);
      return(NULL);
    }
    case L2XSYSTEM: {                /* extra for ltx2x */
      system_etc(rtn_idp);
      return(NULL);
    }
    case L2XREXPR: {                 /* extra for ltx2x two strings -> boolean */
      return(rexpr_etc());
    }
    case XEXISTS: {    /* any arg -> boolean */
      return(exists_etc());
    }
    case XNVL: {       /* two args -> one of these */
      return(nvl_etc());
    }
    case XHIBOUND:         /* agg arg -> int */
    case XHIINDEX:
    case XLOBOUND:
    case XLOINDEX:
    case XSIZEOF: {
      return(hibound_etc());
    }
    case XLENGTH: {        /* string arg -> int */
      return(length_etc());
    }
    case XINSERT:
    case XREMOVE: {
      insert_etc(rtn_idp);
      return(NULL);
    }
    case XBLENGTH:                 /* unimplemented EXPRESS functions */
    case XFORMAT:
    case XROLESOF:
    case XTYPEOF:
    case XUSEDIN:
    case XVALUE:
    case XVALUE_IN:
    case XVALUE_UNIQUE: {
      error(UNIMPLEMENTED_FEATURE);
      return(NULL);
    }
    default : {    /* should not be here */
      error(UNEXPECTED_TOKEN);
      return(NULL);
    }
  }  /* end switch */
}                                             /* end standard_routine_call */
/***************************************************************************/


/***************************************************************************/
/* read_readln(rtn_idp) Process call to read or readln                     */

read_readln(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;
{
  TYPE_STRUCT_PTR actual_parm_tp;      /* actual param type */

  /* parameters are optional for readln */
  if (token == LPAREN) {
    do {
      get_token();
      /* actuals should be variables, but parse anyway */
      if (token == IDENTIFIER) {
        SYMTAB_NODE_PTR idp;
        search_and_find_all_symtab(idp);
        actual_parm_tp = base_type(variable(idp, VARPARM_USE));
/*        if (actual_parm_tp->form != SCALAR_FORM) error(INCOMPATIBLE_TYPES); */
        if (actual_parm_tp != integer_typep &&
            actual_parm_tp != real_typep &&
            actual_parm_tp != logical_typep &&
            actual_parm_tp != string_typep) {
          error(INCOMPATIBLE_TYPES);
        }
      }
      else {
        actual_parm_tp = expression();
        error(INVALID_VAR_PARM);
      }
     /* sync. Should be , or ) */
     synchronize(follow_parm_list, statement_end_list, NULL);
    } while (token == COMMA);  /* end do */
    if_token_get_else_error(RPAREN, MISSING_RPAREN); 
  } /* end if */
  else {
    if (rtn_idp->defn.info.routine.key == READ) error(WRONG_NUMBER_OF_PARMS);
  }

}                                                       /* end read_readln */
/***************************************************************************/



/***************************************************************************/
/* write_writeln(rtn_idp) Process call to write or writeln                 */
/*                        Each actual parameter can be:                    */
/*                        <expr> or                                        */
/*                        <expr> : <expr> or                               */
/*                        <expr> : <expr> : <expr>                         */

write_writeln(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;
{
  TYPE_STRUCT_PTR actual_parm_tp;             /* actual parm type */
  TYPE_STRUCT_PTR field_width_tp, precision_tp;

  /* params are optional for writeln */
  if (token == LPAREN) {
    do {
      get_token();
      actual_parm_tp = base_type(expression());

      if ((actual_parm_tp->form != SCALAR_FORM) &&
          (actual_parm_tp != logical_typep) &&
          (actual_parm_tp->form != STRING_FORM) &&
          (actual_parm_tp->form != ENUM_FORM))
        error(INVALID_EXPRESSION);

      /* optional field width expression */
      if (token == COLON) {
        get_token();
        field_width_tp = base_type(expression());
        if (field_width_tp != integer_typep) error(INCOMPATIBLE_TYPES);

        /* optional precision spec */
        if (token == COLON) {
          get_token();
          precision_tp = base_type(expression());
          if (precision_tp != integer_typep) error(INCOMPATIBLE_TYPES);
       
        } /* end colon if */
      } /* end colon if */
      /* sync. Should be , or ) */
      synchronize(follow_parm_list, statement_end_list, NULL);
    } while (token == COMMA); /* end do */
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  } /* end if */
  else {
    if (rtn_idp->defn.info.routine.key == WRITE) error(WRONG_NUMBER_OF_PARMS);
  }

}                                                     /* end write_writeln */
/***************************************************************************/



/***************************************************************************/
/* print_println(rtn_idp) Process call to print or println                 */
/*                        Each actual parameter can be:                    */
/*                        <expr> or                                        */
/*                        <expr> : <expr> or                               */
/*                        <expr> : <expr> : <expr>                         */
/*    At this point, identical to write_writeln                            */

print_println(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;
{
  TYPE_STRUCT_PTR actual_parm_tp;             /* actual parm type */
  TYPE_STRUCT_PTR field_width_tp, precision_tp;

  /* params are optional for println */
  if (token == LPAREN) {
    do {
      get_token();
      actual_parm_tp = base_type(expression());

      if ((actual_parm_tp->form != SCALAR_FORM) &&
          (actual_parm_tp != logical_typep) &&
          (actual_parm_tp->form != STRING_FORM) &&
          (actual_parm_tp->form != ENUM_FORM))
        error(INVALID_EXPRESSION);

      /* optional field width expression */
      if (token == COLON) {
        get_token();
        field_width_tp = base_type(expression());
        if (field_width_tp != integer_typep) error(INCOMPATIBLE_TYPES);

        /* optional precision spec */
        if (token == COLON) {
          get_token();
          precision_tp = base_type(expression());
          if (precision_tp != integer_typep) error(INCOMPATIBLE_TYPES);
       
        } /* end colon if */
      } /* end colon if */
      /* sync. Should be , or ) */
      synchronize(follow_parm_list, statement_end_list, NULL);
    } while (token == COMMA); /* end do */
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  } /* end if */
  else {
    if (rtn_idp->defn.info.routine.key == WRITE) error(WRONG_NUMBER_OF_PARMS);
  }

}                                                     /* end print_println */
/***************************************************************************/



/***************************************************************************/
/* eof_eoln(rtn_idp)  Process call to eof or eoln. No parameters.          */
/* return boolean result.                                                  */

TYPE_STRUCT_PTR eof_eoln(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;
{
  TYPE_STRUCT_PTR result_tp = logical_typep;

  if (token == LPAREN) {
    error(WRONG_NUMBER_OF_PARMS);
    actual_parm_list(rtn_idp, FALSE);
  }
  return(result_tp);
}                                                          /* end eof_eoln */
/***************************************************************************/



/***************************************************************************/
/* system_etc()         Process call to system, etc                        */
/*                      fun('string')                                      */
/*              One string parameter, no return value                      */
/*    at entry, token is `fun'                                             */
/*    at exit, token is after closing )                                    */

system_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                 /* routine id */
{
  TYPE_STRUCT_PTR actual_parm_tp;         /* actual parm type */

  if (token == LPAREN) {
    get_token();     
    actual_parm_tp = base_type(expression());
    if (actual_parm_tp != string_typep &&
          (actual_parm_tp->form != STRING_FORM)) {
      error(INVALID_EXPRESSION);
    }
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else {
    error(WRONG_NUMBER_OF_PARMS);
  }

  return;
}                                                        /* end SYSTEM_ETC */
/***************************************************************************/



/***************************************************************************/
/* length_etc()         Process call to length, etc                        */
/*                      fun('string')                                      */
/*              One string parameter, integer return value                 */
/*    at entry, token is `fun'                                             */
/*    at exit, token is after closing )                                    */

TYPE_STRUCT_PTR length_etc()
{
  TYPE_STRUCT_PTR actual_parm_tp;         /* actual parm type */
  TYPE_STRUCT_PTR result_tp = integer_typep;  /* result type */
  if (token == LPAREN) {
    get_token();     
    actual_parm_tp = base_type(expression());
    if (actual_parm_tp != string_typep &&
          (actual_parm_tp->form != STRING_FORM)) {
      error(INVALID_EXPRESSION);
    }
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else {
    error(WRONG_NUMBER_OF_PARMS);
  }

  return(result_tp);
}                                                        /* end LENGTH_ETC */
/***************************************************************************/



/***************************************************************************/
/* hibound_etc()         Process call to hibound, etc                      */
/*                      fun(agg)                                           */
/*              One aggregate parameter, integer return value              */
/*    at entry, token is `fun'                                             */
/*    at exit, token is after closing )                                    */

TYPE_STRUCT_PTR hibound_etc()
{
  TYPE_STRUCT_PTR actual_parm_tp;         /* actual parm type */
  TYPE_STRUCT_PTR result_tp = integer_typep;  /* result type */
  if (token == LPAREN) {
    get_token();     
    actual_parm_tp = base_type(expression());
    if ((actual_parm_tp->form != ARRAY_FORM) &&
        (actual_parm_tp->form != BAG_FORM) &&
        (actual_parm_tp->form != LIST_FORM) &&
        (actual_parm_tp->form != SET_FORM) ) {
      error(INVALID_EXPRESSION);
    }
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else {
    error(WRONG_NUMBER_OF_PARMS);
  }

  return(result_tp);
}                                                       /* end HIBOUND_ETC */
/***************************************************************************/



/***************************************************************************/
/* rexpr_etc()         Process call to rexpr, etc                          */
/*                      fun('string', 'string')                            */
/*              Two string parameters, boolean return value                */
/*    at entry, token is `fun'                                             */
/*    at exit, token is after closing )                                    */

TYPE_STRUCT_PTR rexpr_etc()
{
  TYPE_STRUCT_PTR actual_parm_tp;         /* actual parm type */
  TYPE_STRUCT_PTR result_tp = logical_typep; /* result type */

  if (token == LPAREN) {
    get_token();     
    actual_parm_tp = base_type(expression());
    if (actual_parm_tp != string_typep && 
        actual_parm_tp->form != STRING_FORM) {
      error(INVALID_EXPRESSION);
    }
    if_token_get_else_error(COMMA, MISSING_COMMA);
    actual_parm_tp = base_type(expression());
    if (actual_parm_tp != string_typep && 
        actual_parm_tp->form != STRING_FORM) {
      error(INVALID_EXPRESSION);
    }
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else {
    error(WRONG_NUMBER_OF_PARMS);
  }

  return(result_tp);
}                                                         /* end REXPR_ETC */
/***************************************************************************/



/***************************************************************************/
/* exists_etc   Process call to exists, etc                                */
/*              fun(any) -> boolean                                        */
/*          any type parm -> boolean result                                */

TYPE_STRUCT_PTR exists_etc()
{
  TYPE_STRUCT_PTR parm_tp;                   /* actual param type */
  TYPE_STRUCT_PTR result_tp = logical_typep; /* result type */

  if (token == LPAREN) {
    get_token();
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else error(WRONG_NUMBER_OF_PARMS);

  return(result_tp);
}                                                        /* end EXISTS_ETC */
/***************************************************************************/



/***************************************************************************/
/* nvl_etc    Process NVL, etc                                             */
/*            fun(p1, p2) -> p1 or p2                                      */
/*            Two args, any type, returns one of them                      */

TYPE_STRUCT_PTR nvl_etc()
{
  TYPE_STRUCT_PTR parm_tp;                 /* actual param type */

  if (token == LPAREN) {
    get_token();
    parm_tp = base_type(expression());

    if_token_get_else_error(COMMA, MISSING_COMMA);
        /* PERHAPS SHOULD CHECK FOR ASSIGNMENT COMPATIBILITY */
/*
*    if (parm_tp != base_type(expression()) ) {
*      error(INCOMPATIBLE_TYPES);
*    }
*/
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else error(WRONG_NUMBER_OF_PARMS);

  return(parm_tp);
}                                                           /* end NVL_ETC */
/***************************************************************************/



/***************************************************************************/
/* abs_sqr  Process call to abs or sqr.                                    */
/*          integer parm -> integer result                                 */
/*          real parm -> real result                                       */

TYPE_STRUCT_PTR abs_sqr()
{
  TYPE_STRUCT_PTR parm_tp;                 /* actual param type */
  TYPE_STRUCT_PTR result_tp;               /* result type */

  if (token == LPAREN) {
    get_token();
    parm_tp = base_type(expression());

    if ((parm_tp != integer_typep) && (parm_tp != real_typep)) {
      error(INCOMPATIBLE_TYPES);
      result_tp = real_typep;
    }
    else result_tp = parm_tp;

    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else error(WRONG_NUMBER_OF_PARMS);

  return(result_tp);
}                                                           /* end abs_sqr */
/***************************************************************************/



/***************************************************************************/
/* arctan_cos_exp_ln_sin_sqrt  Process call to these                       */
/*          integer parm -> real result                                    */
/*          real parm -> real result                                       */

TYPE_STRUCT_PTR arctan_cos_exp_ln_sin_sqrt()
{
  TYPE_STRUCT_PTR parm_tp;                 /* actual param type */

  if (token == LPAREN) {
    get_token();
    parm_tp = base_type(expression());

    if ((parm_tp != integer_typep) && (parm_tp != real_typep)) {
      error(INCOMPATIBLE_TYPES);
    }
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else error(WRONG_NUMBER_OF_PARMS);

  return(real_typep);
}                                        /* end arctan_cos_exp_ln_sin_sqrt */
/***************************************************************************/



/***************************************************************************/
/* atan  Process call to these                                             */
/*              fun(p1, p2)                                                */
/*          integer parm -> real result                                    */
/*          real parm -> real result                                       */

TYPE_STRUCT_PTR atan()
{
  TYPE_STRUCT_PTR parm_tp;                 /* actual param type */

  if (token == LPAREN) {
    get_token();
    parm_tp = base_type(expression());

    if ((parm_tp != integer_typep) && (parm_tp != real_typep)) {
      error(INCOMPATIBLE_TYPES);
    }
    if_token_get_else_error(COMMA, MISSING_COMMA);
    parm_tp = base_type(expression());
    if ((parm_tp != integer_typep) && (parm_tp != real_typep)) {
      error(INCOMPATIBLE_TYPES);
    }
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else error(WRONG_NUMBER_OF_PARMS);

  return(real_typep);
}                                                              /* end ATAN */
/***************************************************************************/





/***************************************************************************/
/* odd                 Process call to odd.                                */
/*          integer parm -> boolean result                                 */

TYPE_STRUCT_PTR odd()
{
  TYPE_STRUCT_PTR parm_tp;                 /* actual param type */
  TYPE_STRUCT_PTR result_tp = logical_typep;

  if (token == LPAREN) {
    get_token();
    parm_tp = base_type(expression());

    if (parm_tp != integer_typep) {
      error(INCOMPATIBLE_TYPES);
    }

    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else error(WRONG_NUMBER_OF_PARMS);

  return(logical_typep);
}                                                               /* end odd */
/***************************************************************************/



/***************************************************************************/
/* round_trunc  Process call to round or trunc.                            */
/*          real parm -> integer result                                    */

TYPE_STRUCT_PTR round_trunc()
{
  TYPE_STRUCT_PTR parm_tp;                 /* actual param type */

  if (token == LPAREN) {
    get_token();
    parm_tp = base_type(expression());

    if (parm_tp != real_typep) {
      error(INCOMPATIBLE_TYPES);
    }

    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else error(WRONG_NUMBER_OF_PARMS);

  return(integer_typep);
}                                                       /* end round_trunc */
/***************************************************************************/



/***************************************************************************/
/* insert_etc   Process a call to INSERT, etc                              */
/*              list procedures                                            */
/*       INSERT(LIST, GENERIC, INTEGER)                                    */
/*       REMOVE(LIST, INTEGER)                                             */

insert_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;
{
  TYPE_STRUCT_PTR parm_tp;               /* actual parm type */

  if (token == LPAREN) {
    get_token();
    parm_tp = base_type(expression());
    if (parm_tp->form != LIST_FORM) {
      error(INCOMPATIBLE_TYPES);
    }
    if_token_get_else_error(COMMA, MISSING_COMMA);
    if (rtn_idp->defn.info.routine.key == XINSERT) {
      expression();
      if_token_get_else_error(COMMA, MISSING_COMMA);
    }
    parm_tp = base_type(expression());
    if (parm_tp != integer_typep) {
      error(INCOMPATIBLE_TYPES);
    }
    if_token_get_else_error(RPAREN, MISSING_RPAREN);
  }
  else error(WRONG_NUMBER_OF_PARMS);

}                                                        /* end INSERT_ETC */
/***************************************************************************/