diff options
author | Bad Diode <bd@badd10de.dev> | 2022-04-18 16:45:52 -0300 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2022-04-18 16:45:52 -0300 |
commit | 7cf1451ab52586ed9c4eeae1b1ec3b4ebaa83393 (patch) | |
tree | a427a42f549b1dcbc916ed635091a19ee6f489ce /src | |
parent | 3da041f2e17fdeb69bf345aadf89c5fcc1814260 (diff) | |
download | bdl-7cf1451ab52586ed9c4eeae1b1ec3b4ebaa83393.tar.gz bdl-7cf1451ab52586ed9c4eeae1b1ec3b4ebaa83393.zip |
Add cmd options for viz of different compile stages
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 39 |
1 files changed, 34 insertions, 5 deletions
@@ -12,6 +12,15 @@ | |||
12 | #include "semantic.c" | 12 | #include "semantic.c" |
13 | #include "viz.c" | 13 | #include "viz.c" |
14 | 14 | ||
15 | typedef enum ExecMode { | ||
16 | RUN_NORMAL, | ||
17 | PRINT_LEX, | ||
18 | PRINT_PARSE, | ||
19 | PRINT_SEMANTIC, | ||
20 | } ExecMode; | ||
21 | |||
22 | static ExecMode mode = RUN_NORMAL; | ||
23 | |||
15 | void | 24 | void |
16 | init(void) { | 25 | init(void) { |
17 | // STUB | 26 | // STUB |
@@ -27,16 +36,23 @@ process_source(const StringView *source, const char *file_name) { | |||
27 | // Read tokens. | 36 | // Read tokens. |
28 | Token *tokens = tokenize(source); | 37 | Token *tokens = tokenize(source); |
29 | check_errors(file_name); | 38 | check_errors(file_name); |
30 | // print_tokens(tokens); | 39 | if (mode == PRINT_LEX) { |
40 | print_tokens(tokens); | ||
41 | return; | ||
42 | } | ||
31 | 43 | ||
32 | // Parser. | 44 | // Parser. |
33 | Root *roots = parse(tokens); | 45 | Root *roots = parse(tokens); |
34 | check_errors(file_name); | 46 | check_errors(file_name); |
35 | // viz_ast(roots); | 47 | if (mode == PRINT_PARSE) { |
48 | viz_ast(roots); | ||
49 | } | ||
36 | 50 | ||
37 | // Symbol table generation and type checking. | 51 | // Symbol table generation and type checking. |
38 | ParseTree *parse_tree = semantic_analysis(roots); | 52 | ParseTree *parse_tree = semantic_analysis(roots); |
39 | viz_ast(parse_tree->roots); | 53 | if (mode == PRINT_SEMANTIC) { |
54 | viz_ast(parse_tree->roots); | ||
55 | } | ||
40 | } | 56 | } |
41 | 57 | ||
42 | void | 58 | void |
@@ -99,7 +115,8 @@ void | |||
99 | print_usage(void) { | 115 | print_usage(void) { |
100 | printf("Usage: %s [options] <filename filename ...>\n", BIN_NAME); | 116 | printf("Usage: %s [options] <filename filename ...>\n", BIN_NAME); |
101 | printf("\n"); | 117 | printf("\n"); |
102 | printf("\t-h \tShow usage.\n"); | 118 | printf("\t-h \t\tShow usage.\n"); |
119 | printf("\t-p [l | p | s]\tPrint mode for [l]exing, [p]arsing or [s]emantic analysis\n"); | ||
103 | printf("\n"); | 120 | printf("\n"); |
104 | } | 121 | } |
105 | 122 | ||
@@ -108,12 +125,24 @@ main(int argc, char *argv[]) { | |||
108 | init(); | 125 | init(); |
109 | 126 | ||
110 | int option; | 127 | int option; |
111 | while ((option = getopt(argc, argv, "h")) != -1) { | 128 | while ((option = getopt(argc, argv, "hp:")) != -1) { |
112 | switch (option) { | 129 | switch (option) { |
113 | case 'h': { | 130 | case 'h': { |
114 | print_usage(); | 131 | print_usage(); |
115 | goto exit_success; | 132 | goto exit_success; |
116 | } break; | 133 | } break; |
134 | case 'p': { | ||
135 | if (optarg[0] == 'l' && optarg[1] == '\0') { | ||
136 | mode = PRINT_LEX; | ||
137 | } else if (optarg[0] == 'p' && optarg[1] == '\0') { | ||
138 | mode = PRINT_PARSE; | ||
139 | } else if (optarg[0] == 's' && optarg[1] == '\0') { | ||
140 | mode = PRINT_SEMANTIC; | ||
141 | } else { | ||
142 | print_usage(); | ||
143 | return EXIT_FAILURE; | ||
144 | } | ||
145 | } break; | ||
117 | default: { | 146 | default: { |
118 | print_usage(); | 147 | print_usage(); |
119 | return EXIT_FAILURE; | 148 | return EXIT_FAILURE; |