TC & ASC Implemented. yyerrors updated.

This commit is contained in:
Scarlett
2025-04-25 22:14:31 -04:00
parent 55116599f8
commit 3ea41f40fa
7 changed files with 148 additions and 46 deletions

View File

@ -2,8 +2,7 @@
/* The Translators - Spring 2025 */
#include "runner.h"
FILE *ir_flag = NULL;
//Constant_Stack *head = NULL;
int main(int argc, char *argv[]) {
if (argc == 1) {
fprintf(stderr, INVALID);
@ -36,7 +35,6 @@ int main(int argc, char *argv[]) {
alpha_file = fopen(argv[argc - 1], "r");
}
}
cg_flag = fopen("cg.s", "w");
return run(alpha_file);
}
@ -127,8 +125,6 @@ int run(FILE *alpha) {
fseek(alpha, 0, SEEK_SET);
// print_code_lines();
yyin = alpha;
yyparse();
@ -140,15 +136,14 @@ int run(FILE *alpha) {
}
if (st_flag != NULL) {
printdebug("[-st] Symbol Table is enabled.");
print_symbol_table(top, st_flag);
emit_as_file(stdout, begin);
fclose(st_flag);
generate();
fclose(cg_flag);
}
if (asc_flag != NULL) {
printdebug("[-asc] Annotated Source Code is enabled.");
print_code_lines();
fclose(asc_flag);
}
@ -157,14 +152,16 @@ int run(FILE *alpha) {
}
if (ir_flag != NULL) {
printf("Flag -ir is not implemented yet\n");
printdebug("[-ir] Intermediate code is enabled.");
emit_as_file(ir_flag, begin);
fclose(ir_flag);
}
//if (cg_flag != NULL) {
// printf("Flag -cg is not implemented yet\n");
//fclose(cg_flag);
//}
if (cg_flag != NULL) {
printdebug("[-cg] Code generation is enabled.");
generate();
fclose(cg_flag);
}
if (yyin != NULL) {
fclose(yyin);
@ -250,7 +247,26 @@ int is_alpha_file(char *alpha, int file_len) {
return 0; // is alpha file
}
void insert_code_line(char *line, int line_number) {
void insert_code_line(char * error_message, int line_number) {
CodeLine *error_line = malloc(sizeof(CodeLine));
error_line->line_number = line_number;
error_line->line = malloc(strlen(error_message) + 1);
strcpy(error_line->line, error_message);
error_line->next = NULL;
error_line->is_error = true;
if (error_line == NULL || code_head == NULL) return;
int line = error_line->line_number;
CodeLine *current = code_head;
while (current != NULL) {
if (current->line_number == line) {
CodeLine *next_code_line = current->next;
current->next = error_line;
error_line->next = next_code_line;
}
current = current->next;
}
}
void append_code_line(CodeLine *code_line) {
@ -268,9 +284,18 @@ void append_code_line(CodeLine *code_line) {
}
void print_code_lines() {
if (code_head == NULL) {
printf("No code lines to print.\n");
return;
}
CodeLine *current = code_head;
while (current != NULL) {
printf("%d %03d: %s", current->line_number, current->line);
if (current->is_error) {
fprintf(asc_flag, "%s", current->line);
} else {
fprintf(asc_flag, "%03d: %s", current->line_number, current->line);
}
current = current->next;
}
}