]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
fixed definition placeholder generation code
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 13 Oct 2014 20:44:57 +0000 (16:44 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 13 Oct 2014 20:44:57 +0000 (16:44 -0400)
source/sclpl/codegen.c

index b8664771d6c569505498c8c9434c54548b9ed95f..a1575cd4bed698daf3e5909373b300c31fc51ba3 100644 (file)
@@ -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 <sclpl.h>\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("");