A kinda ugly/hacky addition, but overloading () as operator() works now!

This commit is contained in:
Nathan Braswell
2015-05-16 00:09:50 -04:00
parent 795f8715ff
commit aafb52d108
4 changed files with 38 additions and 4 deletions

View File

@@ -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
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 ;

View File

@@ -376,8 +376,24 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* 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<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;
}
} else {
@@ -536,7 +552,6 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* 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

View File

@@ -0,0 +1,2 @@
7
hi

View 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;
}