Added generator-dependent compilation and simple passthrough that allows us to have non-cheated emitted, printing c-code for the first time! (no typechecking or anything yet, but we'll get there). It's also still rough.

This commit is contained in:
Nathan Braswell
2013-12-22 01:34:59 -06:00
parent 6ad406e42d
commit 935cc6f968
9 changed files with 55 additions and 33 deletions

View File

@@ -24,7 +24,9 @@ std::string ASTData::toString() {
}
ValueType ASTData::strToType(std::string type) {
if (type == "bool")
if (type == "void")
return void_type;
else if (type == "bool")
return boolean;
else if (type == "int")
return integer;
@@ -41,22 +43,18 @@ std::string ASTData::ValueTypeToString(ValueType type) {
switch (type) {
case none:
return "none";
break;
case void_type:
return "void";
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 "string";
break;
default:
return "unknown_ValueType";
}
@@ -96,6 +94,10 @@ std::string ASTData::ASTTypeToString(ASTType type) {
return "assignment_statement";
case declaration_statement:
return "declaration_statement";
case if_comp:
return "if_comp";
case simple_passthrough:
return "simple_passthrough";
case function_call:
return "function_call";
case value:

View File

@@ -105,6 +105,10 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
newNode->addChild(newIdentifier);
skipChildren.insert(0); //These, the type and the identifier, have been taken care of.
skipChildren.insert(1);
} else if (name == "if_comp") {
newNode = new NodeTree<ASTData>(name, ASTData(if_comp));
} else if (name == "simple_passthrough") {
newNode = new NodeTree<ASTData>(name, ASTData(simple_passthrough));
} else if (name == "function_call") {
//children[0] is scope
std::string functionCallName = concatSymbolTree(children[1]);
@@ -122,7 +126,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), floating));
} else if (name == "double") {
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), double_percision));
} else if (name == "string") {
} else if (name == "string" || name == "triple_quoted_string") {
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), char_string));
} else {
return new NodeTree<ASTData>();

View File

@@ -1,6 +1,6 @@
#include "CGenerator.h"
CGenerator::CGenerator() {
CGenerator::CGenerator() : generatorString("__C__") {
tabLevel = 0;
}
CGenerator::~CGenerator() {
@@ -26,7 +26,8 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
//Do nothing
break;
case import:
return "#include <" + data.symbol.getName() + ">\n";
return "/* would import \"" + data.symbol.getName() + "\" but....*/\n";
//return "#include <" + data.symbol.getName() + ">\n";
case identifier:
return data.symbol.getName();
case function:
@@ -34,7 +35,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
for (int i = 0; i < children.size()-1; i++) {
if (i > 0)
output += ", ";
output += ASTData::ValueTypeToString(children[i]->getData().valueType) + " " + generate(children[i]);
output += ValueTypeToCType(children[i]->getData().valueType) + " " + generate(children[i]);
}
output+= ")\n" + generate(children[children.size()-1]);
return output;
@@ -65,11 +66,20 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
output += "for (" + strSlice(generate(children[0]),0,-2) + generate(children[1]) + ";" + strSlice(generate(children[2]),0,-3) + ")\n\t" + generate(children[3]);
return output;
case return_statement:
return "return " + generate(children[0]);
if (children.size())
return "return " + generate(children[0]);
else
return "return";
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 if_comp:
if (generate(children[0]) == generatorString)
return generate(children[1]);
return "";
case simple_passthrough:
return strSlice(generate(children[0]), 3, -4);
case function_call:
{
//Handle operators specially for now. Will later replace with
@@ -104,22 +114,20 @@ std::string CGenerator::ValueTypeToCType(ValueType type) {
switch (type) {
case none:
return "none";
break;
case void_type:
return "void";
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";
}

View File

@@ -79,7 +79,7 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
std::cout << "Frontier " << i << " is empty." << std::endl;
std::cout << "Failed on " << input[i].toString() << std::endl;
std::cout << "Nearby is:" << std::endl;
int range = 5;
const int range = 10;
for (int j = (i-range >= 0 ? i-range : 0); j < (i+range < input.size() ? i+range : input.size()); j++)
if (j == i)
std::cout << "||*||*||" << input[j].toString() << "||*||*|| ";

View File

@@ -8,7 +8,7 @@ std::string intToString(int theInt) {
std::string replaceExEscape(std::string first, std::string search, std::string replace) {
size_t pos = 0;
while (pos < first.size()-search.size()) {
while (pos <= first.size()-search.size()) {
pos = first.find(search, pos);
if (pos == std::string::npos)
break;