From 105a969a00fc5e6e23c12b64e4b8d11e28db903e Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sun, 24 Jan 2016 01:49:14 -0500 Subject: [PATCH] Method calls from within method from same object --- stdlib/ast_transformation.krak | 2 +- stdlib/c_generator.krak | 15 ++++++++++++--- tests/to_parse.krak | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/stdlib/ast_transformation.krak b/stdlib/ast_transformation.krak index 1a2a81e..b3bda99 100644 --- a/stdlib/ast_transformation.krak +++ b/stdlib/ast_transformation.krak @@ -269,7 +269,7 @@ obj ast_transformation (Object) { var value_type = null() if (value_str[0] == '"') value_type = type_ptr(base_type::character(), 1) - else if (value_str[0] == '\'') //' lol, comment hack for vim syntax highlighting (my fault, of course) + else if (value_str[0] == '\'') //'// lol, comment hack for vim syntax highlighting (my fault, of course) value_type = type_ptr(base_type::character()) else { // should differentiate between float and double... diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index 86e50b7..dde002b 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -151,20 +151,27 @@ obj c_generator (Object) { fun generate_function_call(node: *ast_node, enclosing_object: *ast_node): string { var func_name = string() var call_string = string() - if (is_function_call(node->function_call.func) && + + // handle the obj.method() style of method call + var dot_style_method_call = is_function_call(node->function_call.func) && is_function(node->function_call.func->function_call.func) && (node->function_call.func->function_call.func->function.name == "->" || node->function_call.func->function_call.func->function.name == ".") && is_function(node->function_call.func->function_call.parameters[1]) && is_type_def(get_ast_scope(node->function_call.func->function_call.parameters[1])->get(string("~enclosing_scope"))[0]) - ) { + + if (dot_style_method_call) { func_name = generate(node->function_call.func->function_call.parameters[1], 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 { + } else { // regular style function name func_name = generate(node->function_call.func, enclosing_object) } + // handle method call from inside method of same object + if (!dot_style_method_call && enclosing_object && get_ast_scope(enclosing_object)->contains_key(func_name)) + call_string += string("this") + var parameters = node->function_call.parameters if (func_name == "+" || func_name == "-" || func_name == "*" || func_name == "/" || func_name == "||" @@ -178,6 +185,8 @@ obj c_generator (Object) { // the post ones need to be post-ed specifically, and take the p off if (func_name == "++p" || func_name == "--p") return string("(") + generate(parameters[0], enclosing_object) + ")" + func_name.slice(0,-2) + + // regular parameter generation parameters.for_each(fun(param: *ast_node) { if (call_string != "") call_string += ", " diff --git a/tests/to_parse.krak b/tests/to_parse.krak index 97508f8..718ca58 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -3,8 +3,9 @@ import to_import: simple_print, a, b, string_id obj Something (ObjectTrait) { var member: int fun method(a: int):int { - return 5+a+member + return 5+a+member + other_method() } + fun other_method(): int return 7; fun return_this(): *Something { return this; }