From 7e7dc10c683ada7bcef54e5d04434ad222e2f121 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 13 Oct 2014 16:44:57 -0400 Subject: [PATCH] fixed definition placeholder generation code --- source/sclpl/codegen.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/source/sclpl/codegen.c b/source/sclpl/codegen.c index b866477..a1575cd 100644 --- a/source/sclpl/codegen.c +++ b/source/sclpl/codegen.c @@ -1,5 +1,6 @@ #include "parser.h" #include "codegen.h" +#include "pprint.h" static tree_t* get_child(tree_t* p_tree, size_t idx) { tree_t* child = NULL; @@ -31,7 +32,7 @@ static void* get_child_val(tree_t* p_tree, size_t idx) { 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) { + if ((NULL != child) && (child->tag == ATOM)) { lex_tok_t* token = child->ptr.tok; if ((token->type == T_VAR) && (0 == strcmp(val, (char*)token->value))) { @@ -47,17 +48,28 @@ static void emit_header(void) { puts("#include \n"); } +static void emit_fn_signature(char* name, tree_t* fnval) { + printf("val %s(", name); + vec_t* params = get_child(fnval, 1)->ptr.vec; + for (size_t i = 0; i < vec_size(params); i++) { + printf("val %s", (char*)get_val((tree_t*)vec_at(params,i))); + if (i+1 < vec_size(params)) + printf(", "); + } + printf(")"); +} 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 { + char* name = (char*)get_child_val(p_tree,1); + tree_t* value = get_child(p_tree, 2); + if (is_formtype(value, "fn")) { + emit_fn_signature(name, value); + puts(";"); + } else { printf("val %s;\n", (char*)get_child_val(p_tree,1)); - //} + } } } puts(""); -- 2.52.0