From: Michael D. Lowis Date: Tue, 29 Dec 2015 01:44:00 +0000 (-0500) Subject: Started working on preliminary code generation X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=89fe98e1afe5b54eeabe0c19fe4a5aff2417724c;p=proto%2Fsclpl.git Started working on preliminary code generation --- diff --git a/Makefile b/Makefile index 56d89fc..5fe3b0a 100644 --- a/Makefile +++ b/Makefile @@ -17,13 +17,14 @@ LDFLAGS += ${LIBS} # Build Targets and Rules #------------------------------------------------------------------------------ OBJS = source/main.o \ - source/lexer.o \ - source/parser.o \ - source/pprint.o \ source/gc.o \ source/vec.o \ + source/pprint.o \ + source/lexer.o \ + source/parser.o \ source/ast.o \ - source/anf.o + source/anf.o \ + source/codegen.o all: options sclpl test diff --git a/source/codegen.c b/source/codegen.c new file mode 100644 index 0000000..9969850 --- /dev/null +++ b/source/codegen.c @@ -0,0 +1,91 @@ +#include + +void codegen(FILE* file, AST* tree) +{ + switch(tree->type) { + case AST_STRING: + fprintf(file, "\"%s\"", string_value(tree)); + break; + + case AST_SYMBOL: + //fprintf(file, "__symbol(\"%s\")", symbol_value(tree)); + break; + + case AST_CHAR: + //fprintf(file, "'%s'", char_value(tree)); + break; + + case AST_INT: + fprintf(file, "%ld", integer_value(tree)); + break; + + case AST_FLOAT: + fprintf(file, "%f", float_value(tree)); + break; + + case AST_BOOL: + fprintf(file, "%s", bool_value(tree) ? "true" : "false"); + break; + + case AST_IDENT: + fprintf(file, "%s", ident_value(tree)); + break; + + case AST_TEMP: + fprintf(file, "_t%lu", temp_value(tree)); + break; + + case AST_DEF: + if (def_value(tree)->type == AST_FUNC) { + fprintf(file, "val %s", def_name(tree)); + codegen(file, def_value(tree)); + } else { + fprintf(file, "val %s", def_name(tree)); + fprintf(file, " = "); + codegen(file, def_value(tree)); + fprintf(file, ";"); + } + break; + + case AST_IF: + break; + + case AST_FUNC: + fprintf(file,"("); + for (size_t i = 0; i < vec_size(func_args(tree)); i++) { + fprintf(file,"val "); + codegen(file, vec_at(func_args(tree), i)); + if (i+1 < vec_size(func_args(tree))) + fprintf(file,", "); + } + fprintf(file,") {\n"); + codegen(file, func_body(tree)); + fprintf(file,"\n}\n"); + break; + + case AST_FNAPP: + codegen(file, fnapp_fn(tree)); + fprintf(file,"("); + for (size_t i = 0; i < vec_size(fnapp_args(tree)); i++) { + codegen(file, vec_at(fnapp_args(tree), i)); + if (i+1 < vec_size(fnapp_args(tree))) + fprintf(file,","); + } + fprintf(file,")"); + break; + + case AST_LET: + fprintf(file,"{val "); + codegen(file, let_var(tree)); + fprintf(file," = "); + codegen(file, let_val(tree)); + fprintf(file,";"); + codegen(file, let_body(tree)); + fprintf(file,"}"); + break; + + case AST_REQ: + default: + break; + } +} diff --git a/source/main.c b/source/main.c index 8af8530..ffc00b7 100644 --- a/source/main.c +++ b/source/main.c @@ -31,6 +31,10 @@ static int emit_anf(void) { } static int emit_csource(void) { + AST* tree = NULL; + Parser* ctx = parser_new(NULL, stdin); + while(NULL != (tree = normalize(toplevel(ctx)))) + codegen(stdout, tree); return 0; } diff --git a/source/sclpl.h b/source/sclpl.h index 1a2282e..730ccaf 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -191,6 +191,13 @@ AST* let_val(AST* let); AST* let_body(AST* let); void let_set_body(AST* let, AST* body); +/* Pretty Printing + *****************************************************************************/ +void pprint_token_type(FILE* file, Tok* token); +void pprint_token_value(FILE* file, Tok* token); +void pprint_token(FILE* file, Tok* token, bool print_loc); +void pprint_tree(FILE* file, AST* tree, int depth); + /* Lexer and Parser Types *****************************************************************************/ typedef struct { @@ -214,12 +221,6 @@ AST* toplevel(Parser* p); // Compiler Passes AST* normalize(AST* tree); - -/* Pretty Printing - *****************************************************************************/ -void pprint_token_type(FILE* file, Tok* token); -void pprint_token_value(FILE* file, Tok* token); -void pprint_token(FILE* file, Tok* token, bool print_loc); -void pprint_tree(FILE* file, AST* tree, int depth); +void codegen(FILE* file, AST* tree); #endif /* SCLPL_H */