summaryrefslogtreecommitdiff
path: root/support/ltx2x/l2xisymt.c
blob: 2c69f735fefee5b5f9bc6dbbd39c78c3c048d608 (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
/* l2xisymt.c  LTX2X interpreter symbol table code */
/* BASED ON: symtab.c  (common names altered) */
/*           added PRINT and PRINTLN to init_symtab */

#include <stdio.h>
#include <time.h>
#include "l2xicmon.h"
#include "l2xierr.h"
#include "l2xisymt.h"
#include "l2xiidbg.h"
#include "l2xiexec.h"


/* EXTERNALS */

extern int level;

extern int DEBUG;

/* GLOBALS */

SYMTAB_NODE_PTR symtab_display[MAX_NESTING_LEVEL];

TYPE_STRUCT_PTR integer_typep, real_typep,   /* predefined types */
                boolean_typep;
TYPE_STRUCT_PTR logical_typep, string_typep,
                binary_typep, generic_typep,
                any_typep;

SYMTAB_NODE_PTR false_idp, true_idp, unknown_idp; /* logical constants */
SYMTAB_NODE_PTR conste_idp, pi_idp;               /* real constants */
SYMTAB_NODE_PTR undef_idp;                        /* `?' constant */
SYMTAB_NODE_PTR day_idp, month_idp, year_idp;

SYMTAB_NODE_PTR string_idp;

TYPE_STRUCT dummy_type = {              /* for bad type defs */
  NO_FORM, /* form */
  0,       /* size */
  NULL     /* type id */
  };


/***************************************************************************/
/* search_symtab(name, np)  Search for a name in the symbol table.         */
/* return pointer to entry if found, else NULL.                            */

SYMTAB_NODE_PTR search_symtab(name, np)
char *name;                       /* name to look for */
SYMTAB_NODE_PTR np;               /* pointer to symtab root */
{
  int cmp;
  entry_debug("search_symtab");

  /* loop to check each node. If not found, continue down the left */
  /* or right subtree. Return when a match */

  while (np != NULL) {
    cmp = strcmp(name, np->name);
    if (cmp == 0) {                 /* got it */
      exit_debug("search_symtab at found");
      return(np);    
    }
    np = cmp < 0 ? np->left : np->right;  /* continue */
  }

  exit_debug("search_symtab at not found");
  return(NULL);  /* not found */
}                                                     /* end search_symtab */
/***************************************************************************/



/***************************************************************************/
/* search_symtab_display(name)  Search all the symbol tables in the        */
/*                              symbol table display for a name.           */
/* return the pointer to the entry if found, else NULL                     */

SYMTAB_NODE_PTR search_symtab_display(name) 
char *name;
{
  short i;
  SYMTAB_NODE_PTR np;    /* pointer to symbol table node */
  entry_debug("search_symtab_display");

  for (i = level; i >= 0; --i) {
    np =search_symtab(name, symtab_display[i]);
    if (np != NULL) {
      exit_debug("search_symtab_display at found");
      return(np);
    }
  }

  exit_debug("search_symtab_display at not found");
  return(NULL);
}                                             /* end search_symtab_display */
/***************************************************************************/



/***************************************************************************/
/* enter_symtab(name, npp) Enter a name into the symtab                    */
/* return pointer to the entry                                             */

SYMTAB_NODE_PTR enter_symtab(name, npp)
char *name;                             /* name to enter */
SYMTAB_NODE_PTR *npp;                   /* ptr to ptr to symtab root */
{
  int cmp;                        /* result of strcmp */
  SYMTAB_NODE_PTR new_nodep;      /* ptr to new entry */
  SYMTAB_NODE_PTR np;             /* ptr to node to test */
  entry_debug("enter_symtab");

  /* create the new node for name */
  new_nodep = alloc_struct(SYMTAB_NODE);
  new_nodep->name = alloc_bytes(strlen(name) + 1);
  strcpy(new_nodep->name, name);
  new_nodep->left = new_nodep->right = new_nodep->next = NULL;
  new_nodep->info = NULL;
  new_nodep->defn.key = UNDEFINED;
  new_nodep->typep = NULL;   
  new_nodep->level = level;
  new_nodep->label_index = 0;

  /* loop to find the insertion point (binary tree) */
  while ((np = *npp) != NULL) {
    cmp = strcmp(name, np->name);
    npp = cmp < 0 ? &(np->left) : &(np->right);
  }
  *npp = new_nodep;    /* replace */

  exit_debug("enter_symtab");
  return(new_nodep);
}                                                      /* end enter_symtab */
/***************************************************************************/



/***************************************************************************/
/* init_symtab()  Initialise the symtab with predefined identifiers,       */
/*                types, and routines                                      */

init_symtab()   /* ---------------------- in final */
{
  SYMTAB_NODE_PTR integer_idp, real_idp, boolean_idp;
  SYMTAB_NODE_PTR logical_idp, binary_idp,
                  generic_idp, anytype_idp;

  int temp;
  time_t tp1, tp2;
  struct tm *ts;

  /* temporarily switch off any debugging output */
  temp = DEBUG;
  DEBUG = 0;

  /* initialise the level 0 (the global?) symbol table */
  symtab_display[0] = NULL; 

  /* predefined Pascal types */
  enter_name_local_symtab(integer_idp, "integer");
  enter_name_local_symtab(real_idp, "real");
  enter_name_local_symtab(boolean_idp, "boolean");
  /* and extra EXPRESS types */
  enter_name_local_symtab(logical_idp, "logical");
  enter_name_local_symtab(string_idp, "string");
  enter_name_local_symtab(binary_idp, "binary");
  enter_name_local_symtab(generic_idp, "generic");
  enter_name_local_symtab(anytype_idp, "???????");

  integer_typep = alloc_struct(TYPE_STRUCT);
  real_typep = alloc_struct(TYPE_STRUCT);
  boolean_typep = alloc_struct(TYPE_STRUCT);
  logical_typep = alloc_struct(TYPE_STRUCT);
  string_typep = alloc_struct(TYPE_STRUCT);
  binary_typep = alloc_struct(TYPE_STRUCT);
  generic_typep = alloc_struct(TYPE_STRUCT);
  any_typep = alloc_struct(TYPE_STRUCT);

  integer_idp->defn.key = TYPE_DEFN;
  integer_idp->typep = integer_typep;
  integer_typep->form = SCALAR_FORM;
  integer_typep->size = sizeof(STACK_ITEM);
  integer_typep->type_idp = integer_idp;

  real_idp->defn.key = TYPE_DEFN;
  real_idp->typep = real_typep;
  real_typep->form = SCALAR_FORM;
  real_typep->size = sizeof(STACK_ITEM);
  real_typep->type_idp = real_idp;

  boolean_idp->defn.key = TYPE_DEFN;
  boolean_idp->typep = boolean_typep;
  boolean_typep->form = ENUM_FORM;
  boolean_typep->size = sizeof(STACK_ITEM);
  boolean_typep->type_idp = boolean_idp;
  boolean_typep->info.enumeration.max = 1;
  boolean_idp->typep->info.enumeration.const_idp = false_idp;

  logical_idp->defn.key = TYPE_DEFN;
  logical_idp->typep = boolean_typep;
  logical_typep->form = ENUM_FORM;
  logical_typep->size = sizeof(STACK_ITEM);
  logical_typep->type_idp = logical_idp;
  logical_typep->info.enumeration.max = 2;
  logical_idp->typep->info.enumeration.const_idp = unknown_idp;

  string_idp->defn.key = TYPE_DEFN;
  string_idp->typep = string_typep;
  string_typep->form = STRING_FORM;
  string_typep->size = sizeof(STACK_ITEM);
  string_typep->type_idp = string_idp;
  string_typep->info.string.max_length = MAX_EXPRESS_STRING;
  string_typep->info.string.length = 0;

  binary_idp->defn.key = TYPE_DEFN;
  binary_idp->typep = binary_typep;
  binary_typep->form = SCALAR_FORM;
  binary_typep->size = sizeof(STACK_ITEM);
  binary_typep->type_idp = binary_idp;

  generic_idp->defn.key = TYPE_DEFN;
  generic_idp->typep = generic_typep;
  generic_typep->form = SCALAR_FORM;
  generic_typep->size = sizeof(STACK_ITEM);
  generic_typep->type_idp = generic_idp;

  anytype_idp->defn.key = TYPE_DEFN;
  anytype_idp->typep = any_typep;
  any_typep->form = SCALAR_FORM;
  any_typep->size = sizeof(STACK_ITEM);
  any_typep->type_idp = anytype_idp;
  

  /* predefined constants */

  enter_name_local_symtab(false_idp, "false");
  enter_name_local_symtab(true_idp, "true");
  enter_name_local_symtab(unknown_idp, "unknown");
  enter_name_local_symtab(pi_idp, "pi");
  enter_name_local_symtab(conste_idp, "const_e");
  enter_name_local_symtab(undef_idp, "?");
  enter_name_local_symtab(day_idp, "the_day");
  enter_name_local_symtab(month_idp, "the_month");
  enter_name_local_symtab(year_idp, "the_year");
  

  false_idp->defn.key = CONST_DEFN;
  false_idp->defn.info.constant.value.integer = FALSE_REP;
  false_idp->typep = logical_typep;
  false_idp->next = unknown_idp;

  unknown_idp->defn.key = CONST_DEFN;
  unknown_idp->defn.info.constant.value.integer = UNKNOWN_REP;
  unknown_idp->typep = logical_typep;
  unknown_idp->next = true_idp;

  true_idp->defn.key = CONST_DEFN;
  true_idp->defn.info.constant.value.integer = TRUE_REP;
  true_idp->typep = logical_typep;

  pi_idp->defn.key = CONST_DEFN;
  pi_idp->typep = real_typep;
  pi_idp->defn.info.constant.value.real = 3.141592654;

  conste_idp->defn.key = CONST_DEFN;
  conste_idp->typep = real_typep;
  conste_idp->defn.info.constant.value.real = 2.718281828;

  undef_idp->defn.key = CONST_DEFN;
  undef_idp->typep = any_typep;
  undef_idp->defn.info.constant.value.character = '\?';

     /* do the current date */
  tp1 = time(&tp2);
  ts = localtime(&tp1);
  sprintf(dbuffer, "year = %d, month = %d, day = %d\n",
                   (ts->tm_year+1900), (ts->tm_mon+1), (ts->tm_mday));
  debug_print(dbuffer);

  day_idp->defn.key = CONST_DEFN;
  day_idp->typep = integer_typep;
  day_idp->defn.info.constant.value.integer = ts->tm_mday;

  month_idp->defn.key = CONST_DEFN;
  month_idp->typep = integer_typep;
  month_idp->defn.info.constant.value.integer = ts->tm_mon + 1;

  year_idp->defn.key = CONST_DEFN;
  year_idp->typep = integer_typep;
  year_idp->defn.info.constant.value.integer = ts->tm_year + 1900;


  /* standard routines */

  enter_standard_routine("read", READ, PROC_DEFN);
  enter_standard_routine("readln", READLN, PROC_DEFN);
  enter_standard_routine("write", WRITE, PROC_DEFN);
  enter_standard_routine("writeln", WRITELN, PROC_DEFN);

  enter_standard_routine("abs", ABS, FUNC_DEFN);
  enter_standard_routine("cos", COS, FUNC_DEFN);
  enter_standard_routine("eof", EOFF, FUNC_DEFN);
  enter_standard_routine("eoln", EOLN, FUNC_DEFN);
  enter_standard_routine("exp", EXP, FUNC_DEFN);
  enter_standard_routine("odd", ODD, FUNC_DEFN);
  enter_standard_routine("round", ROUND, FUNC_DEFN);
  enter_standard_routine("sin", SIN, FUNC_DEFN);
  enter_standard_routine("sqrt", SQRT, FUNC_DEFN);
  enter_standard_routine("trunc", TRUNC, FUNC_DEFN);

  /* extra for ltx2x */
  enter_standard_routine("print", L2XPRINT, PROC_DEFN);
  enter_standard_routine("println", L2XPRINTLN, PROC_DEFN);
  enter_standard_routine("system", L2XSYSTEM, PROC_DEFN);
  enter_standard_routine("rexpr", L2XREXPR, FUNC_DEFN);

  /* extra for EXPRESS */
  enter_standard_routine("acos", XACOS, FUNC_DEFN);
  enter_standard_routine("asin", XASIN, FUNC_DEFN);
  enter_standard_routine("atan", XATAN, FUNC_DEFN);
  enter_standard_routine("log", XLOG, FUNC_DEFN);
  enter_standard_routine("log2", XLOG2, FUNC_DEFN);
  enter_standard_routine("log10", XLOG10, FUNC_DEFN);
  enter_standard_routine("tan", XTAN, FUNC_DEFN);
  enter_standard_routine("blength", XBLENGTH, FUNC_DEFN);
  enter_standard_routine("exists", XEXISTS, FUNC_DEFN);
  enter_standard_routine("format", XFORMAT, FUNC_DEFN);
  enter_standard_routine("hibound", XHIBOUND, FUNC_DEFN);
  enter_standard_routine("hiindex", XHIINDEX, FUNC_DEFN);
  enter_standard_routine("length", XLENGTH, FUNC_DEFN);
  enter_standard_routine("lobound", XLOBOUND, FUNC_DEFN);
  enter_standard_routine("loindex", XLOINDEX, FUNC_DEFN);
  enter_standard_routine("nvl", XNVL, FUNC_DEFN);
  enter_standard_routine("rolesof", XROLESOF, FUNC_DEFN);
  enter_standard_routine("sizeof", XSIZEOF, FUNC_DEFN);
  enter_standard_routine("typeof", XTYPEOF, FUNC_DEFN);
  enter_standard_routine("usedin", XUSEDIN, FUNC_DEFN);
  enter_standard_routine("value", XVALUE, FUNC_DEFN);
  enter_standard_routine("value_in", XVALUE_IN, FUNC_DEFN);
  enter_standard_routine("value_unique", XVALUE_UNIQUE, FUNC_DEFN);
  enter_standard_routine("insert", XINSERT, PROC_DEFN);
  enter_standard_routine("remove", XREMOVE, PROC_DEFN);

  /* restore debugging */
  DEBUG = temp;
  return;
}                                                       /* end init_symtab */
/***************************************************************************/



/***************************************************************************/
/* enter_standard_routine(name, routine_key, defn_key)  Enter a standard   */
/*                procedure or function name into the symtab               */

enter_standard_routine(name, routine_key, defn_key)
char *name;     /* name string */
ROUTINE_KEY routine_key;
DEFN_KEY defn_key;
{
  SYMTAB_NODE_PTR rtn_idp = enter_name_local_symtab(rtn_idp, name);

  rtn_idp->defn.key = defn_key;
  rtn_idp->defn.info.routine.key = routine_key;
  rtn_idp->defn.info.routine.parms = NULL;
  rtn_idp->defn.info.routine.local_symtab = NULL;
  rtn_idp->typep = NULL;

}                                            /* end enter_standard_routine */
/***************************************************************************/



/***************************************************************************/
/* enter_scope(symtab_root)  Enter a new nesting level by creating a new   */
/*             scope. Push the given symbol table onto the display stack   */

enter_scope(symtab_root)              
SYMTAB_NODE_PTR symtab_root;
{
  entry_debug("enter_scope");
  if (++level >= MAX_NESTING_LEVEL) {
    error(NESTING_TOO_DEEP);
    exit(-NESTING_TOO_DEEP);
  }
  symtab_display[level] = symtab_root;

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



/***************************************************************************/
/* exit_scope(symtab_root)  Exit the current new nesting level by closing  */
/*             the current scope. Pop the current symbol table off the     */
/*             display stack, and return a pointer to it                   */

SYMTAB_NODE_PTR exit_scope()          
{
  SYMTAB_NODE_PTR symtab_root = symtab_display[level--];
  entry_debug("exit_scope");
  exit_debug("exit_scope");
  return(symtab_root);
}                                                       /* end enter_scope */
/***************************************************************************/