diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index 87e1073..c1f4e03 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -58,7 +58,7 @@ scoped_identifier = scoped_identifier WS scope_op WS identifier | identifier ; #Note that to prevent confilct with nested templates (T>) it is a nonterminal contructed as follows right_shift = ">" ">" ; -overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" ; +overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" | "\(" "\)" ; func_identifier = identifier | identifier overloadable_operator ; function = "fun" WS func_identifier WS template_dec WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS code_block | "fun" WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS code_block ; diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index f6db8fa..a746695 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -376,8 +376,24 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree newNode = functionLookup(scope, lookupName, types); if (newNode == NULL) { std::cout << "scope lookup failed! Could not find " << lookupName << " in identifier (functionLookup)" << std::endl; - std::cout << "(maybe this is supposted to happen because the function is a template and we're infrencing)" << std::endl; - //throw "LOOKUP ERROR: " + lookupName; + std::cout << "(maybe this is supposted to happen because the function is a template and we're infrencing), or this is a operator() call" << std::endl; + // Ok, now we try the case where the lookupName is an object, and we'll try to look for operator() + // in its scope + for (auto possibleObj : scopeLookup(scope, lookupName)) { + NodeTree *typeDefinition = possibleObj->getDataRef()->valueType->typeDefinition; + if (typeDefinition) { + // ugly for now, it's just operator because the ( and ) have been removed by a removal + // pass + NodeTree* perenOp = functionLookup(typeDefinition, "operator", types); + if (perenOp) { + NodeTree* dotFunctionCall = new NodeTree(".", ASTData(function_call, Symbol(".", true))); + dotFunctionCall->addChild(languageLevelOperators["."][0]); //function definition + dotFunctionCall->addChild(possibleObj); // The object whose method we're calling + dotFunctionCall->addChild(perenOp); //The method we're calling + return dotFunctionCall; + } + } + } return nullptr; } } else { @@ -536,7 +552,6 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree throw "LOOKUP ERROR: " + functionCallName; } return newNode; - //skipChildren.insert(1); } if (children.size() == 1) { newNode = transform(children[0], scope, types, templateTypeReplacements); //Just a promoted child, so do it instead diff --git a/tests/test_functionOperator.expected_results b/tests/test_functionOperator.expected_results new file mode 100644 index 0000000..4503228 --- /dev/null +++ b/tests/test_functionOperator.expected_results @@ -0,0 +1,2 @@ +7 +hi diff --git a/tests/test_functionOperator.krak b/tests/test_functionOperator.krak new file mode 100644 index 0000000..8951a96 --- /dev/null +++ b/tests/test_functionOperator.krak @@ -0,0 +1,17 @@ +import io:* + +typedef FuncObj { + fun operator()(a:int, b:char*): void { + println(a) + println(b) + } +} + +fun main():int { + var obj:FuncObj + obj(7, "hi") + return 0; +} + + +