Author | Jakob Wakeling <[email protected]> |
Date | 2022-01-06 02:42:19 |
Commit | d73e05f98c9a79592bdbb71aacb39a728a3fb03d |
Parent | 58704bd2f2be6db8a7cc5fcecf9ec8c3f14232b3 |
parse: Remove hardcoded ast pointer
Diffstat
M | src/compile.c | | | 3 | +-- |
M | src/llvm/gen.c | | | 26 | +++++++++++++------------- |
M | src/llvm/llvm.h | | | 2 | +- |
M | src/parse.c | | | 61 | +++++++++++++++++++++++++++++++++++++++---------------------- |
M | src/parse.h | | | 19 | +++++++++++-------- |
D | src/util/ast.h | | | 30 | ------------------------------ |
6 files changed, 65 insertions, 76 deletions
diff --git a/src/compile.c b/src/compile.c index 9321549..5761d29 100644 --- a/src/compile.c +++ b/src/compile.c @@ -8,7 +8,6 @@ #include "compile.h" #include "llvm/llvm.h" #include "parse.h" -#include "util/ast.h" #include "util/error.h" #include "util/util.h" @@ -23,7 +22,7 @@ void compile(char *src, UINT 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/llvm/gen.c b/src/llvm/gen.c index 120d2a2..42a18aa 100644 --- a/src/llvm/gen.c +++ b/src/llvm/gen.c @@ -24,15 +24,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(); @@ -45,7 +45,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")) { @@ -81,7 +81,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); @@ -92,10 +92,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!"); } @@ -103,7 +103,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, a->s, ft); @@ -115,11 +115,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 e8df9ec..c72c0d7 100644 --- a/src/llvm/llvm.h +++ b/src/llvm/llvm.h @@ -15,7 +15,7 @@ #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 f27ee55..1541bd9 100644 --- a/src/parse.c +++ b/src/parse.c @@ -23,28 +23,28 @@ char *ast_ks[] = { "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(void) { return calloc(1, sizeof (struct ast_s)); } -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); @@ -64,10 +64,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; @@ -81,11 +81,11 @@ 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(sizeof (ast), (void (*)(void *))&ast_free); + a->cs = stack_init(sizeof (ast *), (void (*)(void *))&ast_free); /* Parse statements until EOF or closing brace */ for (; lex_peek(l).k != LK_EOF && lex_peek(l).k != LK_RBRACE;) { @@ -96,8 +96,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; @@ -112,11 +112,11 @@ 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(sizeof (ast), (void (*)(void *))&ast_free); + a->cs = stack_init(sizeof (ast *), (void (*)(void *))&ast_free); /* Parse optional procedure parameter(s) */ /* TODO parse parameters(s) */ @@ -138,8 +138,22 @@ 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; } + +void ast_print(ast *a, UINT i) { + for (UINT j = 0; j != i; ++j) { printf(" "); } + 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->lc) { ast_print(a->lc, i + 1); } + if (a->rc) { ast_print(a->rc, i + 1); } + if (a->cs.a) for (UINT ci = 0; ci != a->cs.al; ci += 1) { + ast_print(((ast **)a->cs.a)[ci], i + 1); + } + + return; +} diff --git a/src/parse.h b/src/parse.h index bbd0030..90987fc 100644 --- a/src/parse.h +++ b/src/parse.h @@ -20,21 +20,23 @@ typedef enum { AK_DECL, AK_COMPOUND, AK_PROC, AK_RETURN, } ast_k; -typedef struct ast_s *ast; - -struct ast_s { +typedef struct ast_s { ast_k k; UINT ln, cl; type *t; val v; char *s; - ast c, lc, rc; stack cs; + struct ast_s *c, *lc, *rc; stack cs; - union {}; -}; + union { + + }; +} ast; extern char *ast_ks[]; -extern ast ast_init(void); -extern void ast_free(ast a); +extern ast *ast_init(void); +extern void ast_free(ast *a); + +extern ast *parse(lex *l); -extern ast parse(lex *l); +extern void ast_print(ast *a, UINT i); #endif // G_PARSE_H_VB50JOSX diff --git a/src/util/ast.h b/src/util/ast.h deleted file mode 100644 index cfeeba4..0000000 --- a/src/util/ast.h +++ /dev/null @@ -1,30 +0,0 @@ -// ast.h -// Ast header file for G -// Copyright (C) 2021, Jakob Wakeling -// All rights reserved. - - - -#ifndef OMKOV_G_AST_H_TUNIMNOY -#define OMKOV_G_AST_H_TUNIMNOY - -#include "../parse.h" -#include "../util/util.h" - -#include <stdio.h> - -static void ast_print(ast a, UINT i) { - for (UINT j = 0; j != i; ++j) { printf(" "); } - 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->lc) { ast_print(a->lc, i + 1); } - if (a->rc) { ast_print(a->rc, i + 1); } - if (a->cs.a) for (UINT ci = 0; ci != a->cs.al; ci += 1) { - ast_print(((ast *)a->cs.a)[ci], i + 1); - } - - return; -} - -#endif // OMKOV_G_AST_H_TUNIMNOY