Added support for destructors! This is done by making a void, parameterless member method called destruct which is called when a stack object falls out of scope, if it exists. This is implemented by the generator, in this case CGenerator.
This commit is contained in:
@@ -61,7 +61,7 @@ for_loop = "for" WS "\(" WS statement WS boolean_expression WS ";" WS statement
|
||||
|
||||
return_statement = "return" | "return" WS boolean_expression ;
|
||||
|
||||
code_block = "{" WS statement_list WS "}" ;
|
||||
code_block = "{" WS statement_list WS "}" | "{" WS "}" ;
|
||||
|
||||
statement_list = statement_list WS statement | statement ;
|
||||
statement = if_statement | while_loop | for_loop | return_statement WS ";" | boolean_expression WS ";" | assignment_statement WS ";" | declaration_statement WS ";" | code_block | if_comp | simple_passthrough ;
|
||||
|
||||
@@ -180,18 +180,32 @@ std::string CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
||||
return output;
|
||||
}
|
||||
case code_block:
|
||||
{
|
||||
output += "{\n";
|
||||
tabLevel++;
|
||||
std::string destructorString = "";
|
||||
tabLevel++;
|
||||
for (int i = 0; i < children.size(); i++) {
|
||||
//std::cout << "Line " << i << std::endl;
|
||||
std::string line = generate(children[i], enclosingObject);
|
||||
//std::cout << line << std::endl;
|
||||
output += line;
|
||||
}
|
||||
tabLevel--;
|
||||
if (children[i]->getChildren().size() && children[i]->getChildren()[0]->getDataRef()->type == declaration_statement) {
|
||||
NodeTree<ASTData> *identifier = children[i]->getChildren()[0]->getChildren()[0];
|
||||
NodeTree<ASTData> *typeDefinition = identifier->getDataRef()->valueType->typeDefinition;
|
||||
if (!typeDefinition)
|
||||
continue;
|
||||
if (typeDefinition->getDataRef()->scope.find("destruct") == typeDefinition->getDataRef()->scope.end())
|
||||
continue;
|
||||
destructorString += tabs() + CifyName(typeDefinition->getDataRef()->symbol.getName())
|
||||
+ "__" + "destruct" + "(&" + generate(identifier, enclosingObject) + ");\n";//Call the destructor
|
||||
}
|
||||
}
|
||||
output += destructorString;
|
||||
tabLevel--;
|
||||
output += tabs() + "}";
|
||||
return output;
|
||||
case expression:
|
||||
}
|
||||
case expression:
|
||||
output += " " + data.symbol.getName() + ", ";
|
||||
case boolean_expression:
|
||||
output += " " + data.symbol.getName() + " ";
|
||||
|
||||
1
tests/destructorTest.expected_results
Normal file
1
tests/destructorTest.expected_results
Normal file
@@ -0,0 +1 @@
|
||||
Hello Destructors!
|
||||
28
tests/destructorTest.krak
Normal file
28
tests/destructorTest.krak
Normal file
@@ -0,0 +1,28 @@
|
||||
import io;
|
||||
|
||||
typedef DestructorPrint {
|
||||
char* myStr;
|
||||
DestructorPrint* construct(char* str) {
|
||||
myStr = str;
|
||||
return this;
|
||||
}
|
||||
void destruct() {
|
||||
println(myStr);
|
||||
}
|
||||
};
|
||||
|
||||
typedef NoDistruction {
|
||||
int a;
|
||||
void dummyFunc() {}
|
||||
};
|
||||
|
||||
void indirPrint() {
|
||||
DestructorPrint testObj.construct("Hello Destructors!");
|
||||
NoDistruction dummy;
|
||||
}
|
||||
|
||||
int main() {
|
||||
indirPrint();
|
||||
return 0;
|
||||
}
|
||||
|
||||
1
tests/emptyBracesFunction.expected_results
Normal file
1
tests/emptyBracesFunction.expected_results
Normal file
@@ -0,0 +1 @@
|
||||
It was nothing
|
||||
9
tests/emptyBracesFunction.krak
Normal file
9
tests/emptyBracesFunction.krak
Normal file
@@ -0,0 +1,9 @@
|
||||
import io;
|
||||
|
||||
void nothing() {}
|
||||
|
||||
int main() {
|
||||
nothing();
|
||||
println("It was nothing");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user