shortening of str and vec
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import symbol:*
|
||||
import tree:*
|
||||
import vector:*
|
||||
import vec:*
|
||||
import map:*
|
||||
import util:*
|
||||
import string:*
|
||||
import str:*
|
||||
import mem:*
|
||||
import io:*
|
||||
import ast_nodes:*
|
||||
@@ -30,13 +30,13 @@ import hash_set:*
|
||||
PASS THREE 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>>) {
|
||||
fun obj_lower(name_ast_map: *map<str, 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>) {
|
||||
name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
|
||||
// Pass 1
|
||||
var ensure_block_and_munge: 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) {
|
||||
@@ -66,11 +66,11 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
add_to_scope("~enclosing_scope", node, backing.statement)
|
||||
}
|
||||
var condition = backing.condition
|
||||
backing.condition = ast_value_ptr(string("true"), type_ptr(base_type::boolean()))
|
||||
backing.condition = ast_value_ptr(str("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_declaration_statement_ptr(condition_ident, condition))
|
||||
var condition_if = ast_if_statement_ptr(make_operator_call("!", vector(condition_ident)))
|
||||
var condition_if = ast_if_statement_ptr(make_operator_call("!", vec(condition_ident)))
|
||||
condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt())
|
||||
backing.statement->code_block.children.add(1, condition_if)
|
||||
}
|
||||
@@ -83,7 +83,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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->top())
|
||||
add_before_in(ast_declaration_statement_ptr(update_ident, ast_value_ptr(string("false"), type_ptr(base_type::boolean()))),
|
||||
add_before_in(ast_declaration_statement_ptr(update_ident, ast_value_ptr(str("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)
|
||||
@@ -91,14 +91,14 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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()))))
|
||||
backing.body->code_block.children.add(1, ast_assignment_statement_ptr(update_ident, ast_value_ptr(str("true"), type_ptr(base_type::boolean()))))
|
||||
|
||||
var condition = backing.condition
|
||||
backing.condition = ast_value_ptr(string("true"), type_ptr(base_type::boolean()))
|
||||
backing.condition = ast_value_ptr(str("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_declaration_statement_ptr(condition_ident, condition))
|
||||
var condition_if = ast_if_statement_ptr(make_operator_call("!", vector(condition_ident)))
|
||||
var condition_if = ast_if_statement_ptr(make_operator_call("!", vec(condition_ident)))
|
||||
condition_if->if_statement.then_part = ast_branching_statement_ptr(branching_type::break_stmt())
|
||||
backing.body->code_block.children.add(3, condition_if)
|
||||
}
|
||||
@@ -109,7 +109,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
})
|
||||
|
||||
// make sure all blockes munged before we move ahead
|
||||
name_ast_map->for_each(fun(name: string, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
|
||||
name_ast_map->for_each(fun(name: str, syntax_ast_pair: pair<*tree<symbol>,*ast_node>) {
|
||||
var visit = hash_set<*ast_node>()
|
||||
var short_check: 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) {
|
||||
@@ -130,7 +130,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
error("Bad in 2")
|
||||
}
|
||||
ast_node::function_call(backing) {
|
||||
var func_name = string()
|
||||
var func_name = str()
|
||||
if (is_function(backing.func)) {
|
||||
func_name = backing.func->function.name
|
||||
if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/"
|
||||
@@ -148,7 +148,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
var short_circuit_declaration = ast_declaration_statement_ptr(short_circuit_result, backing.parameters[0])
|
||||
var condition = short_circuit_result
|
||||
if (func_name == "||")
|
||||
condition = make_operator_call("!", vector(condition))
|
||||
condition = make_operator_call("!", vec(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
|
||||
@@ -202,12 +202,12 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
else
|
||||
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))))) {
|
||||
if (!in_function_param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "copy_construct", vec(param_type->clone_with_indirection(1))))) {
|
||||
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))))
|
||||
var copy_in = make_method_call(temp_ident, "copy_construct", vec(make_operator_call("&", vec(param))))
|
||||
add_before_in(declaration, replace_before, replace_in)
|
||||
add_before_in(copy_in, replace_before, replace_in)
|
||||
backing.parameters[i] = temp_ident
|
||||
@@ -220,8 +220,8 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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>())) {
|
||||
add_before_in(ast_defer_statement_ptr(make_method_call(temp_return, "destruct", vector<*ast_node>())),
|
||||
if (has_method(func_return_type->type_def, "destruct", vec<*type>())) {
|
||||
add_before_in(ast_defer_statement_ptr(make_method_call(temp_return, "destruct", vec<*ast_node>())),
|
||||
replace_before, replace_in)
|
||||
}
|
||||
replace_with_in(node, temp_return, parent_chain)
|
||||
@@ -237,12 +237,12 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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>()))) {
|
||||
if (!param_type->is_ref && param_type->indirection == 0 && (param_type->is_object() && has_method(param_type->type_def, "destruct", vec<*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>())))
|
||||
ast_defer_statement_ptr(make_method_call(param, "destruct", vec<*ast_node>())))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -251,20 +251,20 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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()) {
|
||||
if (backing.expression && has_method(ident_type->type_def, "copy_construct", vector(get_ast_type(backing.expression)->clone_with_increased_indirection()))) {
|
||||
if (backing.expression && has_method(ident_type->type_def, "copy_construct", vec(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(), 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)))),
|
||||
add_after_in(make_method_call(backing.identifier, "copy_construct", vec(make_operator_call("&", vec(temp_cpy_ctst)))),
|
||||
node, parent_chain->top())
|
||||
// do second so the order's right
|
||||
add_after_in(declaration,
|
||||
node, parent_chain->top())
|
||||
backing.expression = null<ast_node>()
|
||||
}
|
||||
if (has_method(ident_type->type_def, "destruct", vector<*type>())) {
|
||||
add_after_in(ast_defer_statement_ptr(make_method_call(backing.identifier, "destruct", vector<*ast_node>())),
|
||||
if (has_method(ident_type->type_def, "destruct", vec<*type>())) {
|
||||
add_after_in(ast_defer_statement_ptr(make_method_call(backing.identifier, "destruct", vec<*ast_node>())),
|
||||
node, parent_chain->top())
|
||||
}
|
||||
}
|
||||
@@ -283,7 +283,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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))
|
||||
return_value = make_operator_call("&", vec(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)
|
||||
@@ -294,7 +294,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
|
||||
// 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))
|
||||
temp_return = make_operator_call("*", vec(temp_return))
|
||||
backing.return_value = temp_return
|
||||
}
|
||||
}
|
||||
@@ -335,7 +335,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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>) {
|
||||
name_ast_map->for_each(fun(name: str, 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) {
|
||||
@@ -344,7 +344,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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))
|
||||
this_ident = make_operator_call("&", vec(this_ident))
|
||||
backing.func = backing.func->function_call.parameters[1]
|
||||
backing.parameters.add(0, this_ident)
|
||||
} else {
|
||||
@@ -372,7 +372,7 @@ fun obj_lower(name_ast_map: *map<string, pair<*tree<symbol>,*ast_node>>, ast_to_
|
||||
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)
|
||||
replace_with_in(node, make_operator_call("->", vec(this_ident, node)), parent_chain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user