Basic CTCE working! In between commit because #link(a) syntax changed to #link(a)

This commit is contained in:
Nathan Braswell
2016-07-03 22:50:42 -07:00
parent 6fee942756
commit 0f2ac1421a
17 changed files with 183 additions and 183 deletions

View File

@@ -25,10 +25,10 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
// 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_statement_ptr(ast_code_block_ptr(backing.body_statement))
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::if_statement(backing) {
if (!is_code_block(backing.then_part)) backing.then_part = ast_statement_ptr(ast_code_block_ptr(backing.then_part))
if (backing.else_part && !is_code_block(backing.else_part)) backing.else_part = ast_statement_ptr(ast_code_block_ptr(backing.else_part))
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)
}
// no need for case because it's already been lowered
ast_node::while_loop(backing) {
@@ -37,33 +37,33 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
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
var condition_ident = ast_identifier_ptr("condition_temp", get_ast_type(condition), backing.statement)
backing.statement->code_block.children.add(0, ast_statement_ptr(ast_declaration_statement_ptr(condition_ident, condition, false)))
backing.statement->code_block.children.add(0, ast_declaration_statement_ptr(condition_ident, condition, false))
var condition_if = ast_if_statement_ptr(make_operator_call("!", vector(condition_ident)))
condition_if->if_statement.then_part = ast_statement_ptr(ast_branching_statement_ptr(branching_type::break_stmt()))
backing.statement->code_block.children.add(1, ast_statement_ptr(condition_if))
condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt())
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)
add_before_in(backing.init, parent_chain->top(), parent_chain->from_top(1))
add_before_in(backing.init, node, parent_chain->top())
backing.init = null<ast_node>()
// the do_update goes in the block above the for
var update_ident = ast_identifier_ptr("do_update", type_ptr(base_type::boolean()), parent_chain->from_top(1))
add_before_in(ast_statement_ptr(ast_declaration_statement_ptr(update_ident, ast_value_ptr(string("false"), type_ptr(base_type::boolean())), false)),
parent_chain->top(), parent_chain->from_top(1))
var update_ident = ast_identifier_ptr("do_update", type_ptr(base_type::boolean()), parent_chain->top())
add_before_in(ast_declaration_statement_ptr(update_ident, ast_value_ptr(string("false"), type_ptr(base_type::boolean())), false),
node, parent_chain->top())
var update_if = ast_if_statement_ptr(update_ident)
update_if->if_statement.then_part = ast_statement_ptr(ast_code_block_ptr(backing.update))
update_if->if_statement.then_part = ast_code_block_ptr(backing.update)
backing.update = null<ast_node>()
backing.body->code_block.children.add(0, ast_statement_ptr(update_if))
backing.body->code_block.children.add(1, ast_statement_ptr(ast_assignment_statement_ptr(update_ident, ast_value_ptr(string("true"), type_ptr(base_type::boolean())))))
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()))))
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
var condition_ident = ast_identifier_ptr("condition_temp", get_ast_type(condition), backing.body)
backing.body->code_block.children.add(2, ast_statement_ptr(ast_declaration_statement_ptr(condition_ident, condition, false)))
backing.body->code_block.children.add(2, ast_declaration_statement_ptr(condition_ident, condition, false))
var condition_if = ast_if_statement_ptr(make_operator_call("!", vector(condition_ident)))
condition_if->if_statement.then_part = ast_statement_ptr(ast_branching_statement_ptr(branching_type::break_stmt()))
backing.body->code_block.children.add(3, ast_statement_ptr(condition_if))
condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt())
backing.body->code_block.children.add(3, condition_if)
}
}
}
@@ -87,17 +87,17 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
if (func_name == "||" || func_name == "&&") {
var enclosing_block_idx = parent_chain->index_from_top_satisfying(fun(i: *ast_node): bool return is_code_block(i);)
var short_circuit_result = ast_identifier_ptr("short_circut_result", type_ptr(base_type::boolean()), parent_chain->from_top(enclosing_block_idx))
var short_circuit_declaration = ast_statement_ptr(ast_declaration_statement_ptr(short_circuit_result, backing.parameters[0], false))
var short_circuit_declaration = ast_declaration_statement_ptr(short_circuit_result, backing.parameters[0], false)
var condition = short_circuit_result
if (func_name == "||")
condition = make_operator_call("!", vector(condition))
var short_circuit_if = ast_if_statement_ptr(condition)
// how to get proper parent scoping working for this part
short_circuit_if->if_statement.then_part = ast_code_block_ptr(ast_statement_ptr(ast_assignment_statement_ptr(short_circuit_result, backing.parameters[1])))
short_circuit_if->if_statement.then_part = ast_code_block_ptr(ast_assignment_statement_ptr(short_circuit_result, backing.parameters[1]))
add_before_in(short_circuit_declaration, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
add_before_in(ast_statement_ptr(short_circuit_if), 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)
var shorter_tree =stack_from_vector( parent_chain->data.slice(0, parent_chain->size()-enclosing_block_idx))
var shorter_tree = stack_from_vector( parent_chain->data.slice(0, parent_chain->size()-enclosing_block_idx))
run_on_tree_helper(short_circut_op, empty_pass_second_half, short_circuit_declaration, &shorter_tree, false)
run_on_tree_helper(short_circut_op, empty_pass_second_half, short_circuit_if, &shorter_tree, false)
return false
@@ -124,6 +124,12 @@ 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 replace_before: *ast_node
if (enclosing_block_idx > 0)
replace_before = parent_chain->from_top(enclosing_block_idx-1)
else
replace_before = node
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++;) {
var param = backing.parameters[i]
@@ -136,20 +142,20 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
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 declaration = ast_statement_ptr(ast_declaration_statement_ptr(temp_ident, null<ast_node>(), false))
var copy_in = ast_statement_ptr(make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(param)))))
add_before_in(declaration, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
add_before_in(copy_in, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
var declaration = ast_declaration_statement_ptr(temp_ident, null<ast_node>(), false)
var copy_in = make_method_call(temp_ident, "copy_construct", vector(make_operator_call("&", vector(param))))
add_before_in(declaration, replace_before, replace_in)
add_before_in(copy_in, replace_before, replace_in)
backing.parameters[i] = temp_ident
}
}
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>()))) {
var temp_return = ast_identifier_ptr("temporary_return_boomchaka", func_return_type, null<ast_node>())
var declaration = ast_statement_ptr(ast_declaration_statement_ptr(temp_return, node, false))
add_before_in(declaration, parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
add_before_in(ast_statement_ptr(ast_defer_statement_ptr(ast_statement_ptr(make_method_call(temp_return, "destruct", vector<*ast_node>())))),
parent_chain->from_top(enclosing_block_idx-1), parent_chain->from_top(enclosing_block_idx))
var declaration = ast_declaration_statement_ptr(temp_return, node, false)
add_before_in(declaration, replace_before, replace_in)
add_before_in(ast_defer_statement_ptr(make_method_call(temp_return, "destruct", vector<*ast_node>())),
replace_before, replace_in)
replace_with_in(node, temp_return, parent_chain)
}
}
@@ -159,8 +165,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
backing.body_statement->statement.child->code_block.children.add(order++,
ast_statement_ptr(ast_defer_statement_ptr(ast_statement_ptr(make_method_call(param, "destruct", vector<*ast_node>())))))
backing.body_statement->code_block.children.add(order++,
ast_defer_statement_ptr(make_method_call(param, "destruct", vector<*ast_node>())))
}
})
}
@@ -171,18 +177,18 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
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 declaration = ast_statement_ptr(ast_declaration_statement_ptr(temp_cpy_ctst, backing.expression, false))
add_after_in(ast_statement_ptr(make_method_call(backing.identifier, "copy_construct", vector(make_operator_call("&", vector(temp_cpy_ctst))))),
parent_chain->top(), parent_chain->from_top(1))
var declaration = ast_declaration_statement_ptr(temp_cpy_ctst, backing.expression, false)
add_after_in(make_method_call(backing.identifier, "copy_construct", vector(make_operator_call("&", vector(temp_cpy_ctst)))),
node, parent_chain->top())
// do second so the order's right
add_after_in(declaration,
parent_chain->top(), parent_chain->from_top(1))
node, parent_chain->top())
backing.expression = null<ast_node>()
}
if (has_method(ident_type->type_def, "destruct", vector<*type>())) {
// have to go up one because our parent is a statement
add_after_in(ast_statement_ptr(ast_defer_statement_ptr(ast_statement_ptr(make_method_call(backing.identifier, "destruct", vector<*ast_node>())))),
parent_chain->top(), parent_chain->from_top(1))
add_after_in(ast_defer_statement_ptr(make_method_call(backing.identifier, "destruct", vector<*ast_node>())),
node, parent_chain->top())
}
}
}