From c6067f21c88798624185b8334208fc932253afeb Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 12 Oct 2014 22:20:18 -0400 Subject: [PATCH] Started adding code generation --- source/sclpl/ast.c | 76 ------------------------------------------ source/sclpl/ast.h | 65 ------------------------------------ source/sclpl/codegen.c | 74 ++++++++++++++++++++++++++++++++++++++++ source/sclpl/codegen.h | 15 +++++++++ source/sclpl/main.c | 24 +++++++++++++ 5 files changed, 113 insertions(+), 141 deletions(-) delete mode 100644 source/sclpl/ast.c delete mode 100644 source/sclpl/ast.h create mode 100644 source/sclpl/codegen.c create mode 100644 source/sclpl/codegen.h diff --git a/source/sclpl/ast.c b/source/sclpl/ast.c deleted file mode 100644 index e45739f..0000000 --- a/source/sclpl/ast.c +++ /dev/null @@ -1,76 +0,0 @@ -/** - @file ast.c - @brief See header for details - $Revision$ - $HeadURL$ -*/ -#include "ast.h" - -void ast_free(void* p_obj) -{ - (void)p_obj; -} - -ast_t* ast_new(ast_type_t type, void* value) -{ - (void)type; - (void)value; - return NULL; -} - -void ast_set_pos(ast_t* p_ast, const char* file, size_t line, size_t col) -{ - (void)p_ast; - (void)file; - (void)line; - (void)col; -} - -const pos_t* ast_get_pos(ast_t* p_ast) -{ - (void)p_ast; - return NULL; -} - -void ast_set_type(ast_t* p_ast, ast_type_t type) -{ - (void)p_ast; - (void)type; -} - -ast_type_t ast_get_type(ast_t* p_ast) -{ - (void)p_ast; - return UNKNOWN; -} - -void ast_set_value(ast_t* p_ast, void* value) -{ - (void)p_ast; - (void)value; -} - -const void* ast_get_value(ast_t* p_ast) -{ - (void)p_ast; - return NULL; -} - -void ast_set_children(ast_t* p_ast, child_t* p_children) -{ - (void)p_ast; - (void)p_children; -} - -const child_t* ast_get_children(ast_t* p_ast) -{ - (void)p_ast; - return NULL; -} - -ast_t* ast_map(const ast_t* p_ast, ast_map_fn_t p_fn) -{ - (void)p_ast; - (void)p_fn; - return NULL; -} diff --git a/source/sclpl/ast.h b/source/sclpl/ast.h deleted file mode 100644 index 366b7ab..0000000 --- a/source/sclpl/ast.h +++ /dev/null @@ -1,65 +0,0 @@ -/** - @file ast.h - @brief TODO: Describe this file - $Revision$ - $HeadURL$ - */ -#ifndef AST_H -#define AST_H - -#include -#include - -typedef enum { - BOOLEAN, - INTEGER, - CHARACTER, - STRING, - UNKNOWN -} ast_type_t; - -typedef struct { - char* file; - size_t line; - size_t column; -} pos_t; - -struct ast_t; - -typedef struct child_t { - struct ast_t* ast; - struct child_t* next; -} child_t; - -typedef struct ast_t { - pos_t* pos; - ast_type_t type; - void* value; - child_t* children; -} ast_t; - -typedef ast_t* (*ast_map_fn_t)(const ast_t* p_ast); - -void ast_free(void* p_obj); - -ast_t* ast_new(ast_type_t type, void* value); - -void ast_set_pos(ast_t* p_ast, const char* file, size_t line, size_t col); - -const pos_t* ast_get_pos(ast_t* p_ast); - -void ast_set_type(ast_t* p_ast, ast_type_t type); - -ast_type_t ast_get_type(ast_t* p_ast); - -void ast_set_value(ast_t* p_ast, void* value); - -const void* ast_get_value(ast_t* p_ast); - -void ast_set_children(ast_t* p_ast, child_t* p_children); - -const child_t* ast_get_children(ast_t* p_ast); - -ast_t* ast_map(const ast_t* p_ast, ast_map_fn_t p_fn); - -#endif /* AST_H */ diff --git a/source/sclpl/codegen.c b/source/sclpl/codegen.c new file mode 100644 index 0000000..b866477 --- /dev/null +++ b/source/sclpl/codegen.c @@ -0,0 +1,74 @@ +#include "parser.h" +#include "codegen.h" + +static tree_t* get_child(tree_t* p_tree, size_t idx) { + tree_t* child = NULL; + if (p_tree->tag == TREE) { + vec_t* vec = p_tree->ptr.vec; + if (idx < vec_size(vec)) + child = vec_at(vec, idx); + } + return child; +} + +static void* get_val(tree_t* p_tree) { + void* ret = NULL; + if (p_tree->tag == ATOM) { + ret = p_tree->ptr.tok->value; + } + return ret; +} + +static void* get_child_val(tree_t* p_tree, size_t idx) { + void* ret = NULL; + tree_t* child = get_child(p_tree,idx); + if (child != NULL) { + ret = get_val(child); + } + return ret; +} + +static bool is_formtype(tree_t* p_tree, const char* val) { + bool ret = false; + tree_t* child = get_child(p_tree, 0); + if (child->tag == ATOM) { + lex_tok_t* token = child->ptr.tok; + if ((token->type == T_VAR) && + (0 == strcmp(val, (char*)token->value))) { + ret = true; + } + } + return ret; +} + +/*****************************************************************************/ + +static void emit_header(void) { + puts("#include \n"); +} + +static void emit_def_placeholders(vec_t* prgrm) { + for (size_t idx = 0; idx < vec_size(prgrm); idx++) { + tree_t* p_tree = (tree_t*)vec_at(prgrm, idx); + if (is_formtype(p_tree, "def")) { + tree_t* child = get_child(p_tree, 2); + //printf("%p\n", child); + //if (is_formtype(get_child(p_tree, 2), "fn")) { + //printf("val %s();\n", (char*)get_child_val(p_tree,1)); + //} else { + printf("val %s;\n", (char*)get_child_val(p_tree,1)); + //} + } + } + puts(""); +} + +static void emit_footer(void) { + +} + +void codegen_csource(FILE* file, vec_t* program) { + emit_header(); + emit_def_placeholders(program); + emit_footer(); +} diff --git a/source/sclpl/codegen.h b/source/sclpl/codegen.h new file mode 100644 index 0000000..c854d43 --- /dev/null +++ b/source/sclpl/codegen.h @@ -0,0 +1,15 @@ +/** + @file codegen.h + @brief TODO: Describe this file + $Revision$ + $HeadURL$ + */ +#ifndef CODEGEN_H +#define CODEGEN_H + +#include +#include "vec.h" + +void codegen_csource(FILE* file, vec_t* program); + +#endif /* CODEGEN_H */ diff --git a/source/sclpl/main.c b/source/sclpl/main.c index dd2174c..c010236 100644 --- a/source/sclpl/main.c +++ b/source/sclpl/main.c @@ -12,6 +12,7 @@ opts_cfg_t Options_Config[] = { {"tokens", false, "mode", "Emit the token output of lexical analysis for the given file"}, {"ast", false, "mode", "Emit the abstract syntax tree for the given file"}, + {"csource", false, "mode", "Emit the intermediate C source file for the given file"}, {"repl", false, "mode", "Execute the application in a REPL"}, {"staticlib", false, "mode", "Compile the application as a static library"}, {"sharedlib", false, "mode", "Compile the application as a shared library"}, @@ -90,6 +91,27 @@ static int emit_tree(void) { return ret; } +static int emit_csource(void) { + int ret = 0; + parser_t* p_parser = parser_new(NULL, stdin); + vec_t* p_vec = vec_new(0); + while(!parser_eof(p_parser)) { + tree_t* p_tree = grammar_toplevel(p_parser); + if (NULL != p_tree) { + tree_t* p_ast = convert_to_ast(p_tree); + mem_release(p_tree); + vec_push_back(p_vec, p_ast); + } else { + parser_resume(p_parser); + ret = 1; + } + } + codegen_csource(stdout, p_vec); + mem_release(p_vec); + mem_release(p_parser); + return ret; +} + static int exec_repl(void) { parser_t* p_parser = parser_new(":> ", stdin); while(!parser_eof(p_parser)) { @@ -137,6 +159,8 @@ int main(int argc, char **argv) { return emit_tokens(); } else if (opts_equal(NULL, "mode", "ast")) { return emit_tree(); + } else if (opts_equal(NULL, "mode", "csource")) { + return emit_csource(); } else if (opts_equal(NULL, "mode", "staticlib")) { return emit_staticlib(); } else if (opts_equal(NULL, "mode", "sharedlib")) { -- 2.52.0