Now allow one-line functions without codeblocks\! (espically nice for lambdas) fun a():void println(4), or fun():void println(7)
This commit is contained in:
@@ -63,8 +63,8 @@ scoped_identifier = scoped_identifier WS scope_op WS identifier | identifier ;
|
|||||||
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 statement | "fun" WS func_identifier WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS statement ;
|
||||||
lambda = "fun" WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS code_block ;
|
lambda = "fun" WS "\(" WS opt_typed_parameter_list WS "\)" WS dec_type WS statement ;
|
||||||
|
|
||||||
opt_typed_parameter_list = typed_parameter_list | ;
|
opt_typed_parameter_list = typed_parameter_list | ;
|
||||||
typed_parameter_list = typed_parameter_list WS "," WS typed_parameter | typed_parameter ;
|
typed_parameter_list = typed_parameter_list WS "," WS typed_parameter | typed_parameter ;
|
||||||
|
|||||||
@@ -474,7 +474,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
newNode->addChildren(parameters);
|
newNode->addChildren(parameters);
|
||||||
// update type with actual type
|
// update type with actual type
|
||||||
newNode->getDataRef()->valueType = new Type(mapNodesToTypePointers(parameters), newNode->getDataRef()->valueType);
|
newNode->getDataRef()->valueType = new Type(mapNodesToTypePointers(parameters), newNode->getDataRef()->valueType);
|
||||||
newNode->addChild(transform(getNode("code_block", children), scope, types, templateTypeReplacements));
|
newNode->addChild(transform(getNode("statement", children), scope, types, templateTypeReplacements));
|
||||||
std::cout << "finished function" << functionName << std::endl;
|
std::cout << "finished function" << functionName << std::endl;
|
||||||
return newNode;
|
return newNode;
|
||||||
|
|
||||||
@@ -1545,7 +1545,7 @@ NodeTree<ASTData>* ASTTransformation::findOrInstantiateFunctionTemplate(std::vec
|
|||||||
instantiatedFunction->addChildren(parameters);
|
instantiatedFunction->addChildren(parameters);
|
||||||
// update type with actual type
|
// update type with actual type
|
||||||
instantiatedFunction->getDataRef()->valueType = new Type(mapNodesToTypePointers(parameters), instantiatedFunction->getDataRef()->valueType);
|
instantiatedFunction->getDataRef()->valueType = new Type(mapNodesToTypePointers(parameters), instantiatedFunction->getDataRef()->valueType);
|
||||||
instantiatedFunction->addChild(transform(getNode("code_block", templateSyntaxTree->getChildren()), instantiatedFunction, std::vector<Type>(), newTemplateTypeReplacement));
|
instantiatedFunction->addChild(transform(getNode("statement", templateSyntaxTree->getChildren()), instantiatedFunction, std::vector<Type>(), newTemplateTypeReplacement));
|
||||||
|
|
||||||
std::cout << "Fully Instantiated function " << functionName << " to " << fullyInstantiatedName << std::endl;
|
std::cout << "Fully Instantiated function " << functionName << " to " << fullyInstantiatedName << std::endl;
|
||||||
return instantiatedFunction;
|
return instantiatedFunction;
|
||||||
|
|||||||
@@ -301,8 +301,9 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
// this is for using functions as values
|
// this is for using functions as values
|
||||||
if (justFuncName)
|
if (justFuncName)
|
||||||
return ((data.symbol.getName() == "main") ? "" : scopePrefix(from)) + CifyName(data.symbol.getName() + nameDecoration);
|
return ((data.symbol.getName() == "main") ? "" : scopePrefix(from)) + CifyName(data.symbol.getName() + nameDecoration);
|
||||||
|
// Note that we always wrap out child in {}, as we now allow one statement functions without a codeblock
|
||||||
output += "\n" + ValueTypeToCType(data.valueType->returnType, ((data.symbol.getName() == "main") ? "" : scopePrefix(from)) +
|
output += "\n" + ValueTypeToCType(data.valueType->returnType, ((data.symbol.getName() == "main") ? "" : scopePrefix(from)) +
|
||||||
CifyName(data.symbol.getName() + nameDecoration)) + "(" + parameters + ")\n" + generate(children[children.size()-1], enclosingObject, justFuncName);
|
CifyName(data.symbol.getName() + nameDecoration)) + "(" + parameters + ") {\n" + generate(children[children.size()-1], enclosingObject, justFuncName) + "}\n";
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
case code_block:
|
case code_block:
|
||||||
@@ -441,7 +442,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
std::string name = children[0]->getDataRef()->symbol.getName();
|
std::string name = children[0]->getDataRef()->symbol.getName();
|
||||||
ASTType funcType = children[0]->getDataRef()->type;
|
ASTType funcType = children[0]->getDataRef()->type;
|
||||||
std::cout << "Doing function: " << name << std::endl;
|
std::cout << "Doing function: " << name << std::endl;
|
||||||
//Test for specail functions only if what we're testing is, indeed, the definition, not a function call that returns a callable function pointer
|
//Test for special functions only if what we're testing is, indeed, the definition, not a function call that returns a callable function pointer
|
||||||
if (funcType == function) {
|
if (funcType == function) {
|
||||||
if (name == "++" || name == "--")
|
if (name == "++" || name == "--")
|
||||||
return generate(children[1], enclosingObject, true) + name;
|
return generate(children[1], enclosingObject, true) + name;
|
||||||
@@ -553,7 +554,8 @@ std::string CGenerator::generateObjectMethod(NodeTree<ASTData>* enclosingObject,
|
|||||||
std::string functionSignature = "\n" + ValueTypeToCType(data.valueType->returnType, scopePrefix(from) + CifyName(enclosingObject->getDataRef()->symbol.getName()) +"__"
|
std::string functionSignature = "\n" + ValueTypeToCType(data.valueType->returnType, scopePrefix(from) + CifyName(enclosingObject->getDataRef()->symbol.getName()) +"__"
|
||||||
+ CifyName(data.symbol.getName()) + nameDecoration) + "(" + ValueTypeToCType(&enclosingObjectType, "this") + parameters + ")";
|
+ CifyName(data.symbol.getName()) + nameDecoration) + "(" + ValueTypeToCType(&enclosingObjectType, "this") + parameters + ")";
|
||||||
*functionPrototype += functionSignature + ";\n";
|
*functionPrototype += functionSignature + ";\n";
|
||||||
return functionSignature + "\n" + generate(children[children.size()-1], enclosingObject); //Pass in the object so we can properly handle access to member stuff
|
// Note that we always wrap out child in {}, as we now allow one statement functions without a codeblock
|
||||||
|
return functionSignature + " {\n" + generate(children[children.size()-1], enclosingObject) + "}\n"; //Pass in the object so we can properly handle access to member stuff
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
6
tests/test_oneStatementFunction.expected_results
Normal file
6
tests/test_oneStatementFunction.expected_results
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
7
|
||||||
|
8
|
||||||
|
oh yeah
|
||||||
|
better?
|
||||||
|
method
|
||||||
|
templated method
|
||||||
24
tests/test_oneStatementFunction.krak
Normal file
24
tests/test_oneStatementFunction.krak
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import io:*
|
||||||
|
|
||||||
|
fun oneLine1():void println(7)
|
||||||
|
fun oneLine2():int return 8
|
||||||
|
fun oneLineID<T>(a:T):T return a
|
||||||
|
|
||||||
|
obj methods {
|
||||||
|
fun m1():void println("method")
|
||||||
|
fun m2<T>(a:T):T return a
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main():int {
|
||||||
|
oneLine1()
|
||||||
|
println(oneLine2())
|
||||||
|
println(oneLineID("oh yeah"))
|
||||||
|
var lambda = fun():void println("better?");
|
||||||
|
lambda()
|
||||||
|
|
||||||
|
var o: methods
|
||||||
|
o.m1()
|
||||||
|
println(o.m2("templated method"))
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user