-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtable.c
72 lines (64 loc) · 1.36 KB
/
symtable.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "symtable.h"
#define N_SYMS 256
char syms[N_SYMS][SYM_LEN] = { 0 };
int vars[N_SYMS] = { 0 };
int temp = -1;
int scratch;
/* get or add the symbol name to the table. returns the id. */
int get_sym(const char* id) {
int i;
for (i = 0; i < N_SYMS; i++) {
if (syms[i][0] == '\0') {
strncpy(syms[i], id, SYM_LEN);
syms[i][SYM_LEN - 1] = '\0';
return i;
}
if (strcmp(syms[i], id) == 0) {
return i;
}
}
return -1;
}
const char* get_sym_name(int i) {
if (i < 0 || i >= N_SYMS)
return "";
return syms[i];
}
int* get_var(int i) {
if (i < 0 || i >= N_SYMS)
return NULL;
if (i == temp) {
scratch = 1;
return &scratch;
}
return vars + i;
}
void set_temp(int id) {
#ifdef DEBUG
printf("set temp %s (%d)\n", syms[id], id);
#endif
temp = id;
}
void clear_temp() {
#ifdef DEBUG
printf("clear temp %s (%d)\n", syms[temp], temp);
#endif
temp = -1;
}
void dump_syms() {
int i;
for (i = 0; i < N_SYMS; i++) {
if (syms[i][0] == '\0')
break;
if (vars[i] == 0)
continue;
printf("%d %s\t\t", i, syms[i]);
if (i == temp)
printf("(1)\n");
else
printf("%d\n", vars[i]);
}
}