-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstPass.c
133 lines (114 loc) · 4.25 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
/*
====================================================================================
Module: firstPass
Description: implements the first pass compiler algorithm
====================================================================================
*/
#include "header.h"
/*----------------------------------------------------------------------------*/
/*
* Description: implements the first pass algorithm (from the course booklet)
* Input: FILE pointer to assembly language file
* Output: 1 if successful, 0 otherwise
*/
/*----------------------------------------------------------------------------*/
int firstPassManager(Data * data, FILE *file){
char lineHolder[MAX_LINE_LEN+1];
/* NULL means EOF. When we reach EOF it means we're done */
while(fgets(lineHolder,MAX_LINE_LEN,file) != NULL ){
/* printf("%s\n",lineHolder); */
data->line=lineHolder;
data->lc++;
lineHandler(data,file);
}
return 1;
}
/*----------------------------------------------------------------------------*/
/*
* Description: does basic line parsing and assigns input to other function for further parsing
* Input: pointer to Data struct, FILE pointer to assembly language file
* Output: 1 if successful, 0 otherwise
*/
/*----------------------------------------------------------------------------*/
int lineHandler(Data * data,FILE *file){
char tag[MAX_TAG_LEN];
if (lineLengthCheck(data, file) == 0) {
return 0;
}
if(lineEmptyCheck(data)==1){
return 1;
}
if(lineCommentCheck(data)==1){
return 1;
}
getTag(data,tag);
/* there's a tag at the start of the line */
if (*tag != '\0'){
/*printf("%s\n",tag);*/
if (tagDupCheck (data, tag) == 1){
printf("[Error] on line %d: you have the tag %s more then once.\n",data->lc, tag);
data->containError=TRUE;
return 0;
}
eatSpace(data);
}
if(*(data->line)=='.'){
return directivesManager(data, tag);
}
/*
if(checkUpperCase(*(data->line))==0){
return firstPassCommandsManager(data,tag);
}
*/
/* printf("[Error] on line %d: line isn't valid a input\n",data->lc); */
data->containError=TRUE;
return 0;
}
/*----------------------------------------------------------------------------*/
/*
* Description: check if a line is longer than MAX_LINE_LEN
* Input: pointer to Data struct, FILE pointer to assembly language file
* Output: 1 if not longer, 0 otherwise
*/
/*----------------------------------------------------------------------------*/
int lineLengthCheck(Data * data, FILE *file){
if(strlen(data->line)>=MAX_LINE_LEN-1 && (data->line)[MAX_LINE_LEN-1] != EOF && (data->line)[MAX_LINE_LEN-1] != '\n'){
printf("[Error] on line %d: line is longer than %d chars\n", data->lc,MAX_LINE_LEN);
data->containError=TRUE;
/* get rid of the rest of the line so we don't get the rest of it with fgets instead of a new one */
eatLine(file);
return 0;
}
return 1;
}
/*----------------------------------------------------------------------------*/
/*
* Description: check whether a tag already exists
* Input: pointer to Data struct, pointer to a character array
* Output: 1 if duplicate, 0 otherwise
*/
/*----------------------------------------------------------------------------*/
int tagDupCheck(Data * data, char * tag){
int index;
for (index = 0; index< data->tc;index++){
if (strcmp(data->tagArr[index].name,tag) == 0){
return 1;
}
}
return 0;
}
/*----------------------------------------------------------------------------*/
/*
* Description: adds a tag to the tag array inside the Data struct
* Input: pointer to Data struct, tag name pointer, address to point tag at
* Output: nothing
*/
/*----------------------------------------------------------------------------*/
/* void addTag(Data * data, char * tag, int dirAddress){ */
void addTag(Data * data, char * tag, int dirAddress,int kind){
data->tagArr = realloc(data->tagArr, sizeof(Tag)*(data->tc+2));
strcpy(data->tagArr[data->tc].name,tag);
data->tagArr[data->tc].address=dirAddress;
data->tagArr[data->tc].kind=kind;
data->tc++;
}