-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
235 lines (205 loc) · 7.16 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "parser.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
// Implements the parser, which parses a line
// And outputs the parsed instruction to be compiled
extern const int MAX_A_VALUE;
// Loop though each character in line and parse instruction
// Set instruction_parsed, a_value, dest, comp, and jump
bool parse_line(char *line, enum instruction *instruction_parsed, // currentline may be removed
char *a_or_label_value, struct c_instruction_value *dest,
struct c_instruction_value *comp,
struct c_instruction_value *jump) {
// What we are currently doing in the line
enum instruction parsing_status = NONE;
// Pointer to the current value being changed
char *pointer_to_value_changed = NULL;
// Loop though each character in line
for (size_t i = 0; i < strlen(line); i++) {
// Check for current character and either add it to value or
// set the current instruction
switch (line[i]) {
// A instruction
case '@':
if (parsing_status == NONE) {
parsing_status = A_INSTRUCTION;
*instruction_parsed = A_INSTRUCTION;
pointer_to_value_changed = a_or_label_value;
} else {
printf("SYNTAX: Unexpected character @");
return false;
};
break;
// Labels
// TODO: Finish label code
case '(':
if (parsing_status == NONE) {
parsing_status = LABEL;
*instruction_parsed = LABEL;
} else {
printf("SYNTAX: Unexpected character (");
return false;
}
break;
case ')':
if (parsing_status == LABEL || parsing_status == FINISHED_VALUE) {
parsing_status = FINISHED_INSTRUCTION;
} else {
printf("SYNTAX: Unexpected character )");
return false;
}
break;
// Comments
case '/':
// Switch though the current parsing status to
// Check if we can actually break out of the loop
switch (parsing_status) {
case SLASH_ONE:
parsing_status = SLASH_TWO;
break;
case FINISHED_INSTRUCTION:
case NONE:
parsing_status = SLASH_ONE;
break;
case C_INSTRUCTION:
case FINISHED_VALUE:
// If we are in C_INSTRUCTION and every value is set correctly
// (if the value is valid it should be set to something)
if (*instruction_parsed == C_INSTRUCTION) {
parsing_status = SLASH_ONE;
break;
}
__attribute__((fallthrough));
default:
printf("SYNTAX: Unexpected character (%c)", line[i]);
return false;
}
break;
// C instructions
case '0':
case '1':
case '-':
case 'D':
case 'A':
case '!':
case 'M':
// If parsing status is not set
if (parsing_status == NONE) {
parsing_status = C_INSTRUCTION;
*instruction_parsed = C_INSTRUCTION;
dest->validity = true;
pointer_to_value_changed = dest->value;
}
// Also run default since it is the part of the code that adds to current
// value
__attribute__((fallthrough));
// Handle other characters (mostly appending it the current value)
default:
// This part of the code allows for spacing like @ 19 or MD = 0
if (isspace(line[i])) {
// Make sure we at least wrote something
// before trying to set it to finished value
if (pointer_to_value_changed != NULL &&
strcmp(pointer_to_value_changed, "") != 0) {
// Handle spacing, mostly set it to FINISHED_VALUE
switch (parsing_status) {
case A_INSTRUCTION:
parsing_status = FINISHED_INSTRUCTION;
break;
case C_INSTRUCTION:
parsing_status = FINISHED_VALUE;
break;
case LABEL:
parsing_status = FINISHED_VALUE;
break;
case SLASH_ONE:
printf("SYNTAX: Unexpected character (%c)", line[i]);
return false;
case NONE:
case FINISHED_VALUE:
case FINISHED_INSTRUCTION:
case SLASH_TWO:
break;
}
}
// Parse labels
} else if (parsing_status == LABEL) {
strncat(a_or_label_value, &line[i], 1);
// Parse A instruction
} else if (parsing_status == A_INSTRUCTION) {
// WARNING Can overflow if the buffer isn't big enough
// Theorically it should be big enough because of the code in compiler.c
// Which allocates enough memory for a full line
strncat(a_or_label_value, &line[i], 1);
// Parse C instructions
} else if ((parsing_status == FINISHED_VALUE &&
*instruction_parsed == C_INSTRUCTION) ||
parsing_status == C_INSTRUCTION) {
switch (line[i]) {
// dest -> comp
case '=':
// If the pointer to value changed is dest
// (value supposed to be before comp)
if (pointer_to_value_changed == dest->value) {
parsing_status = C_INSTRUCTION;
comp->validity = true;
pointer_to_value_changed = comp->value;
} else {
printf("SYNTAX: Unexpected character (%c)", line[i]);
return false;
}
break;
// comp -> jump
case ';':
// if the current value is dest (we are assuming it's dest at the
// start) then the actual one is comp since we encountered a ;
if (pointer_to_value_changed == dest->value) {
strcpy(comp->value, dest->value);
comp->validity = true;
dest->validity = false;
strcpy(dest->value, "");
} else if (pointer_to_value_changed != comp->value) {
printf("SYNTAX: Unexpected character (%c)", line[i]);
return false;
}
parsing_status = C_INSTRUCTION;
// Set current value to jump
pointer_to_value_changed = jump->value;
jump->validity = true;
break;
// Another character, add it to current value
default:
// Check if we are indeed in a C_INSTRUCTION (not FINISHED_VALUE)
// And make sure we aren't going to overflow our string (maximum 3)
if (parsing_status == C_INSTRUCTION &&
strlen(pointer_to_value_changed) < 3)
strncat(pointer_to_value_changed, &line[i], 1);
else {
printf("SYNTAX: Unexpected character (%c)", line[i]);
return false;
}
break;
}
// Unhandled character
} else {
printf("SYNTAX: Unexpected character (%c)", line[i]);
return false;
}
}
// Break since we are in a comment
if (parsing_status == SLASH_TWO)
break;
}
// Make sure dest, jump and comp is set correctly
// (if they are valid they MUST contain something)
// make sure we are a c instruction
if ((*instruction_parsed == C_INSTRUCTION) &&
((dest->validity == true && strcmp(dest->value, "") == 0) ||
(jump->validity == true && strcmp(jump->value, "") == 0) ||
(strcmp(comp->value, "") == 0))) {
printf("SYNTAX: Unexpected character at the end");
return false;
}
return true;
}