Lot's of work on a CGenerator, AST and language improvements
This commit is contained in:
@@ -43,19 +43,19 @@ std::string ASTData::ValueTypeToString(ValueType type) {
|
||||
return "none";
|
||||
break;
|
||||
case boolean:
|
||||
return "boolean";
|
||||
return "bool";
|
||||
break;
|
||||
case integer:
|
||||
return "integer";
|
||||
return "int";
|
||||
break;
|
||||
case floating:
|
||||
return "floating";
|
||||
return "float";
|
||||
break;
|
||||
case double_percision:
|
||||
return "double_percision";
|
||||
return "double";
|
||||
break;
|
||||
case char_string:
|
||||
return "char_string";
|
||||
return "string";
|
||||
break;
|
||||
default:
|
||||
return "unknown_ValueType";
|
||||
@@ -103,6 +103,9 @@ std::string ASTData::ASTTypeToString(ASTType type) {
|
||||
case assignment_statement:
|
||||
return "assignment_statement";
|
||||
break;
|
||||
case declaration_statement:
|
||||
return "declaration_statement";
|
||||
break;
|
||||
case function_call:
|
||||
return "function_call";
|
||||
break;
|
||||
|
||||
@@ -31,7 +31,9 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
||||
} else if (name == "code_block") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(code_block));
|
||||
} else if (name == "typed_parameter") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(typed_parameter));
|
||||
newNode = transform(children[1]); //Transform to get the identifier
|
||||
newNode->getDataRef()->valueType = ASTData::strToType(concatSymbolTree(children[0])); //Get the type (left child) and set our new identifer to be that type
|
||||
return newNode;
|
||||
} else if (name == "expression") {
|
||||
//If this is an actual part of an expression, not just a premoted term
|
||||
if (children.size() > 1) {
|
||||
@@ -62,6 +64,13 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(return_statement));
|
||||
} else if (name == "assignment_statement") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(assignment_statement));
|
||||
} else if (name == "declaration_statement") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(declaration_statement));
|
||||
NodeTree<ASTData>* newIdentifier = transform(children[1]); //Transform the identifier
|
||||
newIdentifier->getDataRef()->valueType = ASTData::strToType(concatSymbolTree(children[0]));//set the type of the identifier
|
||||
newNode->addChild(newIdentifier);
|
||||
skipChildren.insert(0); //These, the type and the identifier, have been taken care of.
|
||||
skipChildren.insert(1);
|
||||
} else if (name == "function_call") {
|
||||
//children[0] is scope
|
||||
std::string functionCallName = concatSymbolTree(children[1]);
|
||||
@@ -89,7 +98,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
||||
for (int i = 0; i < children.size(); i++) {
|
||||
if (skipChildren.find(i) == skipChildren.end()) {
|
||||
NodeTree<ASTData>* transChild = transform(children[i]);
|
||||
if (transChild->getData().type)
|
||||
if (transChild->getData().type) //Only add the children that have a real ASTData::ASTType, that is, legit ASTData.
|
||||
newNode->addChild(transChild);
|
||||
else
|
||||
delete transChild;
|
||||
|
||||
125
src/CGenerator.cpp
Normal file
125
src/CGenerator.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "CGenerator.h"
|
||||
|
||||
CGenerator::CGenerator() {
|
||||
tabLevel = 0;
|
||||
}
|
||||
CGenerator::~CGenerator() {
|
||||
|
||||
}
|
||||
|
||||
std::string CGenerator::tabs() {
|
||||
std::string returnTabs;
|
||||
for (int i = 0; i < tabLevel; i++)
|
||||
returnTabs += "\t";
|
||||
return returnTabs;
|
||||
}
|
||||
|
||||
std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
||||
ASTData data = from->getData();
|
||||
std::vector<NodeTree<ASTData>*> children = from->getChildren();
|
||||
std::string output = "";
|
||||
switch (data.type) {
|
||||
case translation_unit:
|
||||
//Do nothing
|
||||
break;
|
||||
case interpreter_directive:
|
||||
//Do nothing
|
||||
break;
|
||||
case import:
|
||||
return "#include \"" + data.symbol.getName() + "\"\n";
|
||||
break;
|
||||
case identifier:
|
||||
return data.symbol.getName();
|
||||
break;
|
||||
case function:
|
||||
output += "\n" + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + "(";
|
||||
for (int i = 0; i < children.size()-1; i++) {
|
||||
if (i > 0)
|
||||
output += ", ";
|
||||
output += ASTData::ValueTypeToString(children[i]->getData().valueType) + " " + generate(children[i]);
|
||||
}
|
||||
output+= ")\n" + generate(children[children.size()-1]);
|
||||
return output;
|
||||
break;
|
||||
case code_block:
|
||||
output += tabs() + "{\n";
|
||||
tabLevel++;
|
||||
for (int i = 0; i < children.size(); i++)
|
||||
output += generate(children[i]);
|
||||
tabLevel--;
|
||||
output += tabs() + "}";
|
||||
return output;
|
||||
break;
|
||||
case expression:
|
||||
output += " " + data.symbol.getName() + ", ";
|
||||
break;
|
||||
case boolean_expression:
|
||||
output += " " + data.symbol.getName() + " ";
|
||||
break;
|
||||
case statement:
|
||||
return tabs() + generate(children[0]) + ";\n";
|
||||
break;
|
||||
case if_statement:
|
||||
output += "if (" + generate(children[0]) + ") \n" + generate(children[1]);
|
||||
if (children.size() > 2)
|
||||
output += " else " + generate(children[2]);
|
||||
return output;
|
||||
break;
|
||||
case return_statement:
|
||||
return "return " + generate(children[0]);
|
||||
case assignment_statement:
|
||||
return generate(children[0]) + " = " + generate(children[1]);
|
||||
case declaration_statement:
|
||||
return ASTData::ValueTypeToString(children[0]->getData().valueType) + " " + generate(children[0]) + " = " + generate(children[1]);
|
||||
case function_call:
|
||||
{
|
||||
//Handle operators specially for now. Will later replace with
|
||||
//Inlined functions in the standard library
|
||||
std::string name = data.symbol.getName();
|
||||
if (name == "+" || name == "-" || name == "*" || name == "/") {
|
||||
return "((" + generate(children[0]) + ")" + name + "(" + generate(children[1]) + "))";
|
||||
}
|
||||
output += data.symbol.getName() + "(";
|
||||
for (int i = 0; i < children.size(); i++)
|
||||
if (i < children.size()-1)
|
||||
output += generate(children[i]) + ", ";
|
||||
else output += generate(children[i]);
|
||||
output += ") ";
|
||||
return output;
|
||||
}
|
||||
case value:
|
||||
return data.symbol.getName();
|
||||
|
||||
default:
|
||||
std::cout << "Nothing!" << std::endl;
|
||||
}
|
||||
for (int i = 0; i < children.size(); i++)
|
||||
output += generate(children[i]);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string CGenerator::ValueTypeToCType(ValueType type) {
|
||||
switch (type) {
|
||||
case none:
|
||||
return "none";
|
||||
break;
|
||||
case boolean:
|
||||
return "bool";
|
||||
break;
|
||||
case integer:
|
||||
return "int";
|
||||
break;
|
||||
case floating:
|
||||
return "float";
|
||||
break;
|
||||
case double_percision:
|
||||
return "double";
|
||||
break;
|
||||
case char_string:
|
||||
return "char*";
|
||||
break;
|
||||
default:
|
||||
return "unknown_ValueType";
|
||||
}
|
||||
}
|
||||
@@ -45,9 +45,9 @@ Symbol Lexer::next() {
|
||||
//std::cout << "Current at is \"" << input.substr(currentPosition) << "\" currentPos is " << currentPosition <<std::endl;
|
||||
return Symbol(longestRegEx->getPattern(), true, eatenString);
|
||||
} else {
|
||||
//std::cout << "Found no applicable regex" << std::endl;
|
||||
//std::cout << "Remaining is ||" << input.substr(currentPosition) << "||" << std::endl;
|
||||
return Symbol();
|
||||
std::cout << "Found no applicable regex" << std::endl;
|
||||
std::cout << "Remaining is ||" << input.substr(currentPosition) << "||" << std::endl;
|
||||
return Symbol("$INVALID$", true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Parser.h"
|
||||
|
||||
Parser::Parser() : EOFSymbol("$EOF$", true), nullSymbol("$NULL$", true){
|
||||
Parser::Parser() : EOFSymbol("$EOF$", true), nullSymbol("$NULL$", true), invalidSymbol("$INVALID$", true){
|
||||
table.setSymbols(EOFSymbol, nullSymbol);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,11 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
|
||||
input.push_back(currentToken);
|
||||
while (currentToken != EOFSymbol) {
|
||||
currentToken = lexer.next();
|
||||
//std::cout << "CurrentToken is " << currentToken.toString() << std::endl;
|
||||
if (currentToken == invalidSymbol) {
|
||||
std::cout << "Invalid Symbol!" << std::endl;
|
||||
throw "Invalid Symbol, cannot lex";
|
||||
}
|
||||
input.push_back(currentToken);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user