-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
97 lines (83 loc) · 1.99 KB
/
util.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
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
#include "globals.h"
// only for yacc
void yyerror(char *s, ...) {
va_list ap;
va_start(ap, s);
fprintf(stderr, "Syntax Error in line %d: ", yylineno);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
exit(1);
}
void error(ErrorType type, char *s, ...) {
va_list ap;
va_start(ap, s);
switch(type) {
case LexicalError:
case SyntaxError:
case SemanticError:
case TypeError:
// with lineno
fprintf(stderr, "%s in line %d: ", errortype_to_str(type), yylineno);
break;
case Bug:
case RuntimeError:
// without lineno
fprintf(stderr, "%s: ", errortype_to_str(type));
break;
default:
error(Bug, "unknown error type");
}
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
exit(1);
}
void warning(char *s, ...) {
va_list ap;
va_start(ap, s);
fprintf(stderr, "Warning: ");
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
char* errortype_to_str(ErrorType type) {
switch(type) {
case Bug: return "Bug";
case RuntimeError: return "Runtime Error";
case LexicalError: return "Lexical Error";
case SyntaxError: return "Syntax Error";
case SemanticError: return "Semantic Error";
case TypeError: return "Type Error";
default: error(Bug, "unknown error type");
}
}
char* type_to_str(Type type) {
switch(type) {
case IntT: return "IntT";
case IntArrayT: return "IntArrayT";
case VoidT: return "VoidT";
case BoolT: return "BoolT";
default: error(Bug, "unknown type");
}
}
char* stkind_to_str(StKind kind) {
switch(kind) {
case Global_Var: return "Global_Var";
case Fun_Param: return "Fun_Param";
case Compound_Var: return "Compound_Var";
default: error(Bug, "unknown symtab kind");
}
}
char* operator_to_str(int op) {
switch(op) {
case '+': return "+";
case '-': return "-";
case '*': return "*";
case '/': return "/";
case EQ: return "==";
case NE: return "!=";
case LT: return "<";
case LE: return "<=";
case GT: return ">";
case GE: return ">=";
default: error(Bug, "unknown operator");
}
}