Save state before re-write of RegEx.

This commit is contained in:
Nathan Braswell
2014-01-07 13:14:58 -05:00
parent 82df9b1592
commit 0297f29dcd
13 changed files with 139 additions and 57 deletions

View File

@@ -5,28 +5,29 @@
#include <map> #include <map>
#include "Symbol.h" #include "Symbol.h"
//Circular dependency
class Type;
#include "Type.h" #include "Type.h"
#ifndef NULL #ifndef NULL
#define NULL 0 #define NULL 0
#endif #endif
enum ASTType {undef, translation_unit, interpreter_directive, import, identifier, enum ASTType {undef, translation_unit, interpreter_directive, import, identifier, type_def,
function, code_block, function, code_block, typed_parameter, expression, boolean_expression, statement,
typed_parameter, expression, boolean_expression, statement,
if_statement, while_loop, for_loop, return_statement, assignment_statement, declaration_statement, if_statement, while_loop, for_loop, return_statement, assignment_statement, declaration_statement,
if_comp, simple_passthrough, function_call, value}; if_comp, simple_passthrough, function_call, value};
class ASTData { class ASTData {
public: public:
ASTData(); ASTData();
ASTData(ASTType type, Type valueType = Type()); ASTData(ASTType type, Type *valueType = NULL);
ASTData(ASTType type, Symbol symbol, Type valueType = Type()); ASTData(ASTType type, Symbol symbol, Type *valueType = NULL);
~ASTData(); ~ASTData();
std::string toString(); std::string toString();
static std::string ASTTypeToString(ASTType type); static std::string ASTTypeToString(ASTType type);
ASTType type; ASTType type;
Type valueType; Type* valueType;
Symbol symbol; Symbol symbol;
std::map<std::string, NodeTree<ASTData>*> scope; std::map<std::string, NodeTree<ASTData>*> scope;
private: private:

View File

@@ -4,6 +4,7 @@
#include <set> #include <set>
#include <map> #include <map>
#include "Type.h"
#include "ASTData.h" #include "ASTData.h"
#include "NodeTransformation.h" #include "NodeTransformation.h"
#include "Importer.h" #include "Importer.h"
@@ -18,6 +19,7 @@ class ASTTransformation: public NodeTransformation<Symbol,ASTData> {
NodeTree<ASTData>* transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope); NodeTree<ASTData>* transform(NodeTree<Symbol>* from, NodeTree<ASTData>* scope);
std::string concatSymbolTree(NodeTree<Symbol>* root); std::string concatSymbolTree(NodeTree<Symbol>* root);
NodeTree<ASTData>* scopeLookup(NodeTree<ASTData>* scope, std::string lookup); NodeTree<ASTData>* scopeLookup(NodeTree<ASTData>* scope, std::string lookup);
Type* typeFromString(std::string type, NodeTree<ASTData>* scope);
private: private:
Importer * importer; Importer * importer;
}; };

View File

@@ -18,7 +18,7 @@ class CGenerator {
~CGenerator(); ~CGenerator();
void generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName); void generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName);
std::string generate(NodeTree<ASTData>* from); std::string generate(NodeTree<ASTData>* from);
static std::string ValueTypeToCType(Type type); static std::string ValueTypeToCType(Type *type);
std::string generatorString; std::string generatorString;
private: private:

View File

@@ -8,6 +8,9 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
//Circular dependency
class ASTData;
#include "ASTData.h"
#include "util.h" #include "util.h"
enum ValueType {none, void_type, boolean, integer, floating, double_percision, character }; enum ValueType {none, void_type, boolean, integer, floating, double_percision, character };
@@ -18,10 +21,13 @@ class Type {
Type(); Type();
Type(ValueType typeIn, int indirectionIn); Type(ValueType typeIn, int indirectionIn);
Type(ValueType typeIn); Type(ValueType typeIn);
Type(std::string typeIn); Type(NodeTree<ASTData>* typeDefinitionIn);
Type(NodeTree<ASTData>* typeDefinitionIn, int indirectionIn);
Type(ValueType typeIn, NodeTree<ASTData>* typeDefinitionIn, int indirectionIn);
~Type(); ~Type();
std::string toString(); std::string toString();
ValueType baseType; ValueType baseType;
NodeTree<ASTData>* typeDefinition;
int indirection; int indirection;
private: private:
}; };

View File

@@ -1,10 +1,9 @@
Goal = translation_unit ; Goal = translation_unit ;
translation_unit = interpreter_directive WS unorderd_list_part WS ; translation_unit = interpreter_directive WS unorderd_list_part WS ;
unorderd_list_part = import_list WS unorderd_list_part | function WS unorderd_list_part | if_comp WS unorderd_list_part | simple_passthrough WS unorderd_list_part | declaration_statement WS ";" WS unorderd_list_part | import_list | function | if_comp | simple_passthrough | declaration_statement WS ";" ; unorderd_list_part = import WS unorderd_list_part | function WS unorderd_list_part | type_def WS ";" WS unorderd_list_part | if_comp WS unorderd_list_part | simple_passthrough WS unorderd_list_part | declaration_statement WS ";" WS unorderd_list_part | import | function | type_def WS ";" | if_comp | simple_passthrough | declaration_statement WS ";" ;
type = type WS "\*" | "void" | "int" | "float" | "double" | "char" | identifier ; type = type WS "\*" | "void" | "int" | "float" | "double" | "char" | identifier ;
import_list = import_list WS import | import ;
import = "import" WS identifier WS ";" ; import = "import" WS identifier WS ";" ;
interpreter_directive = "#!" WS path | ; interpreter_directive = "#!" WS path | ;
@@ -19,8 +18,8 @@ WS = "( | |
if_comp = "__if_comp__" WS identifier WS if_comp_pred ; if_comp = "__if_comp__" WS identifier WS if_comp_pred ;
if_comp_pred = code_block | simple_passthrough ; if_comp_pred = code_block | simple_passthrough ;
simple_passthrough = "__simple_passthrough__" WS triple_quoted_string ; simple_passthrough = "__simple_passthrough__" WS triple_quoted_string ;
triple_quoted_string = "\"\"\"(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|;|%|=|\+| | |
|\\|/|\||\(|\)|\"|#|<|\*|>|0|1|2|3|4|5|6|7|8|9)+\"\"\"" ; triple_quoted_string = "((b)|a)*" ;
identifier = alpha | alpha alphanumeric ; identifier = alpha | alpha alphanumeric ;
@@ -34,6 +33,11 @@ opt_parameter_list = parameter_list | ;
parameter_list = parameter_list WS "," WS parameter | parameter ; parameter_list = parameter_list WS "," WS parameter | parameter ;
parameter = boolean_expression ; parameter = boolean_expression ;
type_def = "typedef" WS identifier WS type | "typedef" WS identifier WS "{" WS class_innerds WS "}" | "typedef" WS identifier WS "{" WS declaration_block WS "}" ;
class_innerds = visibility_block WS class_innerds | visibility_block ;
visibility_block = "public:" WS declaration_block | "protected:" WS declaration_block | "private:" WS declaration_block ;
declaration_block = declaration_statement WS ";" WS declaration_block | function WS declaration_block | declaration_statement WS ";" | function ;
if_statement = "if" WS "\(" WS boolean_expression WS "\)" WS statement ; if_statement = "if" WS "\(" WS boolean_expression WS "\)" WS statement ;
while_loop = "while" WS boolean_expression WS statement ; while_loop = "while" WS boolean_expression WS statement ;
@@ -62,7 +66,7 @@ unarad = number | identifier | function_call | bool | string | character | "\("
number = integer | float | double ; number = integer | float | double ;
assignment_statement = identifier WS "=" WS boolean_expression | identifier WS "\+=" WS boolean_expression | identifier WS "-=" WS boolean_expression | identifier WS "\*=" WS boolean_expression | identifier WS "/=" WS boolean_expression ; assignment_statement = identifier WS "=" WS boolean_expression | identifier WS "\+=" WS boolean_expression | identifier WS "-=" WS boolean_expression | identifier WS "\*=" WS boolean_expression | identifier WS "/=" WS boolean_expression ;
declaration_statement = type WS identifier WS "=" WS boolean_expression ; declaration_statement = type WS identifier WS "=" WS boolean_expression | type WS identifier ;
alphanumeric = alphanumeric numeric | alphanumeric alpha | numeric | alpha ; alphanumeric = alphanumeric numeric | alphanumeric alpha | numeric | alpha ;
hexadecimal = "0x(1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+" ; hexadecimal = "0x(1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+" ;
@@ -71,10 +75,13 @@ integer = sign numeric | sign hexadecimal | "null" ;
float = sign numeric "." numeric "f" ; float = sign numeric "." numeric "f" ;
double = sign numeric "." numeric | sign numeric "." numeric "d" ; double = sign numeric "." numeric | sign numeric "." numeric "d" ;
bool = "true" | "false" | "True" | "False" ; bool = "true" | "false" | "True" | "False" ;
character = "'(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|%| | |\\|/|\||\(|\)|0|1|2|3|4|5|6|7|8|9)'" ; character = "'(`|1|2|3|4|5|6|7|8|9|0|-|=| |q|w|e|r|t|y|u|i|o|p|[|]|\\|a|s|d|f|g|h|j|k|l|;|'|
|z|x|c|v|b|n|m|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|Q|W|E|R|T|Y|U|I|O|P|{|}|\||A|S|D|F|G|H|J|K|L|:|\"|Z|X|C|V|B|N|M|<|>|\?| )'" ;
alpha = "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|_)+" ; alpha = "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|_)+" ;
numeric = "(0|1|2|3|4|5|6|7|8|9)+" ; numeric = "(0|1|2|3|4|5|6|7|8|9)+" ;
string = triple_quoted_string | "\"(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|%| | |\\|/|\||\(|\)|0|1|2|3|4|5|6|7|8|9)+\"" ; string = triple_quoted_string | "\"(`|1|2|3|4|5|6|7|8|9|0|-|=| |q|w|e|r|t|y|u|i|o|p|[|]|\\|a|s|d|f|g|h|j|k|l|;|'|
|z|x|c|v|b|n|m|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|Q|W|E|R|T|Y|U|I|O|P|{|}|\||A|S|D|F|G|H|J|K|L|:|Z|X|C|V|B|N|M|<|>|\?| )*\"" ;
comment = "//(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|;|%|=|\+| | |\\|/|\||\(|\)|\*|\"|0|1|2|3|4|5|6|7|8|9)+ comment = "//(`|1|2|3|4|5|6|7|8|9|0|-|=| |q|w|e|r|t|y|u|i|o|p|[|]|\\|a|s|d|f|g|h|j|k|l|;|'|z|x|c|v|b|n|m|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|Q|W|E|R|T|Y|U|I|O|P|{|}|\||A|S|D|F|G|H|J|K|L|:|\"|Z|X|C|V|B|N|M|<|>|\?| )*
" | "/\*(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|,|!|\?|_|-|:|%|=|\+| | |\\|/|\||\(|\)|\"|0|1|2|3|4|5|6|7|8|9)+\*/" ; " | "/\*(`|1|2|3|4|5|6|7|8|9|0|-|=| |q|w|e|r|t|y|u|i|o|p|[|]|\\|a|s|d|f|g|h|j|k|l|;|'|
|z|x|c|v|b|n|m|,|.|/|~|!|@|#|$|%|^|&|\*|\(|\)|_|\+|Q|W|E|R|T|Y|U|I|O|P|{|}|\||A|S|D|F|G|H|J|K|L|:|\"|Z|X|C|V|B|N|M|<|>|\?| )*\*/" ;

View File

@@ -2,14 +2,15 @@
ASTData::ASTData() { ASTData::ASTData() {
this->type = undef; this->type = undef;
this->valueType = NULL;
} }
ASTData::ASTData(ASTType type, Type valueType) { ASTData::ASTData(ASTType type, Type *valueType) {
this->type = type; this->type = type;
this->valueType = valueType; this->valueType = valueType;
} }
ASTData::ASTData(ASTType type, Symbol symbol, Type valueType) { ASTData::ASTData(ASTType type, Symbol symbol, Type *valueType) {
this->type = type; this->type = type;
this->valueType = valueType; this->valueType = valueType;
this->symbol = symbol; this->symbol = symbol;
@@ -20,7 +21,7 @@ ASTData::~ASTData() {
} }
std::string ASTData::toString() { std::string ASTData::toString() {
return ASTTypeToString(type) + (symbol.isTerminal() ? " " + symbol.toString() : "") + " " + valueType.toString(); return ASTTypeToString(type) + (symbol.isTerminal() ? " " + symbol.toString() : "") + " " + (valueType ? valueType->toString() : "no_type");
} }
std::string ASTData::ASTTypeToString(ASTType type) { std::string ASTData::ASTTypeToString(ASTType type) {
@@ -35,6 +36,8 @@ std::string ASTData::ASTTypeToString(ASTType type) {
return "import"; return "import";
case function: case function:
return "function"; return "function";
case type_def:
return "type_def";
case code_block: case code_block:
return "code_block"; return "code_block";
case typed_parameter: case typed_parameter:

View File

@@ -64,9 +64,14 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
throw "LOOKUP ERROR: " + lookupName; throw "LOOKUP ERROR: " + lookupName;
} }
//newNode = new NodeTree<ASTData>(name, ASTData(identifier, Symbol(concatSymbolTree(children[0]), true))); //newNode = new NodeTree<ASTData>(name, ASTData(identifier, Symbol(concatSymbolTree(children[0]), true)));
} else if (name == "type_def") {
std::string typeAlias = concatSymbolTree(children[0]);
newNode = new NodeTree<ASTData>(name, ASTData(type_def, Symbol(typeAlias, true, typeAlias), typeFromString(concatSymbolTree(children[1]), scope)));
scope->getDataRef()->scope[typeAlias] = newNode;
return newNode;
} else if (name == "function") { } else if (name == "function") {
std::string functionName = concatSymbolTree(children[1]); std::string functionName = concatSymbolTree(children[1]);
newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), Type(concatSymbolTree(children[0])))); newNode = new NodeTree<ASTData>(name, ASTData(function, Symbol(functionName, true), typeFromString(concatSymbolTree(children[0]), scope)));
skipChildren.insert(0); skipChildren.insert(0);
skipChildren.insert(1); skipChildren.insert(1);
scope->getDataRef()->scope[functionName] = newNode; scope->getDataRef()->scope[functionName] = newNode;
@@ -80,7 +85,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
//newNode = transform(children[1]); //Transform to get the identifier //newNode = transform(children[1]); //Transform to get the identifier
std::string parameterName = concatSymbolTree(children[1]); std::string parameterName = concatSymbolTree(children[1]);
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), Type(typeString))); newNode = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(parameterName, true), typeFromString(typeString, scope)));
scope->getDataRef()->scope[parameterName] = newNode; scope->getDataRef()->scope[parameterName] = newNode;
return newNode; return newNode;
} else if (name == "boolean_expression" || name == "and_boolean_expression" || name == "bool_exp") { } else if (name == "boolean_expression" || name == "and_boolean_expression" || name == "bool_exp") {
@@ -180,10 +185,9 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
// NodeTree<ASTData>* newIdentifier = transform(children[1], scope); //Transform the identifier // NodeTree<ASTData>* newIdentifier = transform(children[1], scope); //Transform the identifier
// newIdentifier->getDataRef()->valueType = Type(concatSymbolTree(children[0]));//set the type of the identifier // newIdentifier->getDataRef()->valueType = Type(concatSymbolTree(children[0]));//set the type of the identifier
std::string newIdentifierStr = concatSymbolTree(children[1]); std::string newIdentifierStr = concatSymbolTree(children[1]);
std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type std::string typeString = concatSymbolTree(children[0]);//Get the type (left child) and set our new identifer to be that type
NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), Type(typeString))); NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), typeFromString(typeString, scope)));
scope->getDataRef()->scope[newIdentifierStr] = newIdentifier; scope->getDataRef()->scope[newIdentifierStr] = newIdentifier;
newNode->addChild(newIdentifier); newNode->addChild(newIdentifier);
@@ -213,19 +217,19 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
return transform(children[0], scope); //Don't need a parameter node, just the value return transform(children[0], scope); //Don't need a parameter node, just the value
} else if (name == "type") { } else if (name == "type") {
std::string theConcat = concatSymbolTree(from); //We have no symbol, so this will concat our children std::string theConcat = concatSymbolTree(from); //We have no symbol, so this will concat our children
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(theConcat, true), Type(theConcat))); newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(theConcat, true), typeFromString(theConcat, scope)));
} else if (name == "number") { } else if (name == "number") {
return transform(children[0], scope); return transform(children[0], scope);
} else if (name == "integer") { } else if (name == "integer") {
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), Type(integer))); newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), new Type(integer)));
} else if (name == "float") { } else if (name == "float") {
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), Type(floating))); newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), new Type(floating)));
} else if (name == "double") { } else if (name == "double") {
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), Type(double_percision))); newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(from), true), new Type(double_percision)));
} else if (name == "char") { } else if (name == "char") {
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), Type(character, 1))); //Indirection of 1 for array newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), new Type(character, 1))); //Indirection of 1 for array
} else if (name == "string" || name == "triple_quoted_string") { } else if (name == "string" || name == "triple_quoted_string") {
newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), Type(character, 1))); //Indirection of 1 for array newNode = new NodeTree<ASTData>(name, ASTData(value, Symbol(concatSymbolTree(children[0]), true), new Type(character, 1))); //Indirection of 1 for array
} else { } else {
return new NodeTree<ASTData>(); return new NodeTree<ASTData>();
} }
@@ -278,3 +282,29 @@ NodeTree<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* scope, std:
//std::cout << "upper scope does not exist" << std::endl; //std::cout << "upper scope does not exist" << std::endl;
return NULL; return NULL;
} }
Type* ASTTransformation::typeFromString(std::string typeIn, NodeTree<ASTData>* scope) {
int indirection = 0;
ValueType baseType;
NodeTree<ASTData>* typeDefinition = NULL;
while (typeIn[typeIn.size() - indirection - 1] == '*') indirection++;
std::string edited = strSlice(typeIn, 0, -(indirection + 1));
if (edited == "void")
baseType = void_type;
else if (edited == "bool")
baseType = boolean;
else if (edited == "int")
baseType = integer;
else if (edited == "float")
baseType = floating
; else if (edited == "double")
baseType = double_percision;
else if (edited == "char")
baseType = character;
else {
baseType = none;
typeDefinition = scopeLookup(scope, edited);
std::cout << "scopeLookup of type " << edited << " returned " << typeDefinition << std::endl;
}
return new Type(baseType, typeDefinition, indirection);
}

View File

@@ -41,6 +41,10 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
std::string output = ""; std::string output = "";
switch (data.type) { switch (data.type) {
case translation_unit: case translation_unit:
//Do here because we may need the typedefs before the declarations of variables
for (int i = 0; i < children.size(); i++)
if (children[i]->getDataRef()->type == type_def)
output += generate(children[i]) + "\n";
//Declare everything in translation unit scope here. (allows stuff from other files, automatic forward declarations) //Declare everything in translation unit scope here. (allows stuff from other files, automatic forward declarations)
for (auto i = data.scope.begin(); i != data.scope.end(); i++) { for (auto i = data.scope.begin(); i != data.scope.end(); i++) {
NodeTree<ASTData>* declaration = i->second; NodeTree<ASTData>* declaration = i->second;
@@ -59,11 +63,20 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
} }
output += "); /*func*/\n"; output += "); /*func*/\n";
break; break;
case type_def:
//type
output += "/*typedef " + declarationData.symbol.getName() + " */\n";
break;
default: default:
//std::cout << "Declaration? named " << declaration->getName() << " of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl; //std::cout << "Declaration? named " << declaration->getName() << " of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl;
output += "/*unknown declaration named " + declaration->getName() + "*/\n"; output += "/*unknown declaration named " + declaration->getName() + "*/\n";
} }
} }
//Do here because we need the newlines
for (int i = 0; i < children.size(); i++)
if (children[i]->getDataRef()->type != type_def)
output += generate(children[i]) + "\n";
return output;
break; break;
case interpreter_directive: case interpreter_directive:
//Do nothing //Do nothing
@@ -73,6 +86,8 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
//return "#include <" + data.symbol.getName() + ">\n"; //return "#include <" + data.symbol.getName() + ">\n";
case identifier: case identifier:
return data.symbol.getName(); return data.symbol.getName();
case type_def:
return "typedef " + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + ";";
case function: case function:
output += "\n" + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + "("; output += "\n" + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + "(";
for (int i = 0; i < children.size()-1; i++) { for (int i = 0; i < children.size()-1; i++) {
@@ -116,6 +131,9 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
case assignment_statement: case assignment_statement:
return generate(children[0]) + " = " + generate(children[1]); return generate(children[0]) + " = " + generate(children[1]);
case declaration_statement: case declaration_statement:
if (children.size() == 1)
return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0]) + ";";
else
return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0]) + " = " + generate(children[1]) + ";"; return ValueTypeToCType(children[0]->getData().valueType) + " " + generate(children[0]) + " = " + generate(children[1]) + ";";
case if_comp: case if_comp:
if (generate(children[0]) == generatorString) if (generate(children[0]) == generatorString)
@@ -161,10 +179,13 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
return output; return output;
} }
std::string CGenerator::ValueTypeToCType(Type type) { std::string CGenerator::ValueTypeToCType(Type *type) {
std::string return_type; std::string return_type;
switch (type.baseType) { switch (type->baseType) {
case none: case none:
if (type->typeDefinition)
return_type = type->typeDefinition->getDataRef()->symbol.getName();
else
return_type = "none"; return_type = "none";
break; break;
case void_type: case void_type:
@@ -189,7 +210,7 @@ std::string CGenerator::ValueTypeToCType(Type type) {
return_type = "unknown_ValueType"; return_type = "unknown_ValueType";
break; break;
} }
for (int i = 0; i < type.indirection; i++) for (int i = 0; i < type->indirection; i++)
return_type += "*"; return_type += "*";
return return_type; return return_type;
} }

View File

@@ -19,6 +19,7 @@ Importer::Importer(Parser* parserIn) {
removeSymbols.push_back(Symbol("while", true)); removeSymbols.push_back(Symbol("while", true));
removeSymbols.push_back(Symbol("__if_comp__", true)); removeSymbols.push_back(Symbol("__if_comp__", true));
removeSymbols.push_back(Symbol("comp_simple_passthrough", true)); removeSymbols.push_back(Symbol("comp_simple_passthrough", true));
removeSymbols.push_back(Symbol("typedef", true));
collapseSymbols.push_back(Symbol("opt_typed_parameter_list", false)); collapseSymbols.push_back(Symbol("opt_typed_parameter_list", false));
collapseSymbols.push_back(Symbol("opt_parameter_list", false)); collapseSymbols.push_back(Symbol("opt_parameter_list", false));

View File

@@ -20,6 +20,7 @@ void Lexer::setInput(std::string inputString) {
} }
void Lexer::addRegEx(std::string regExString) { void Lexer::addRegEx(std::string regExString) {
std::cout << regExString << " at lexer" << std::endl;
regExs.push_back(new RegEx(regExString)); regExs.push_back(new RegEx(regExString));
} }

View File

@@ -2,8 +2,10 @@
#include <cassert> #include <cassert>
RegEx::RegEx(std::string inPattern) { RegEx::RegEx(std::string inPattern) {
std::cout << inPattern << " at rexex" << std::endl;
pattern = inPattern; pattern = inPattern;
construct(); construct();
std::cout << inPattern << " at rexex post" << std::endl;
deperenthesize(); deperenthesize();
} }
@@ -310,5 +312,12 @@ void RegEx::test() {
assert(re.longMatch("ab") == 1); assert(re.longMatch("ab") == 1);
} }
{
RegEx re("((ab)|c)*");
assert(re.longMatch("ababc") == 5);
assert(re.longMatch("ad") == 1);
assert(re.longMatch("ababccd") == 6);
}
std::cout << "RegEx tests pass\n"; std::cout << "RegEx tests pass\n";
} }

View File

@@ -85,7 +85,7 @@ void Table::exportTable(std::ofstream &file) {
//std::vector<Symbol> rightSide; //std::vector<Symbol> rightSide;
std::vector<Symbol> rightSide = rule->getRightSide(); std::vector<Symbol> rightSide = rule->getRightSide();
size = rightSide.size(); size = rightSide.size();
std::cout << leftHandle.toString() << std::endl; //std::cout << leftHandle.toString() << std::endl;
file.write((char*)&size, sizeof(int)); file.write((char*)&size, sizeof(int));
for (int l = 0; l < rightSide.size(); l++) { for (int l = 0; l < rightSide.size(); l++) {
//Save the name //Save the name

View File

@@ -15,27 +15,22 @@ Type::Type(ValueType typeIn, int indirectionIn) {
baseType = typeIn; baseType = typeIn;
} }
Type::Type(std::string typeIn) { Type::Type(NodeTree<ASTData>* typeDefinitionIn) {
indirection = 0; indirection = 0;
while (typeIn[typeIn.size() - indirection - 1] == '*') indirection++;
std::string edited = strSlice(typeIn, 0, -(indirection + 1));
if (edited == "void")
baseType = void_type;
else if (edited == "bool")
baseType = boolean;
else if (edited == "int")
baseType = integer;
else if (edited == "float")
baseType = floating;
else if (edited == "double")
baseType = double_percision;
else if (edited == "char")
baseType = character;
else
baseType = none; baseType = none;
//std::cout << ":ALKJF:LSKDJF:SDJF:LKSJDF\t\t\t" << typeIn << "\t" << edited << std::endl; typeDefinition = typeDefinitionIn;
}
Type::Type(NodeTree<ASTData>* typeDefinitionIn, int indirectionIn) {
indirection = indirectionIn;
baseType = none;
typeDefinition = typeDefinitionIn;
} }
Type::Type(ValueType typeIn, NodeTree<ASTData>* typeDefinitionIn, int indirectionIn) {
baseType = typeIn;
indirection = indirectionIn;
typeDefinition = typeDefinitionIn;
}
Type::~Type() { Type::~Type() {
} }
@@ -44,6 +39,9 @@ std::string Type::toString() {
std::string typeString; std::string typeString;
switch (baseType) { switch (baseType) {
case none: case none:
if (typeDefinition)
typeString = typeDefinition->getDataRef()->symbol.getName();
else
typeString = "none"; typeString = "none";
break; break;
case void_type: case void_type:
@@ -65,6 +63,9 @@ std::string Type::toString() {
typeString = "char"; typeString = "char";
break; break;
default: default:
if (typeDefinition)
typeString = typeDefinition->getDataRef()->symbol.getName();
else
typeString = "unknown_type"; typeString = "unknown_type";
} }
for (int i = 0; i < indirection; i++) for (int i = 0; i < indirection; i++)