From c4dea26cca91421f78eb5218d56c9bc0d9f5fe96 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Tue, 6 May 2014 13:54:53 -0400 Subject: [PATCH] Finished simple array notation, fixed address of operator. --- src/ASTTransformation.cpp | 31 ++++++++++++++++++------------- src/CGenerator.cpp | 6 ++++-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index 900c66f..97b7c15 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -23,6 +23,7 @@ ASTTransformation::ASTTransformation(Importer *importerIn) { languageLevelScope["-="].push_back(new NodeTree("function", ASTData(function, Symbol("-=", true), NULL))); languageLevelScope["."].push_back(new NodeTree("function", ASTData(function, Symbol(".", true), NULL))); languageLevelScope["->"].push_back(new NodeTree("function", ASTData(function, Symbol("->", true), NULL))); + languageLevelScope["[]"].push_back(new NodeTree("function", ASTData(function, Symbol("[]", true), NULL))); } ASTTransformation::~ASTTransformation() { @@ -181,8 +182,9 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree } else if (name == "factor") { //Do factor here, as it has all the weird unary operators //If this is an actual part of an expression, not just a premoted child //NO SUPPORT FOR CASTING YET + std::string funcName; if (children.size() == 2) { - std::string funcName = concatSymbolTree(children[0]); + funcName = concatSymbolTree(children[0]); NodeTree* param; if (funcName == "*" || funcName == "&" || funcName == "++" || funcName == "--" || funcName == "-" || funcName == "!" || funcName == "~") param = transform(children[1], scope, types); @@ -196,16 +198,19 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree std::cout << "scope lookup error! Could not find " << funcName << " in factor " << std::endl; throw "LOOKUP ERROR: " + funcName; } - newNode = function; - // newNode = new NodeTree(funcName, ASTData(function_call, Symbol(funcName, true))); - // newNode->addChild(function); - // newNode->addChild(param); - // if (function->getDataRef()->valueType) - // newNode->getDataRef()->valueType = function->getDataRef()->valueType; - // else - // newNode->getDataRef()->valueType = param->getDataRef()->valueType; - return newNode; + return function; + } else if (children.size() >= 4) { //Array brackets [] + funcName = "[]"; + std::vector*> transformedChildren; + transformedChildren.push_back(transform(children[0], scope, types)); + transformedChildren.push_back(transform(children[2], scope, types)); + NodeTree* function = doFunction(scope, funcName, transformedChildren); + if (function == NULL) { + std::cout << "scope lookup error! Could not find " << funcName << " in factor " << std::endl; + throw "LOOKUP ERROR: " + funcName; + } + return function; } else { return transform(children[0], scope, types); //Just a promoted child, so do it instead } @@ -411,10 +416,10 @@ NodeTree* ASTTransformation::doFunction(NodeTree* scope, std:: std::cout< oldTypes = mapNodesToTypes(nodes); - if (lookup == "*" || lookup == "&") { + if (lookup == "*" || lookup == "&" || lookup == "[]") { Type* newType = oldTypes[0].clone(); - lookup == "*" ? newType->indirection-- : newType->indirection++; - newNode->getDataRef()->valueType = newType, std::cout << "Operator " + lookup << std::endl; + lookup == "*" || lookup == "[]" ? newType->indirection-- : newType->indirection++; + newNode->getDataRef()->valueType = newType, std::cout << "Operator " + lookup << " is altering indirection "<< std::endl; } else { newNode->getDataRef()->valueType = function->getDataRef()->valueType, std::cout << "Some other ||" << lookup << "||" << std::endl; } diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index c6c7723..3a856f6 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -198,8 +198,10 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* enc if (funcType == function) { if (name == "++" || name == "--") return generate(children[1], enclosingObject) + name; - if (name == "*" && children.size() == 2) //Is dereference, not multiplication - return "*(" + generate(children[1], enclosingObject) + ")"; + if (name == "*" || name == "&" && children.size() == 2) //Is dereference, not multiplication, or address-of + return name + "(" + generate(children[1], enclosingObject) + ")"; + if (name == "[]") + return "(" + generate(children[1], enclosingObject) + ")[" +generate(children[2],enclosingObject) + "]"; if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=" || name == "<" || name == ">" || name == "%" || name == "+=" || name == "-=" || name == "*=" || name == "/=" || name == "||" || name == "&&" || name == "!" )