G

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

AuthorJakob Wakeling <[email protected]>
Date2021-09-25 03:55:47
Commitf9a604aa2fc773b05be329c7e68d758d53965d48
Parent8c4b7796549fd132e0679b64502dbe96830c33fe

parse: Recreate parse.h

Diffstat

M src/compile.c | 8 ++++----
D src/g.h | 41 -----------------------------------------
M src/keyword.h | 2 +-
M src/lex.c | 4 ++--
M src/llvm/gen.c | 28 ++++++++++++++--------------
M src/llvm/llvm.h | 4 ++--
M src/parse.c | 54 ++++++++++++++++++++++++++----------------------------
A src/parse.h | 40 ++++++++++++++++++++++++++++++++++++++++
M src/util/ast.h | 10 +++++-----

9 files changed, 94 insertions, 97 deletions

diff --git a/src/compile.c b/src/compile.c
index d9ea135..8bb0de6 100644
--- a/src/compile.c
+++ b/src/compile.c
@@ -5,11 +5,11 @@
 

 
-#include "util/util.h"
 #include "compile.h"
-#include "g.h"
 #include "llvm/llvm.h"
+#include "parse.h"
 #include "util/ast.h"
+#include "util/util.h"
 
 #include "cll/error.h"
 
@@ -24,7 +24,7 @@ void compile(u8 *src, usize len) {
 	lex l = lex_init(src, len);
 	if (lflag) { lex_debug(&l); goto ret; }
 
-	ast *a = parse(&l);
+	ast a = parse(&l);
 	if (pflag) { ast_print(a, 0); goto ret; }
 
 	llvm_test(a);
diff --git a/src/g.h b/src/g.h
deleted file mode 100644
index 012b849..0000000
--- a/src/g.h
+++ /dev/null
@@ -1,41 +0,0 @@
-// g.h
-// Common header file for G
-// Copyright (C) 2021, Jakob Wakeling
-// All rights reserved.
-
-
-
-#ifndef G_G_H_PHZNJHJ7
-#define G_G_H_PHZNJHJ7
-
-#include "type.h"
-#include "util/stack.h"
-#include "value.h"
-#include "util/util.h"
-#include "lex.h"
-
-typedef enum {
-	AK_NIL,
-	
-	AK_DECL, AK_COMPOUND,
-	
-	AK_INT, AK_FLT,
-	
-	AK_PROC, AK_RETURN,
-} ast_k;
-
-typedef struct { usize ln, cl; } pos;
-
-typedef struct ast {
-	ast_k k; pos p; type *t; val v; u8 *s;
-	struct ast *c, *cl, *cr; stack *cs;
-} ast;
-
-extern char *ast_ks[];
-
-extern ast *ast_init(void);
-extern void ast_free(ast *a);
-
-extern ast *parse(lex *l);
-
-#endif // G_G_H_PHZNJHJ7
diff --git a/src/keyword.h b/src/keyword.h
index 65b12ff..239ecd9 100644
--- a/src/keyword.h
+++ b/src/keyword.h
@@ -8,7 +8,7 @@
 #ifndef G_KEYWORD_H_UEY4FULO
 #define G_KEYWORD_H_UEY4FULO
 
-#include "g.h"
+#include "lex.h"
 #include "util/util.h"
 
 typedef struct { tok_k k; u64 h; char *name; } kwd;
diff --git a/src/lex.c b/src/lex.c
index 0341f3d..3aaf23f 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -5,8 +5,8 @@
 

 
-#include "g.h"
 #include "keyword.h"
+#include "lex.h"
 #include "type.h"
 #include "util/util.h"
 
diff --git a/src/llvm/gen.c b/src/llvm/gen.c
index c031591..eb63ae7 100644
--- a/src/llvm/gen.c
+++ b/src/llvm/gen.c
@@ -5,7 +5,7 @@
 

 
-#include "../g.h"
+#include "../parse.h"
 #include "../type.h"
 #include "../util/util.h"
 #include "llvm.h"
@@ -25,15 +25,15 @@ static LLVMContextRef llvm_context;
 static LLVMModuleRef  llvm_module;
 static LLVMBuilderRef llvm_builder;
 
-static LLVMValueRef gen_decl(ast *a);
+static LLVMValueRef gen_decl(ast a);
 
-static LLVMValueRef gen_compound(ast *a);
+static LLVMValueRef gen_compound(ast a);
 
-static LLVMValueRef gen_proc(ast *a);
+static LLVMValueRef gen_proc(ast a);
 
-static LLVMValueRef gen_return(ast *a);
+static LLVMValueRef gen_return(ast a);
 
-static LLVMValueRef gen_int(ast *a);
+static LLVMValueRef gen_int(ast a);
 
 void llvm_init(void) {
 	llvm_context = LLVMGetGlobalContext();
@@ -46,7 +46,7 @@ void llvm_free(void) {
 	LLVMDisposeModule(llvm_module);
 }
 
-void llvm_test(ast *a) {
+void llvm_test(ast a) {
 	llvm_init(); gen_decl(a);
 
 	if (LLVMWriteBitcodeToFile(llvm_module, "llvm.bc")) {
@@ -82,7 +82,7 @@ void llvm_test(ast *a) {
 }
 
 /* Generate IR for a declaration. */
-static LLVMValueRef gen_decl(ast *a) {
+static LLVMValueRef gen_decl(ast a) {
 	/* TODO actually implement this properly. */
 	if (a->k == AK_DECL && a->c->k == AK_PROC) {
 		return gen_proc(a->c);
@@ -93,10 +93,10 @@ static LLVMValueRef gen_decl(ast *a) {
 }
 
 /* Generate IR for a compound statement. */
-static LLVMValueRef gen_compound(ast *a) {
+static LLVMValueRef gen_compound(ast a) {
 	/* TODO actually implement this properly. */
-	if (((ast *)a->cs->a[0])->k == AK_RETURN) {
-		return gen_int(((ast *)a->cs->a[0])->c);
+	if (((ast)a->cs->a[0])->k == AK_RETURN) {
+		return gen_int(((ast)a->cs->a[0])->c);
 	}
 	else { error(1, "gen_compound else!"); }
 
@@ -104,7 +104,7 @@ static LLVMValueRef gen_compound(ast *a) {
 }
 
 /* Generate IR for a procedure. */
-static LLVMValueRef gen_proc(ast *a) {
+static LLVMValueRef gen_proc(ast a) {
 	LLVMTypeRef ft = LLVMFunctionType(llvm_type(a->t), NULL, 0, 0);
 	LLVMValueRef f = LLVMAddFunction(llvm_module, (char *)a->s, ft);
 
@@ -116,11 +116,11 @@ static LLVMValueRef gen_proc(ast *a) {
 }
 
 /* Generate IR for a return statement. */
-static LLVMValueRef gen_return(ast *a) {
+static LLVMValueRef gen_return(ast a) {
 	return NULL;
 }
 
 /* Generate IR for an integer. */
-static LLVMValueRef gen_int(ast *a) {
+static LLVMValueRef gen_int(ast a) {
 	return LLVMConstInt(LLVMInt64Type(), a->v.v_int, false);
 }
diff --git a/src/llvm/llvm.h b/src/llvm/llvm.h
index 17f710e..e8df9ec 100644
--- a/src/llvm/llvm.h
+++ b/src/llvm/llvm.h
@@ -8,14 +8,14 @@
 #ifndef G_LLVM_LLVM_H_CZUMSHFW
 #define G_LLVM_LLVM_H_CZUMSHFW
 
-#include "../g.h"
+#include "../parse.h"
 #include "../type.h"
 #include "../util/util.h"
 
 #include <llvm-c/Types.h>
 
 extern void llvm_init(void);
-void llvm_test(ast *a);
+void llvm_test(ast a);
 
 extern LLVMTypeRef llvm_type(type *t);
 
diff --git a/src/parse.c b/src/parse.c
index d963360..6213500 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -5,7 +5,8 @@
 

 
-#include "g.h"
+#include "lex.h"
+#include "parse.h"
 #include "type.h"
 #include "util/stack.h"
 #include "util/util.h"
@@ -18,37 +19,33 @@
 #include <string.h>
 
 char *ast_ks[] = {
-	"AK_NIL",
+	"AK_NULL", "AK_INT", "AK_FLT",
 
-	"AK_DECL", "AK_COMPOUND",
-	
-	"AK_INT", "AK_FLT",
-	
-	"AK_PROC", "AK_RETURN",
+	"AK_DECL", "AK_COMPOUND", "AK_PROC", "AK_RETURN",
 };
 
-static ast *parse_decl(lex *l);
+static ast parse_decl(lex *l);
 
-static ast *parse_stmt(lex *l);
-static ast *parse_stmt_compound(lex *l);
+static ast parse_stmt(lex *l);
+static ast parse_stmt_compound(lex *l);
 
-static ast *parse_expr(lex *l);
+static ast parse_expr(lex *l);
 
-static ast *parse_proc(lex *l);
+static ast parse_proc(lex *l);
 
-static ast *parse_int(lex *l);
+static ast parse_int(lex *l);
 
-ast *ast_init() { return calloc(1, sizeof (ast)); }
-void ast_free(ast *a) { stack_free(a->cs); free(a); return; }
+ast  ast_init(void) { return calloc(1, sizeof (struct ast_s)); }
+void ast_free(ast a) { stack_free(a->cs); free(a); return; }
 
 /* Parse a program. */
-ast *parse(lex *l) {
+ast parse(lex *l) {
 	return parse_decl(l);
 }
 
 /* Parse a declaration. */
-static ast *parse_decl(lex *l) {
-	ast *a = ast_init();
+static ast parse_decl(lex *l) {
+	ast a = ast_init();
 
 	a->s = lex_kind(l, LK_ID).v_str; lex_kind(l, LK_COLON);
 
@@ -68,10 +65,10 @@ static ast *parse_decl(lex *l) {
 }
 
 /* Parse a statement. */
-static ast *parse_stmt(lex *l) {
+static ast parse_stmt(lex *l) {
 	if (lex_peek(l).k == LK_LBRACE) { return parse_stmt_compound(l); }
 
-	ast *a = ast_init();
+	ast a = ast_init();
 
 	switch (lex_peek(l).k) {
 	case LK_RETURN: { lex_kind(l, LK_RETURN); a->k = AK_RETURN; a->c = parse_expr(l); } break;
@@ -85,9 +82,9 @@ static ast *parse_stmt(lex *l) {
 }
 
 /* Parse a compound statement. */
-static ast *parse_stmt_compound(lex *l) {
+static ast parse_stmt_compound(lex *l) {
 	lex_kind(l, LK_LBRACE);
-	ast *a = ast_init(); a->k = AK_COMPOUND;
+	ast a = ast_init(); a->k = AK_COMPOUND;
 
 	a->cs = stack_init();
 
@@ -100,8 +97,8 @@ static ast *parse_stmt_compound(lex *l) {
 }
 
 /* Parse an expression. */
-static ast *parse_expr(lex *l) {
-	ast *a = ast_init();
+static ast parse_expr(lex *l) {
+	ast a = ast_init();
 
 	switch (lex_peek(l).k) {
 	case LK_PROC: { return parse_proc(l); } break;
@@ -116,9 +113,9 @@ static ast *parse_expr(lex *l) {
 }
 
 /* Parse a procedure. */
-static ast *parse_proc(lex *l) {
+static ast parse_proc(lex *l) {
 	lex_kind(l, LK_PROC); lex_kind(l, LK_LPAREN);
-	ast *a = ast_init(); a->k = AK_PROC;
+	ast a = ast_init(); a->k = AK_PROC;
 
 	a->cs = stack_init();
 
@@ -142,8 +139,8 @@ static ast *parse_proc(lex *l) {
 	a->c = parse_stmt_compound(l); return a;
 }
 
-static ast *parse_int(lex *l) {
+static ast parse_int(lex *l) {
 	val v = val_strint(lex_kind(l, LK_INT).v_str);
-	ast *a = ast_init(); a->k = AK_INT; a->v = v;
+	ast a = ast_init(); a->k = AK_INT; a->v = v;
 	return a;
 }
diff --git a/src/parse.h b/src/parse.h
new file mode 100644
index 0000000..c69ab70
--- /dev/null
+++ b/src/parse.h
@@ -0,0 +1,40 @@
+// parse.h
+// Parser header file for G
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+
+
+#ifndef G_PARSE_H_VB50JOSX
+#define G_PARSE_H_VB50JOSX
+
+#include "lex.h"
+#include "type.h"
+#include "util/stack.h"
+#include "util/util.h"
+#include "value.h"
+
+typedef enum {
+	AK_NULL, AK_INT, AK_FLT,
+	
+	AK_DECL, AK_COMPOUND, AK_PROC, AK_RETURN,
+} ast_k;
+
+typedef struct ast_s *ast;
+
+struct ast_s {
+	ast_k k; usize ln, cl;
+	type *t; val v; u8 *s;
+	ast c, lc, rc; stack *cs;
+	
+	union {};
+};
+
+extern char *ast_ks[];
+
+extern ast  ast_init(void);
+extern void ast_free(ast a);
+
+extern ast  parse(lex *l);
+
+#endif // G_PARSE_H_VB50JOSX
diff --git a/src/util/ast.h b/src/util/ast.h
index ceab4d7..732869d 100644
--- a/src/util/ast.h
+++ b/src/util/ast.h
@@ -8,18 +8,18 @@
 #ifndef OMKOV_G_AST_H_TUNIMNOY
 #define OMKOV_G_AST_H_TUNIMNOY
 
-#include "../g.h"
+#include "../parse.h"
 #include "../util/util.h"
 
 #include <stdio.h>
 
-static void ast_print(ast *a, usize i) {
+static void ast_print(ast a, usize i) {
 	for (usize j = 0; j != i; ++j) { printf("    "); }
-	printf("%zu:%zu: %s: %s\n", a->p.ln, a->p.cl, ast_ks[a->k], a->s);
+	printf("%zu:%zu: %s: %s\n", a->ln, a->cl, ast_ks[a->k], a->s);
 
 	if (a->c) { ast_print(a->c, i + 1); }
-	if (a->cl) { ast_print(a->cl, i + 1); }
-	if (a->cr) { ast_print(a->cr, i + 1); }
+	if (a->lc) { ast_print(a->lc, i + 1); }
+	if (a->rc) { ast_print(a->rc, i + 1); }
 	if (a->cs) for (usize ci = 0; ci != a->cs->al; ++ci) {
 		ast_print(a->cs->a[ci], i + 1);
 	}