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"
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) {
fprintf(stderr, INVALID);
return -1;
@ -36,9 +29,7 @@ int main(int argc, char *argv[]) {
} else {
for (int i = 1; i < argc - 1; i++) {
if (check_flag(argv[i], argv[argc - 1]) != 0) {
fprintf(stderr,
"INVALID FLAG(S): Use -help to "
"view valid inputs \n");
fprintf(stderr, INVALID);
return -1;
}
}
@ -61,11 +52,30 @@ int check_flag(char *arg, char *alpha) {
}
fprintf(stderr, "FLAGS REPEAT\n");
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) {
if (tc_flag == NULL) {
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) {
DEBUG = true;
return 0;
@ -76,7 +86,6 @@ int check_flag(char *arg, char *alpha) {
}
void incr(int lnum, int cnum, int tok) {
// if (tok == COMMENT) {
for (int i = 0; i < yyleng; i++) {
if (yytext[i] == '\n') {
line_number++;
@ -84,14 +93,10 @@ void incr(int lnum, int cnum, int tok) {
}
column_number++;
}
// }else{
// column_number += yyleng;
// }
}
void print_tok(int tok) {
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok,
yytext);
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok, yytext);
}
int run(FILE *alpha) {
@ -104,71 +109,34 @@ int run(FILE *alpha) {
return -1;
}
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();
if (cur == NULL) {
printdebug("%s[FATAL] cur is null", COLOR_LIGHTRED);
}
if (top == NULL) {
printdebug("%s[FATAL] top is null", COLOR_LIGHTRED);
}
if (st_flag != NULL) {
print_symbol_table(top, 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) {
//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();
FILE *f = fdopen(1, "w");
print_symbol_table(top, f);
fclose(f);
if (yyin != NULL) {
fclose(yyin);
@ -207,11 +175,10 @@ int new_file(char *arg, char *alpha) {
type_len = TOK_LEN;
} else if (strcmp(arg, "-st") == 0) {
type_len = ST_LEN;
} else if (strcmp(arg, "-tc") == 0){
} else if (strcmp(arg, "-tc") == 0) {
type_len = TC_LEN;
}else {
fprintf(stderr,
"INVALID FLAG: Use -help to view valid inputs\n");
} else {
fprintf(stderr, INVALID);
return -1;
}
@ -229,9 +196,14 @@ int new_file(char *arg, char *alpha) {
tok_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-st") == 0) {
st_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-asc") == 0) {
asc_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-tc") == 0) {
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;
}

View File

@ -6,15 +6,18 @@
#define ST_LEN 2
#define TC_LEN 2
#define HELP \
"HELP:\nHow to run the alpha compiler:\n./alpha [options] " \
"program\nValid " \
"options:\n-tok output the token number, token, line number, and " \
"column " \
"number for each of the tokens to the .tok file\n-st output the " \
"symbol " \
"table for the program to the .st file\n-help print this message " \
"and exit " \
"the alpha compiler\n-debug print debugging messages in grammar rules \n"
"HELP:\n" \
" How to run the alpha compiler:\n" \
" ./alpha [options] program\n" \
"Valid options:\n" \
" -tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n" \
" -st output the symbol table for the program to the .st file\n" \
" -asc output the annotated source code for the program to the .asc file, including syntax errors\n" \
" -tc run the type checker and report type errors to the .asc file\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 INVALID \
@ -42,6 +45,9 @@ FILE *alpha_file;
FILE *tok_flag = NULL;
FILE *st_flag = NULL;
FILE *tc_flag = NULL;
FILE *ir_flag = NULL;
FILE *cg_flag = NULL;
FILE *asc_flag = NULL;
bool DEBUG = false;
int no_flag = 0;
int arg;