-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.c
110 lines (90 loc) · 2.06 KB
/
label.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
/*
Interface that handles SPL labels.
*/
#include "label.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
extern int linecount;
static label *_root_label = NULL;
static int _namegen = 1;
static l_while *_rootWhile = NULL;
/* Create a new label with a random unused name */
label *label_create()
{
char name[30];
sprintf(name, "_L%d", _namegen);
_namegen++;
return _label_create(name);
}
/* Create a new label with given name */
label *_label_create(const char *name)
{
label *new_label = (label *)malloc(sizeof(label));
new_label->name = strdup(name);
new_label->next = NULL;
return new_label;
}
/* Adds a new label to the labels list */
label *label_add(const char *name)
{
label *new_label;
if (label_get(name) != NULL)
{
fprintf(stderr, "\n%d: Label '%s' redeclared.", linecount, name);
exit(0);
}
new_label = _label_create(name);
new_label->next = _root_label;
_root_label = new_label;
return new_label;
}
/* Get the label element with the name */
label *label_get(const char *name)
{
label *temp = _root_label;
while (temp != NULL)
{
if (strcmp(temp->name, name) == 0)
return temp;
temp = temp->next;
}
return NULL;
}
/* Returns the name of the label */
char *label_getName(label *ll)
{
return ll->name;
}
/* Deallocates the label */
void label_free(label *ll)
{
free(ll->name);
free(ll);
}
/* Push the label of a while block into the stack */
void label_pushWhile(label *start, label *end)
{
l_while *newl = (l_while *)malloc(sizeof(l_while));
newl->start = start;
newl->end = end;
newl->next = _rootWhile;
_rootWhile = newl;
}
/* Pop the label of a while block from the stack */
void label_popWhile()
{
l_while *temp = _rootWhile;
_rootWhile = _rootWhile->next;
free(temp);
}
/* Get the end label of the innermost while */
label *label_getWhileEnd()
{
return _rootWhile->end;
}
/* Get the start label of the innermost while */
label *label_getWhileStart()
{
return _rootWhile->start;
}