169 lines
4.6 KiB
C
169 lines
4.6 KiB
C
//#include "symbol_table.h"
|
|
#include "runner.h"
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc == 1) {
|
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs \n");
|
|
return -1;
|
|
} else if (argc == 2) {
|
|
//can be either -help or .alpha file
|
|
if (is_help(argv[1])) {
|
|
return 0;
|
|
} else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) {
|
|
//run flex for now
|
|
no_flag = SET_FLAG; //no argument but valid input
|
|
alpha_file = fopen(argv[1], "r");
|
|
|
|
} else {
|
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs\n");
|
|
return -1;
|
|
}
|
|
} else {
|
|
//last input must be .alpha
|
|
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
|
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file at end of input or use -help for more inputs \n");
|
|
return -1;
|
|
} else {
|
|
//now check that other args are valid (flags will not be null if flag is present)
|
|
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);
|
|
}
|
|
|
|
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 {
|
|
fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n");
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
int run(FILE *alpha) {
|
|
int token;
|
|
//check that file exists
|
|
if (alpha == NULL) {
|
|
fprintf(stderr, "INPUT FILE NOT FOUND\n");
|
|
return -1;
|
|
}
|
|
yyin = alpha;
|
|
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);
|
|
//return -1;
|
|
}
|
|
column_number += yyleng;
|
|
}
|
|
|
|
if (st_flag != NULL) {
|
|
//output symbol table, file pointer is
|
|
//print_symbol_table(top,st_flag);
|
|
}
|
|
|
|
if (yyin != NULL) {
|
|
fclose(yyin);
|
|
}
|
|
|
|
if (tok_flag != NULL) {
|
|
fclose(tok_flag);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
bool is_help(char * input) {
|
|
if (strcmp(input, "-help") == 0) {
|
|
printf("%s", HELP);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
int new_file(char *arg, char *alpha) {
|
|
int type_len;
|
|
const char *basename = alpha;
|
|
const char *slash = strchr(alpha, '/');
|
|
while (slash != NULL) {
|
|
basename = slash + 1;
|
|
slash = strchr(basename, '/');
|
|
}
|
|
if (strcmp(arg, "-tok") == 0) {
|
|
type_len = TOK_LEN;
|
|
} else if (strcmp(arg, "-st") == 0) {
|
|
type_len = ST_LEN;
|
|
} else {
|
|
fprintf(stderr, "INVALID FLAG: Use -help to view valid inputs\n");
|
|
return -1;
|
|
}
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
if (strcmp(arg, "-tok") == 0) {
|
|
tok_flag = fopen(file_name, "w");
|
|
} else if (strcmp(arg, "-st") == 0) {
|
|
st_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
|
|
}
|
|
|
|
void enter_scope(int line, int column){
|
|
curr = CreateScope(curr, line, column);
|
|
}
|
|
void exit_scope() {
|
|
if(curr->Parent_Scope == NULL){
|
|
printf("Can't close top");
|
|
return;
|
|
}
|
|
curr = curr->Parent_Scope;
|
|
}
|