More work on ADTs
This commit is contained in:
@@ -41,6 +41,7 @@ class Type {
|
||||
void decreaseIndirection();
|
||||
void modifyIndirection(int mod);
|
||||
Type withIncreasedIndirection();
|
||||
Type *withIncreasedIndirectionPtr();
|
||||
Type withDecreasedIndirection();
|
||||
|
||||
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::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)
|
||||
|
||||
// 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)) {
|
||||
std::string ident_name = concatSymbolTree(getNode("identifier", j));
|
||||
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
|
||||
//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 {
|
||||
enum_variant_identifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(ident_name, true), adtDef->getDataRef()->valueType));
|
||||
// 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);
|
||||
addToScope(ident_name, enum_variant_identifier, adtDef);
|
||||
|
||||
1368
src/CGenerator.cpp
1368
src/CGenerator.cpp
File diff suppressed because it is too large
Load Diff
@@ -199,7 +199,7 @@ std::string Type::toString(bool showTraits) {
|
||||
if (is_reference)
|
||||
typeString = "ref " + typeString;
|
||||
for (int i = 0; i < indirection; i++)
|
||||
typeString = "*" + typeString;
|
||||
typeString += "*";
|
||||
if (indirection < 0)
|
||||
typeString += "negative indirection: " + intToString(indirection);
|
||||
if (traits.size() && showTraits) {
|
||||
@@ -240,6 +240,11 @@ Type Type::withIncreasedIndirection() {
|
||||
newOne->increaseIndirection();
|
||||
return *newOne;
|
||||
}
|
||||
Type *Type::withIncreasedIndirectionPtr() {
|
||||
Type *newOne = clone();
|
||||
newOne->increaseIndirection();
|
||||
return newOne;
|
||||
}
|
||||
Type Type::withDecreasedIndirection() {
|
||||
Type *newOne = clone();
|
||||
newOne->decreaseIndirection();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import io:*
|
||||
/*import io:**/
|
||||
|
||||
adt options {
|
||||
option0,
|
||||
@@ -11,12 +11,13 @@ adt maybe_int {
|
||||
}
|
||||
|
||||
fun handle_possibility(it: maybe_int) {
|
||||
if (it == maybe_int::no_int())
|
||||
println("no int")
|
||||
if (it == maybe_int::no_int()) {
|
||||
/*println("no int")*/
|
||||
}
|
||||
/*if (it == maybe_int::an_int) {*/
|
||||
else {
|
||||
print("an int: ")
|
||||
println(it.an_int)
|
||||
/*print("an int: ")*/
|
||||
/*println(it.an_int)*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,10 +33,12 @@ fun can_pass(it: options): options {
|
||||
|
||||
fun main():int {
|
||||
var it: options = can_pass(options::option0())
|
||||
if (it == options::option0())
|
||||
println("nope")
|
||||
if (it == options::option1())
|
||||
println("option1")
|
||||
if (it == options::option0()) {
|
||||
/*println("nope")*/
|
||||
}
|
||||
if (it == options::option1()) {
|
||||
/*println("option1")*/
|
||||
}
|
||||
|
||||
var possibility = give_maybe(false)
|
||||
handle_possibility(possibility)
|
||||
|
||||
Reference in New Issue
Block a user