diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index c592373..1a2a81e 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -151,7 +151,7 @@ obj ast_transformation (Object) { // make sure not a template // huh, I guess I can't actually assign to the backing. // This is actually a little bit of a problem, maybe these should be pointers also. All the pointers! - node->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[node]), translation_unit) + node->function.body_statement = transform_statement(get_node("statement", ast_to_syntax[node]), node) } } }) @@ -193,7 +193,7 @@ obj ast_transformation (Object) { print("There are "); print(possibilities.size); println(" possibilites for this object type lookup") for (var i = 0; i < possibilities.size; i++;) { match(*possibilities[i]) { - ast_node::type_def(backing) return backing.self_type + ast_node::type_def(backing) return backing.self_type->clone_with_indirection(indirection) } } println("No objects in lookup, returning none") @@ -250,9 +250,12 @@ obj ast_transformation (Object) { return nodes.map(fun(node: *tree): *ast_node return transform(node, scope);) } fun transform_identifier(node: *tree, scope: *ast_node, searching_for: search_type): *ast_node { - // for identifier we have to do scope lookup, etc, and maybe limit to function - // NOT THIS + // first, we check for and generate this var name = concat_symbol_tree(node) + if (name == "this") { + while (!is_type_def(scope)) scope = get_ast_scope(scope)->get(string("~enclosing_scope"))[0] + return ast_identifier_ptr("this", scope->type_def.self_type->clone_with_indirection(1)) + } /*return ast_identifier_ptr(name, type_ptr(base_type::void_return()))*/ match (searching_for) { search_type::none() return identifier_lookup(name, scope) diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index 117094f..86e50b7 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -158,7 +158,10 @@ obj c_generator (Object) { is_type_def(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(string("~enclosing_scope"))[0]) ) { func_name = generate(node->function_call.func->function_call.parameters[1], enclosing_object) - call_string = string("&") + generate(node->function_call.func->function_call.parameters[0], enclosing_object) + // don't add & if it was -> + if (node->function_call.func->function_call.func->function.name == ".") + call_string += string("&") + call_string += generate(node->function_call.func->function_call.parameters[0], enclosing_object) } else { func_name = generate(node->function_call.func, enclosing_object) } @@ -243,7 +246,7 @@ obj c_generator (Object) { base_type::floating() return string("float") + indirection base_type::double_precision() return string("double") + indirection base_type::object() { - return type->type_def->type_def.name + return type->type_def->type_def.name + indirection } base_type::function() { var temp = indirection + string("function: (") diff --git a/stdlib/type.krak b/stdlib/type.krak index 72d4dd3..d09e9f9 100644 --- a/stdlib/type.krak +++ b/stdlib/type.krak @@ -98,7 +98,7 @@ obj type (Object) { for (var i = 0; i < indirection; i++;) indirection_str += "*" match (base) { base_type::none() return indirection_str + string("none") - base_type::object() return indirection_str + string("obj:") + type_def->type_def.name + base_type::object() return indirection_str + type_def->type_def.name base_type::template() return indirection_str + string("template") base_type::template_type() return indirection_str + string("template_type") base_type::void_return() return indirection_str + string("void_return") @@ -115,6 +115,11 @@ obj type (Object) { } return string("impossible type, indirection:") + indirection } + fun clone_with_indirection(ind: int): *type { + var to_ret = new() + to_ret->copy_construct(this) + to_ret->indirection = ind + return to_ret + } } - diff --git a/tests/to_import.krak b/tests/to_import.krak index 18ac7df..9b25b2b 100644 --- a/tests/to_import.krak +++ b/tests/to_import.krak @@ -9,6 +9,9 @@ fun simple_print(to_print: int) { printf("%d", to_print); """ } +fun string_id(it: *char): *char { + return it; +} var a = 1 var b:int diff --git a/tests/to_parse.krak b/tests/to_parse.krak index 7b21930..97508f8 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -1,10 +1,13 @@ -import to_import: simple_print, a, b +import to_import: simple_print, a, b, string_id obj Something (ObjectTrait) { var member: int fun method(a: int):int { return 5+a+member } + fun return_this(): *Something { + return this; + } } /*fun some_function(): int return 0;*/ @@ -46,6 +49,10 @@ fun main(): int { simple_print("\n") simple_print(an_obj.method(1)) simple_print("\n") + simple_print(string_id("WoooopsEee\n")) + var with_ptr = an_obj.return_this() + simple_print(with_ptr->method(2)) + simple_print("\n") return 0 }