Implemented init position calls

This commit is contained in:
Nathan Braswell
2016-01-28 20:51:40 -05:00
parent 42b942737b
commit 17d4371d5c
4 changed files with 29 additions and 12 deletions

View File

@@ -712,14 +712,17 @@ fun is_declaration_statement(node: *ast_node): bool {
obj declaration_statement (Object) {
var identifier: *ast_node
var expression: *ast_node
var init_method_call: *ast_node
fun construct(identifier_in: *ast_node, expression_in: *ast_node): *declaration_statement {
identifier = identifier_in
expression = expression_in
init_method_call = null<ast_node>()
return this
}
fun copy_construct(old: *declaration_statement) {
identifier = old->identifier
expression = old->expression
init_method_call = old->init_method_call
}
fun destruct() {
}
@@ -728,7 +731,7 @@ obj declaration_statement (Object) {
copy_construct(&other)
}
fun operator==(other: ref declaration_statement): bool {
return identifier == other.identifier && expression == other.expression
return identifier == other.identifier && expression == other.expression && init_method_call == other.init_method_call
}
}
fun ast_if_comp_ptr(): *ast_node {
@@ -895,7 +898,7 @@ fun get_ast_children(node: *ast_node): vector<*ast_node> {
ast_node::branching_statement(backing) return vector<*ast_node>()
ast_node::defer_statement(backing) return vector(backing.statement)
ast_node::assignment_statement(backing) return vector(backing.to, backing.from)
ast_node::declaration_statement(backing) return vector(backing.identifier, backing.expression)
ast_node::declaration_statement(backing) return vector(backing.identifier, backing.expression, backing.init_method_call)
ast_node::if_comp(backing) return vector<*ast_node>(backing.statement)
ast_node::simple_passthrough(backing) return vector<*ast_node>()
ast_node::function_call(backing) return vector(backing.func) + backing.parameters

View File

@@ -260,7 +260,6 @@ obj ast_transformation (Object) {
while (!is_type_def(scope)) scope = get_ast_scope(scope)->get(string("~enclosing_scope"))[0]
return ast_identifier_ptr("this", scope->type_def.self_type->clone_with_indirection(1))
}
/*return ast_identifier_ptr(name, type_ptr(base_type::void_return()))*/
match (searching_for) {
search_type::none() return identifier_lookup(name, scope)
search_type::function(type_vec) return function_lookup(name, scope, type_vec)
@@ -315,7 +314,9 @@ obj ast_transformation (Object) {
return new_statement
}
fun transform_declaration_statement(node: *tree<symbol>, scope: *ast_node): *ast_node {
var name = concat_symbol_tree(get_node("identifier", node))
// this might have an init position method call
var identifiers = get_nodes("identifier", node)
var name = concat_symbol_tree(identifiers[0])
// may have type, or an expression, or both
var type_syntax_node = get_node("type", node)
var expression_syntax_node = get_node("boolean_expression", node)
@@ -330,6 +331,13 @@ obj ast_transformation (Object) {
if (!ident_type) error("declaration statement with no type or expression from which to inference type")
var identifier = ast_identifier_ptr(name, ident_type)
var declaration = ast_declaration_statement_ptr(identifier, expression)
// ok, deal with the possible init position method call
if (identifiers.size == 2) {
var method = transform(identifiers[1], ident_type->type_def)
var parameters = get_nodes("parameter", node).map(fun(child: *tree<symbol>): *ast_node return transform(get_node("boolean_expression", child), scope);)
var method_access = ast_function_call_ptr(get_builtin_function(string("."), vector(ident_type, get_ast_type(method))), vector(identifier, method))
declaration->declaration_statement.init_method_call = ast_function_call_ptr(method_access, parameters)
}
add_to_scope(name, identifier, scope)
return declaration
}
@@ -379,13 +387,11 @@ obj ast_transformation (Object) {
// 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<symbol>): *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, parameter_types), parameters)*/
var f = ast_function_call_ptr(transform(get_node("unarad", node), scope, search_type::function(parameter_types)), parameters)
print("function call function ")
println(f->function_call.func)
print("function call parameters ")
f->function_call.parameters.for_each(fun(param: *ast_node) print(param);)
/*print("function call function ")*/
/*println(f->function_call.func)*/
/*print("function call parameters ")*/
/*f->function_call.parameters.for_each(fun(param: *ast_node) print(param);)*/
return f
}
fun transform_expression(node: *tree<symbol>, scope: *ast_node): *ast_node return transform_expression(node, scope, search_type::none())

View File

@@ -176,6 +176,7 @@ obj c_generator (Object) {
var identifier = node->declaration_statement.identifier
var to_ret = code_triple() + type_to_c(identifier->identifier.type) + " " + identifier->identifier.name
if (node->declaration_statement.expression) to_ret += code_triple(" = ") + generate(node->declaration_statement.expression, enclosing_object, null<stack<pair<bool,stack<*ast_node>>>>())
if (node->declaration_statement.init_method_call) to_ret += code_triple(";\n") + generate(node->declaration_statement.init_method_call, enclosing_object, null<stack<pair<bool,stack<*ast_node>>>>())
return to_ret
}
fun generate_assignment_statement(node: *ast_node, enclosing_object: *ast_node): code_triple {