Implemented function values when type inferenced (not explicit types or lambdas)

This commit is contained in:
Nathan Braswell
2016-02-20 21:02:41 -05:00
parent f51a676aed
commit 1795f1b4f1
4 changed files with 33 additions and 39 deletions

View File

@@ -370,7 +370,9 @@ obj ast_transformation (Object) {
}
match (searching_for) {
search_type::none() return identifier_lookup(name, scope)
search_type::function(type_vec) return function_lookup(name, scope, type_vec)
search_type::function(type_vec) {
return function_lookup(name, scope, type_vec)
}
}
println("FAILED SEARCH FOR")
return null<ast_node>()
@@ -378,7 +380,7 @@ obj ast_transformation (Object) {
fun transform_value(node: *tree<symbol>, scope: *ast_node): *ast_node {
var value_str = concat_symbol_tree(node)
var value_type = null<type>()
if (value_str[0] == '"')
if (value_str[0] == '"') // " // Comment hack for emacs now
value_type = type_ptr(base_type::character(), 1)
else if (value_str[0] == '\'') //'// lol, comment hack for vim syntax highlighting (my fault, of course)
value_type = type_ptr(base_type::character())
@@ -535,13 +537,13 @@ obj ast_transformation (Object) {
var func_name = string()
var parameters = vector<*ast_node>()
if (node->children.size == 1) {
var possible_func = transform(node->children[0], scope, searching_for, template_replacements)
if (!possible_func) match (searching_for) {
search_type::function(type_vec) possible_func = find_or_instantiate_template_function(node->children[0], null<tree<symbol>>(), scope, type_vec, template_replacements, map<string, *type>());
var possible_value = transform(node->children[0], scope, searching_for, template_replacements)
if (!possible_value) match (searching_for) {
search_type::function(type_vec) possible_value = find_or_instantiate_template_function(node->children[0], null<tree<symbol>>(), scope, type_vec, template_replacements, map<string, *type>());
}
if (!possible_func)
if (!possible_value)
println(concat_symbol_tree(node) + ": HAS NO POSSIBLE FUNCTION OR FUNCTION TEMPLATE SOLUTIONS")
return possible_func
return possible_value
} else if (node->children.size == 2) {
var template_inst = get_node("template_inst", node)
if (template_inst) {
@@ -759,7 +761,7 @@ fun function_lookup(name: string, scope: *ast_node, param_types: vector<*type>):
var results = scope_lookup(name, scope)
print(results.size); println(" number of results")
for (var i = 0; i < results.size; i++;) {
if (is_function(results[i]) && function_satisfies_params(results[i], param_types)) {
if ((is_function(results[i]) || is_identifier(results[i])) && function_satisfies_params(results[i], param_types)) {
return results[i]
}
}

View File

@@ -76,6 +76,7 @@ obj code_triple (Object) {
obj c_generator (Object) {
var id_counter: int
var ast_name_map: map<*ast_node, string>
var function_typedef_string: string
fun construct(): *c_generator {
id_counter = 0
ast_name_map.construct()
@@ -100,8 +101,7 @@ obj c_generator (Object) {
var top_level_c_passthrough: string = ""
var variable_extern_declarations: string = ""
var structs: string = "\n/**Type Structs**/\n"
var function_typedef_string_pre: string = ""
var function_typedef_string: string = ""
function_typedef_string.construct()
var function_prototypes: string = "\n/**Function Prototypes**/\n"
var function_definitions: string = "\n/**Function Definitions**/\n"
var variable_declarations: string = "\n/**Variable Declarations**/\n"
@@ -199,7 +199,7 @@ obj c_generator (Object) {
})
})
return make_pair(prequal+plain_typedefs+top_level_c_passthrough+variable_extern_declarations+structs+function_typedef_string_pre+function_typedef_string+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)
return make_pair(prequal+plain_typedefs+top_level_c_passthrough+variable_extern_declarations+structs+function_typedef_string+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)
}
fun generate_if_comp(node: *ast_node, enclosing_object: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
if (node->if_comp.wanted_generator == "__C__")
@@ -508,9 +508,18 @@ obj c_generator (Object) {
return get_name(type->type_def) + indirection
}
base_type::function() {
var temp = indirection + string("function: (")
type->parameter_types.for_each(fun(parameter_type: *type) temp += parameter_type->to_string() + " ";)
return temp + ")" + type->return_type->to_string()
var temp_name = string("temp") + get_id()
var temp = temp_name + ")("
var first = true
type->parameter_types.for_each(fun(parameter_type: *type) {
if (!first) {
temp += ", "
}
first = false
temp += type_to_c(parameter_type) + " "
})
function_typedef_string += string("typedef ") + type_to_c(type->return_type) + " (*" + temp + ");"
return temp_name + indirection
}
}
return string("impossible type") + indirection

View File

@@ -1,7 +1,7 @@
import io:*
import map:*
fun main():int {
fun main(): int {
var m = map(3,1)
m.set(2,2)
m.set(1,3)

View File

@@ -1,29 +1,12 @@
import simple_print: *
obj SimpleContainer<T> {
var data: T
fun print_data() {
var indirection: T
indirection = data
println(indirection)
}
fun with_other<U>(other: U) {
var indirection: U
indirection = other
println(data)
println(other)
}
fun return_as<U>(): U {
var indirection: T
indirection = data
return data
}
fun construct(dataIn: T) data = dataIn
fun print_and_return(data: int): int {
println(data)
return data
}
fun main(): int {
var it: SimpleContainer<int>
it.data = 8
it.with_other("Wooo object template")
println(it.return_as<float>())
var v = print_and_return
println(v(7))
// println(print_and_return(7))
return 0
}