-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstPass.c
137 lines (113 loc) · 3.38 KB
/
FirstPass.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
#include "declarationsHeader.h"
extern struct keyWords keys[];
/*
* Takes one line from the file and runs tests over it and gathering info for the second pass.
*/
void firstPass(FILE *fd) {
int IC = STARTING_ADDRESS, DC = 0;
line = (char *) malloc(sizeof(char) * LINE_SIZE);
MEMORY_ERROR(line)
resetLineNumber();
while (getLineFromFile(line, fd)) {
parseLine(line);
incrementLinesNumber();
if (lineIsComment(line))
continue;
createLineList();
analyzeLine(&DC);
if (!generalError)
codeTheLine(&IC, &DC);
freeLineList();
}
updateSymbolTable(&IC);
free(line);
}
/*
* Codes the current line according to the type of the line.
*/
void codeTheLine(int *IC, int *DC) {
if (lineWithDataDefinition())/*.data line - array or an int.*/
codeDataLine(DC);
else if (lineWithStringDefinition())/*Its a line with a string.*/
codeStringLine(DC);
else if (lineWithExternLabel()) /*Its a line with extern label*/
externSymLine();
else if (lineWithCommandDefinition())/*Command line- calculating the number of words it'll take*/
calCmdLine(IC);
}
boolean bothParametersAreRegisters(char *firstParam, char *secondParam) {
return isRegister(firstParam) && isRegister(secondParam);
}
void checkForFloatErrors(char *firstParam, char *secondParam) {
if (firstParam != NULL && strchr(firstParam, DOT))
printErrorWithComment(float_err, firstParam);
if (secondParam != NULL && strchr(secondParam, DOT))
printErrorWithComment(float_err, secondParam);
}
/*
* Returns true if there a label before the parameters.
* Else, returns false.
*/
boolean labelBeforeParam() {
char lineCpy[LINE_SIZE], cmd[CMD_LENGTH];
strcpy(lineCpy, line);
getCommand(cmd);
/*Testing if the line has a label before the parameters*/
if (strcmp(cmd, "jmp") != EQUAL && strcmp(cmd, "bne") != EQUAL && strcmp(cmd, "jsr") != EQUAL)
return FALSE;
if (strchr(lineCpy, O_BRACKETS) == NULL)
return FALSE;
return TRUE;
}
/*
* Returns true if there's a label definition withing the line.
* Else, returns false.
*/
boolean lineWithLabelDefinition() {
char lineCpy[LINE_SIZE];
strcpy(lineCpy, line);
/*Searching for ':' in the line.*/
if (strchr(lineCpy, ISLABEL))
return TRUE;
return FALSE;
}
/*
* Returns true if there's a data definition withing the line.
* Else, returns false.
*/
boolean lineWithDataDefinition() {
char lineCpy[LINE_SIZE];
strcpy(lineCpy, line);
//*Searching for .data in the line.*/
if (strstr((lineCpy), GUIDING_SENTENCE_INT))
return TRUE;
return FALSE;
}
/*
* Returns true if there's a string definition withing the line.
* Else, returns false.
*/
boolean lineWithStringDefinition() {
char lineCpy[LINE_SIZE];
strcpy(lineCpy, line);
/*Searching for .string in the line.*/
if (strstr((lineCpy), GUIDING_SENTENCE_STRING))
return TRUE;
return FALSE;
}
/*
* Returns true if there's a command definition withing the line.
* Else, returns false.
*/
boolean lineWithCommandDefinition() {
char lineCpy[LINE_SIZE];
int i = 0;
strcpy(lineCpy, line);
/*Searching for a sub string matching the our commands.*/
for (i = 0; i < ACTIONS_NUM; i++) {
if (strstr(lineCpy, (keys[i].symbol))) {
return TRUE;
}
}
return FALSE;
}