-
Notifications
You must be signed in to change notification settings - Fork 0
/
errormsg.c
71 lines (57 loc) · 1.34 KB
/
errormsg.c
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
/*
* errormsg.c - functions used in all phases of the compiler to give
* error messages about the Tiger program.
*
*/
#include "errormsg.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
bool anyErrors = FALSE;
static string fileName = "";
static int lineNum = 1;
int EM_tokPos = 0;
extern FILE *yyin;
typedef struct intList {
int i;
struct intList *rest;
} * IntList;
static IntList intList(int i, IntList rest) {
IntList l = checked_malloc(sizeof *l);
l->i = i;
l->rest = rest;
return l;
}
static IntList linePos = NULL;
void EM_newline(void) {
lineNum++;
linePos = intList(EM_tokPos, linePos);
}
void EM_error(int pos, char *message, ...) {
va_list ap;
IntList lines = linePos;
int num = lineNum;
anyErrors = TRUE;
while (lines && lines->i >= pos) {
lines = lines->rest;
num--;
}
if (fileName) fprintf(stderr, "%s:", fileName);
if (lines) fprintf(stderr, "%d.%d: ", num, pos - lines->i);
va_start(ap, message);
vfprintf(stderr, message, ap);
va_end(ap);
fprintf(stderr, "\n");
}
void EM_reset(string fname) {
anyErrors = FALSE;
fileName = fname;
lineNum = 1;
linePos = intList(0, NULL);
yyin = fopen(fname, "r");
if (!yyin) {
EM_error(0, "cannot open");
exit(1);
}
}