More work on ADTs
This commit is contained in:
@@ -41,6 +41,7 @@ class Type {
|
|||||||
void decreaseIndirection();
|
void decreaseIndirection();
|
||||||
void modifyIndirection(int mod);
|
void modifyIndirection(int mod);
|
||||||
Type withIncreasedIndirection();
|
Type withIncreasedIndirection();
|
||||||
|
Type *withIncreasedIndirectionPtr();
|
||||||
Type withDecreasedIndirection();
|
Type withDecreasedIndirection();
|
||||||
|
|
||||||
Type* withoutReference();
|
Type* withoutReference();
|
||||||
|
|||||||
@@ -197,6 +197,15 @@ void ASTTransformation::secondPass(NodeTree<ASTData>* ast, NodeTree<Symbol>* par
|
|||||||
std::cout << "there are " << getNodes("adt_option", i).size() << " adt_options" << std::endl;
|
std::cout << "there are " << getNodes("adt_option", i).size() << " adt_options" << std::endl;
|
||||||
std::string name = concatSymbolTree(getNode("identifier", i));
|
std::string name = concatSymbolTree(getNode("identifier", i));
|
||||||
NodeTree<ASTData>* adtDef = ast->getDataRef()->scope[name][0]; //No overloaded types (besides uninstantiated templates, which can have multiple versions based on types or specilizations)
|
NodeTree<ASTData>* adtDef = ast->getDataRef()->scope[name][0]; //No overloaded types (besides uninstantiated templates, which can have multiple versions based on types or specilizations)
|
||||||
|
|
||||||
|
// Let's make an equality function prototype
|
||||||
|
Type *thisADTType = adtDef->getDataRef()->valueType;
|
||||||
|
NodeTree<ASTData>* equalityFunc = new NodeTree<ASTData>("function", ASTData(function, Symbol("operator==", true), new Type(std::vector<Type*>{thisADTType}, new Type(boolean))));
|
||||||
|
//NodeTree<ASTData>* equalityFunc = new NodeTree<ASTData>("function", ASTData(function, Symbol("operator==", true), new Type(std::vector<Type*>{thisADTType, thisADTType}, new Type(boolean))));
|
||||||
|
adtDef->addChild(equalityFunc);
|
||||||
|
addToScope("operator==", equalityFunc, adtDef);
|
||||||
|
addToScope("~enclosing_scope", adtDef, equalityFunc);
|
||||||
|
|
||||||
for (NodeTree<Symbol>* j : getNodes("adt_option", i)) {
|
for (NodeTree<Symbol>* j : getNodes("adt_option", i)) {
|
||||||
std::string ident_name = concatSymbolTree(getNode("identifier", j));
|
std::string ident_name = concatSymbolTree(getNode("identifier", j));
|
||||||
std::cout << "add ing " << ident_name << " to " << name << " for ADT" << std::endl;
|
std::cout << "add ing " << ident_name << " to " << name << " for ADT" << std::endl;
|
||||||
@@ -209,11 +218,11 @@ void ASTTransformation::secondPass(NodeTree<ASTData>* ast, NodeTree<Symbol>* par
|
|||||||
|
|
||||||
// also make a function prototype for a function that returns an instance of this type. If we don't contain a type, it's just the literal
|
// also make a function prototype for a function that returns an instance of this type. If we don't contain a type, it's just the literal
|
||||||
//enum_variant_function = new NodeTree<ASTData>("function", ASTData(function, Symbol("fun_"+ident_name, true), new Type(std::vector<Type*>{actual_type}, adtDef->getDataRef()->valueType)));
|
//enum_variant_function = new NodeTree<ASTData>("function", ASTData(function, Symbol("fun_"+ident_name, true), new Type(std::vector<Type*>{actual_type}, adtDef->getDataRef()->valueType)));
|
||||||
enum_variant_function = new NodeTree<ASTData>("function", ASTData(function, Symbol(ident_name, true), new Type(std::vector<Type*>{actual_type}, adtDef->getDataRef()->valueType)));
|
enum_variant_function = new NodeTree<ASTData>("function", ASTData(function, Symbol(ident_name, true), new Type(std::vector<Type*>{actual_type}, thisADTType)));
|
||||||
} else {
|
} else {
|
||||||
enum_variant_identifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(ident_name, true), adtDef->getDataRef()->valueType));
|
enum_variant_identifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(ident_name, true), adtDef->getDataRef()->valueType));
|
||||||
// now a function in both cases...
|
// now a function in both cases...
|
||||||
enum_variant_function = new NodeTree<ASTData>("function", ASTData(function, Symbol(ident_name, true), new Type(std::vector<Type*>(), adtDef->getDataRef()->valueType)));
|
enum_variant_function = new NodeTree<ASTData>("function", ASTData(function, Symbol(ident_name, true), new Type(std::vector<Type*>(), thisADTType)));
|
||||||
}
|
}
|
||||||
adtDef->addChild(enum_variant_identifier);
|
adtDef->addChild(enum_variant_identifier);
|
||||||
addToScope(ident_name, enum_variant_identifier, adtDef);
|
addToScope(ident_name, enum_variant_identifier, adtDef);
|
||||||
|
|||||||
@@ -81,7 +81,15 @@ std::string CGenerator::generateTypeStruct(NodeTree<ASTData>* from) {
|
|||||||
enumString += tabs() + generate(child, nullptr).oneString() + (data.type == adt_def ? ",\n" : "\n");
|
enumString += tabs() + generate(child, nullptr).oneString() + (data.type == adt_def ? ",\n" : "\n");
|
||||||
} else {
|
} else {
|
||||||
if (data.type == adt_def) {
|
if (data.type == adt_def) {
|
||||||
functionString += "\n" + ValueTypeToCType(child->getDataRef()->valueType->returnType, "fun_" + child->getDataRef()->symbol.getName()) + "(" +
|
std::string fun_name = child->getDataRef()->symbol.getName();
|
||||||
|
std::string first_param;
|
||||||
|
if (fun_name == "operator==") {
|
||||||
|
fun_name = "fun_" + data.symbol.getName() + "__" + CifyName(fun_name);
|
||||||
|
first_param = ValueTypeToCType(child->getDataRef()->valueType->parameterTypes[0]->withIncreasedIndirectionPtr(), "this") + ", ";
|
||||||
|
} else {
|
||||||
|
fun_name = "fun_" + fun_name;
|
||||||
|
}
|
||||||
|
functionString += "\n" + ValueTypeToCType(child->getDataRef()->valueType->returnType, fun_name) + "(" + first_param +
|
||||||
(child->getDataRef()->valueType->parameterTypes.size() ? ValueTypeToCType(child->getDataRef()->valueType->parameterTypes[0], "in") : "") + "); /*adt func*/\n";
|
(child->getDataRef()->valueType->parameterTypes.size() ? ValueTypeToCType(child->getDataRef()->valueType->parameterTypes[0], "in") : "") + "); /*adt func*/\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ std::string Type::toString(bool showTraits) {
|
|||||||
if (is_reference)
|
if (is_reference)
|
||||||
typeString = "ref " + typeString;
|
typeString = "ref " + typeString;
|
||||||
for (int i = 0; i < indirection; i++)
|
for (int i = 0; i < indirection; i++)
|
||||||
typeString = "*" + typeString;
|
typeString += "*";
|
||||||
if (indirection < 0)
|
if (indirection < 0)
|
||||||
typeString += "negative indirection: " + intToString(indirection);
|
typeString += "negative indirection: " + intToString(indirection);
|
||||||
if (traits.size() && showTraits) {
|
if (traits.size() && showTraits) {
|
||||||
@@ -240,6 +240,11 @@ Type Type::withIncreasedIndirection() {
|
|||||||
newOne->increaseIndirection();
|
newOne->increaseIndirection();
|
||||||
return *newOne;
|
return *newOne;
|
||||||
}
|
}
|
||||||
|
Type *Type::withIncreasedIndirectionPtr() {
|
||||||
|
Type *newOne = clone();
|
||||||
|
newOne->increaseIndirection();
|
||||||
|
return newOne;
|
||||||
|
}
|
||||||
Type Type::withDecreasedIndirection() {
|
Type Type::withDecreasedIndirection() {
|
||||||
Type *newOne = clone();
|
Type *newOne = clone();
|
||||||
newOne->decreaseIndirection();
|
newOne->decreaseIndirection();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import io:*
|
/*import io:**/
|
||||||
|
|
||||||
adt options {
|
adt options {
|
||||||
option0,
|
option0,
|
||||||
@@ -11,12 +11,13 @@ adt maybe_int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun handle_possibility(it: maybe_int) {
|
fun handle_possibility(it: maybe_int) {
|
||||||
if (it == maybe_int::no_int())
|
if (it == maybe_int::no_int()) {
|
||||||
println("no int")
|
/*println("no int")*/
|
||||||
|
}
|
||||||
/*if (it == maybe_int::an_int) {*/
|
/*if (it == maybe_int::an_int) {*/
|
||||||
else {
|
else {
|
||||||
print("an int: ")
|
/*print("an int: ")*/
|
||||||
println(it.an_int)
|
/*println(it.an_int)*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,10 +33,12 @@ fun can_pass(it: options): options {
|
|||||||
|
|
||||||
fun main():int {
|
fun main():int {
|
||||||
var it: options = can_pass(options::option0())
|
var it: options = can_pass(options::option0())
|
||||||
if (it == options::option0())
|
if (it == options::option0()) {
|
||||||
println("nope")
|
/*println("nope")*/
|
||||||
if (it == options::option1())
|
}
|
||||||
println("option1")
|
if (it == options::option1()) {
|
||||||
|
/*println("option1")*/
|
||||||
|
}
|
||||||
|
|
||||||
var possibility = give_maybe(false)
|
var possibility = give_maybe(false)
|
||||||
handle_possibility(possibility)
|
handle_possibility(possibility)
|
||||||
|
|||||||
Reference in New Issue
Block a user