Now support parameters for lambdas

This commit is contained in:
Nathan Braswell
2017-05-15 21:25:40 -04:00
parent 265a994858
commit bfe65fd854
3 changed files with 13 additions and 11 deletions

View File

@@ -97,10 +97,12 @@ fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node
var lambda_call_type = type_ptr(vector(lambda_struct_type) + t.parameter_types, t.return_type, 0, false, false, true) var lambda_call_type = type_ptr(vector(lambda_struct_type) + t.parameter_types, t.return_type, 0, false, false, true)
// create parameters // create parameters
var lambda_call_func_param = ast_identifier_ptr("func_struct", lambda_struct_type, null<ast_node>()) var lambda_call_func_param = ast_identifier_ptr("func_struct", lambda_struct_type, null<ast_node>())
var lambda_call_parameters = vector(lambda_call_func_param) var lambda_call_parameters = vector(lambda_call_func_param) + cleaned->parameter_types.map(fun(t:*type): *ast_node {
return ast_identifier_ptr("pass_through_param", t, null<ast_node>())
})
var lambda_call_function = ast_function_ptr(string("lambda_call"), lambda_call_type, lambda_call_parameters, false) var lambda_call_function = ast_function_ptr(string("lambda_call"), lambda_call_type, lambda_call_parameters, false)
lambda_call_function->function.body_statement = ast_code_block_ptr() lambda_call_function->function.body_statement = ast_code_block_ptr()
lambda_call_function->function.body_statement->code_block.children.add(ast_return_statement_ptr(ast_function_call_ptr(access_expression(lambda_call_func_param, "func"), vector<*ast_node>()))) lambda_call_function->function.body_statement->code_block.children.add(ast_return_statement_ptr(ast_function_call_ptr(access_expression(lambda_call_func_param, "func"), lambda_call_parameters.slice(1,-1))))
// create call body with if, etc // create call body with if, etc
lambda_type_to_struct_type_and_call_func[t] = make_pair(lambda_struct_type, lambda_call_function) lambda_type_to_struct_type_and_call_func[t] = make_pair(lambda_struct_type, lambda_call_function)

View File

@@ -192,32 +192,32 @@ fun get_builtin_function(name: string, param_types: vector<*type>, syntax: *tree
// none of the builtin functions should take in references // none of the builtin functions should take in references
param_types = param_types.map(fun(t: *type): *type return t->clone_without_ref();) param_types = param_types.map(fun(t: *type): *type return t->clone_without_ref();)
if (name == "==" || name == "!=" || name == ">" || name == "<" || name == "<=" || name == ">" || name == ">=" || name == "&&" || name == "||" || name == "!") if (name == "==" || name == "!=" || name == ">" || name == "<" || name == "<=" || name == ">" || name == ">=" || name == "&&" || name == "||" || name == "!")
return ast_function_ptr(name, type_ptr(param_types, type_ptr(base_type::boolean()), 0, false, false, false), vector<*ast_node>(), false) return ast_function_ptr(name, type_ptr(param_types, type_ptr(base_type::boolean()), 0, false, false, true), vector<*ast_node>(), false)
if (name == "." || name == "->") { if (name == "." || name == "->") {
if (name == "->" && param_types[0]->indirection == 0) if (name == "->" && param_types[0]->indirection == 0)
error(syntax, string("drereferencing not a pointer: ") + name) error(syntax, string("drereferencing not a pointer: ") + name)
else if (name == "." && param_types[0]->indirection != 0) else if (name == "." && param_types[0]->indirection != 0)
error(syntax, string("dot operator on a pointer: ") + name) error(syntax, string("dot operator on a pointer: ") + name)
else else
return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, false), vector<*ast_node>(), false) return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, true), vector<*ast_node>(), false)
} }
if (name == "[]") { if (name == "[]") {
if (param_types[0]->indirection == 0) if (param_types[0]->indirection == 0)
error(syntax, string("drereferencing not a pointer: ") + name) error(syntax, string("drereferencing not a pointer: ") + name)
else else
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, false), vector<*ast_node>(), false) return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, true), vector<*ast_node>(), false)
} }
if (name == "&" && param_types.size == 1) if (name == "&" && param_types.size == 1)
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_increased_indirection(), 0, false, false, false), vector<*ast_node>(), false) return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_increased_indirection(), 0, false, false, true), vector<*ast_node>(), false)
if (name == "*" && param_types.size == 1) { if (name == "*" && param_types.size == 1) {
if (param_types[0]->indirection == 0) if (param_types[0]->indirection == 0)
error(syntax, string("drereferencing not a pointer: ") + name) error(syntax, string("drereferencing not a pointer: ") + name)
else else
return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, false), vector<*ast_node>(), false) return ast_function_ptr(name, type_ptr(param_types, param_types[0]->clone_with_decreased_indirection(), 0, false, false, true), vector<*ast_node>(), false)
} }
if (param_types.size > 1 && param_types[1]->rank() > param_types[0]->rank()) if (param_types.size > 1 && param_types[1]->rank() > param_types[0]->rank())
return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, false), vector<*ast_node>(), false) return ast_function_ptr(name, type_ptr(param_types, param_types[1], 0, false, false, true), vector<*ast_node>(), false)
return ast_function_ptr(name, type_ptr(param_types, param_types[0], 0, false, false, false), vector<*ast_node>(), false) return ast_function_ptr(name, type_ptr(param_types, param_types[0], 0, false, false, true), vector<*ast_node>(), false)
} }
fun possible_object_equality(lvalue: *ast_node, rvalue: *ast_node): *ast_node { fun possible_object_equality(lvalue: *ast_node, rvalue: *ast_node): *ast_node {
var ltype = get_ast_type(lvalue) var ltype = get_ast_type(lvalue)

View File

@@ -1,6 +1,6 @@
fun main(argc: int, argv: **char): int { fun main(argc: int, argv: **char): int {
var a = fun(): int { return 11; } var a = fun(i: int, x: int): int { return i+x; }
return a() return a(12, 11)
} }