-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
184 lines (163 loc) · 3.36 KB
/
parser.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "parser.h"
struct symbolList* symbolListHead = NULL; //Symbol Table
struct symbolList* symbolListTail = NULL; //Symbol Table
int nextMem = 16;
//Cleans a line by removing whitespace.
char* getCleanLine(char* in)
{
int size = strlen(in);
char* out = malloc( sizeof(char) * ( size + 1 ) );
int i = 0;
int outsize = 0;
for(i = 0; i < size; i++)
{
if(in[i] == '/')
break;
if(isspace(in[i]))
{
if(in[i] == '\n')
break;
continue;
}
out[outsize++] = in[i];
}
out[outsize] = '\0';
return out;
}
//Takes a clean line and returns the destination token
void getDest(char* in, char* out)
{
char* destExists = strchr(in, '=');
if(destExists == NULL)
{
out[0] = '\0';
return;
}
int i = 0;
int end = destExists - in;
for (i = 0; i < end; i++)
{
out[i] = in[i];
}
out[i] = '\0';
}
//Takes a clean line and returns the computation token
void getComp(char* in, char* out)
{
char* destExists = strchr(in, '=');
int i = 0,j = 0;
if (destExists != NULL)
i = destExists - in + 1;
while(1)
{
out[j++] = in[i++];
if(in[i] == '\0' || in[i] == ';')
break;
}
out[j]='\0';
return;
}
//Takes a clean line and returns a jump token.
void getJump(char* in, char* out)
{
char* jumpExists = strchr(in, ';');
if(jumpExists == NULL)
{
out[0] = '\0';
return;
}
int i;
for (i =1; i<4; i++)
out[i-1] = jumpExists[i];
out[3] = '\0';
}
//Takes a clean line and returns the memory address.
void getSymbolValue(char* in, char* out)
{
if(isdigit(in[1]))
{
strncpy(out,in+1, 5);
out[5] = '\0';
}
else
{
findOrCreateSymbol(in+1, out);
}
}
//Initialize the symbol table
void setupParserSymbolTable()
{
setupSymbolTable(&symbolListHead, &symbolListTail);
}
//Clean up the parser table
void cleanupParserSymbolTable()
{
cleanupSymbolTable(&symbolListHead);
}
//Takes a clean line and returns a ParsedCommand struct
struct ParsedCommand* getParsedCommand(char* in)
{
struct ParsedCommand* p = malloc(sizeof(struct ParsedCommand));
p->ct = getCommandType(in);
if(p->ct == A_TYPE)
{
getSymbolValue(in, p->memLocation);
}
if(p->ct == C_TYPE)
{
getDest(in, p->dest);
getComp(in, p->comp);
getJump(in, p->jump);
}
return p;
}
//Takes a clean line and returns the CommandType
enum CommandType getCommandType(char* in)
{
if (in[0] == '@')
return A_TYPE;
if (in[0] == '(')
return L_TYPE;
return C_TYPE;
}
//Clean up a ParsedCommand
void freeParsedCommand(struct ParsedCommand* p)
{
free(p);
}
//Finds a symbol in the symbol table or creates a new one
void findOrCreateSymbol(char* in, char* out)
{
struct symbolList* curr = symbolListHead;
while (curr != NULL)
{
if (strcmp(curr->symbol, in) == 0)
{
snprintf(out, 6, "%d", curr->val);
return;
}
curr = curr->next;
}
struct symbolList* new = malloc(sizeof(struct symbolList));
new->symbol = malloc(strlen(in) + 1);
strcpy(new->symbol, in);
new->val = nextMem++;
new->next = NULL;
symbolListTail->next = new;
symbolListTail = new;
snprintf(out, 6, "%d", new->val);
}
//Adds labels to the symbol table
void processLabel(char* cleanLine, int lineCount)
{
int len = strchr(cleanLine, ')') - cleanLine;
char* label = malloc(len);
strncpy(label, cleanLine+1, len-1);
label[len-1] = '\0';
struct symbolList* new = malloc(sizeof(struct symbolList ));
new-> symbol = label;
new-> val = lineCount;
new->next = NULL;
symbolListTail->next = new;
symbolListTail = new;
}