flag updates

This commit is contained in:
Scarlett
2025-04-15 16:25:51 -04:00
parent b7c6ebb3f2
commit b325548b97
2 changed files with 184 additions and 206 deletions

View File

@ -4,13 +4,6 @@
#include "runner.h" #include "runner.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// if last argument is debug then set to true and ignore it for the rest
// of this file
//if (argc > 1 && strcmp(argv[argc - 1], "-debug") == 0) {
// DEBUG = true;
// argc--;
// }
if (argc == 1) { if (argc == 1) {
fprintf(stderr, INVALID); fprintf(stderr, INVALID);
return -1; return -1;
@ -36,9 +29,7 @@ int main(int argc, char *argv[]) {
} else { } else {
for (int i = 1; i < argc - 1; i++) { for (int i = 1; i < argc - 1; i++) {
if (check_flag(argv[i], argv[argc - 1]) != 0) { if (check_flag(argv[i], argv[argc - 1]) != 0) {
fprintf(stderr, fprintf(stderr, INVALID);
"INVALID FLAG(S): Use -help to "
"view valid inputs \n");
return -1; return -1;
} }
} }
@ -61,11 +52,30 @@ int check_flag(char *arg, char *alpha) {
} }
fprintf(stderr, "FLAGS REPEAT\n"); fprintf(stderr, "FLAGS REPEAT\n");
return -1; return -1;
} else if (strcmp("-asc", arg) == 0) {
if (asc_flag == NULL) {
return new_file(arg, alpha);
}
fprintf(stderr, "FLAGS REPEAT\n");
return -1;
} else if (strcmp("-tc", arg) == 0) { } else if (strcmp("-tc", arg) == 0) {
if (tc_flag == NULL) { if (tc_flag == NULL) {
return new_file(arg, alpha); return new_file(arg, alpha);
} }
fprintf(stderr, "FLAGS REPEAT\n");
return -1;
} else if (strcmp("-ir", arg) == 0) {
if (ir_flag == NULL) {
return new_file(arg, alpha);
}
fprintf(stderr, "FLAGS REPEAT\n");
return -1;
} else if (strcmp("-cg", arg) == 0) {
if (cg_flag == NULL) {
return new_file(arg, alpha);
}
fprintf(stderr, "FLAGS REPEAT\n");
return -1;
} else if (strcmp("-debug", arg) == 0) { } else if (strcmp("-debug", arg) == 0) {
DEBUG = true; DEBUG = true;
return 0; return 0;
@ -76,7 +86,6 @@ int check_flag(char *arg, char *alpha) {
} }
void incr(int lnum, int cnum, int tok) { void incr(int lnum, int cnum, int tok) {
// if (tok == COMMENT) {
for (int i = 0; i < yyleng; i++) { for (int i = 0; i < yyleng; i++) {
if (yytext[i] == '\n') { if (yytext[i] == '\n') {
line_number++; line_number++;
@ -84,14 +93,10 @@ void incr(int lnum, int cnum, int tok) {
} }
column_number++; column_number++;
} }
// }else{
// column_number += yyleng;
// }
} }
void print_tok(int tok) { void print_tok(int tok) {
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok, fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok, yytext);
yytext);
} }
int run(FILE *alpha) { int run(FILE *alpha) {
@ -104,71 +109,34 @@ int run(FILE *alpha) {
return -1; return -1;
} }
yyin = alpha; yyin = alpha;
// TOK FLAG
// if (tok_flag != NULL) {
// while (0 != (token = yylex())) {
// if (tok_flag != NULL) {
// fprintf(tok_flag, "%d %d %3d \"%s\"\n",
// line_number, column_number,
// token, yytext);
// }
/*if (token == COMMENT) {
for (int i = 0; i < yyleng; i++) {
if (yytext[i] == '\n') {
line_number++;
column_number = 0;
}
column_number++;
}
continue;
}
if (token == 1999) {
printf(
"On line number %d and column
number %d we have an invalid " "character:%s\n",
line_number, column_number,
yytext);
}
column_number += yyleng; */
/* }
fclose(tok_flag);
if (yyin != NULL) {
fclose(yyin);
}
return 0;
}
*/
if (st_flag != NULL) {
yyparse(); yyparse();
if (cur == NULL) { if (st_flag != NULL) {
printdebug("%s[FATAL] cur is null", COLOR_LIGHTRED);
}
if (top == NULL) {
printdebug("%s[FATAL] top is null", COLOR_LIGHTRED);
}
print_symbol_table(top, st_flag); print_symbol_table(top, st_flag);
fclose(st_flag); fclose(st_flag);
if (yyin != NULL) {
fclose(yyin);
} }
return 0;
if (asc_flag != NULL) {
printf("Flag -asc is not implemented yet\n");
fclose(asc_flag);
} }
if (tc_flag != NULL) { if (tc_flag != NULL) {
//SCARLETT NEEDS TO ADD THIS FUNCTIONALITY printf("Flag -tc is not implemented yet\n");
fclose(tc_flag);
}
if (ir_flag != NULL) {
printf("Flag -ir is not implemented yet\n");
fclose(ir_flag);
}
if (cg_flag != NULL) {
printf("Flag -cg is not implemented yet\n");
fclose(cg_flag);
} }
yyparse(); yyparse();
FILE *f = fdopen(1, "w");
print_symbol_table(top, f);
fclose(f);
if (yyin != NULL) { if (yyin != NULL) {
fclose(yyin); fclose(yyin);
@ -207,11 +175,10 @@ int new_file(char *arg, char *alpha) {
type_len = TOK_LEN; type_len = TOK_LEN;
} else if (strcmp(arg, "-st") == 0) { } else if (strcmp(arg, "-st") == 0) {
type_len = ST_LEN; type_len = ST_LEN;
} else if (strcmp(arg, "-tc") == 0){ } else if (strcmp(arg, "-tc") == 0) {
type_len = TC_LEN; type_len = TC_LEN;
}else { } else {
fprintf(stderr, fprintf(stderr, INVALID);
"INVALID FLAG: Use -help to view valid inputs\n");
return -1; return -1;
} }
@ -229,9 +196,14 @@ int new_file(char *arg, char *alpha) {
tok_flag = fopen(file_name, "w"); tok_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-st") == 0) { } else if (strcmp(arg, "-st") == 0) {
st_flag = fopen(file_name, "w"); st_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-asc") == 0) {
asc_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-tc") == 0) { } else if (strcmp(arg, "-tc") == 0) {
tc_flag = fopen(file_name, "w"); tc_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-ir") == 0) {
ir_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-cg") == 0) {
cg_flag = fopen(file_name, "w");
} }
return 0; return 0;
} }

View File

@ -6,15 +6,18 @@
#define ST_LEN 2 #define ST_LEN 2
#define TC_LEN 2 #define TC_LEN 2
#define HELP \ #define HELP \
"HELP:\nHow to run the alpha compiler:\n./alpha [options] " \ "HELP:\n" \
"program\nValid " \ " How to run the alpha compiler:\n" \
"options:\n-tok output the token number, token, line number, and " \ " ./alpha [options] program\n" \
"column " \ "Valid options:\n" \
"number for each of the tokens to the .tok file\n-st output the " \ " -tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n" \
"symbol " \ " -st output the symbol table for the program to the .st file\n" \
"table for the program to the .st file\n-help print this message " \ " -asc output the annotated source code for the program to the .asc file, including syntax errors\n" \
"and exit " \ " -tc run the type checker and report type errors to the .asc file\n" \
"the alpha compiler\n-debug print debugging messages in grammar rules \n" " -ir run the intermediate representation generator, writing output to the .ir file\n" \
" -cg run the (x86 assembly) code generator, writing output to the .s file\n" \
" -debug produce debugging messages to stderr\n" \
" -help print this message and exit the alpha compiler\n"
#define SET_FLAG 1 // Used to set flags for arg types #define SET_FLAG 1 // Used to set flags for arg types
#define INVALID \ #define INVALID \
@ -42,6 +45,9 @@ FILE *alpha_file;
FILE *tok_flag = NULL; FILE *tok_flag = NULL;
FILE *st_flag = NULL; FILE *st_flag = NULL;
FILE *tc_flag = NULL; FILE *tc_flag = NULL;
FILE *ir_flag = NULL;
FILE *cg_flag = NULL;
FILE *asc_flag = NULL;
bool DEBUG = false; bool DEBUG = false;
int no_flag = 0; int no_flag = 0;
int arg; int arg;