From f70aed95892a9f72d7045f4bfb0e4c4b3d6f581b Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Tue, 9 Feb 2016 04:16:03 -0500 Subject: [PATCH] Implemented ultra-basic/incomplete function template instantiation type inference for Kalypso that just assigns the param type to the concat'd string right away which works in the most basic of cases. Will expand to cover pointers soon, and the rest when those features are implemented (object templates, function types) --- stdlib/ast_transformation.krak | 26 ++++++++++++++++++++++++-- stdlib/os.krak | 9 +++++++++ tests/to_parse.krak | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index 205dad6..bbf297d 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -7,6 +7,7 @@ import util:* import string:* import mem:* import io:* +import os:* import importer:* import ast_nodes:* import type:* @@ -511,6 +512,20 @@ fun get_builtin_function(name: string, param_types: vector<*type>): *ast_node { } fun unify_type(template_type: *tree, param_type: *type, new_map: *map, template_replacements: map) { println(string("Unifying type: ") + concat_symbol_tree(template_type)) + // first get rid of the reference if we have it - we don't care for unification + template_type = get_node("pre_reffed", template_type) + // There are a couple options for the template parameter type here + // 1) template type - perfect, stick it in the map, that's what we're here for + // 2) basic type - stick it in the map, it won't get copied out so no worries + // 3) pointer type, go down a level + // 4) function type - fine, unify on all parameters and return types + // 5) instantiated template - fun stuff, have to figure out what it was origionally + // instantiated with, but we don't have to worry about it yet as I haven't gotten + // to object templates at all ;) + if (template_type->children.size == 1) + new_map->set(concat_symbol_tree(template_type), param_type) + else + error("TYPE INFERENCE NOT GOOD ENOUGH") } fun find_or_instantiate_function_template(identifier: *tree, template_inst: *tree, scope: *ast_node, param_types: vector<*type>, template_replacements: map): *ast_node { var name = concat_symbol_tree(identifier) @@ -518,15 +533,21 @@ fun find_or_instantiate_function_template(identifier: *tree, template_in var results = scope_lookup(name, scope) var real_types = vector<*type>() var real_types_deref = vector() + var had_real_types = false if (template_inst) { real_types = get_nodes("type", template_inst).map(fun(t: *tree): *type return transform_type(t, scope, template_replacements);) real_types_deref = real_types.map(fun(t:*type):type return *t;) + had_real_types = true } for (var i = 0; i < results.size; i++;) { if (is_function_template(results[i])) { + println(string() + i + " is a template!") var template_types = results[i]->function_template.template_types var template_type_replacements = results[i]->function_template.template_type_replacements - if (!real_types.size) { + if (!had_real_types) { + // reset the vars, cuz we might be iterating through multiple of them + real_types = vector<*type>() + real_types_deref = vector() // Template Function Instance Inference time var typed_params = get_nodes("typed_parameter", results[i]->function_template.syntax_node).map(fun(t: *tree): *tree return get_node("type",t);) if (param_types.size != typed_params.size) @@ -705,6 +726,7 @@ fun error(message: *char) error(string(message)); fun error(message: string) { println("****ERROR****") println(message) - while (true){} + exit(-1) + /*while (true){}*/ } diff --git a/stdlib/os.krak b/stdlib/os.krak index e85f4d5..4979f23 100644 --- a/stdlib/os.krak +++ b/stdlib/os.krak @@ -15,5 +15,14 @@ fun system(call_string: *char): int { } } +fun exit() exit(0); +fun exit(code: int) { + __if_comp__ __C__ { + simple_passthrough(code::) """ + exit(code); + """ + } +} + diff --git a/tests/to_parse.krak b/tests/to_parse.krak index e69673a..36ae936 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -37,7 +37,7 @@ fun return_something_p_1(it: Something): Something { return it } */ -fun id(in: *T): *T return in; +/*fun id(in: *T): *T return in;*/ fun id(in: T): T return in; fun other_id(in: T): T { var a: T