-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
207 lines (173 loc) · 5.75 KB
/
main.c
File metadata and controls
207 lines (173 loc) · 5.75 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
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
/*
* main.c, P232 main function.
*
* P232, a C programming language preprocessor to perform arithmetic operations on matrices.
* Yeditepe University CSE232 course group term project.
*/
#include "P_header.h"
// Function declarations
void parse_line(char* line);
void enter_array_table();
void process_and_expand_directive();
void print_with_spaces(const char* expanded_line);
int find_array_index(const char* array_name);
char* ltrim(char* s, int* left_trim_space_count);
char *rtrim(char* s);
char* trim(char* s, int* left_trim_space_count);
int array_table_index = 0;
int line_left_space_count = 0;
FILE* source_file = NULL;
FILE* expanded_file = NULL;
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <filename>.c\n", argv[0]);
return 1;
}
char* filename = argv[1];
source_file = fopen(filename, "r");
expanded_file = fopen("expanded.c", "w");
if (!source_file) {
printf("Couldn't open source file \"%s\"\n", filename);
return 1;
} else if (!expanded_file) {
printf("Error at opening expanded file!\n");
return 1;
}
char line[1024];
while (fgets(line, sizeof(line), source_file)) {
char trimmed_line[1024];
strcpy(trimmed_line, trim(line, &line_left_space_count));
if (trimmed_line[0] == '@') {
parse_line(line);
if (strcmp(PT.oper, "@int") == 0)
enter_array_table();
process_and_expand_directive();
} else {
fprintf(expanded_file, "%s\n", line);
}
}
fclose(source_file);
fclose(expanded_file);
return 0;
}
// Parse the line and fill the Parse Table
void parse_line(char* line) {
char* saveptr; // Pointer for internal strtok_r state
char* oper = strtok_r(line, " (,)<=.+*;", &saveptr);
if (oper != NULL) {
strcpy(PT.oper, oper);
} else {
PT.oper[0] = '\0';
}
char* lhs = strtok_r(NULL, " (,)<=.+*;", &saveptr);
if (lhs != NULL) {
strcpy(PT.lhs, lhs);
} else {
PT.lhs[0] = '\0';
}
char* rhs1 = strtok_r(NULL, " (,)<=.+*;", &saveptr);
if (rhs1 != NULL) {
strcpy(PT.rhs1, rhs1);
} else {
PT.rhs1[0] = '\0';
}
char* rhs2 = strtok_r(NULL, " (,)<=.+*;", &saveptr);
if (rhs2 != NULL) {
strcpy(PT.rhs2, rhs2);
} else {
PT.rhs2[0] = '\0';
}
}
void enter_array_table() {
strcpy(AT[array_table_index].name, PT.lhs);
strcpy(AT[array_table_index].size1, PT.rhs1);
strcpy(AT[array_table_index].size2, PT.rhs2);
if (PT.rhs2[0] == '\0')
AT[array_table_index].dim = 1;
else
AT[array_table_index].dim = 2;
array_table_index++;
}
// Process directive based on parsed information
void process_and_expand_directive() {
char* expanded_line = NULL;
char undefined_string[100] = {0};
if (strcmp(PT.oper, "@int") == 0)
expanded_line = declaration(array_table_index - 1);
else if (strcmp(PT.oper, "@read") == 0)
expanded_line = read(find_array_index(PT.lhs));
else if (strcmp(PT.oper, "@copy") == 0)
expanded_line = copy(find_array_index(PT.rhs1),find_array_index(PT.lhs));
else if (strcmp(PT.oper, "@init") == 0)
expanded_line = initialize(find_array_index(PT.lhs));
else if (strcmp(PT.oper, "@print") == 0)
expanded_line = print(find_array_index(PT.lhs));
else if (strcmp(PT.oper, "@dotp") == 0)
expanded_line = matrix_dot_product(find_array_index(PT.rhs1),find_array_index(PT.rhs2));
else if (strcmp(PT.oper, "@add") == 0)
expanded_line = matrix_addition(find_array_index(PT.lhs),find_array_index(PT.rhs1),find_array_index(PT.rhs2));
else if (strcmp(PT.oper, "@mmult") == 0)
expanded_line = matrix_multiplication(find_array_index(PT.lhs),find_array_index(PT.rhs1),find_array_index(PT.rhs2));
else if (strcmp(PT.oper, "@sum") == 0)
expanded_line = reduction_operations_sum(find_array_index(PT.lhs));
else if (strcmp(PT.oper, "@aver") == 0)
expanded_line = reduction_operations_aver(find_array_index(PT.lhs));
if (expanded_line != NULL) {
print_with_spaces(expanded_line);
} else {
sprintf(undefined_string, "Undefined directive: %s", PT.oper);
print_with_spaces(undefined_string);
}
free(expanded_line);
}
void print_with_spaces(const char* expanded_line) {
char spaces[line_left_space_count + 1]; // +1 for the null terminator
memset(spaces, ' ', line_left_space_count);
spaces[line_left_space_count] = '\0';
const char *temp = expanded_line;
while (*temp) {
fprintf(expanded_file, "%s", spaces); // Print spaces at the beginning of each line
// Print characters until a newline or end of string
while (*temp && *temp != '\n') {
fputc(*temp, expanded_file);
temp++;
}
// Handle newline at the end of line
if (*temp == '\n') {
fputc(*temp, expanded_file);
temp++;
} else {
// If we reach the end of the string without a newline, add one
fputc('\n', expanded_file);
}
}
}
int find_array_index(const char* array_name) {
for (int i = 0; i < 20 && AT[i].name[0] != '\0'; i++) {
if (strcmp(AT[i].name, array_name) == 0) {
return i;
}
}
printf("ERR: Undefined Array: %s\n", array_name);
return -1;
}
char* ltrim(char* s, int* left_trim_space_count)
{
*left_trim_space_count = 0;
while(isspace(*s)) {
(*left_trim_space_count)++;
s++;
}
return s;
}
char *rtrim(char* s)
{
char* back = s + strlen(s);
while(isspace(*--back));
*(back+1) = '\0';
return s;
}
char* trim(char* s, int* left_trim_space_count)
{
return rtrim(ltrim(s, left_trim_space_count));
}