A kinda ugly/hacky addition, but overloading () as operator() works now!
This commit is contained in:
@@ -58,7 +58,7 @@ scoped_identifier = scoped_identifier WS scope_op WS identifier | identifier ;
|
|||||||
|
|
||||||
#Note that to prevent confilct with nested templates (T<A<B>>) it is a nonterminal contructed as follows
|
#Note that to prevent confilct with nested templates (T<A<B>>) it is a nonterminal contructed as follows
|
||||||
right_shift = ">" ">" ;
|
right_shift = ">" ">" ;
|
||||||
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" ;
|
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" | "\(" "\)" ;
|
||||||
func_identifier = identifier | identifier overloadable_operator ;
|
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 ;
|
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 ;
|
||||||
|
|
||||||
|
|||||||
@@ -376,8 +376,24 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
newNode = functionLookup(scope, lookupName, types);
|
newNode = functionLookup(scope, lookupName, types);
|
||||||
if (newNode == NULL) {
|
if (newNode == NULL) {
|
||||||
std::cout << "scope lookup failed! Could not find " << lookupName << " in identifier (functionLookup)" << std::endl;
|
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;
|
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;
|
||||||
//throw "LOOKUP ERROR: " + lookupName;
|
// 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<ASTData> *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<ASTData>* perenOp = functionLookup(typeDefinition, "operator", types);
|
||||||
|
if (perenOp) {
|
||||||
|
NodeTree<ASTData>* dotFunctionCall = new NodeTree<ASTData>(".", 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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -536,7 +552,6 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
throw "LOOKUP ERROR: " + functionCallName;
|
throw "LOOKUP ERROR: " + functionCallName;
|
||||||
}
|
}
|
||||||
return newNode;
|
return newNode;
|
||||||
//skipChildren.insert(1);
|
|
||||||
}
|
}
|
||||||
if (children.size() == 1) {
|
if (children.size() == 1) {
|
||||||
newNode = transform(children[0], scope, types, templateTypeReplacements); //Just a promoted child, so do it instead
|
newNode = transform(children[0], scope, types, templateTypeReplacements); //Just a promoted child, so do it instead
|
||||||
|
|||||||
2
tests/test_functionOperator.expected_results
Normal file
2
tests/test_functionOperator.expected_results
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
7
|
||||||
|
hi
|
||||||
17
tests/test_functionOperator.krak
Normal file
17
tests/test_functionOperator.krak
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user