140 lines
10 KiB
Plaintext
140 lines
10 KiB
Plaintext
import symbol:*
|
|
import tree:*
|
|
import vector:*
|
|
import map:*
|
|
import util:*
|
|
import string:*
|
|
import mem:*
|
|
import io:*
|
|
import ast_nodes:*
|
|
import ast_transformation:*
|
|
import pass_common:*
|
|
|
|
// 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
|
|
fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_syntax: *map<*ast_node, *tree<symbol>>) {
|
|
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_statement_ptr(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))
|
|
}
|
|
// 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)
|
|
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.statement)
|
|
backing.statement->code_block.children.add(0, ast_statement_ptr(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))
|
|
}
|
|
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))
|
|
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_if = ast_if_statement_ptr(update_ident)
|
|
update_if->if_statement.then_part = ast_statement_ptr(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())))))
|
|
|
|
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)))
|
|
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))
|
|
}
|
|
}
|
|
}
|
|
run_on_tree(ensure_block_and_munge, empty_pass_second_half, syntax_ast_pair.second)
|
|
// Pass 2
|
|
var short_circut_op: fun(*ast_node,*stack<*ast_node>): bool = fun(node: *ast_node, parent_chain: *stack<*ast_node>): bool {
|
|
match(*node) {
|
|
ast_node::function_call(backing) {
|
|
var func_name = string()
|
|
if (is_function(backing.func)) {
|
|
func_name = backing.func->function.name
|
|
if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/"
|
|
|| func_name == "<" || func_name == ">" || func_name == "<=" || func_name == ">="
|
|
|| func_name == "==" || func_name == "!=" || func_name == "%" || func_name == "^"
|
|
|| func_name == "|" || func_name == "&" || func_name == "." || func_name == "->"
|
|
|| func_name == "." || func_name == "->" || func_name == "[]" || func_name == "++p" || func_name == "--p"
|
|
|| func_name == "*" || func_name == "&"
|
|
)
|
|
return true
|
|
}
|
|
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 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])))
|
|
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))
|
|
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))
|
|
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
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
run_on_tree(short_circut_op, empty_pass_second_half, syntax_ast_pair.second)
|
|
// Pass 3
|
|
var construct_in_destruct_out = fun(node: *ast_node, parent_chain: *stack<*ast_node>) {
|
|
match(*node) {
|
|
ast_node::function(backing) {
|
|
var order = 0;
|
|
backing.parameters.for_each(fun(param: *ast_node) {
|
|
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>())))))
|
|
}
|
|
})
|
|
}
|
|
ast_node::declaration_statement(backing) {
|
|
var ident_type = get_ast_type(backing.identifier)
|
|
if (is_translation_unit(parent_chain->top()) || is_type_def(parent_chain->top()))
|
|
return;
|
|
if (!ident_type->is_ref && ident_type->indirection == 0 && (ident_type->is_object() && 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))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
run_on_tree(empty_pass_first_half, construct_in_destruct_out, syntax_ast_pair.second)
|
|
})
|
|
}
|
|
|