faster deserilitation, super basic enums
This commit is contained in:
@@ -68,7 +68,7 @@ std::vector<NodeTree<Symbol>*> ASTTransformation::getNodes(std::string lookup, s
|
||||
return results;
|
||||
}
|
||||
|
||||
//First pass defines all type_defs (objects and ailises), and if_comp/simple_passthrough
|
||||
//First pass defines all type_defs (objects and ailises), ADTs, and if_comp/simple_passthrough
|
||||
NodeTree<ASTData>* ASTTransformation::firstPass(std::string fileName, NodeTree<Symbol>* parseTree) {
|
||||
NodeTree<ASTData>* translationUnit = new NodeTree<ASTData>("translation_unit", ASTData(translation_unit, Symbol(fileName, false)));
|
||||
std::vector<NodeTree<Symbol>*> children = parseTree->getChildren();
|
||||
@@ -97,6 +97,12 @@ NodeTree<ASTData>* ASTTransformation::firstPass(std::string fileName, NodeTree<S
|
||||
firstDec->getDataRef()->valueType = new Type(firstDec);
|
||||
}
|
||||
|
||||
} else if (i->getDataRef()->getName() == "adt_def") {
|
||||
std::string name = concatSymbolTree(i->getChildren()[0]);
|
||||
NodeTree<ASTData>* adt_dec = addToScope("~enclosing_scope", translationUnit, new NodeTree<ASTData>("adt_def", ASTData(adt_def, Symbol(name, true, name))));
|
||||
addToScope(name, adt_dec, translationUnit);
|
||||
translationUnit->addChild(adt_dec);
|
||||
adt_dec->getDataRef()->valueType = new Type(adt_dec);
|
||||
} else if (i->getDataRef()->getName() == "if_comp") {
|
||||
std::cout << "IF COMP" << std::endl;
|
||||
NodeTree<ASTData>* newNode = addToScope("~enclosing_scope", translationUnit, new NodeTree<ASTData>(i->getDataRef()->getName(), ASTData(if_comp)));
|
||||
@@ -158,7 +164,7 @@ std::set<std::string> ASTTransformation::parseTraits(NodeTree<Symbol>* traitsNod
|
||||
return traits;
|
||||
}
|
||||
|
||||
//Second pass defines data inside objects, outside declaration statements, and function prototypes (since we have type_defs now)
|
||||
//Second pass defines data inside objects + ADTs, outside declaration statements, and function prototypes (since we have type_defs+ADTs now)
|
||||
void ASTTransformation::secondPass(NodeTree<ASTData>* ast, NodeTree<Symbol>* parseTree) {
|
||||
topScope = ast; //Top scope is maintained for templates, which need to add themselves to the top scope from where ever they are instantiated
|
||||
std::vector<NodeTree<Symbol>*> children = parseTree->getChildren();
|
||||
@@ -186,6 +192,19 @@ void ASTTransformation::secondPass(NodeTree<ASTData>* ast, NodeTree<Symbol>* par
|
||||
}
|
||||
//Do the inside of classes here
|
||||
secondPassDoClassInsides(typeDef, typedefChildren, std::map<std::string, Type*>());
|
||||
} else if (i->getDataRef()->getName() == "adt_def") {
|
||||
std::string name = concatSymbolTree(i->getChildren()[0]);
|
||||
NodeTree<ASTData>* adtDef = ast->getDataRef()->scope[name][0]; //No overloaded types (besides uninstantiated templates, which can have multiple versions based on types or specilizations)
|
||||
for (NodeTree<Symbol>* j : i->getChildren()) {
|
||||
if (j->getDataRef()->getName() == "identifier") {
|
||||
std::string ident_name = concatSymbolTree(j);
|
||||
std::cout << "add ing " << ident_name << " to " << name << " for ADT" << std::endl;
|
||||
NodeTree<ASTData>* enum_variant_identifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(ident_name, true), adtDef->getDataRef()->valueType));
|
||||
adtDef->addChild(enum_variant_identifier);
|
||||
addToScope(ident_name, enum_variant_identifier, adtDef);
|
||||
addToScope("~enclosing_scope", adtDef, enum_variant_identifier);
|
||||
}
|
||||
}
|
||||
} else if (i->getDataRef()->getName() == "function") {
|
||||
//Do prototypes of functions
|
||||
ast->addChild(secondPassFunction(i, ast, std::map<std::string, Type*>()));
|
||||
@@ -280,6 +299,8 @@ void ASTTransformation::thirdPass(NodeTree<ASTData>* ast, NodeTree<Symbol>* pars
|
||||
thirdPassFunction(j, searchScopeForFunctionDef(typeDef, j, std::map<std::string, Type*>()), std::map<std::string, Type*>()); //do member method
|
||||
}
|
||||
}
|
||||
} else if (i->getDataRef()->getName() == "adt_def") {
|
||||
// nothing to do here yet, but eventually we will set up our internal objs, etc
|
||||
} else if (i->getDataRef()->getName() == "function") {
|
||||
//Do prototypes of functions
|
||||
if (i->getChildren()[1]->getData().getName() == "template_dec")
|
||||
@@ -1009,7 +1030,7 @@ NodeTree<ASTData>* ASTTransformation::functionLookup(NodeTree<ASTData>* scope, s
|
||||
if (possibleMatches.size()) {
|
||||
for (auto i : possibleMatches) {
|
||||
//We're not looking for types
|
||||
if (i->getDataRef()->type == type_def)
|
||||
if (i->getDataRef()->type == type_def || i->getDataRef()->type == adt_def)
|
||||
continue;
|
||||
Type* functionType = i->getDataRef()->valueType;
|
||||
|
||||
|
||||
@@ -51,15 +51,20 @@ std::string CGenerator::getID() {
|
||||
return intToString(id++);
|
||||
}
|
||||
|
||||
std::string CGenerator::generateClassStruct(NodeTree<ASTData>* from) {
|
||||
std::string CGenerator::generateTypeStruct(NodeTree<ASTData>* from) {
|
||||
auto data = from->getData();
|
||||
auto children = from->getChildren();
|
||||
std::string objectString = "struct __struct_dummy_" + scopePrefix(from) + CifyName(data.symbol.getName()) + "__ {\n";
|
||||
std::string objectString;
|
||||
if (data.type == type_def)
|
||||
objectString = "struct __struct_dummy_";
|
||||
else if (data.type == adt_def)
|
||||
objectString = "enum __adt_dummy_";
|
||||
objectString += scopePrefix(from) + CifyName(data.symbol.getName()) + "__ {\n";
|
||||
tabLevel++;
|
||||
for (int i = 0; i < children.size(); i++) {
|
||||
for (int i = (data.type == adt_def ? 1 : 0); i < children.size(); i++) {
|
||||
//std::cout << children[i]->getName() << std::endl;
|
||||
if (children[i]->getName() != "function")
|
||||
objectString += tabs() + generate(children[i], nullptr).oneString() + "\n";
|
||||
objectString += tabs() + generate(children[i], nullptr).oneString() + (data.type == adt_def ? ",\n" : "\n");
|
||||
}
|
||||
tabLevel--;
|
||||
objectString += "};";
|
||||
@@ -161,12 +166,15 @@ std::pair<std::string, std::string> CGenerator::generateTranslationUnit(std::str
|
||||
typedefPoset.addRelationship(children[i], decType->typeDefinition); // Add a dependency
|
||||
}
|
||||
}
|
||||
} else if (children[i]->getDataRef()->type == adt_def) {
|
||||
//
|
||||
typedefPoset.addVertex(children[i]); // We add this definition by itself just in case there are no dependencies.
|
||||
}
|
||||
}
|
||||
}
|
||||
//Now generate the typedef's in the correct, topological order
|
||||
for (NodeTree<ASTData>* i : typedefPoset.getTopoSort())
|
||||
classStructs += generateClassStruct(i) + "\n";
|
||||
classStructs += generateTypeStruct(i) + "\n";
|
||||
|
||||
// Declare everything in translation unit scope here (now for ALL translation units). (allows stuff from other files, automatic forward declarations)
|
||||
// Also, everything in all of the import's scopes
|
||||
@@ -253,6 +261,24 @@ std::pair<std::string, std::string> CGenerator::generateTranslationUnit(std::str
|
||||
functionDefinitions += objectFunctionDefinitions + "/* Done with " + declarationData.symbol.getName() + " */\n";
|
||||
}
|
||||
break;
|
||||
case adt_def:
|
||||
{
|
||||
//type
|
||||
plainTypedefs += "/* adt " + declarationData.symbol.getName() + " */\n";
|
||||
//plainTypedefs += "typedef struct __adt_dummy_" +
|
||||
plainTypedefs += "typedef enum __adt_dummy_" +
|
||||
scopePrefix(declaration) + CifyName(declarationData.symbol.getName()) + "__ " +
|
||||
scopePrefix(declaration) + CifyName(declarationData.symbol.getName()) + ";\n";
|
||||
// We use a seperate string for this because we only include it if this is the file we're defined in
|
||||
std::string enumString = "/* Enum Definition for " + declarationData.symbol.getName() + " */\n";
|
||||
// skip the name of the thing
|
||||
for (int j = 1; j < decChildren.size(); j++) {
|
||||
std::cout << decChildren[j]->getName() << std::endl;
|
||||
if (decChildren[j]->getName() == "identifier") //If object method and not template
|
||||
enumString += "an_option \n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
//std::cout << "Declaration? named " << declaration->getName() << " of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl;
|
||||
cOutput += "/*unknown declaration named " + declaration->getName() + "*/\n";
|
||||
|
||||
@@ -38,6 +38,7 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths, std:
|
||||
removeSymbols.push_back(Symbol("comp_simple_passthrough", true));
|
||||
removeSymbols.push_back(Symbol("def_nonterm", false));
|
||||
removeSymbols.push_back(Symbol("obj_nonterm", false));
|
||||
removeSymbols.push_back(Symbol("adt_nonterm", false));
|
||||
removeSymbols.push_back(Symbol("template", true));
|
||||
removeSymbols.push_back(Symbol("\\|", true));
|
||||
//collapseSymbols.push_back(Symbol("scoped_identifier", false));
|
||||
@@ -45,8 +46,7 @@ Importer::Importer(Parser* parserIn, std::vector<std::string> includePaths, std:
|
||||
collapseSymbols.push_back(Symbol("param_assign_list", false));
|
||||
collapseSymbols.push_back(Symbol("opt_typed_parameter_list", false));
|
||||
collapseSymbols.push_back(Symbol("opt_parameter_list", false));
|
||||
collapseSymbols.push_back(Symbol("opt_import_list", false));
|
||||
collapseSymbols.push_back(Symbol("import_list", false));
|
||||
collapseSymbols.push_back(Symbol("identifier_list", false));
|
||||
collapseSymbols.push_back(Symbol("statement_list", false));
|
||||
collapseSymbols.push_back(Symbol("parameter_list", false));
|
||||
collapseSymbols.push_back(Symbol("typed_parameter_list", false));
|
||||
|
||||
Reference in New Issue
Block a user