G

G Programming Language
git clone http://git.omkov.net/G
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2021-09-08 01:08:19
Commit6070d4c635e627855f43977a16b501b8bf47ecc6
Parent534efc4ea454ffe0ef4b995f035fea749ea07676

lex: Tidy up lexer debug

Diffstat

M src/compile.c | 25 ++++++++-----------------
M src/g.h | 3 +++
M src/lex.c | 7 +++++++

3 files changed, 18 insertions, 17 deletions

diff --git a/src/compile.c b/src/compile.c
index 7ff2691..fcb5e03 100644
--- a/src/compile.c
+++ b/src/compile.c
@@ -13,34 +13,24 @@
 
 #include "cll/error.h"
 
+#include <llvm-c/Core.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 
 bool lflag = false, pflag = false;
 
 void compile(u8 *src, UINT len) {
-	if (lflag) {
-		lex l = lex_init(src, len);
-		
-		for (tok t = lex_next(&l); t.k != LK_EOF; free(t.s), t = lex_next(&l)) {
-			printf("%zu:%zu: %s \"%s\"\n", t.p.ln, t.p.cl+1, tok_ks[t.k], t.s);
-		}
-	}
-	else if (pflag) {
-		
-	}
-	else { goto compile; }
-	return;
-	
-compile:;
 	lex l = lex_init(src, len);
-	ast *a = parse(&l);
+	if (lflag) { lex_debug(&l); goto ret; }
 
-	ast_print(a, 0);
+	ast *a = parse(&l);
+	if (pflag) { ast_print(a, 0); goto ret; }
 
 	llvm_test(a);
 
-	return;
+ret:;
+	LLVMShutdown(); return;
 }
 
 void compile_file(const char *file) {
diff --git a/src/g.h b/src/g.h
index 8372aa2..97f975d 100644
--- a/src/g.h
+++ b/src/g.h
@@ -60,4 +60,6 @@ extern void ast_free(ast *a);
 
 extern ast *parse(lex *l);
 
+extern void lex_debug(lex *l);
+
 #endif // G_G_H_PHZNJHJ7
diff --git a/src/lex.c b/src/lex.c
index 0f7a496..ea5914b 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -122,3 +122,10 @@ tok lex_kind(lex *l, tok_k k) {
 
 /* Return the current token. */
 tok lex_peek(lex *l) { return l->t; }
+
+/* Print lexer debug output and exit. */
+void lex_debug(lex *l) {
+	for (tok t = lex_next(l); t.k != LK_EOF; free(t.s), t = lex_next(l)) {
+		printf("%zu:%zu: %s \"%s\"\n", t.p.ln, t.p.cl + 1, tok_ks[t.k], t.s);
+	}
+}