diff --git a/stdlib/ast_nodes.krak b/stdlib/ast_nodes.krak index f042e57..2e7eb1e 100644 --- a/stdlib/ast_nodes.krak +++ b/stdlib/ast_nodes.krak @@ -943,6 +943,14 @@ fun get_ast_scope(node: *ast_node): *map> { ast_node::value() return &node->value.scope } } +fun get_ast_type(node: *ast_node): *type { + match (*node) { + ast_node::identifier() return node->identifier.type + ast_node::function() return node->function.type + ast_node::function_call() return get_ast_type(node->function_call.func) + ast_node::value() return node->value.value_type + } +} fun ast_to_dot(root: *ast_node): string { var ret = string("digraph Kaken {\n") diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index e2c39bb..e907950 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -269,10 +269,11 @@ obj ast_transformation (Object) { return ast_return_statement_ptr(transform(node->children[0], scope)) } fun transform_function_call(node: *tree, scope: *ast_node): *ast_node { - // don't bother a full transform for parameter, just get the boolean expression from it + // don't bother with a full transform for parameters with their own function, just get the boolean expression and transform it var parameters = get_nodes("parameter", node).map(fun(child: *tree): *ast_node return transform(get_node("boolean_expression", child), scope);) + var parameter_types = parameters.map(fun(param: *ast_node): *type return get_ast_type(param);) /*return ast_function_call_ptr(function_lookup(concat_symbol_tree(get_node("unarad", node)), scope), parameters)*/ - var f = ast_function_call_ptr(function_lookup(concat_symbol_tree(get_node("unarad", node)), scope), parameters) + var f = ast_function_call_ptr(function_lookup(concat_symbol_tree(get_node("unarad", node)), scope, parameter_types), parameters) print("function call function ") println(f->function_call.func) print("function call parameters ") @@ -287,7 +288,7 @@ obj ast_transformation (Object) { } return results[0] } - fun function_lookup(name: string, scope: *ast_node): *ast_node { + fun function_lookup(name: string, scope: *ast_node, param_types: vector<*type>): *ast_node { var results = scope_lookup(name, scope) if (!results.size) { println(string("identifier lookup failed for ") + name) diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index 66b49b3..b301156 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -53,16 +53,17 @@ obj c_generator (Object) { // check for and add to parameters if a closure var parameter_types = string() var parameters = string() + var decorated_name = generate_function(child) // also add in name decoration backing.parameters.for_each(fun(parameter: *ast_node) { if (parameter_types != "") { parameter_types += ", "; parameters += ", ";} parameter_types += type_to_c(parameter->identifier.type) parameters += type_to_c(parameter->identifier.type) + " " + parameter->identifier.name }) - function_prototypes += type_to_c(backing.type->return_type) + " " + backing.name + "(" + parameter_types + ");\n" + function_prototypes += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameter_types + ");\n" // add parameters to destructor thingy (for returns)? Or should that be a different pass? - function_definitions += type_to_c(backing.type->return_type) + " " + backing.name + "(" + parameters + ") {\n" + generate_statement(backing.body_statement) + function_definitions += type_to_c(backing.type->return_type) + " " + decorated_name + "(" + parameters + ") {\n" + generate_statement(backing.body_statement) // emit parameter destructors? function_definitions += "}\n" } @@ -91,7 +92,10 @@ obj c_generator (Object) { } // this generates the function as a value, not the actuall function fun generate_function(node: *ast_node): string { - return node->function.name + var str = node->function.name + node->function.parameters.for_each(fun(param: *ast_node) str += string("_") + type_decoration(param->identifier.type);) + return str + } fun generate_function_call(node: *ast_node): string { var call_string = string() @@ -118,6 +122,28 @@ obj c_generator (Object) { } return string("/* COULD NOT GENERATE */") } + fun type_decoration(type: *type): string { + var indirection = string() + for (var i = 0; i < type->indirection; i++;) indirection += "p" + if (type->indirection) indirection += "_" + match (type->base) { + base_type::none() return indirection + string("none") + base_type::template() return indirection + string("template") + base_type::template_type() return indirection + string("template_type") + base_type::void_return() return indirection + string("void") + base_type::boolean() return indirection + string("bool") + base_type::character() return indirection + string("char") + base_type::integer() return indirection + string("int") + base_type::floating() return indirection + string("float") + base_type::double_precision() return indirection + string("double") + base_type::function() { + var temp = indirection + string("function_") + type->parameter_types.for_each(fun(parameter_type: *type) temp += type_decoration(parameter_type) + "_";) + return indirection + temp + "_" + type_decoration(type->return_type) + } + } + return string("impossible type") + indirection + } fun type_to_c(type: *type): string { var indirection = string() for (var i = 0; i < type->indirection; i++;) indirection += "*" diff --git a/tests/to_import.krak b/tests/to_import.krak index 6a443c6..34c1d81 100644 --- a/tests/to_import.krak +++ b/tests/to_import.krak @@ -4,6 +4,11 @@ fun simple_print(to_print: *char) { printf("%s", to_print); """ } +fun simple_print(to_print: int) { + __if_comp__ __C__ simple_passthrough(to_print::) """ + printf("%d", to_print); + """ +} var a = 1 var b = 2 diff --git a/tests/to_parse.krak b/tests/to_parse.krak index 6f62a69..ae9f775 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -12,7 +12,8 @@ fun some_other_function(in: bool): float { return 0.0 } fun main(): int { - simple_print("Hello World!") + simple_print("Hello World!\n") + simple_print(1337) return 0 }