From 785c6a6a8e632ce2e9bb34b22fe8a428dae92fbd Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Thu, 14 Jan 2016 12:57:16 -0500 Subject: [PATCH] Function lookup now handles overloading --- stdlib/ast_transformation.krak | 31 +++++++++++++++++++++++-------- stdlib/type.krak | 10 +++++++++- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index e907950..ccba382 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -280,15 +280,30 @@ obj ast_transformation (Object) { f->function_call.parameters.for_each(fun(param: *ast_node) print(param);) return f } - fun identifier_lookup(name: string, scope: *ast_node): *ast_node { - var results = scope_lookup(name, scope) - if (!results.size) { - println(string("identifier lookup failed for ") + name) - return null() - } - return results[0] - } fun function_lookup(name: string, scope: *ast_node, param_types: vector<*type>): *ast_node { + var param_string = string() + param_types.for_each(fun(t: *type) param_string += t->to_string() + ", ";) + var results = scope_lookup(name, scope) + for (var i = 0; i < results.size; i++;) + if (is_function(results[i])) { + var func_param_types = get_ast_type(results[i])->parameter_types + if (func_param_types.size != param_types.size) + continue + var param_types_match = true + for (var j = 0; j < param_types.size; j++;) { + if (*func_param_types[j] != *param_types[j]) { + param_types_match = false + break + } + } + if (param_types_match) + return results[i] + } else + println(string("either isn't function or types don't match ") + get_ast_type(results[i])->to_string() + " with needed " + param_string) + println(string("function lookup failed for ") + name) + return null() + } + fun identifier_lookup(name: string, scope: *ast_node): *ast_node { var results = scope_lookup(name, scope) if (!results.size) { println(string("identifier lookup failed for ") + name) diff --git a/stdlib/type.krak b/stdlib/type.krak index 4b79520..a0709bf 100644 --- a/stdlib/type.krak +++ b/stdlib/type.krak @@ -38,17 +38,19 @@ obj type (Object) { base.copy_construct(&base_type::none()) parameter_types.construct() indirection = 0 + return_type = null() return this } fun construct(base_in: base_type, indirection_in: int): *type { base.copy_construct(&base_in) parameter_types.construct() indirection = indirection_in + return_type = null() return this } fun construct(parameter_types_in: vector<*type>, return_type_in: *type, indirection_in: int): *type { base.copy_construct(&base_type::function()) - parameter_types.copy_construct(¶meter_types) + parameter_types.copy_construct(¶meter_types_in) return_type = return_type_in indirection = indirection_in return this @@ -67,6 +69,12 @@ obj type (Object) { base.destruct() parameter_types.destruct() } + fun operator!=(other: ref type):bool return !(*this == other); + fun operator==(other: ref type):bool { + if ( (return_type && other.return_type && *return_type != *other.return_type) || (return_type && !other.return_type) || (!return_type && other.return_type) ) + return false + return base == other.base && parameter_types == other.parameter_types && indirection == other.indirection + } fun to_string(): string { match (base) { base_type::none() return string("none, indirection: ") + indirection