-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataStructures.c
executable file
·136 lines (94 loc) · 2.89 KB
/
dataStructures.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*Module to interact with the symbol table , contains functions, and declarations of variables */
#include "dataStructures.h"
#include "errorshandling.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
extern int IC ,DC;
/*Type constains a symbol node in the linked list
should be private because its contains internal implementation*/
typedef struct symbolnode {
SYMBOL symbol;
struct symbolnode * next;
}SYMBOLNODE;
/*static ptrs for the symbol table */
static SYMBOLNODE* head = NULL;
static SYMBOLNODE* last = NULL;
/*checks for an existing symbol with the specified name */
int checkForExistingSym(char* name) {
SYMBOLNODE* tmp = head;
while (tmp != NULL) {
if (strcmp(tmp->symbol.name, name) == 0) { /*There is an exisitng one*/
extern int LC;
raiseSymError(name)
return 1;
}
tmp = tmp->next;
}
return 0;
}
long getExistingSymbol(char* name, int * type) {
SYMBOLNODE* tmp = head;
while (tmp != NULL) {
if (strcmp(tmp->symbol.name, name) == 0) { /*There is an exisitng one*/
if(type != NULL)
*type = tmp->symbol.type; /*updating its type */
return tmp->symbol.value;
}
tmp = tmp->next;
}
return -1;
}
/*checks for an existing macro in the symbol table */
int checkForExistingMacro(char* name, long * status) {
SYMBOLNODE* tmp = head;
while (tmp != NULL) {
if (strcmp(tmp->symbol.name, name) == 0 && tmp->symbol.type == MACRO) { /*There is an exisitng one*/
if(status !=NULL)
*status = tmp ->symbol.value;
return 1;
}
tmp = tmp->next;
}
return 0;
}
/*Pushes a new symbol to the table*/
int pushToSymbolTable(char* name, SYMBOL_TYPE type, long value) {
SYMBOLNODE* newsym;
/*Checking is there is already a sym with this name*/
if(checkForExistingSym(name))
return 0;
/*Lets generate a new symbol first*/
newsym = (SYMBOLNODE*)malloc(sizeof(SYMBOLNODE));
strcpy(newsym->symbol.name, name);
newsym->symbol.type = type;
newsym->symbol.value = value;
newsym->next = NULL;
if (head == NULL) {
head = newsym;
last = newsym;
}
else
last->next = newsym;
last = newsym;
return 1;
}
/*Iterating over the table and updating each data symbol value in the end of the first pass*/
void updateEachDataSymbol(){
SYMBOLNODE* tmp = head;
while (tmp != NULL) {
if(tmp ->symbol.type == DATA)
tmp->symbol.value += (IC + CODING_REGION_START);
tmp = tmp ->next;
}
}
/*frees the symbol table */
void freeSymbolTable(){
SYMBOLNODE * tmp, * current = head;
while(current != NULL){
tmp = current ->next;
free(current); /*frees the current node */
current = tmp;
}
head = NULL;
}