-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsym.c
112 lines (90 loc) · 1.85 KB
/
sym.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "sym.h"
#include "codegen.h"
/* unique labels */
static int lbl;
sym_t *variables;
sym_t *returns;
sym_t *breaks;
sym_t *continues;
int
new_label (void) {
return (++lbl);
}
void
sym_init (void) {
lbl = 0;
variables = NULL;
returns = NULL;
breaks = NULL;
continues = NULL;
}
void
sym_dist_inc (int b) {
if (variables) {variables->dist += b;}
if (returns) {returns->dist += b;}
if (breaks) {breaks->dist += b;}
if (continues) {continues->dist += b;}
}
void
sym_dist_dec (int b) {
if (variables) {variables->dist -= b;}
if (returns) {returns->dist -= b;}
if (breaks) {breaks->dist -= b;}
if (continues) {continues->dist -= b;}
}
sym_t* sym_create_lbl (int lbl) {
sym_t *sym;
sym = (sym_t*) malloc(sizeof(sym_t));
memset(sym, 0, sizeof(sym_t));
sym->lbl = lbl;
sym->dist = 0;
return sym;
}
sym_t* sym_create_name (const char* name) {
sym_t *sym;
sym = (sym_t*) malloc(sizeof(sym_t));
memset(sym, 0, sizeof(sym_t));
strncpy(sym->name, name, MAX_TOKEN_SIZE);
sym->dist = 0;
return sym;
}
void
sym_push (sym_t** stack, sym_t *sym) {
sym->next = *stack;
*stack = sym;
}
void
sym_pop (sym_t** stack) {
sym_t *sym;
sym = *stack;
*stack = (*stack)->next;
free(sym);
return;
}
int
sym_dist_top (sym_t** stack) {
return ((*stack)->dist);
}
int
sym_dist_name (sym_t** stack, const char* name, int *dist) {
sym_t *sym;
if (dist) {
*dist = 0;
}
for (sym = *stack; sym; sym = sym->next) {
if (dist) {
*dist += sym->dist;
}
if (!strcmp(sym->name, name)) {
return 1;
}
}
return 0;
}
int
sym_lbl_top (sym_t** stack) {
return ((*stack)->lbl);
}