From 48dc1f8e4d2f4e9d978fe12bd0f6bb57ebcff3b1 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Tue, 24 Mar 2015 15:47:08 -0400 Subject: [PATCH] Fixed - and + as unary operators, negative numbers, etc --- src/ASTTransformation.cpp | 24 ++++++++++--------- src/CGenerator.cpp | 2 +- ...est_negative_number_unary.expected_results | 5 ++++ tests/test_negative_number_unary.krak | 14 +++++++++++ 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 tests/test_negative_number_unary.expected_results create mode 100644 tests/test_negative_number_unary.krak diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index ed3255e..313213a 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -523,15 +523,6 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree std::cerr << "scope lookup error! Could not find " << functionCallName << " in expression " << std::endl; throw "LOOKUP ERROR: " + functionCallName; } - - // //Set the value of this function call - if (newNode->getDataRef()->valueType == NULL && rhs->getDataRef()->valueType) { - std::cout << "The value type from doFunction was null! (for " << functionCallName << ")" << std::endl; - newNode->getDataRef()->valueType = rhs->getDataRef()->valueType; - } - //else - // newNode->getDataRef()->valueType = NULL; - std::cout << "function call to " << functionCallName << " - " << newNode->getName() << " is now " << newNode->getDataRef()->valueType << std::endl; return newNode; //skipChildren.insert(1); } else if (children.size() == 2) { @@ -547,14 +538,17 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree if (children.size() == 2) { funcName = concatSymbolTree(children[0]); NodeTree* param; - if (funcName == "*" || funcName == "&" || funcName == "++" || funcName == "--" || funcName == "-" || funcName == "!" || funcName == "~") + // I think this is where we look at pre vs post operators + if (funcName == "*" || funcName == "&" || funcName == "++" || funcName == "--" || funcName == "+" || funcName == "-" || funcName == "!" || funcName == "~") param = transform(children[1], scope, types, templateTypeReplacements); else funcName = concatSymbolTree(children[1]), param = transform(children[0], scope, types, templateTypeReplacements); + std::cout << "\t\t\t funcName= " << funcName << " param: " << param->getDataRef()->symbol.getName() << std::endl; //std::cout << "scope lookup from factor" << std::endl; std::vector*> transformedChildren; transformedChildren.push_back(param); NodeTree* function = doFunction(scope, funcName, transformedChildren, templateTypeReplacements); + std::cout << "\t\t\t AFTER dofunction= " << std::endl; if (function == NULL) { std::cerr << "scope lookup error! Could not find " << funcName << " in factor " << std::endl; throw "LOOKUP ERROR: " + funcName; @@ -805,7 +799,15 @@ NodeTree* ASTTransformation::doFunction(NodeTree* scope, std:: } else { newNode->getDataRef()->valueType = function->getDataRef()->valueType, std::cout << "Some other ||" << lookup << "||" << std::endl; } - std::cout << "Num of newNode children " << newNode->getChildren().size() << std::endl; + + // Set the value of this function call if it has not already been set + // It's important that it's the last parameter, the rhs if it has one + // because of the . operator, etc + if (newNode->getDataRef()->valueType == NULL) { + std::cout << "The value type from doFunction was null! (for " << lookup << ")" << std::endl; + newNode->getDataRef()->valueType = oldTypes[oldTypes.size()-1].clone(); + std::cout << "function call to " << lookup << " - " << newNode->getName() << " is now " << newNode->getDataRef()->valueType << std::endl; + } return newNode; } diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index 8b58b2b..6ec7148 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -382,7 +382,7 @@ std::string CGenerator::generate(NodeTree* from, NodeTree* enc if (funcType == function) { if (name == "++" || name == "--") return generate(children[1], enclosingObject) + name; - if ( (name == "*" || name == "&" || name == "!" ) && children.size() == 2) //Is dereference, not multiplication, address-of, or other unary operator + if ( (name == "*" || name == "&" || name == "!" || name == "-" || name == "+" ) && children.size() == 2) //Is dereference, not multiplication, address-of, or other unary operator return name + "(" + generate(children[1], enclosingObject) + ")"; if (name == "[]") return "(" + generate(children[1], enclosingObject) + ")[" +generate(children[2],enclosingObject) + "]"; diff --git a/tests/test_negative_number_unary.expected_results b/tests/test_negative_number_unary.expected_results new file mode 100644 index 0000000..c3bb0e5 --- /dev/null +++ b/tests/test_negative_number_unary.expected_results @@ -0,0 +1,5 @@ +-1 +1 +-1 +7 +-7 diff --git a/tests/test_negative_number_unary.krak b/tests/test_negative_number_unary.krak new file mode 100644 index 0000000..7f1a2e3 --- /dev/null +++ b/tests/test_negative_number_unary.krak @@ -0,0 +1,14 @@ +import io:* + +|int| main() { + |int| a = -1 + println(a) + println(-a) + println(+a); // this is still -1! (as C has it, anyway) (darn comment/semicolon interaction) + + |int| b = 7 + println(b) + println(-b) + + return 0; +}