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:
Nathan Braswell
2015-05-27 12:27:50 -04:00
parent 85834789e4
commit b71bb84070
5 changed files with 39 additions and 7 deletions

View File

@@ -474,7 +474,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
newNode->addChildren(parameters);
// update type with actual type
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;
return newNode;
@@ -1545,7 +1545,7 @@ NodeTree<ASTData>* ASTTransformation::findOrInstantiateFunctionTemplate(std::vec
instantiatedFunction->addChildren(parameters);
// update type with actual type
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;
return instantiatedFunction;

View File

@@ -301,8 +301,9 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
// this is for using functions as values
if (justFuncName)
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)) +
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;
}
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();
ASTType funcType = children[0]->getDataRef()->type;
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 (name == "++" || 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()) +"__"
+ CifyName(data.symbol.getName()) + nameDecoration) + "(" + ValueTypeToCType(&enclosingObjectType, "this") + parameters + ")";
*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
}