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,242 +4,214 @@
#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;
}
if (argc == 1) {
fprintf(stderr, INVALID);
return -1;
else if (argc == 2) {
if (is_help(argv[1])) {
return 0;
} else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) {
no_flag = SET_FLAG;
alpha_file = fopen(argv[1], "r");
} else {
fprintf(stderr, INVALID);
return -1;
}
}
else if (argc == 2) {
if (is_help(argv[1])) {
return 0;
} else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) {
no_flag = SET_FLAG;
alpha_file = fopen(argv[1], "r");
} else {
fprintf(stderr, INVALID);
return -1;
else {
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) !=
0) {
fprintf(stderr, INVALID);
return -1;
} else {
for (int i = 1; i < argc - 1; i++) {
if (check_flag(argv[i], argv[argc - 1]) != 0) {
fprintf(stderr, INVALID);
return -1;
}
}
alpha_file = fopen(argv[argc - 1], "r");
}
else {
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) !=
0) {
fprintf(stderr, INVALID);
return -1;
} 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");
return -1;
}
}
alpha_file = fopen(argv[argc - 1], "r");
}
}
return run(alpha_file);
}
return run(alpha_file);
}
int check_flag(char *arg, char *alpha) {
if (strcmp("-tok", arg) == 0) {
if (tok_flag == NULL) {
return new_file(arg, alpha);
}
fprintf(stderr, "FLAGS REPEAT\n");
return -1;
} else if (strcmp("-st", arg) == 0) {
if (st_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);
}
} else if (strcmp("-debug", arg) == 0) {
DEBUG = true;
return 0;
} else {
fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n");
return -1;
if (strcmp("-tok", arg) == 0) {
if (tok_flag == NULL) {
return new_file(arg, alpha);
}
fprintf(stderr, "FLAGS REPEAT\n");
return -1;
} else if (strcmp("-st", arg) == 0) {
if (st_flag == NULL) {
return new_file(arg, 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;
} else {
fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n");
return -1;
}
}
void incr(int lnum, int cnum, int tok) {
// if (tok == COMMENT) {
for (int i = 0; i < yyleng; i++) {
if (yytext[i] == '\n') {
line_number++;
column_number = 0;
}
column_number++;
for (int i = 0; i < yyleng; i++) {
if (yytext[i] == '\n') {
line_number++;
column_number = 0;
}
// }else{
// column_number += yyleng;
// }
column_number++;
}
}
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) {
int token;
top = cur = init(CreateScope(NULL, 1, 1));
int token;
top = cur = init(CreateScope(NULL, 1, 1));
// If file is not found
if (alpha == NULL) {
fprintf(stderr, "INPUT FILE NOT FOUND\n");
return -1;
}
yyin = alpha;
// If file is not found
if (alpha == NULL) {
fprintf(stderr, "INPUT FILE NOT FOUND\n");
return -1;
}
yyin = alpha;
yyparse();
// 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 (st_flag != NULL) {
print_symbol_table(top, st_flag);
fclose(st_flag);
}
if (yyin != NULL) {
fclose(yyin);
}
return 0;
}
*/
if (st_flag != NULL) {
yyparse();
if (asc_flag != NULL) {
printf("Flag -asc is not implemented yet\n");
fclose(asc_flag);
}
if (cur == NULL) {
printdebug("%s[FATAL] cur is null", COLOR_LIGHTRED);
}
if (tc_flag != NULL) {
printf("Flag -tc is not implemented yet\n");
fclose(tc_flag);
}
if (top == NULL) {
printdebug("%s[FATAL] top is null", COLOR_LIGHTRED);
}
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);
}
print_symbol_table(top, st_flag);
fclose(st_flag);
if (yyin != NULL) {
fclose(yyin);
}
return 0;
}
yyparse();
if (tc_flag != NULL) {
//SCARLETT NEEDS TO ADD THIS FUNCTIONALITY
}
if (yyin != NULL) {
fclose(yyin);
}
yyparse();
FILE *f = fdopen(1, "w");
print_symbol_table(top, f);
fclose(f);
if (yyin != NULL) {
fclose(yyin);
}
return 0;
return 0;
}
bool is_help(char *input) {
if (strcmp(input, "-help") == 0) {
printf("%s", HELP);
return true;
}
if (strcmp(input, "-help") == 0) {
printf("%s", HELP);
return true;
}
return false;
return false;
}
int new_file(char *arg, char *alpha) {
int type_len;
const char *basename = alpha;
const char *slash = strchr(alpha, '/');
int type_len;
const char *basename = alpha;
const char *slash = strchr(alpha, '/');
while (slash != NULL) {
basename = slash + 1;
slash = strchr(basename, '/');
}
while (slash != NULL) {
basename = slash + 1;
slash = strchr(basename, '/');
}
mkdir("./out", 0777);
mkdir("./out", 0777);
char *new_basename = calloc(strlen(basename) + 5, sizeof(char));
strcpy(new_basename, "./out/");
strcat(new_basename, basename);
basename = new_basename;
char *new_basename = calloc(strlen(basename) + 5, sizeof(char));
strcpy(new_basename, "./out/");
strcat(new_basename, basename);
basename = new_basename;
if (strcmp(arg, "-tok") == 0) {
type_len = TOK_LEN;
} else if (strcmp(arg, "-st") == 0) {
type_len = ST_LEN;
} else if (strcmp(arg, "-tc") == 0){
type_len = TC_LEN;
}else {
fprintf(stderr,
"INVALID FLAG: Use -help to view valid inputs\n");
return -1;
}
if (strcmp(arg, "-tok") == 0) {
type_len = TOK_LEN;
} else if (strcmp(arg, "-st") == 0) {
type_len = ST_LEN;
} else if (strcmp(arg, "-tc") == 0) {
type_len = TC_LEN;
} else {
fprintf(stderr, INVALID);
return -1;
}
// calculate lengths
int basename_len = strlen(basename);
char *file_name =
calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
// calculate lengths
int basename_len = strlen(basename);
char *file_name =
calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
// coy filename and add extension
strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
strcat(file_name, ".");
strcat(file_name, arg + 1);
// coy filename and add extension
strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
strcat(file_name, ".");
strcat(file_name, arg + 1);
if (strcmp(arg, "-tok") == 0) {
tok_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-st") == 0) {
st_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-tc") == 0) {
tc_flag = fopen(file_name, "w");
}
return 0;
if (strcmp(arg, "-tok") == 0) {
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;
}
int is_alpha_file(char *alpha, int file_len) {
if (strcmp(".alpha",
alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
return -1; // not alpha file
}
return 0; // is alpha file
if (strcmp(".alpha",
alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
return -1; // not alpha file
}
return 0; // is alpha file
}

View File

@ -5,20 +5,23 @@
#define TOK_LEN 3
#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"
#define HELP \
"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 \
"INVALID INPUT: Include a .alpha file or use -help for more inputs \n"
#define SET_FLAG 1 // Used to set flags for arg types
#define INVALID \
"INVALID INPUT: Include a .alpha file or use -help for more inputs \n"
#include <stdbool.h>
#include <stdio.h>
@ -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;