Repository navigation
Expand file tree
/
Copy pathparse_instructions.c
More file actions
180 lines (154 loc) · 4.31 KB
/
Copy pathparse_instructions.c
File metadata and controls
180 lines (154 loc) · 4.31 KB
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
/*
* this file is intended to parse an instruction or macro lines
*/
#include "parsing.h"
/*********************** global variables ***********************/
extern char line[LINE_LENGTH]; /* holds the current line which now is being assembled */
extern unsigned int index; /* the index which the line has already been assembled up untill that index */
extern unsigned int currentLine; /* counting the lines in the program */
extern unsigned int dc; /* data counter */
/************************** functions **************************/
/* parse & checks if a line is a valid macro definition */
boolean parseMacroLine()
{
boolean succeeded = TRUE;
char macroName[LINE_LENGTH];
char c;
int macroValue;
copyNextWord(macroName);
c = nextSymbol();
if (c != '=') /* Missing equality sign */
{
printErrorMsg("Missing equality sign");
succeeded = FALSE;
}
else
{
succeeded &= validLabelName(macroName, FALSE);
skipSpaces();
skipC_Character('='); /* skip the equality sign */
succeeded &= (succeeded) ? getNum(¯oValue) : succeeded;
}
if (!endOfLine()) /* if the line doesn't end */
{
printErrorMsg("Extraneous text");
succeeded = FALSE;
}
if (succeeded)
insertNewMacro(macroName, macroValue);
return succeeded;
}
/*checks if an instruction of type data line is valid */
boolean parseDataLine()
{
boolean succeeded = TRUE;
boolean commaFlag;
int dataValue;
if (hasComma())
{
printErrorMsg("Invalid comma");
return FALSE;
}
while ((!endOfLine()) && succeeded)
{
succeeded &= getNum(&dataValue);
if ((commaFlag = hasComma()) && endOfLine())
{
printErrorMsg("Comma at the end of line");
succeeded = FALSE;
}
else if (!commaFlag && (!endOfLine()))
{
printErrorMsg("Missing comma");
succeeded = FALSE;
}
else if (commaFlag)
{
if (hasComma())
{
printErrorMsg("Multiple commas betweeen values");
succeeded = FALSE;
}
}
if (succeeded)
succeeded &= insertDataWord(dataValue, dc++);
}
return succeeded;
}
/* checks if an insruction of type string is valid */
boolean parseStringLine()
{
boolean succeeded = TRUE;
char string[LINE_LENGTH];
char c;
int i = 0;
if (nextSymbol() != '\"')
{
printErrorMsg("Invalid string - string must start with a quotation mark");
return FALSE;
}
skipC_Character('\"');
if (nextSymbol() != '\"')
{
printErrorMsg("Invalid string - string must end with a quotation mark");
return FALSE;
}
copyNextWord(string);
skipC_Character('\"');
if (!endOfLine())
{
printErrorMsg("Extraneous text");
return FALSE;
}
if (succeeded)
do
{
succeeded &= insertDataWord((unsigned int)(c = string[i++]), dc++);
} while ((c != '\0') && succeeded);
/* while (((c = string[i++]) != '\0') && succeeded)
succeeded &= insertDataWord((unsigned int)c, dc++);*/
return succeeded;
}
/* checks if an extern line is valid */
boolean parseExternLine()
{
boolean succeeded = TRUE;
char externName[LINE_LENGTH];
if (endOfLine())
{
printErrorMsg("Missing the extern name)");
return FALSE;
}
copyNextWord(externName);
if (!endOfLine())
{
printErrorMsg("Extraneous text");
return FALSE;
}
succeeded &= validLabelName(externName, FALSE);
if (succeeded)
insertVariable(EXTERN_DATA, externName, ZERO);
return succeeded;
}
/* checks if an entry line is valid */
boolean parseEntryLine()
{
boolean succeeded = TRUE;
char entryName[LINE_LENGTH];
if (endOfLine())
{
printErrorMsg("Missing entry name)");
return FALSE;
}
copyNextWord(entryName);
if (!endOfLine())
{
printErrorMsg("Extraneous text");
return FALSE;
}
succeeded &= validLabelName(entryName, TRUE);
succeeded &= endOfLine();
if (succeeded)
insertEntry(entryName, ZERO); /* in the second scam the assembler will check if the entry exists and change its address */
return succeeded;
}