summaryrefslogtreecommitdiff
path: root/support/ltx2x/l2xixstd.c
blob: 43f513e889c0616e10c885b6b497750a84809b8f (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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
/* l2xixstd.c  LTX2X interpreter standard procedure/function Executor routines */
/*  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 <stdlib.h>
#include <math.h> 
#include "l2xicmon.h"
#include "l2xierr.h"
#include "l2xiscan.h"
#include "l2xisymt.h"
#include "l2xiprse.h"
#include "l2xiidbg.h"
#include "l2xiexec.h"

#include "listsetc.h"


#define DEFAULT_NUMERIC_FIELD_WIDTH 10
#define DEFAULT_PRECISION 4
  /* added for ltx2x */
#define MAX_LTX2X_BUFFER 2000

/* EXTERNALS */


extern int level;
extern int exec_line_number;         /* no. of line executed */

extern ICT *code_segmentp;           /* code segment ptr */ /* used? */
extern TOKEN_CODE ctoken;            /* token from code segment */
 
extern STACK_ITEM *stack;                  /* runtime stack */
extern STACK_ITEM_PTR tos;                 /* ptr to top of runtime stack */
extern STACK_ITEM_PTR stack_frame_basep;   /* ptr to stack fame base */
extern STACK_ITEM_PTR stack_display[];     /*  ????????? */


extern BOOLEAN is_value_undef();

extern STRING get_stacked_string();

extern STACK_TYPE form2stack[];            /* map form type to stack type */
extern TYPE_FORM stack2form[];             /* map stack type to form type */

extern STACK_ITEM_PTR create_copy_value();

/* FORWARDS */

TYPE_STRUCT_PTR exec_eof_eoln(), exec_abs_sqr(),
                exec_arctan_cos_exp_ln_sin_sqrt(),
                exec_odd(), exec_round_trunc();
TYPE_STRUCT_PTR exec_atan(), exec_exists_etc(), exec_nvl_etc();
TYPE_STRUCT_PTR exec_rexpr_etc(), exec_hibound_etc(), exec_length_etc();

/* GLOBALS */

BOOLEAN eof_flag = FALSE;
char acbuffer[MAX_LTX2X_BUFFER];        /* added for ltx2x */



/************************************************************************/
/* exec_standard_routine_call(rtn_idp)  Execute a call to a standard    */
/*                                      procedure or function           */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_standard_routine_call(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  entry_debug("exec_standard_routine_call");

  switch (rtn_idp->defn.info.routine.key) {
    case READ:
    case READLN: {
      exec_read_readln(rtn_idp);
      exit_debug("exec_standard_routine_call");
      return(NULL);
    }
    case WRITE: 
    case WRITELN: {
      exec_write_writeln(rtn_idp);
      exit_debug("exec_standard_routine_call");
      return(NULL);
    }
    case EOFF: 
    case EOLN: {
      exit_debug("exec_standard_routine_call");
      return(exec_eof_eoln(rtn_idp));
    }
    case ABS:        /* real or int -> real or int */
              {
      exit_debug("exec_standard_routine_call");
      return(exec_abs_sqr(rtn_idp));
    }
    case COS:        /* real or int -> real */
    case EXP:
    case SIN:
    case SQRT:
    case XACOS:  
    case XASIN:
    case XLOG:
    case XLOG2:
    case XLOG10:
    case XTAN: {
      exit_debug("exec_standard_routine_call");
      return(exec_arctan_cos_exp_ln_sin_sqrt(rtn_idp));
    }
    case XATAN: {             /* extra for EXPRESS */
      exit_debug("exec_standard_routine_call");
      return(exec_atan(rtn_idp));
    }
    case ODD: {      /* int -> boolean */
      exit_debug("exec_standard_routine_call");
      return(exec_odd());
    }
    case ROUND:      /* real -> int */
    case TRUNC: {
      exit_debug("exec_standard_routine_call");
      return(exec_round_trunc(rtn_idp));
    }
    case L2XPRINT: 
    case L2XPRINTLN: {                         /* added for ltx2x */
      exec_print_println(rtn_idp);
      exit_debug("exec_standard_routine_call");
      return(NULL);
    }
    case L2XSYSTEM: {                          /* added for ltx2x */
      exec_system_etc(rtn_idp);
      exit_debug("exec_standard_routine_call");
      return(NULL);
    }
    case L2XREXPR: {                           /* added for ltx2x */
      exit_debug("exec_standard_routine_call");
      return(exec_rexpr_etc(rtn_idp));
    }
    case XHIBOUND:
    case XHIINDEX:
    case XLOBOUND:
    case XLOINDEX:
    case XSIZEOF: {
      exit_debug("exec_standard_routine_call");
      return(exec_hibound_etc(rtn_idp));
    }
    case XLENGTH: {
      exit_debug("exec_standard_routine_call");
      return(exec_length_etc(rtn_idp));
    }
    case XEXISTS: {
      exit_debug("exec_standard_routine_call");
      return(exec_exists_etc(rtn_idp));
    }
    case XNVL: {
      exit_debug("exec_standard_routine_call");
      return(exec_nvl_etc(rtn_idp));
    }
    case XINSERT:
    case XREMOVE: {
      exec_insert_etc(rtn_idp);
      exit_debug("exec_standard_routine_call");
      return(NULL);
    }
    case XBLENGTH:
    case XFORMAT:
    case XROLESOF:
    case XTYPEOF:
    case XUSEDIN:
    case XVALUE:
    case XVALUE_IN:
    case XVALUE_UNIQUE:  {               /* unimplemented EXPRESS stuff */
      runtime_error(UNIMPLEMENTED_RUNTIME_FEATURE);
      exit_debug("exec_standard_routine_call");
      return(NULL);
    }
    default: {
      runtime_error(UNIMPLEMENTED_RUNTIME_FEATURE);
      break;
    }

  } /* end switch */

  exit_debug("exec_standard_routine_call");
  return(NULL);
}                                     /* end exec_standard_routine_call */
/************************************************************************/



/************************************************************************/
/* exec_read_readln(rtn_idp)  Execute a call to READ or READLN          */

exec_read_readln(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  SYMTAB_NODE_PTR parm_idp;            /* param id */
  TYPE_STRUCT_PTR parm_tp;             /* param type */
  STACK_ITEM_PTR targetp;              /* ptr to read target */
  XPRSAINT i1;
  XPRSAREAL r1;
  int len;
  char ch;
  char tbuff[MAX_LTX2X_BUFFER];
  STRING lhs;

  entry_debug("exec_read_readln");

  /* params are optional for readln */
  get_ctoken();
  if (ctoken == LPAREN) {           /* id list */
    do {
      get_ctoken();
      parm_idp = get_symtab_cptr();
      parm_tp = base_type(exec_variable(parm_idp, VARPARM_USE));
      targetp = (STACK_ITEM_PTR) get_address(tos);
      pop();             /* pop off address */

      if (parm_tp == integer_typep) {
        scanf("%d", &i1); 
        put_integer(targetp, i1);
      }
      else if (parm_tp == real_typep) {
        scanf("%g", &r1); 
        put_real(targetp, r1);
      }
      else {             /* a string or a logical */
        scanf("%sMAX_LTX2X_BUFFER", tbuff);
        len = strlen(tbuff);
        sprintf(dbuffer, "strlen(str) = %d, str = %s\n", len, tbuff);
        debug_print(dbuffer);
        if (parm_tp == logical_typep) {   /* check which one */
          if (len == 4 && (tbuff[0] == 't' || tbuff[0] == 'T')) { /* TRUE */
            put_true(targetp);
          }
          else if (len == 5 && (tbuff[0] == 'f' || tbuff[0] == 'F')) {  /* FALSE */
            put_false(targetp);
          }
          else if (len == 7 && (tbuff[0] == 'u' || tbuff[0] == 'U')) { /* UNKNOWN */
            put_unknown(targetp);
          }
          else {   /* an error */
            runtime_error(INVALID_FUNCTION_ARGUMENT);
            put_unknown(targetp);
          }
	}
        else {             /* a string */
          free(targetp->value.string);
          lhs = alloc_bytes(len+1);
          sprintf(dbuffer, "lhs = %d", lhs);
          debug_print(dbuffer);
          strcpy(lhs, tbuff);
          sprintf(dbuffer, ", str = %s\n", lhs);
          debug_print(dbuffer);
          put_string(targetp, lhs);
        }
      }

      trace_data_store(parm_idp, parm_idp->typep, targetp, parm_tp);
    } while (ctoken == COMMA); /* end do */
    get_ctoken();           /* token after RPAREN */
  }

  if (rtn_idp->defn.info.routine.key == READLN) {
    do {
      ch = getchar();
    } while(!eof_flag && (ch != '\n'));
  }

  exit_debug("exec_read_readln");
  return;
}                                              /* end exec_read_readln  */
/************************************************************************/



/************************************************************************/
/* exec_write_writeln(rtn_idp)  Execute a call to WRITE or WRITELN      */
/*        Each actual parameter can be: <expr>                          */
/*                                  or  <expr> : <expr>                 */
/*                                  or  <expr> : <expr> : <expr>        */

exec_write_writeln(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;               /* parameter type */
  STACK_TYPE stype;
  XPRSAINT field_width;
  XPRSAINT precision;
  entry_debug("exec_write_writeln");

  /* parameters are optional for writeln */
  get_ctoken();
  if (ctoken == LPAREN) {
    do {
      /* push value */
      get_ctoken();
      parm_tp = base_type(exec_expression());

      /* check if dynamic agg */
      if (is_dynagg(parm_tp)) parm_tp = parm_tp->info.dynagg.elmt_typep;

      field_width = DEFAULT_NUMERIC_FIELD_WIDTH;
      precision = DEFAULT_PRECISION;

      /* optional field width expresion */
      if (ctoken == COLON) {
        get_ctoken();
        exec_expression();
        if (!is_value_undef(tos)) {
          field_width = get_integer(tos);
        }
        pop();                      /* field width */

        /* optional decimal places expresion */
        if (ctoken == COLON) {
          get_ctoken();
          exec_expression();
          if (!is_value_undef(tos)) {
            precision = get_integer(tos);
          }
          pop();                      /* precision */
        }
      }

      if (parm_tp->form == ARRAY_FORM) {  /* array, address on top of stack */
        if (get_stackval_type(tos) == STKADD) {
          copy_value(tos, get_address(tos));
        }
      }

     stype = get_stackval_type(tos);

      /* write value */
      if (is_value_undef(tos)) {
        printf("%*c", field_width, get_undef(tos));
      }
      else if (stype == STKINT) {
        printf("%*d", field_width, get_integer(tos));
      }
      else if (stype == STKREA) {
        printf("%*.*g", field_width, precision, get_real(tos));
      }
      else if (stype == STKLOG) {
        field_width = 0;
        switch (get_logical(tos)) {
          case TRUE_REP: {
            printf("%*s", -field_width, "TRUE");
            break;
	  }
          case FALSE_REP: {
            printf("%*s", -field_width, "FALSE");
            break;
	  }
          case UNKNOWN_REP: {
            printf("%*s", -field_width, "UNKNOWN");
            break;
          }
          default: {
            printf("%*s", -field_width, "??UNKNOWN??");
            break;
	  }
        } /* end switch */
      }
      else if (parm_tp == string_typep || parm_tp->form == STRING_FORM) {
        field_width = 0;
        printf("%*s", -field_width, get_stacked_string(tos) );
      }

      pop();     /* value */
    } while (ctoken == COMMA);  /* end do */

    get_ctoken();      /* token after RPAREN */
  } /* end of if over parameters */

  if (rtn_idp->defn.info.routine.key == WRITELN) putchar('\n');

  exit_debug("exec_write_writeln");
  return;
}                                            /* end exec_write_writeln  */
/************************************************************************/



/************************************************************************/
/* exec_print_println(rtn_idp)  Execute a call to PRINT or PRINTLN      */
/*        Each actual parameter can be: <expr>                          */
/*                                  or  <expr> : <expr>                 */
/*                                  or  <expr> : <expr> : <expr>        */
/*  Identical to exec_write_writeln, except output is to ltx2x myprint  */

exec_print_println(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;               /* parameter type */
  STACK_TYPE stype;
  XPRSAINT field_width;
  XPRSAINT precision;
  entry_debug("exec_print_println");

  /* parameters are optional for println */
  get_ctoken();
  if (ctoken == LPAREN) {
    do {
      /* push value */
      get_ctoken();
      parm_tp = base_type(exec_expression());

      /* check if dynamic agg */
      if (is_dynagg(parm_tp)) {
         parm_tp = parm_tp->info.dynagg.elmt_typep;
       }

      field_width = DEFAULT_NUMERIC_FIELD_WIDTH;
      precision = DEFAULT_PRECISION;


      /* optional field width expresion */
      if (ctoken == COLON) {
        get_ctoken();
        exec_expression();
        if (!is_value_undef(tos)) {
          field_width = get_integer(tos);
        }
        pop();                      /* field width */

        /* optional decimal places expresion */
        if (ctoken == COLON) {
          get_ctoken();
          exec_expression();
          if (!is_value_undef(tos)) {
            precision = get_integer(tos);
          }
          pop();                      /* precision */
        }
      }

      if (parm_tp->form == ARRAY_FORM) {   /* array, address on top of stack */
        if (get_stackval_type(tos) == STKADD) {
          copy_value(tos, get_address(tos));
        }
      }
      
      stype = get_stackval_type(tos);
      /* write value */
      if (is_value_undef(tos)) {
        sprintf(acbuffer, "%*c", field_width, get_undef(tos));
      }
      else if (stype == STKINT) {
        sprintf(acbuffer, "%*d", field_width, get_integer(tos));
      }
      else if (stype == STKREA) {
        sprintf(acbuffer, "%*.*g", field_width, precision, get_real(tos));
      }
      else if (stype == STKLOG) {
        field_width = 0;
        switch (get_logical(tos)) {
          case TRUE_REP: {
            sprintf(acbuffer, "%*s", -field_width, "TRUE");
            break;
	  }
          case FALSE_REP: {
            sprintf(acbuffer, "%*s", -field_width, "FALSE");
            break;
	  }
          case UNKNOWN_REP: {
            sprintf(acbuffer, "%*s", -field_width, "UNKNOWN");
            break;
	  }
          default: {
            sprintf(acbuffer, "%*s", -field_width, "??UNKNOWN??");
            break;
	  }
        } /* end switch */
      }
      else if (parm_tp == string_typep || parm_tp->form == STRING_FORM) {
        field_width = 0;
        sprintf(acbuffer, "%*s", -field_width, get_stacked_string(tos) );
      }
      myprint(acbuffer);
      pop();     /* value */
    } while (ctoken == COMMA);  /* end do */

    get_ctoken();      /* token after RPAREN */
  } /* end of if over parameters */

  if (rtn_idp->defn.info.routine.key == L2XPRINTLN) myprint("\n");

  exit_debug("exec_print_println");
  return;
}                                            /* end exec_print_println  */
/************************************************************************/



/************************************************************************/
/* exec_insert_etc(rtn_idp)  Execute a call to procedure INSERT, etc    */
/*        INSERT(<list>, <item>, <posn>)                                */
/*        REMOVE(<list>, <posn>)                                        */
/*  at entry: ctoken is `proc'                                          */
/*  at exit:  ctoken is the token after the closing )                   */

exec_insert_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;               /* parameter type */
  LBS_PTR list;
  LBS_NODE_PTR nod;
  STACK_ITEM_PTR pitem;
  XPRSAINT pos;
  int code = rtn_idp->defn.info.routine.key;

  entry_debug("exec_insert_etc (l2xixstd.c)");

  /* first parameter */
  get_ctoken();       /* should be ( */
  get_ctoken();       /* should be param 1 */
  parm_tp = base_type(exec_expression());
  if (parm_tp->form != LIST_FORM) {
    runtime_error(INVALID_FUNCTION_ARGUMENT);
  }
  list = (LBS_PTR) get_address_type(tos, STKLST);
  sprintf(dbuffer, "list = %d\n", list);
  debug_print(dbuffer);
  pop();  /* first parm */
  get_ctoken();    /* start of next parameter */

  if (code == XINSERT) {             /* do INSERT second param */
    exec_expression();
    pitem = create_copy_value(tos);
    sprintf(dbuffer, "pitem = %d\n", pitem);
    debug_print(dbuffer);
    get_ctoken();   /* start of next parameter */
  }

     /* final parameter */
  parm_tp = base_type(exec_expression());
  pos = get_integer(tos);
  pop();  /* last parm */
  get_ctoken();     /* token after closing ) */

  switch (code) {
    case XINSERT: {
      nod = lbs_insert(list, (genptr) pitem, pos);
      sprintf(dbuffer, "inserted node = %d, with data = %d, at pos = %d, into list = %d\n", 
                        nod, pitem, pos, list);
      debug_print(dbuffer);
      pop(); /* middle parm */
      break;
    }
    case XREMOVE: {
      nod = lbs_remove(list, pos);
      sprintf(dbuffer, "removed node = %d\n", nod);
      debug_print(dbuffer);
      break;
    }
  } /* end switch */

  exit_debug("exec_insert_etc");
  return;
}                                               /* end EXEC_INSERT_ETC  */
/************************************************************************/



/************************************************************************/
/* exec_eof_eoln(rtn_idp)  Execute a call to EOF or EOLN                */
/*                         No parameters => boolean result              */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_eof_eoln(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  char ch = getchar();
  entry_debug("exec_eof_eoln");
  
  switch (rtn_idp->defn.info.routine.key) {
    case EOFF: {
      if (eof_flag || feof(stdin)) {
        eof_flag = TRUE;
        push_true();
      }
      else {
        push_false();
        ungetc(ch, stdin);
      }
      break;
    }
    case EOLN: {
      if (eof_flag || feof(stdin)) {
        eof_flag = TRUE;
        push_true();
      }
      else {
        push_logical(ch == '\n' ? TRUE_REP : FALSE_REP);
        ungetc(ch, stdin);
      }
      break;
    }
  } /* end switch */

  get_ctoken();             /* token after function name */

  exit_debug("exec_eof_eoln");
  return(logical_typep);
}                                                 /* end exec_eof_eoln  */
/************************************************************************/



/************************************************************************/
/* exec_system_etc(rtn_idp)    Execute a call to system, etc            */
/*                         fun('string')                                */
/*                         String parameter, no result                  */
/*    at entry, ctoken is `fun'                                         */
/*    at exit, ctoken is token after closing )                          */

exec_system_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                      /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;                    /* actual param type */
  entry_debug("exec_system_etc");

  get_ctoken();             /* should be ( */
  get_ctoken();             /* start of param */
  parm_tp = base_type(exec_expression());
  if (parm_tp->form != STRING_FORM) {
    runtime_error(INVALID_FUNCTION_ARGUMENT);
  }
  else {
    switch (rtn_idp->defn.info.routine.key) {
      case L2XSYSTEM : {
        system(get_stacked_string(tos));
        break;
      }
      default : {
        runtime_error(UNIMPLEMENTED_RUNTIME_FEATURE);
        break; 
      }
    } /* end switch */
  }

  get_ctoken();         /* token after closing ) */
  exit_debug("exec_system_etc");
  return;
}                                                /* end EXEC_SYSTEM_ETC */
/************************************************************************/



/************************************************************************/
/* exec_rexpr_etc(rtn_idp)  Execute a call to REXPR, etc                */
/*           In general, any function fun(p1, p2) that:                 */
/*           p1 and p2 are strings --> boolean result                   */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_rexpr_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm1_tp, parm2_tp;       /* actual param types */
  TYPE_STRUCT_PTR result_tp = logical_typep;
  STRING parm1, parm2;              /* parameters */
  BOOLEAN undef_parm = FALSE;
  int code = rtn_idp->defn.info.routine.key;
  int result;
  entry_debug("exec_rexpr_etc (l2xixstd.c)");

  get_ctoken();    /* LPAREN */
  get_ctoken();    /* start of first parameter */
  parm1_tp = base_type(exec_expression());
  if (is_value_undef(tos)) {
    undef_parm = TRUE;
  }
  else {
    parm1 = get_stacked_string(tos);
  }
/*  get_ctoken();    COMMA */
  get_ctoken();    /* start of second parameter */
  parm2_tp = base_type(exec_expression());
  if (is_value_undef(tos)) {
    undef_parm = TRUE;
  }
  else {
    parm2 = get_stacked_string(tos);
  }
  pop();

  if (code == L2XREXPR) {  /* parm1 = string, parm2 = pattern */
    if (undef_parm) {
      put_undef(tos);
    }
    else { 
      result = rexpr(parm1, parm2);
      if (result < 0) {
        runtime_error(INVALID_REGULAR_EXPRESSION);
        put_undef(tos);
      }
      else if (result == 0) {
        put_false(tos);
      }
      else {
        put_true(tos);
      }
    }
  }

  get_ctoken();   /* token after RPAREN */

  exit_debug("exec_rexpr_etc");
  return(result_tp);
}                                                 /* end EXEC_REXPR_ETC */
/************************************************************************/



/************************************************************************/
/* exec_hibound_etc(rtn_idp)  Execute a call to HIBOUND, etc            */
/*                   agg type -> integer                                */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_hibound_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;                       /* actual param type */
  TYPE_STRUCT_PTR result_tp = integer_typep;           /* result type */
  XPRSAINT result = 0;
  STACK_TYPE stype;
  TYPE_FORM ftype;
  int code = rtn_idp->defn.info.routine.key;
  entry_debug("exec_hibound_etc");

  get_ctoken();     /* LPAREN */
  get_ctoken();
  parm_tp = base_type(exec_expression());
  if (is_value_undef(tos)) {
    put_undef(tos);
    get_ctoken();   /* token after RPAREN */
    exit_debug("exec_hibound_etc");
    return(result_tp);
  }
  ftype = parm_tp->form;
  if ((ftype != ARRAY_FORM) &&
      (ftype != BAG_FORM) &&
      (ftype != LIST_FORM) &&
      (ftype != SET_FORM) ) {
    runtime_error(INVALID_FUNCTION_ARGUMENT);
    put_undef(tos);
    get_ctoken();   /* token after RPAREN */
    exit_debug("exec_hibound_etc");
    return(result_tp);
  }

  stype = get_stackval_type(tos);
  if (stype != form2stack[ftype] &&
      stype != STKADD) {
    stack_warning(form2stack[ftype], stype);
  }

  switch (code) {
    case XHIBOUND: {                         /* declared upper bound */
      if (parm_tp->form == ARRAY_FORM) {
        result = parm_tp->info.array.max_index;
      }
      else {
        result = parm_tp->info.dynagg.max_index;
      }
      break;
    }
    case XHIINDEX: {           /* declared array upper bound, or # of elements */
      if (parm_tp->form == ARRAY_FORM) {
        result = parm_tp->info.array.max_index;
      }
      else {
        result = NELS((LBS_PTR) get_address_type(tos, stype));
      }
      break;
    }
    case XLOBOUND: {                         /* declared lower bound */
      if (parm_tp->form == ARRAY_FORM) {
        result = parm_tp->info.array.min_index;
      }
      else {
        result = parm_tp->info.dynagg.min_index;
      }
      break;
    }
    case XLOINDEX: {                       /* declared array lower bound, or 1 */
      if (parm_tp->form == ARRAY_FORM) {
        result = parm_tp->info.array.min_index;
      }
      else {
        result = 1;
      }
      break;
    }
    case XSIZEOF: {                         /* # of actual elements */
      if (parm_tp->form == ARRAY_FORM) {
        result = parm_tp->info.array.max_index - parm_tp->info.array.min_index + 1;
      }
      else {
        result = NELS((LBS_PTR) get_address_type(tos, stype));
      }
      break;
    }
   
    default: {           /* should not be here */
      runtime_error(UNIMPLEMENTED_RUNTIME_FEATURE);
      get_ctoken();
      put_undef(tos);
      exit_debug("exec_hibound_etc");
      return(result_tp);
    }
  } /* end switch */

  get_ctoken();   /* token after RPAREN */

  put_integer(tos, result);
  exit_debug("exec_hibound_etc");
  return(result_tp);
}                                               /* end EXEC_HIBOUND_ETC */
/************************************************************************/



/************************************************************************/
/* exec_length_etc(rtn_idp)    Execute a call to LENGTH, etc            */
/*                         fun('string')                                */
/*                         String parameter, integer result             */
/*    at entry, ctoken is `fun'                                         */
/*    at exit, ctoken is token after closing )                          */

TYPE_STRUCT_PTR exec_length_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                      /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;                    /* actual param type */
  TYPE_STRUCT_PTR result_tp;                  /* returned type */
  XPRSAINT result = 0;
  entry_debug("exec_length_etc (l2xixstd.c)");

  get_ctoken();             /* should be ( */
  get_ctoken();             /* start of param */
  parm_tp = base_type(exec_expression());
  if (parm_tp->form != STRING_FORM) {
    runtime_error(INVALID_FUNCTION_ARGUMENT);
  }
  else {
    switch (rtn_idp->defn.info.routine.key) {
      case XLENGTH : {                             /* # of chars in a string */
        result = (XPRSAINT) strlen(get_stacked_string(tos));
        break;
      }
      default : {
        runtime_error(UNIMPLEMENTED_RUNTIME_FEATURE);
        break; 
      }
    } /* end switch */
  }

  get_ctoken();         /* token after closing ) */
  put_integer(tos, result);
  exit_debug("exec_length_etc");
  return(result_tp);
}                                                /* end EXEC_LENGTH_ETC */
/************************************************************************/



/************************************************************************/
/* exec_exists_etc(rtn_idp)  Execute a call to EXISTS, etc              */
/*                   any type -> boolean                                */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_exists_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;             /* actual param type */
  TYPE_STRUCT_PTR result_tp;           /* result type */
  int code = rtn_idp->defn.info.routine.key;
  entry_debug("exec_exists_etc");

  get_ctoken();     /* LPAREN */
  get_ctoken();
  parm_tp = base_type(exec_expression());

  if (code == XEXISTS) {
    if (is_value_undef(tos)) {
      put_true(tos);
    }
    else {
      put_false(tos);
    }
  }

  get_ctoken();   /* token after RPAREN */

  exit_debug("exec_exists_etc");
  return(logical_typep);
}                                                /* end EXEC_EXISTS_ETC */
/************************************************************************/



/************************************************************************/
/* exec_nvl_etc(rtn_idp)  Execute a call to NVL, etc                    */
/*           In general, any function fun(p1, p2) that:                 */
/*           any compatible params --> compatible result                */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_nvl_etc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm1_tp, parm2_tp;       /* actual param types */
  TYPE_STRUCT_PTR result_tp;
  STACK_ITEM_PTR parm1, parm2;              /* parameters */
  int code = rtn_idp->defn.info.routine.key;
  entry_debug("exec_nvl_etc");

  get_ctoken();    /* LPAREN */
  get_ctoken();    /* start of first parameter */
  parm1_tp = base_type(exec_expression());
  parm1 = tos;
  get_ctoken();    /* COMMA */
  get_ctoken();    /* start of second parameter */
  parm2_tp = base_type(exec_expression());
  parm2 = tos;


  if (code == XNVL) {
    if (is_value_undef(parm1)) {
      copy_value(parm1, parm2);
      result_tp = parm2_tp;
    }
    else {
      result_tp = parm1_tp;
    }
  }

  get_ctoken();   /* token after RPAREN */

  exit_debug("exec_nvl_etc");
  return(result_tp);
}                                                   /* end EXEC_NVL_ETC */
/************************************************************************/



/************************************************************************/
/* exec_abs_sqr(rtn_idp)  Execute a call to ABS or SQR                  */
/*                         Integer --> integer result                   */
/*                         real --> real result                         */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_abs_sqr(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;             /* actual param type */
  TYPE_STRUCT_PTR result_tp;           /* result type */
  XPRSAINT i1;
  XPRSAREAL r1;
  int code = rtn_idp->defn.info.routine.key;
  entry_debug("exec_abs_sqr");

  get_ctoken();     /* LPAREN */
  get_ctoken();
  parm_tp = base_type(exec_expression());

  if (is_value_undef(tos)) {
    ;
  }
  if (code == ABS) {
    if (parm_tp == integer_typep) {
       i1 = get_integer(tos);
       if (i1 >= 0) {
         put_integer(tos, i1);
       }
       else {
         put_integer(tos, -i1);
       }
    }
    else {
      r1 = (XPRSAREAL) fabs((double) get_real(tos));
      put_real(tos, r1);
    }
  }

  get_ctoken();   /* token after RPAREN */

  exit_debug("exec_abs_sqr");
  return(parm_tp);
}                                                  /* end exec_abs_sqr  */
/************************************************************************/



/************************************************************************/
/* exec_arctan_cos_exp_ln_sin_sqrt(rtn_idp)  Execute a call to ARCTAN,  */
/*                  COS, EXP, LN, SIN or SQRT                           */
/*           In general, any function fun(p1) that:                     */
/*           integer or real param --> real result                      */
/* return a pointer to the type stucture of the call                    */
/* NOTE calling C library routines acos() and asin() give wierd interp error */

TYPE_STRUCT_PTR exec_arctan_cos_exp_ln_sin_sqrt(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm_tp;       /* actual param type */
  int code = rtn_idp->defn.info.routine.key;
  XPRSAREAL r1, r2;
  entry_debug("exec_arctan_cos_exp_ln_sin_sqrt");

  get_ctoken();    /* LPAREN */
  get_ctoken();
  parm_tp = base_type(exec_expression());

  if (is_value_undef(tos)) {
    get_ctoken();   /* token after RPAREN */
    exit_debug("exec_arctan_cos_exp_ln_sin_sqrt");
    return(real_typep);
  }
  
  if (parm_tp == integer_typep) {
    put_real(tos, (XPRSAREAL) get_integer(tos));
  }

  r1 = (double) get_real(tos);

         /* check input value */
  if (((code == SQRT) && (r1 < 0.0)) ||
      ((code == XACOS) && (r1 < -1.0 || r1 > 1.0)) ||
      ((code == XASIN) && (r1 < -1.0 || r1 > 1.0)) ||
      ((code == XLOG) && (r1 <= 0.0)) ||
      ((code == XLOG2) && (r1 <= 0.0)) ||
      ((code == XLOG10) && (r1 <= 0.0)) ) {
    runtime_error(INVALID_FUNCTION_ARGUMENT);
    exit_debug("exec_arctan_cos_exp_ln_sin_sqrt");
  }
  else {
    switch (rtn_idp->defn.info.routine.key) {
      case COS: {
        put_real(tos, (XPRSAREAL) cos(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (COS)");
        break;
      }
      case EXP: {
        put_real(tos, (XPRSAREAL) exp(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (EXP)");
        break;
      }
      case SIN: {
        put_real(tos, (XPRSAREAL) sin(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (SIN)");
        break;
      }
      case SQRT: {
        put_real(tos, (XPRSAREAL) sqrt(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (SQRT)");
        break;
      }
      case XACOS: {           
        put_real(tos, (XPRSAREAL) acos(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (ACOS)");
        break;
      }
      case XASIN: {
        put_real(tos, (XPRSAREAL) asin(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (ASIN)");
        break;
      }
      case XLOG: {
        put_real(tos, (XPRSAREAL) log(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (LOG)");
        break;
      }
      case XLOG2: {    /* log_a(x) = ln(x)/ln(a) : ln(2) = 0.6931 47180 55994 */
        put_real(tos, (1.442695 * ((XPRSAREAL) log(r1))));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (LOG2)");
        break;
      }
      case XLOG10: {
        put_real(tos, (XPRSAREAL) log10(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (LOG10)");
        break;
      }
      case XTAN: {
        put_real(tos, (XPRSAREAL) tan(r1));
  exit_debug("exec_arctan_cos_exp_ln_sin_sqrt (TAN)");
        break;
      }
    } /* end switch */
  }

  get_ctoken();   /* token after RPAREN */

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



/************************************************************************/
/* exec_atan(rtn_idp)  Execute a call to ATAN,                          */
/*           In general, any function fun(p1, p2) that:                 */
/*           integer or real param --> real result                      */
/* return a pointer to the type stucture of the call                    */
/*   NOTE: Calling C library function atan2() gives wierd interp. error */

TYPE_STRUCT_PTR exec_atan(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  TYPE_STRUCT_PTR parm1_tp, parm2_tp;       /* actual param type */
  TYPE_STRUCT_PTR result_tp;
  STACK_ITEM_PTR parm1, parm2;
  int code = rtn_idp->defn.info.routine.key;
  XPRSAREAL r1;
  XPRSAREAL r2;
  entry_debug("exec_atan");

  get_ctoken();    /* LPAREN */
  get_ctoken();    /* start of first parameter */
  parm1_tp = base_type(exec_expression());
  parm1 = tos;

  get_ctoken();    /* COMMA */
  get_ctoken();    /* start of second parameter */
  parm2_tp = base_type(exec_expression());
  parm2 = tos;

  if (code == XATAN) {
    if (is_value_undef(parm1) || is_value_undef(parm2)) {
      put_undef(parm1);
    }
    else {
      if (parm1_tp == integer_typep) {
        put_real(parm1, (XPRSAREAL) get_integer(parm1));
      }
      r1 = get_real(parm1);
      if (parm2_tp == integer_typep) {
        put_real(parm2, (XPRSAREAL) get_integer(parm2));
      }
      r2 = get_real(parm2);
      if (r1 == 0.0 && r2 == 0.0) {
        runtime_error(INVALID_FUNCTION_ARGUMENT);
      }
      else {
        r1 = (double) r1;
        r2 = (double) r2;
        put_real(parm1, (XPRSAREAL) atan2(r1, r2));
      }
    }
  }

  pop();
  get_ctoken();   /* token after RPAREN */

  exit_debug("exec_atan");
  return(real_typep);
}                                                     /* end EXEC_ATAN  */
/************************************************************************/



/************************************************************************/
/* exec_odd()  Execute a call to ODD                                    */
/*                    integer param --> boolean result                  */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_odd()
{
  XPRSAINT i1;
  entry_debug("exec_odd");
  get_ctoken();  /* LPAREN */
  get_ctoken();
  exec_expression();

  if (!is_value_undef(tos)) {
    i1 = get_integer(tos);
    i1 &= 1;
    if (i1 == 0) {
      put_false(tos);
    }
    else {
      put_true(tos);
    }
  }

  get_ctoken();  /* after RPAREN */

  exit_debug("exec_odd");
  return(logical_typep);
}                                                      /* end exec_odd  */
/************************************************************************/



/************************************************************************/
/* exec_round_trunc(rtn_idp)  Execute a call to ROUND or TRUNC          */
/*                            real param --> integer result             */
/* return a pointer to the type stucture of the call                    */

TYPE_STRUCT_PTR exec_round_trunc(rtn_idp)
SYMTAB_NODE_PTR rtn_idp;                  /* routine id */
{
  XPRSAREAL r1;
  XPRSAINT i1;

  entry_debug("exec_round_trunc");
  get_ctoken();  /* LPAREN */
  get_ctoken();
  exec_expression();

  if (!is_value_undef(tos)) {
    r1 = get_real(tos);
    if (rtn_idp->defn.info.routine.key == ROUND) {
      i1 = r1 > 0.0
                   ? (XPRSAINT) (r1 + 0.5)
	           : (XPRSAINT) (r1 - 0.5);
    }
    else {
      i1 = (XPRSAINT) r1;
    }
    put_integer(tos, i1);
  }

  get_ctoken();  /* after RPAREN */

  exit_debug("exec_round_trunc");
  return(integer_typep);
}                                              /* end exec_round_trunc  */
/************************************************************************/