summaryrefslogtreecommitdiff
path: root/support/ltx2x/l2xierr.c
blob: 3b84a2759f948147255d3ce417dc411803caa3c3 (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
/* l2xierr.c  LTX2X interpreter error handler */
/*  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"

/* externals */

extern char *tokenp;
extern BOOLEAN print_flag;
extern char source_buffer[];
extern char *bufferp;

/* error messages: keyed to enum types in error.h from l2xiertc.h */

char *error_messages[] = {
#define petc(a, b) b,
#define pwtc(a, b)
#define rtetc(a, b)
#define rtwtc(a, b)
#include "l2xiertc.h"
#undef petc
#undef pwtc
#undef rtetc
#undef rtwtc
      };


char *warning_messages[] = {
#define petc(a, b) 
#define pwtc(a, b) b,
#define rtetc(a, b)
#define rtwtc(a, b)
#include "l2xiertc.h"
#undef petc
#undef pwtc
#undef rtetc
#undef rtwtc
      };

char *runtime_error_messages[] = {
#define petc(a, b) 
#define pwtc(a, b)
#define rtetc(a, b) b,
#define rtwtc(a, b)
#include "l2xiertc.h"
#undef petc
#undef pwtc
#undef rtetc
#undef rtwtc
      };

char *runtime_warning_messages[] = {
#define petc(a, b) 
#define pwtc(a, b)
#define rtetc(a, b)
#define rtwtc(a, b) b,
#include "l2xiertc.h"
#undef petc
#undef pwtc
#undef rtetc
#undef rtwtc
      };

/* stack types */
typedef enum {
#define fotc(a, b, c, d) 
#define sotc(a, b, c, d) a,
#define sftc(a, b, c, d) a,
#include "l2xisftc.h"
#undef fotc
#undef sotc
#undef sftc
} STACK_TYPE;


/* LOCALS */
#define EOS '\0'

/* GLOBALS  */

int isynt_error_count = 0;   /* number of syntax errors */
int isynt_warn_count = 0;    /* number of syntax warnings */
int irun_error_count = 0;    /* number of runtime errors */
int irun_warn_count = 0;     /* number of runtime warnings */
char message_buffer[MAX_PRINT_LINE_LENGTH];

/***************************************************************************/
/* error(code)   Print an arrow under the error, then the error message    */

error(code)
ERROR_CODE code;     /* error code */
{
  extern int buffer_offset;
  char *message = error_messages[code];
  int offset = buffer_offset - 2;

  ++isynt_error_count;

  /* print the arrow pointing to just scanned token */
  if (print_flag) offset += 8;
  sprintf(message_buffer, "%*s^\n", offset, " ");
  if (print_flag) {
    print_line(message_buffer);
  }
  else {
     print_error(message_buffer);
   }

  /* print the error message */
  sprintf(message_buffer, " ***ERROR: %s.\n", message);
  if (print_flag) {
    print_line(message_buffer);
  }
  else {
     print_error(message_buffer);
   }

  *tokenp = EOS;

  if (isynt_error_count > MAX_SYNTAX_ERRORS) {
    sprintf(message_buffer, "Too many syntax errors.\n");
    if (print_flag) {
      print_line(message_buffer);
    }
    else {
      print_error(message_buffer);
    }
    exit(-TOO_MANY_SYNTAX_ERRORS);
  }

}                                                             /* end error */
/***************************************************************************/



/***************************************************************************/
/* compile_warning(code)   Print an arrow under the error, then the        */
/*                         warning message                                 */

compile_warning(code)
WARNING_CODE code;     /* warning code */
{
  extern int buffer_offset;
  char *message = warning_messages[code];
  int offset = buffer_offset - 2;

  ++isynt_warn_count;

  /* print the arrow pointing to just scanned token */
  if (print_flag) offset += 8;
  sprintf(message_buffer, "%*s^\n", offset, " ");
  if (print_flag) {
    print_line(message_buffer);
  }
  else {
     print_error(message_buffer);
   }

  /* print the warning message */
  sprintf(message_buffer, " ***WARNING: %s.\n", message);
  if (print_flag) {
    print_line(message_buffer);
  }
  else {
     print_error(message_buffer);
   }

  *tokenp = EOS;

}                                                           /* end warning */
/***************************************************************************/



/***************************************************************************/
/* runtime_error(code)   Print a runtime error message and then debug      */

runtime_error(code)  
RUNTIME_ERROR_CODE code;     /* error code */
{
  extern int exec_line_number; 
  extern long exec_stmt_count;
  char *message = runtime_error_messages[code];
  extern BOOLEAN debugger_command_flag;

  ++irun_error_count;

  if (SLD_OFF) {    /* source level debugger disabled -- abort */
    sprintf(message_buffer, "\n*** RUNTIME ERROR in line %d: %s\n",
             exec_line_number, message);
    print_error(message_buffer);
    sprintf(message_buffer, "\nUnsuccessful completion. %ld statements executed.\n\n",
                             exec_stmt_count);
    print_error(message_buffer);
    exit(-code);    
  }

  if (debugger_command_flag) {
    print_error(message);
    print_error("\n");
  }
  else {
    sprintf(message_buffer, "\n*** RUNTIME ERROR in line %d: %s\n",
             exec_line_number, message);
    print_error(message_buffer);
    read_debugger_command();
  }

}                                                     /* end runtime_error */
/***************************************************************************/



/***************************************************************************/
/* runtime_warning(code)   Print a runtime warning message and then debug  */

runtime_warning(code)  
RUNTIME_WARNING_CODE code;     /* warning code */
{
  extern int exec_line_number; 
  extern long exec_stmt_count;
  char *message = runtime_warning_messages[code];
  extern BOOLEAN debugger_command_flag;

  if (INVALID_STACK_ACCESS == code) return;

  ++irun_warn_count;

  if (SLD_OFF) {    /* source level debugger disabled */
    sprintf(message_buffer, "\n*** RUNTIME WARNING in line %d: %s\n",
             exec_line_number, message);
    print_error(message_buffer);
    return;
  }

  if (debugger_command_flag) {
    print_error(message);
    print_error("\n");
  }
  else {
    sprintf(message_buffer, "\n*** RUNTIME WARNING in line %d: %s\n",
             exec_line_number, message);
    print_error(message_buffer);
    read_debugger_command();
  }
  return;
}                                                   /* end runtime_warning */
/***************************************************************************/



/***************************************************************************/
/* stack_warning(etype, ftype)   Print a runtime warning message about     */
/*               stack access and then debug                               */

stack_warning(etype, ftype)  
STACK_TYPE etype;                 /* expected type */
STACK_TYPE ftype;                 /* type actually found */
{
  extern int exec_line_number; 
  extern long exec_stmt_count;
  RUNTIME_WARNING_CODE code1, code2;
  extern BOOLEAN debugger_command_flag;

  code1 = RUNTIME_WARN;
  switch (etype) {           /* report the expected type */
    case STKINT:
    case STKREA: {
      code1 = EXPECTED_NUMBER;
      break;
    }
    case STKLOG: {
      code1 = EXPECTED_LOGICAL;
      break;
    }
    case STKADD: {
      code1 = EXPECTED_ADDRESS;
      break;
    }
    case STKARY: {
      code1 = EXPECTED_ARRAY;
      break;
    }
    case STKBAG: {
      code1 = EXPECTED_BAG;
      break;
    }
    case STKLST: {
      code1 = EXPECTED_LIST;
      break;
    }
    case STKSET: {
      code1 = EXPECTED_SET;
      break;
    }
    case STKSTR: {
      code1 = EXPECTED_STRING;
      break;
    }
    case STKENT: {
      code1 = EXPECTED_ENTITY;
      break;
    }
    case STKUDF: {
      code1 = EXPECTED_UDF;
      break;
    }
  }  /* finished expected type */

  code2 = RUNTIME_WARN;
  switch (ftype) {           /* report the found type */
    case STKINT:
    case STKREA: {
      code2 = FOUND_NUMBER;
      break;
    }
    case STKLOG: {
      code2 = FOUND_LOGICAL;
      break;
    }
    case STKADD: {
      code2 = FOUND_ADDRESS;
      break;
    }
    case STKARY: {
      code2 = FOUND_ARRAY;
      break;
    }
    case STKBAG: {
      code2 = FOUND_BAG;
      break;
    }
    case STKLST: {
      code2 = FOUND_LIST;
      break;
    }
    case STKSET: {
      code2 = FOUND_SET;
      break;
    }
    case STKSTR: {
      code2 = FOUND_STRING;
      break;
    }
    case STKENT: {
      code2 = FOUND_ENTITY;
      break;
    }
    case STKUDF: {
      code2 = FOUND_UDF;
      break;
    }
  }  /* finished found type */



  if (SLD_OFF) {    /* source level debugger disabled */
    sprintf(message_buffer, "\n*** RUNTIME WARNING in line %d:\n    %s\n    %s\n",
             exec_line_number, 
             runtime_warning_messages[code1],
             runtime_warning_messages[code2]);
    print_error(message_buffer);
    return;
  }

  if (debugger_command_flag) {
    sprintf(message_buffer, "\n    %s\n    %s\n",
             runtime_warning_messages[code1],
             runtime_warning_messages[code2]);
    print_error(message_buffer);
  }
  else {
    sprintf(message_buffer, "\n*** RUNTIME WARNING in line %d:\n    %s\n    %s\n",
             exec_line_number, 
             runtime_warning_messages[code1],
             runtime_warning_messages[code2]);
    print_error(message_buffer);
    read_debugger_command();
  }
  return;
}                                                     /* end STACK_WARNING */
/***************************************************************************/



/***************************************************************************/
/* print_error(string) Prints to error file(s)                             */

print_error(string)
char string[];
{

  fprintf(ferr, "%s", string);
  fflush(ferr);
  fprintf(stderr, "%s", string);
  fflush(stderr);
  if (filout != stdout) {
    fprintf(filout, "%s", string);
    fflush(filout);
  }
  return;
}                                                       /* end print_error */
/***************************************************************************/