-
Notifications
You must be signed in to change notification settings - Fork 0
/
secondRun.c
163 lines (131 loc) · 4.65 KB
/
secondRun.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
/*
====================================================================================
Name : secondRun.c
Description: This file holds methods relevant to the second transition run
====================================================================================
*/
#include <stdio.h>
#include "operationEncoder.h"
#include "consts.h"
#include "structs.h"
#include "functions.h"
#include "secondRun.h"
/*
* Description: Executes the second transition
* Input: 1. Input file handle
* 2. Name of input file
* 3. Code length in memory word
* 4. Data length in memory word
*/
void second_transition_execute(FILE* pFile, char* file_name_without_extension,
unsigned int previous_transition_ic, unsigned int previous_transition_dc) {
compiler_output_files output_files;
int line_number = 0;
/* Creates transition first data */
transition_data* transition = create_transition_data();
if (transition == NULL) {
return;
}
/* Create ob file */
output_files.ob_file = create_output_file(file_name_without_extension, CODE_FILE_EXT);
if (output_files.ob_file == NULL) {
return;
}
output_files.entry_file = NULL;
output_files.extern_file = NULL;
/* Write first line to ob file */
fprintf(output_files.ob_file,"%x %x\n",previous_transition_ic,previous_transition_dc);
/* Initializes IC to zero*/
transition->IC = 0;
/* Reads all code lines */
while (!feof(pFile) && !transition->is_runtimer_error) {
char line[MAX_LINE_LENGTH];
line_number++;
if (fgets(line, MAX_LINE_LENGTH + 1, pFile)) {
/* This isn't an empty line or a comment */
if (!is_empty_or_comment(line)) {
transition->current_line_information =
create_line_info(file_name_without_extension, line_number, line);
/* Process line */
if (transition->current_line_information != NULL) {
second_transition_process_line(transition, &output_files);
free(transition->current_line_information);
} else {
transition->is_runtimer_error = true;
}
}
}
}/*end of while*/
/* No error has occurred */
if (!transition->is_runtimer_error && !transition->is_compiler_error) {
/* Write data initialization section into ob file */
write_data_to_output_file(output_files.ob_file);
/* Close files */
if (output_files.ob_file != NULL) {
fclose(output_files.ob_file);
}
if (output_files.extern_file != NULL) {
fclose(output_files.extern_file);
}
if (output_files.entry_file != NULL) {
fclose(output_files.entry_file);
}
} else {
/* Close and delete file */
if (output_files.ob_file != NULL) {
char* full_name = allocate_string(strlen(file_name_without_extension) + strlen(CODE_FILE_EXT));
fclose(output_files.ob_file);
if (full_name != NULL) {
strcpy(full_name, file_name_without_extension);
strcat(full_name, CODE_FILE_EXT);
remove(full_name);
free(full_name);
} else {
print_runtime_error("Couldn't delete compilation files");
}
}
}
}
/*
* Description: Processes a line in input file
* Input: 1. Current transition data
* 2. Output files
*/
void second_transition_process_line(transition_data* transition, compiler_output_files* output_files) {
char* type = NULL;
int index;
/* Skips label if exists */
skip_label(transition->current_line_information);
index = transition->current_line_information->current_index;
/* Read line type */
type = get_next_word(transition);
/*if there is no type*/
if (type == NULL) {
print_compiler_error("Invalid line", transition->current_line_information);
transition->is_compiler_error = true;
}/*end of compiler error*/
/* Line is data initialization - Ignores it */
else if ((strcmp(type, DATA_OPERATION) == 0) || (strcmp(type, STRING_OPERATION) == 0)) {
/* Ignore */
}/*end of else if data*/
/*Line is extern */
else if (strcmp(type, EXTERN_OPERATION) == 0) {
output_files->extern_file = create_output_file(transition->current_line_information->file_name, EXTERN_FILE_EXT);
if (output_files->extern_file == NULL) {
transition->is_runtimer_error = true;
}
}/*end of else if extern*/
/*Line is entry*/
else if (strcmp(type, ENTRY_OPERATION) == 0) {
/* Process entry */
second_transition_process_entry(transition, output_files);
}/*end of else if entry*/
else {
/* Process operation */
transition->current_line_information->current_index = index;
second_transition_process_operation(transition, output_files);
}/*end of else*/
if (type != NULL) {
free(type);
}
}