commit a4132f919b44f8e35e89f4738f6f724b294d60f4
parent dc9974fb600d123869216885a9ae182adee84b31
Author: Eyal Sawady <ecs@d2evs.net>
Date: Sat, 19 Dec 2020 19:33:51 -0500
Decide at runtime which compilation stages to run
For example, if HA_STAGE=lex is set in the environment, only run the
lexer. This makes it easier to test the lexer in a scenario where the
parser would abort.
Diffstat:
M | src/main.c | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 45 insertions(+), 0 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -1,20 +1,65 @@
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include "ast.h"
#include "check.h"
#include "lex.h"
#include "parse.h"
+enum stage {
+ STAGE_LEX,
+ STAGE_PARSE,
+ STAGE_CHECK,
+ STAGE_GEN,
+ STAGE_EMIT,
+};
+
+enum stage
+parse_stage(const char *s)
+{
+ if (s == NULL) {
+ return STAGE_GEN;
+ } else if (strcmp(s, "lex") == 0) {
+ return STAGE_LEX;
+ } else if (strcmp(s, "parse") == 0) {
+ return STAGE_PARSE;
+ } else if (strcmp(s, "check") == 0) {
+ return STAGE_CHECK;
+ } else if (strcmp(s, "gen") == 0) {
+ return STAGE_GEN;
+ } else if (strcmp(s, "emit") == 0) {
+ return STAGE_EMIT;
+ } else {
+ fprintf(stderr, "Unknown HA_STAGE value '%s'\n", s);
+ exit(1);
+ }
+}
+
int
main(int argc, char *argv[])
{
+ enum stage stage = parse_stage(getenv("HA_STAGE"));
+
struct lexer lexer;
lex_init(&lexer, stdin);
+ if (stage == STAGE_LEX) {
+ struct token tok;
+ while (lex(&lexer, &tok) != T_EOF);
+ lex_finish(&lexer);
+ return 0;
+ }
struct ast_unit aunit = {0};
parse(&lexer, &aunit.subunits);
lex_finish(&lexer);
+ if (stage == STAGE_PARSE) {
+ return 0;
+ }
struct unit unit = {0};
check(&aunit, &unit);
+ if (stage == STAGE_CHECK) {
+ return 0;
+ }
return 0;
}