From 7cf1451ab52586ed9c4eeae1b1ec3b4ebaa83393 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Mon, 18 Apr 2022 16:45:52 -0300 Subject: Add cmd options for viz of different compile stages --- src/main.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index cdf4167..56c36d7 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,15 @@ #include "semantic.c" #include "viz.c" +typedef enum ExecMode { + RUN_NORMAL, + PRINT_LEX, + PRINT_PARSE, + PRINT_SEMANTIC, +} ExecMode; + +static ExecMode mode = RUN_NORMAL; + void init(void) { // STUB @@ -27,16 +36,23 @@ process_source(const StringView *source, const char *file_name) { // Read tokens. Token *tokens = tokenize(source); check_errors(file_name); - // print_tokens(tokens); + if (mode == PRINT_LEX) { + print_tokens(tokens); + return; + } // Parser. Root *roots = parse(tokens); check_errors(file_name); - // viz_ast(roots); + if (mode == PRINT_PARSE) { + viz_ast(roots); + } // Symbol table generation and type checking. ParseTree *parse_tree = semantic_analysis(roots); - viz_ast(parse_tree->roots); + if (mode == PRINT_SEMANTIC) { + viz_ast(parse_tree->roots); + } } void @@ -99,7 +115,8 @@ void print_usage(void) { printf("Usage: %s [options] \n", BIN_NAME); printf("\n"); - printf("\t-h \tShow usage.\n"); + printf("\t-h \t\tShow usage.\n"); + printf("\t-p [l | p | s]\tPrint mode for [l]exing, [p]arsing or [s]emantic analysis\n"); printf("\n"); } @@ -108,12 +125,24 @@ main(int argc, char *argv[]) { init(); int option; - while ((option = getopt(argc, argv, "h")) != -1) { + while ((option = getopt(argc, argv, "hp:")) != -1) { switch (option) { case 'h': { print_usage(); goto exit_success; } break; + case 'p': { + if (optarg[0] == 'l' && optarg[1] == '\0') { + mode = PRINT_LEX; + } else if (optarg[0] == 'p' && optarg[1] == '\0') { + mode = PRINT_PARSE; + } else if (optarg[0] == 's' && optarg[1] == '\0') { + mode = PRINT_SEMANTIC; + } else { + print_usage(); + return EXIT_FAILURE; + } + } break; default: { print_usage(); return EXIT_FAILURE; -- cgit v1.2.1