From e3761429de464c42cc2798dc858bf6b43883d9dc Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Tue, 18 Apr 2023 07:13:55 +0200 Subject: ast: Create AST visitor to traverse the tree In the future we want to have the possibility of traverse the tree and pretty print it or generate binary for other platform like LLVM or transpile to C. This solution also implements the gas assembly x86_64 Linux code generation by using the visitor interface. Signed-off-by: Johnny Richard --- src/pipac.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'src/pipac.c') diff --git a/src/pipac.c b/src/pipac.c index 6e6df9e..e56d8a1 100644 --- a/src/pipac.c +++ b/src/pipac.c @@ -21,21 +21,15 @@ #include "lexer.h" #include "parser.h" #include "string_view.h" +#include "gas_assembly_generator.h" void generate_gas_x86_64_linux(ast_function_t *func) { - if (!string_view_eq(func->name, string_view_from_str("main"))) { - fprintf(stderr, "[ERROR]: no main function has been defined!\n"); - exit(EXIT_FAILURE); - } + gas_assembly_generator_t gen; + gas_assembly_generator_init(&gen, stdout); - printf(".global _start\n"); - printf(".text\n"); - printf("_start:\n"); - printf(" mov $1, %%al\n"); - printf(" mov $%d, %%ebx\n", func->body.number); - printf(" int $0x80\n"); + ast_visitor_visit(&gen, func); } void -- cgit v1.2.3