Added automatic distructor calling for going out of scope, found out that += is broken (and just comes through as =)

This commit is contained in:
Nathan Braswell
2016-01-29 22:46:09 -05:00
parent da38ae03ed
commit 6f1f31a212
5 changed files with 353 additions and 331 deletions

View File

@@ -338,8 +338,8 @@ obj code_block (Object) {
return children == other.children && scope == other.scope
}
}
fun ast_statement_ptr(): *ast_node {
var to_ret.construct(): statement
fun ast_statement_ptr(child: *ast_node): *ast_node {
var to_ret.construct(child): statement
var ptr = new<ast_node>()
ptr->copy_construct(&ast_node::statement(to_ret))
return ptr
@@ -353,9 +353,10 @@ fun is_statement(node: *ast_node): bool {
obj statement (Object) {
var scope: map<string, vector<*ast_node>>
var child: *ast_node
fun construct(): *statement {
fun construct(child_in: *ast_node): *statement {
child = null<ast_node>()
scope.construct()
child = child_in
return this
}
fun copy_construct(old: *statement) {

View File

@@ -160,6 +160,8 @@ obj ast_transformation (Object) {
fun fourth_pass(parse_tree: *tree<symbol>, translation_unit: *ast_node) {
println(string("Fourth Pass for ") + translation_unit->translation_unit.name)
}
}
fun transform_type(node: *tree<symbol>, scope: *ast_node, template_replacements: map<string, *type>): *type {
// check for references and step down
// always get to pre-reffed level
@@ -200,9 +202,7 @@ obj ast_transformation (Object) {
return type_ptr(base_type::none(), indirection)
}
}
/*NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope, std::vector<Type> types, bool limitToFunction, std::map<std::string, Type*> templateTypeReplacements) {*/
fun transform(node: *tree<symbol>, scope: *ast_node): *ast_node return transform(node, scope, search_type::none())
fun transform(node: *tree<symbol>, scope: *ast_node, searching_for: search_type): *ast_node {
var name = node->data.name
if (name == "identifier" || name == "scoped_identifier") {
@@ -308,11 +308,7 @@ obj ast_transformation (Object) {
new_passthrough->simple_passthrough.passthrough_str = concat_symbol_tree(get_node("triple_quoted_string", node)).slice(3,-4)
return new_passthrough
}
fun transform_statement(node: *tree<symbol>, scope: *ast_node): *ast_node {
var new_statement = ast_statement_ptr()
new_statement->statement.child = transform(node->children[0], scope)
return new_statement
}
fun transform_statement(node: *tree<symbol>, scope: *ast_node): *ast_node return ast_statement_ptr(transform(node->children[0], scope));
fun transform_declaration_statement(node: *tree<symbol>, scope: *ast_node): *ast_node {
// this might have an init position method call
var identifiers = get_nodes("identifier", node)
@@ -335,12 +331,26 @@ obj ast_transformation (Object) {
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)
declaration->declaration_statement.init_method_call = make_method_call(identifier, method, parameters)
}
add_to_scope(name, identifier, scope)
return declaration
}
fun has_method(object: *ast_node, name: *char, parameter_types: vector<*type>): bool return has_method(object, string(name), parameter_types);
fun has_method(object: *ast_node, name: string, parameter_types: vector<*type>): bool {
return function_lookup(name, object, parameter_types) || false
}
fun make_method_call(object_ident: *ast_node, name: *char, parameters: vector<*ast_node>): *ast_node return make_method_call(object_ident, string(name), parameters);
fun make_method_call(object_ident: *ast_node, name: string, parameters: vector<*ast_node>): *ast_node {
var method = function_lookup(name, get_ast_type(object_ident)->type_def, parameters.map(fun(param: *ast_node): *type return get_ast_type(param);))
print("Here is the Method: ")
println(method)
return make_method_call(object_ident, method, parameters)
}
fun make_method_call(object_ident: *ast_node, method: *ast_node, parameters: vector<*ast_node>): *ast_node {
var method_access = ast_function_call_ptr(get_builtin_function(string("."), vector(get_ast_type(object_ident), get_ast_type(method))), vector(object_ident, method))
return ast_function_call_ptr(method_access, parameters)
}
fun transform_assignment_statement(node: *tree<symbol>, scope: *ast_node): *ast_node {
var assignment = ast_assignment_statement_ptr(transform(get_node("factor", node), scope), transform(get_node("boolean_expression", node), scope))
return assignment
@@ -498,7 +508,6 @@ obj ast_transformation (Object) {
}
return results
}
}
fun concat_symbol_tree(node: *tree<symbol>): string {
var str.construct(): string

View File

@@ -8,6 +8,8 @@ import tree:*
import symbol:*
import ast_nodes:*
import poset:*
// we import ast_transformation for its make_method_call function
import ast_transformation:*
fun code_triple(): code_triple return code_triple(string(), string(), string());
fun code_triple(only: *char): code_triple return code_triple(string(), string(only), string());
@@ -172,11 +174,14 @@ obj c_generator (Object) {
}
fun generate_statement(node: *ast_node, enclosing_object: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple return generate(node->statement.child, enclosing_object, defer_stack) + ";\n";
fun generate_declaration_statement(node: *ast_node, enclosing_object: *ast_node, defer_stack: *stack<pair<bool,stack<*ast_node>>>): code_triple {
// add destruct to defer_stac
// add destruct to defer_stack
var identifier = node->declaration_statement.identifier
var ident_type = identifier->identifier.type
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>>>>())
if (ident_type->is_object() && has_method(ident_type->type_def, "destruct", vector<*type>()))
defer_stack->top().second.push(ast_statement_ptr(make_method_call(identifier, "destruct", vector<*ast_node>())))
return to_ret
}
fun generate_assignment_statement(node: *ast_node, enclosing_object: *ast_node): code_triple {

View File

@@ -121,5 +121,11 @@ obj type (Object) {
to_ret->indirection = ind
return to_ret
}
fun is_object(): bool {
match (base) {
base_type::object() return true
}
return false
}
}

View File

@@ -17,8 +17,8 @@ obj Something (ObjectTrait) {
member = old->member
}
fun destruct() {
simple_println("Destructing a Something")
member = -800
simple_print("Destructing a Something: ")
simple_println(member)
}
fun method(a: int):int {
return 5+a+member + other_method()
@@ -107,6 +107,7 @@ fun main(): int {
simple_println(test_methods.member)
simple_println(test_methods_param.member)
var second_obj = test_methods
second_obj.member += 5
simple_println(second_obj.member)
return 0
}