working on moving rest of obj stuff into obj_lower

This commit is contained in:
Nathan Braswell
2017-11-03 00:39:58 -04:00
parent 5b8ef4ce2e
commit cb720e5cd6
9 changed files with 295 additions and 144 deletions

View File

@@ -41,13 +41,13 @@ fun main(argc: int, argv: **char):int {
file_contents = string("RealGoal = boolean_expression ;\n") + file_contents
doing_repl = true
} else if (string(argv[1]) == "-v" || string(argv[1]) == "--version") {
var version_c_string = #ctce(fun(): *char {
var version_string = string("Self-hosted Kraken compiler \"Kalypso\" - revision ") + from_system_command(string("git rev-list HEAD | wc -l"), 100) +
", commit: " + from_system_command(string("git rev-parse HEAD"), 100) +
", compile date: " + from_system_command(string("date"), 100)
return version_string.toCharArray()
}())
println(version_c_string)
/*var version_c_string = #ctce(fun(): *char {*/
/*var version_string = string("Self-hosted Kraken compiler \"Kalypso\" - revision ") + from_system_command(string("git rev-list HEAD | wc -l"), 100) +*/
/*", commit: " + from_system_command(string("git rev-parse HEAD"), 100) +*/
/*", compile date: " + from_system_command(string("date"), 100) */
/*return version_string.toCharArray()*/
/*}())*/
/*println(version_c_string)*/
exit(0)
}
var input_file_offset = 1

View File

@@ -46,6 +46,7 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
node->adt_def.option_funcs.for_each(fun(func: *ast_node) {
var adt_type = replacement->type_def.self_type
var block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", func, block)
func->function.body_statement = block
var to_ret = ast_identifier_ptr(string("to_ret"), adt_type, block)
block->code_block.children.add(ast_declaration_statement_ptr(to_ret, null<ast_node>()))
@@ -66,6 +67,7 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
})
node->adt_def.regular_funcs.for_each(fun(func: *ast_node) {
var block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", func, block)
func->function.body_statement = block
if (func->function.name == "operator==") {
var other = func->function.parameters[0]
@@ -139,6 +141,7 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
match(*node) {
ast_node::match_statement(backing) {
var block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", parent_chain->item_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i) || is_function(i);), block)
var value = backing.value
var holder = ast_identifier_ptr(string("holder"), get_ast_type(value)->clone_with_increased_indirection(), block)
block->code_block.children.add(ast_declaration_statement_ptr(holder, null<ast_node>()))
@@ -156,6 +159,7 @@ fun adt_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
var condition = make_operator_call("==", vector(make_operator_call("->", vector(holder, flag)), ast_value_ptr(to_string(option_num), type_ptr(base_type::integer()))))
var if_stmt = ast_if_statement_ptr(condition)
var inner_block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", block, inner_block)
var unpack_ident = case_stmt->case_statement.unpack_ident
if (unpack_ident) {
var get_option = make_operator_call(".", vector(make_operator_call("->", vector(holder, data)), option))

View File

@@ -12,6 +12,10 @@ import io:*
import type:*
adt ast_node {
translation_unit: translation_unit,
import: import,
@@ -1070,7 +1074,9 @@ obj value (Object) {
fun get_ast_children(node: *ast_node): vector<*ast_node> {
match (*node) {
ast_node::translation_unit(backing) return backing.children + backing.lambdas
// Don't get lambdas, let them come up naturally in passes (so can get enclosing function and stuff)
/*ast_node::translation_unit(backing) return backing.children + backing.lambdas*/
ast_node::translation_unit(backing) return backing.children
ast_node::import(backing) return vector<*ast_node>()
ast_node::identifier(backing) return vector<*ast_node>()
ast_node::type_def(backing) return backing.variables + backing.methods

View File

@@ -780,76 +780,10 @@ obj ast_transformation (Object) {
fun transform_lambda(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node {
var function_node = second_pass_function(node, scope, template_replacements, false)
function_node->function.body_statement = transform_statement(get_node("statement", node), function_node, template_replacements)
function_node->function.closed_variables = find_closed_variables(function_node, function_node->function.body_statement)
while (!is_translation_unit(scope)) scope = get_ast_scope(scope)->get(string("~enclosing_scope"))[0]
scope->translation_unit.lambdas.add(function_node)
return function_node
}
fun find_closed_variables(func: *ast_node, node: *ast_node): set<*ast_node> {
if (!node) return set<*ast_node>()
match (*node) {
ast_node::identifier(backing) {
if (!in_scope_chain(backing.enclosing_scope, func))
return set(node);
}
ast_node::code_block(backing) {
var to_ret = set<*ast_node>()
backing.children.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::function_call(backing) {
if (is_function(backing.func) && (backing.func->function.name == "." || backing.func->function.name == "->"))
return find_closed_variables(func, backing.parameters.first())
var to_ret = find_closed_variables(func, backing.func)
backing.parameters.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::function(backing) {
// now if this is a method, it is called without an access operator because we are in a lambda inside another method
// this is because we don't look at the right side of access operators, note the above function_call case
if (backing.scope.contains_key(string("~enclosing_scope"))) {
var enclosing = backing.scope[string("~enclosing_scope")][0]
if (is_type_def(enclosing))
return set(make_this(enclosing))
if (is_template(enclosing) && is_type_def(enclosing->template.scope[string("~enclosing_scope")][0]))
return set(make_this(enclosing->template.scope[string("~enclosing_scope")][0]))
}
// if this is a lambda, we need to check all of the things it closes over
// we don't need an if - if it's empty and not a lambda, it's empty
// and we don't close over actual functions
var to_ret = set<*ast_node>()
backing.closed_variables.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::return_statement(backing) return find_closed_variables(func, backing.return_value)
ast_node::if_statement(backing) return find_closed_variables(func, backing.condition) + find_closed_variables(func, backing.then_part) + find_closed_variables(func, backing.else_part)
ast_node::match_statement(backing) {
var to_ret = set<*ast_node>()
backing.cases.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::case_statement(backing) return find_closed_variables(func, backing.statement)
ast_node::while_loop(backing) return find_closed_variables(func, backing.condition) + find_closed_variables(func, backing.statement)
ast_node::for_loop(backing) {
return find_closed_variables(func, backing.init) + find_closed_variables(func, backing.condition) +
find_closed_variables(func, backing.update) + find_closed_variables(func, backing.body)
}
ast_node::return_statement(backing) return find_closed_variables(func, backing.return_value)
ast_node::defer_statement(backing) return find_closed_variables(func, backing.statement)
ast_node::assignment_statement(backing) return find_closed_variables(func, backing.to) + find_closed_variables(func, backing.from)
ast_node::declaration_statement(backing) return find_closed_variables(func, backing.expression) + find_closed_variables(func, backing.init_method_call)
ast_node::if_comp(backing) return find_closed_variables(func, backing.statement)
ast_node::cast(backing) return find_closed_variables(func, backing.value)
}
return set<*ast_node>()
}
fun in_scope_chain(node: *ast_node, high_scope: *ast_node): bool {
if (node == high_scope)
return true
if (get_ast_scope(node)->contains_key(string("~enclosing_scope")))
return in_scope_chain(get_ast_scope(node)->get(string("~enclosing_scope"))[0], high_scope)
return false
}
fun transform_expression(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *ast_node
return transform_expression(node, scope, search_type::none(), template_replacements)
fun transform_expression(node: *tree<symbol>, scope: *ast_node, searching_for: search_type, template_replacements: map<string, *type>): *ast_node {
@@ -1023,7 +957,6 @@ obj ast_transformation (Object) {
continue
if (results[i]->template.instantiated_map.contains_key(real_types_deref)) {
/*println("USING CACHED TEMPLATE FUNCITON")*/
inst_func = results[i]->template.instantiated_map[real_types_deref]
} else {
inst_func = second_pass_function(results[i]->template.syntax_node, results[i], template_type_replacements, false)

View File

@@ -122,10 +122,10 @@ obj c_generator (Object) {
var parameter_types = string()
var parameters = string()
// lambdas can have the enclosing object too, if it's needed (lambda in a method)
if (enclosing_object && !is_lambda) {
parameter_types = type_to_c(enclosing_object->type_def.self_type) + "*"
parameters = type_to_c(enclosing_object->type_def.self_type) + "* this"
}
/*if (enclosing_object && !is_lambda) {*/
/*parameter_types = type_to_c(enclosing_object->type_def.self_type) + "*"*/
/*parameters = type_to_c(enclosing_object->type_def.self_type) + "* this"*/
/*}*/
var decorated_name = string()
if (backing.is_extern)
@@ -264,8 +264,7 @@ obj c_generator (Object) {
to_ret += get_name(identifier) + " = " + generate(node->declaration_statement.expression, enclosing_object, enclosing_func)
}
if (node->declaration_statement.init_method_call) {
to_ret += ";\n"
to_ret += generate(node->declaration_statement.init_method_call, enclosing_object, enclosing_func)
error("init_method_call remaining")
}
return to_ret
}
@@ -301,8 +300,8 @@ obj c_generator (Object) {
fun generate_identifier(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node): string {
if (get_ast_type(node)->is_ref)
error("still existin ref in identifier")
if (enclosing_object && get_ast_scope(enclosing_object)->contains_key(node->identifier.name) && get_ast_scope(enclosing_object)->get(node->identifier.name).contains(node))
return "(this->" + get_name(node) + ")"
/*if (enclosing_object && get_ast_scope(enclosing_object)->contains_key(node->identifier.name) && get_ast_scope(enclosing_object)->get(node->identifier.name).contains(node))*/
/*return "(this->" + get_name(node) + ")"*/
return get_name(node)
}
fun generate_return_statement(node: *ast_node, enclosing_object: *ast_node, enclosing_func: *ast_node): string {
@@ -364,21 +363,21 @@ obj c_generator (Object) {
if (func_return_type->is_ref)
error("still ref in function calling")
if (is_dot_style_method_call(node)) {
func_name = generate_function(node->function_call.func->function_call.parameters[1])
// don't add & if it was ->
if (node->function_call.func->function_call.func->function.name == ".")
call_string += "&"
/*if (is_dot_style_method_call(node)) {*/
/*func_name = generate_function(node->function_call.func->function_call.parameters[1])*/
/*// don't add & if it was ->*/
/*if (node->function_call.func->function_call.func->function.name == ".")*/
/*call_string += "&"*/
call_string += generate(node->function_call.func->function_call.parameters[0], enclosing_object, enclosing_func)
} else {
/*call_string += generate(node->function_call.func->function_call.parameters[0], enclosing_object, enclosing_func)*/
/*} else {*/
func_name = generate(node->function_call.func, enclosing_object, enclosing_func)
// handle method call from inside method of same object
if (enclosing_object && method_in_object(node->function_call.func, enclosing_object)) {
call_string += "this";
}
}
/*if (enclosing_object && method_in_object(node->function_call.func, enclosing_object)) {*/
/*call_string += "this";*/
/*}*/
/*}*/
var parameters = node->function_call.parameters
if ( parameters.size == 2 && (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/"

View File

@@ -13,7 +13,6 @@ import hash_set:*
import pass_common:*
fun defer_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var enclosing_function_stack = stack<*ast_node>()
var visited = hash_set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
var defer_triple_stack = stack<stack<stack<*ast_node>>>()
@@ -38,7 +37,6 @@ fun defer_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_t
loop_stack.push(defer_triple_stack.top().size())
}
ast_node::function(backing) {
enclosing_function_stack.push(node)
defer_triple_stack.push(stack<stack<*ast_node>>())
}
}
@@ -47,29 +45,21 @@ fun defer_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_t
match(*node) {
ast_node::branching_statement(backing) {
var block = ast_code_block_ptr()
add_to_scope("~enclosing_scope", parent_chain->item_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i) || is_function(i);), block)
replace_with_in(node, block, parent_chain)
for (var i = 0; i < defer_triple_stack.top().size() - loop_stack.top(); i++;)
block->code_block.children.add_all(defer_triple_stack.top().from_top(i).reverse_vector())
block->code_block.children.add(node)
}
ast_node::return_statement(backing) {
var block = ast_code_block_ptr()
replace_with_in(node, block, parent_chain)
var return_value = node->return_statement.return_value
if (return_value) {
if (get_ast_type(enclosing_function_stack.top())->return_type->is_ref)
return_value = make_operator_call("&", vector(return_value))
var temp_return = ast_identifier_ptr("temp_boom_return", get_ast_type(return_value)->clone_without_ref(), block)
block->code_block.children.add(ast_declaration_statement_ptr(temp_return, null<ast_node>()))
block->code_block.children.add(assign_or_copy_construct_statement(temp_return, return_value))
// dereference so that the real ref can take it back
if (get_ast_type(enclosing_function_stack.top())->return_type->is_ref)
temp_return = make_operator_call("*", vector(temp_return))
node->return_statement.return_value = temp_return
var block = parent_chain->top()
if (!is_code_block(block))
error("defer doesn't have block - it should from obj lower")
for (var i = 0; i < defer_triple_stack.top().size(); i++;) {
defer_triple_stack.top().from_top(i).reverse_vector().for_each(fun(c: *ast_node) {
add_before_in(c, node, block)
})
}
for (var i = 0; i < defer_triple_stack.top().size(); i++;)
block->code_block.children.add_all(defer_triple_stack.top().from_top(i).reverse_vector())
block->code_block.children.add(node)
}
ast_node::code_block(backing) {
node->code_block.children.add_all(defer_triple_stack.top().pop().reverse_vector())
@@ -82,7 +72,6 @@ fun defer_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_t
}
ast_node::function(backing) {
defer_triple_stack.pop()
enclosing_function_stack.pop()
}
}
}

View File

@@ -27,12 +27,76 @@ fun make_function_parent_block(function: *ast_node, parent: *ast_node, parent_bl
result.parent_function = parent_function
return result
}
fun find_closed_variables(func: *ast_node, node: *ast_node): set<*ast_node> {
if (!node) return set<*ast_node>()
match (*node) {
ast_node::identifier(backing) {
if (!in_scope_chain(backing.enclosing_scope, func)) {
if (backing.name == "temporary_return_boomchaka" ||
backing.name == "temp_boom_return")
error("trying to close over temp return")
else
return set(node);
}
}
ast_node::code_block(backing) {
var to_ret = set<*ast_node>()
backing.children.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::function_call(backing) {
if (is_function(backing.func) && (backing.func->function.name == "." || backing.func->function.name == "->"))
return find_closed_variables(func, backing.parameters.first())
var to_ret = find_closed_variables(func, backing.func)
backing.parameters.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::function(backing) {
// if this is a lambda, we need to check all of the things it closes over
var to_ret = set<*ast_node>()
backing.closed_variables.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::return_statement(backing) return find_closed_variables(func, backing.return_value)
ast_node::if_statement(backing) return find_closed_variables(func, backing.condition) + find_closed_variables(func, backing.then_part) + find_closed_variables(func, backing.else_part)
ast_node::match_statement(backing) {
var to_ret = set<*ast_node>()
backing.cases.for_each(fun(n: *ast_node) to_ret += find_closed_variables(func, n);)
return to_ret
}
ast_node::case_statement(backing) return find_closed_variables(func, backing.statement)
ast_node::while_loop(backing) return find_closed_variables(func, backing.condition) + find_closed_variables(func, backing.statement)
ast_node::for_loop(backing) {
return find_closed_variables(func, backing.init) + find_closed_variables(func, backing.condition) +
find_closed_variables(func, backing.update) + find_closed_variables(func, backing.body)
}
ast_node::return_statement(backing) return find_closed_variables(func, backing.return_value)
ast_node::defer_statement(backing) return find_closed_variables(func, backing.statement)
ast_node::assignment_statement(backing) return find_closed_variables(func, backing.to) + find_closed_variables(func, backing.from)
ast_node::declaration_statement(backing) return find_closed_variables(func, backing.expression) + find_closed_variables(func, backing.init_method_call)
ast_node::if_comp(backing) return find_closed_variables(func, backing.statement)
ast_node::cast(backing) return find_closed_variables(func, backing.value)
}
return set<*ast_node>()
}
fun in_scope_chain(node: *ast_node, high_scope: *ast_node): bool {
if (node == high_scope)
return true
if (get_ast_scope(node)->contains_key(string("~enclosing_scope")))
return in_scope_chain(get_ast_scope(node)->get(string("~enclosing_scope"))[0], high_scope)
return false
}
fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var visited = hash_set<*ast_node>()
var lambdas = set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
lambdas.add(syntax_ast_pair.second->translation_unit.lambdas)
// do in order so that inner lambdas are done before outer ones, so enclosed
// variables can propegate outwards
syntax_ast_pair.second->translation_unit.lambdas.for_each(fun(n: *ast_node) {
n->function.closed_variables = find_closed_variables(n, n->function.body_statement)
})
})
var all_types = hash_set<*type>()
var function_value_creation_points = vector<function_parent_block>()
@@ -172,13 +236,13 @@ fun function_value_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node
body->code_block.children.add(ast_assignment_statement_ptr(access_expression(ident, "data"), closure_param))
l->function.closed_variables.for_each(fun(v: *ast_node) {
// HACK
if (v->identifier.name == "this") {
// add in an assignment at the beginning of the lambda
if (!is_code_block(l->function.body_statement))
error("lambda body isn't a block in function_value_lower")
l->function.body_statement->code_block.children.add(0, ast_declaration_statement_ptr(ast_identifier_ptr("this", v->identifier.type, l->function.body_statement),
make_operator_call("*", vector(access_expression(closure_lambda_param, v->identifier.name)))))
}
/*if (v->identifier.name == "this") {*/
/*// add in an assignment at the beginning of the lambda*/
/*if (!is_code_block(l->function.body_statement))*/
/*error("lambda body isn't a block in function_value_lower")*/
/*l->function.body_statement->code_block.children.add(0, ast_declaration_statement_ptr(ast_identifier_ptr("this", v->identifier.type, l->function.body_statement),*/
/*make_operator_call("*", vector(access_expression(closure_lambda_param, v->identifier.name)))))*/
/*}*/
// have to make sure to clean here as well
var closed_param_type = v->identifier.type
if (lambda_type_to_struct_type_and_call_func.contains_key(*closed_param_type))

View File

@@ -10,34 +10,58 @@ import ast_nodes:*
import ast_transformation:*
import pass_common:*
import hash_set:*
/*
Here's how we lower objects
PASS ONE THROUGH name_ast_map
1 first, we make sure that all functions, if statments, while loops and for loops have code blocks
as children, not just statements.
1 during the same pass, we "uglify" for loops and while loops so that functions that need pre and post statements always
have a code block to insert them into that makes sure that they get run.
1 we also make a set of all type_defs for pass 4
// Here's how we lower objects
// 1 first, we make sure that all functions, if statments, while loops and for loops have code blocks
// as children, not just statements.
// 1 during the same pass, we "uglify" for loops and while loops so that functions that need pre and post statements always
// have a code block to insert them into that makes sure that they get run.
// 2 in another pass (more complicated because different children have different parent scopes)
// we transform the short circuit operators
// 3 then on the pass up the chain, at function calls we add in they copy_construct in and defer destruct out
// temporaries.
// 3 this is also when we add in defer destructs for function parameters (inside the function) and declaration statements
2 in another pass (more complicated because different children have different parent scopes)
we transform the short circuit operators
3 then on the pass up the chain, at function calls we add in they copy_construct in and defer destruct out
temporaries. Also, pull out init method calls.
3 this is also when we add in defer destructs for function parameters (inside the function) and declaration statements
PASS TWO THROUGH name_ast_map
4 change all methods to take in self, change all method calls to pass in self, change all in method references to be explicit
*/
fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
var visited1 = hash_set<*ast_node>()
var visited2 = hash_set<*ast_node>()
var visited3 = hash_set<*ast_node>()
var functions_visited_for_construct_in_destruct_out = hash_set<*ast_node>()
var all_type_defs = set<*ast_node>()
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
// Pass 1
var ensure_block_and_munge = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
match(*node) {
ast_node::function(backing) if (backing.body_statement && !is_code_block(backing.body_statement)) backing.body_statement = ast_code_block_ptr(backing.body_statement)
ast_node::type_def(backing) all_type_defs.add(node)
ast_node::function(backing) if (backing.body_statement && !is_code_block(backing.body_statement)) {
backing.body_statement = ast_code_block_ptr(backing.body_statement)
add_to_scope("~enclosing_scope", node, backing.body_statement)
if (!is_code_block(backing.body_statement))
error("BUT EXTRA WHY")
}
ast_node::if_statement(backing) {
if (!is_code_block(backing.then_part)) backing.then_part = ast_code_block_ptr(backing.then_part)
if (backing.else_part && !is_code_block(backing.else_part)) backing.else_part = ast_code_block_ptr(backing.else_part)
if (!is_code_block(backing.then_part)) {
backing.then_part = ast_code_block_ptr(backing.then_part)
add_to_scope("~enclosing_scope", node, backing.then_part)
}
if (backing.else_part && !is_code_block(backing.else_part)) {
backing.else_part = ast_code_block_ptr(backing.else_part)
add_to_scope("~enclosing_scope", node, backing.else_part)
}
}
// no need for case because it's already been lowered
ast_node::while_loop(backing) {
if (!is_code_block(backing.statement)) backing.statement = ast_code_block_ptr(backing.statement)
if (!is_code_block(backing.statement)) {
backing.statement = ast_code_block_ptr(backing.statement)
add_to_scope("~enclosing_scope", node, backing.statement)
}
var condition = backing.condition
backing.condition = ast_value_ptr(string("true"), type_ptr(base_type::boolean()))
// objects do not coerce to booleans, so it should be ok for this not to be a ref
@@ -48,7 +72,10 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
backing.statement->code_block.children.add(1, condition_if)
}
ast_node::for_loop(backing) {
if (!is_code_block(backing.body)) backing.body = ast_code_block_ptr(backing.body)
if (!is_code_block(backing.body)) {
backing.body = ast_code_block_ptr(backing.body)
add_to_scope("~enclosing_scope", node, backing.body)
}
add_before_in(backing.init, node, parent_chain->top())
backing.init = null<ast_node>()
// the do_update goes in the block above the for
@@ -56,7 +83,9 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
add_before_in(ast_declaration_statement_ptr(update_ident, ast_value_ptr(string("false"), type_ptr(base_type::boolean()))),
node, parent_chain->top())
var update_if = ast_if_statement_ptr(update_ident)
add_to_scope("~enclosing_scope", backing.body, update_if)
update_if->if_statement.then_part = ast_code_block_ptr(backing.update)
add_to_scope("~enclosing_scope", update_if, update_if->if_statement.then_part)
backing.update = null<ast_node>()
backing.body->code_block.children.add(0, update_if)
backing.body->code_block.children.add(1, ast_assignment_statement_ptr(update_ident, ast_value_ptr(string("true"), type_ptr(base_type::boolean()))))
@@ -70,6 +99,36 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt())
backing.body->code_block.children.add(3, condition_if)
}
ast_node::return_statement(backing) {
var block = parent_chain->top()
if (!is_code_block(block))
error("Isn't block in return statement obj munging")
/*var block = ast_code_block_ptr()*/
/*add_to_scope("~enclosing_scope", parent_chain->item_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i) || is_function(i);), block)*/
/*replace_with_in(node, block, parent_chain)*/
var return_value = backing.return_value
var enclosing_function = parent_chain->item_from_top_satisfying(is_function)
if (!is_code_block(enclosing_function->function.body_statement))
error("this would by unusual")
if (return_value) {
if (get_ast_type(enclosing_function)->return_type->is_ref)
return_value = make_operator_call("&", vector(return_value))
var temp_return = ast_identifier_ptr("temp_boom_return", get_ast_type(return_value)->clone_without_ref(), block)
add_to_scope("temp_boom_return", temp_return, block)
add_to_scope("~enclosing_scope", block, temp_return)
/*block->code_block.children.add(ast_declaration_statement_ptr(temp_return, null<ast_node>()))*/
/*block->code_block.children.add(assign_or_copy_construct_statement(temp_return, return_value))*/
add_before_in(ast_declaration_statement_ptr(temp_return, null<ast_node>()), node, block)
add_before_in(assign_or_copy_construct_statement(temp_return, return_value), node, block)
// dereference so that the real ref can take it back
if (get_ast_type(enclosing_function)->return_type->is_ref)
temp_return = make_operator_call("*", vector(temp_return))
backing.return_value = temp_return
}
if (!is_code_block(enclosing_function->function.body_statement))
error("is this would by unusual")
/*block->code_block.children.add(node)*/
}
}
}
run_on_tree(ensure_block_and_munge, empty_pass_second_half(), syntax_ast_pair.second, &visited1)
@@ -97,8 +156,10 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
if (func_name == "||")
condition = make_operator_call("!", vector(condition))
var short_circuit_if = ast_if_statement_ptr(condition)
add_to_scope("~enclosing_scope", parent_chain->from_top(enclosing_block_idx), short_circuit_if)
// how to get proper parent scoping working for this part
short_circuit_if->if_statement.then_part = ast_code_block_ptr(ast_assignment_statement_ptr(short_circuit_result, backing.parameters[1]))
add_to_scope("~enclosing_scope", short_circuit_if, short_circuit_if->if_statement.then_part)
add_before_in(short_circuit_declaration, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
add_before_in(short_circuit_if, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
replace_with_in(node, short_circuit_result, parent_chain)
@@ -128,12 +189,14 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
)
return
}
var enclosing_block_idx = parent_chain->index_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i);)
var enclosing_block_idx = parent_chain->index_from_top_satisfying(is_code_block)
var replace_before: *ast_node
if (enclosing_block_idx > 0)
replace_before = parent_chain->from_top(enclosing_block_idx-1)
else
else if (enclosing_block_idx == 0)
replace_before = node
else
error("gonna corrupt")
var replace_in = parent_chain->from_top(enclosing_block_idx)
var func_type = get_ast_type(backing.func)
for (var i = 0; i < backing.parameters.size; i++;) {
@@ -146,7 +209,9 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
in_function_param_type = get_ast_type(param)->clone_without_ref()
var param_type = get_ast_type(param)
if (!in_function_param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "copy_construct", vector(param_type->clone_with_indirection(1))))) {
var temp_ident = ast_identifier_ptr("temporary_param_boom", param_type->clone_without_ref(), null<ast_node>())
var temp_ident = ast_identifier_ptr("temporary_param_boom", param_type->clone_without_ref(), replace_in)
add_to_scope("temporary_param_boom", temp_ident, replace_in)
add_to_scope("~enclosing_scope", replace_in, temp_ident)
var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>())
var copy_in = make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(param))))
add_before_in(declaration, replace_before, replace_in)
@@ -155,9 +220,10 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
}
}
var func_return_type = func_type->return_type
/*if (!func_return_type->is_ref && func_return_type->indirection == 0 && (func_return_type->is_object() && has_method(func_return_type->type_def, "destruct", vector<*type>()))) {*/
if (!func_return_type->is_ref && func_return_type->indirection == 0 && func_return_type->is_object()) {
var temp_return = ast_identifier_ptr("temporary_return_boomchaka", func_return_type, null<ast_node>())
var temp_return = ast_identifier_ptr("temporary_return_boomchaka", func_return_type, replace_in)
add_to_scope("temporary_return_boomchaka", temp_return, replace_in)
add_to_scope("~enclosing_scope", replace_in, temp_return)
var declaration = ast_declaration_statement_ptr(temp_return, node)
add_before_in(declaration, replace_before, replace_in)
if (has_method(func_return_type->type_def, "destruct", vector<*type>())) {
@@ -179,6 +245,8 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
var param_type = get_ast_type(param)
if (!param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "destruct", vector<*type>()))) {
// the first pass ensures a code_block child
if (!is_code_block(backing.body_statement))
error("BUT WHY")
backing.body_statement->code_block.children.add(order++,
ast_defer_statement_ptr(make_method_call(param, "destruct", vector<*ast_node>())))
}
@@ -190,7 +258,9 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
return;
if (!ident_type->is_ref && ident_type->indirection == 0 && ident_type->is_object()) {
if (backing.expression && has_method(ident_type->type_def, "copy_construct", vector(get_ast_type(backing.expression)->clone_with_increased_indirection()))) {
var temp_cpy_ctst = ast_identifier_ptr("temp_declaration_copy_construct", get_ast_type(backing.expression)->clone_without_ref(), null<ast_node>())
var temp_cpy_ctst = ast_identifier_ptr("temp_declaration_copy_construct", get_ast_type(backing.expression)->clone_without_ref(), parent_chain->top())
add_to_scope("temp_declaration_copy_construct", temp_cpy_ctst, parent_chain->top())
add_to_scope("~enclosing_scope", parent_chain->top(), temp_cpy_ctst)
var declaration = ast_declaration_statement_ptr(temp_cpy_ctst, backing.expression)
add_after_in(make_method_call(backing.identifier, "copy_construct", vector(make_operator_call("&", vector(temp_cpy_ctst)))),
node, parent_chain->top())
@@ -204,10 +274,92 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
node, parent_chain->top())
}
}
if (backing.init_method_call) {
add_after_in(backing.init_method_call, node, parent_chain)
backing.init_method_call = null<ast_node>()
}
}
}
}
run_on_tree(empty_pass_first_half(), construct_in_destruct_out, syntax_ast_pair.second, &visited3)
})
var visited4 = hash_set<*ast_node>()
var var_to_obj = map<*ast_node, *ast_node>()
var fun_to_obj = map<*ast_node, *ast_node>()
all_type_defs.for_each(fun(t: *ast_node) {
t->type_def.variables.for_each(fun(v: *ast_node) {
if (is_declaration_statement(v)) {
var_to_obj[v->declaration_statement.identifier] = t
} else {
// is template
v->template.instantiated.for_each(fun(tv: *ast_node) {
var_to_obj[v->declaration_statement.identifier] = t
})
}
})
t->type_def.methods.for_each(fun(m: *ast_node) {
if (is_function(m)) {
fun_to_obj[m] = t
} else {
// is template
m->template.instantiated.for_each(fun(tm: *ast_node) {
fun_to_obj[tm] = t
})
}
})
})
fun_to_obj.for_each(fun(method: *ast_node, object: *ast_node) {
var this_type = object->type_def.self_type->clone_with_increased_indirection()
var this_ident = ast_identifier_ptr("this", this_type, method)
method->function.parameters.add(0, this_ident)
add_to_scope("this", this_ident, method)
add_to_scope("~enclosing_scope", method, this_ident)
method->function.type->parameter_types.add(0, this_type)
})
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
// Pass 4
var unmethod: fun(*ast_node,*stack<*ast_node>,*hash_set<*ast_node>): bool = fun(node: *ast_node, parent_chain: *stack<*ast_node>, visited: *hash_set<*ast_node>): bool {
match(*node) {
ast_node::function_call(backing) {
// add this to call (including if it's implicit)
if (is_dot_style_method_call(node)) {
var this_ident = backing.func->function_call.parameters[0]
if (backing.func->function_call.func->function.name == ".")
this_ident = make_operator_call("&", vector(this_ident))
backing.func = backing.func->function_call.parameters[1]
backing.parameters.add(0, this_ident)
} else {
// add this
// don't need to check to see if is implicit because a non implicit one wouldn't be in the map
var enclosing_obj = fun_to_obj.get_with_default(backing.func, null<ast_node>())
if (enclosing_obj) {
var this_ident = parent_chain->item_from_top_satisfying(fun(i: *ast_node): bool {
return is_function(i) && fun_to_obj.get_with_default(i, null<ast_node>()) == enclosing_obj
})->function.parameters[0]
backing.parameters.add(0, this_ident)
}
}
}
ast_node::identifier(backing) {
// not if this is the declaration
if (!is_declaration_statement(parent_chain->top()) || parent_chain->top()->declaration_statement.identifier != node) {
// needs to make sure this is actually implicit
var enclosing_obj = var_to_obj.get_with_default(node, null<ast_node>())
if (enclosing_obj && !(is_function_call(parent_chain->top()) &&
is_function(parent_chain->top()->function_call.func) &&
(parent_chain->top()->function_call.func->function.name == "." ||
parent_chain->top()->function_call.func->function.name == "->") &&
parent_chain->top()->function_call.parameters[1] == node)) {
var this_ident = parent_chain->item_from_top_satisfying(fun(i: *ast_node): bool {
return is_function(i) && fun_to_obj.get_with_default(i, null<ast_node>()) == enclosing_obj
})->function.parameters[0]
replace_with_in(node, make_operator_call("->", vector(this_ident, node)), parent_chain)
}
}
}
}
return true
}
run_on_tree(unmethod, empty_pass_second_half(), syntax_ast_pair.second, &visited4)
})
}

View File

@@ -361,6 +361,11 @@ fun replace_with_in(orig: *ast_node, new: *ast_node, in: *ast_node) {
return
}
}
ast_node::function(backing) {
if (backing.body_statement == orig)
backing.body_statement = new
return
}
ast_node::function_call(backing) {
if (backing.func == orig) {
backing.func = new
@@ -458,8 +463,7 @@ fun run_on_tree_helper(func_before: fun(*ast_node,*stack<*ast_node>,*hash_set<*a
ast_node::declaration_statement(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, visited);)
ast_node::if_comp(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, visited);)
ast_node::function_call(backing) {
/*if (!is_function(backing.func))*/
run_on_tree_helper(func_before, func_after, backing.func, parent_chain, visited)
run_on_tree_helper(func_before, func_after, backing.func, parent_chain, visited)
node->function_call.parameters.for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, visited);)
}
ast_node::cast(backing) get_ast_children(node).for_each(fun(n: *ast_node) run_on_tree_helper(func_before, func_after, n, parent_chain, visited);)