added -help functionality

This commit is contained in:
Annie
2025-02-18 14:22:28 -05:00
parent 910760661e
commit 7a52aa51bf
2 changed files with 16 additions and 1 deletions

View File

@ -6,10 +6,15 @@ int main(int argc, char *argv[]) {
int token;
FILE *output;
if (argc == 1 || argc > 3 || (argc == 2 && is_alpha_file(argv[1], strlen(argv[1])) != 0)) {
if (argc == 1 || argc > 3) {
fprintf(stderr, "invalid input with 1, >3, or non .alpha arg \n");
return -1; //no alpha file or too many args
} else if (argc == 2) {
if (is_help(argv[1])) {
return 0;
} else if (is_alpha_file(argv[1], strlen(argv[1])) != 0) {
return -1;
}
arg = NO_ARG; //no argument but valid input
yyin = fopen(argv[1], "r");
} else {
@ -48,6 +53,14 @@ int main(int argc, char *argv[]) {
return 0;
}
bool is_help(char * input) {
if (strcmp(input, "-help") == 0) {
printf("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");
return true;
}
return false;
}
char *is_tok(int argc, char *argv[]) {
if (argc == 3 && strcmp("-tok", argv[1]) == 0) {
char *input_prog = argv[2];

View File

@ -15,6 +15,7 @@
#include <stdlib.h>
#include "flex.h"
#include "typedefs.h"
#include <stdbool.h>
extern int line_number, column_number;
extern char *yytext;
@ -24,3 +25,4 @@ int arg;
int main(int argc, char* argv[]);
char *is_tok(int argc, char* argv[]);
int is_alpha_file(char *file, int file_len);
bool is_help(char * input);