From 22fbd61360d76054e1ba394f4a9741e58b2f5cc7 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Wed, 2 Jul 2014 01:18:27 -0700 Subject: [PATCH] Fixed a pretty bad error in isNullable logic, I must have been tired. Also, edited grammer to support a[n].b, which was previously done with wrong operator precedence so that that construction was illegal. vector.krak still doesn't quite parse, but that's because of some error with if (!name) which I will fix later. Bedtime. --- krakenGrammer.kgm | 4 ++-- src/ASTTransformation.cpp | 33 ++++++++++++++++++--------------- src/Parser.cpp | 18 ++++++++++++++---- src/RNGLRParser.cpp | 2 +- stdlib/mem.krak | 4 ++-- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index da9c8c5..755de32 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -75,8 +75,8 @@ comparator = "==" | "<=" | ">=" | "!=" | "<" | ">" ; expression = expression WS "<<" WS term | expression WS ">>" WS shiftand | shiftand ; shiftand = shiftand WS "-" WS term | shiftand WS "\+" WS term | term ; term = term WS forward_slash WS factor | term WS "\*" WS factor | term WS "%" WS factor | factor ; -factor = "\+\+" WS unarad | unarad WS "\+\+" | "--" WS unarad | unarad WS "--" | "\+" WS unarad | "-" WS unarad | "!" WS unarad | "~" WS unarad | "\(" WS type WS "\)" WS unarad | "\*" WS unarad | "&" WS unarad | unarad WS "[" WS expression WS "]" | unarad ; -unarad = number | identifier | identifier WS template_inst | function_call | bool | string | character | "\(" WS boolean_expression WS "\)" | access_operation ; +factor = "\+\+" WS unarad | unarad WS "\+\+" | "--" WS unarad | unarad WS "--" | "\+" WS unarad | "-" WS unarad | "!" WS unarad | "~" WS unarad | "\(" WS type WS "\)" WS unarad | "\*" WS unarad | "&" WS unarad | unarad ; +unarad = number | identifier | identifier WS template_inst | function_call | bool | string | character | "\(" WS boolean_expression WS "\)" | access_operation | unarad WS "[" WS expression WS "]" ; number = integer | float | double ; access_operation = unarad "." identifier | unarad "->" identifier ; diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index 160e14c..d50d8dc 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -467,9 +467,21 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree return transform(children[0], scope, types, templateTypeReplacements, instantiateTemplates); //Just a promoted term, so do child } //Here's the order of ops stuff - } else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad" || name == "access_operation") { //unarad can ride through, it should always just be a promoted child + } else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad" || name == "access_operation") { //If this is an actual part of an expression, not just a premoted child if (children.size() > 2) { + /* else if (children.size() >= 4) { //Array brackets [] + funcName = "[]"; + std::vector*> transformedChildren; + transformedChildren.push_back(transform(children[0], scope, types, templateTypeReplacements, instantiateTemplates)); + transformedChildren.push_back(transform(children[2], scope, types, templateTypeReplacements, instantiateTemplates)); + NodeTree* function = doFunction(scope, funcName, transformedChildren, templateTypeReplacements); + if (function == NULL) { + std::cout << "scope lookup error! Could not find " << funcName << " in factor " << std::endl; + throw "LOOKUP ERROR: " + funcName; + } + return function; + }*/ NodeTree* lhs = transform(children[0], scope, std::vector(), templateTypeReplacements, instantiateTemplates); //LHS does not inherit types NodeTree* rhs; if (name == "access_operation") { @@ -480,7 +492,9 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree rhs = transform(children[2], scope, types, templateTypeReplacements, instantiateTemplates); std::string functionCallName = concatSymbolTree(children[1]); - //std::cout << "scope lookup from expression or similar" << std::endl; + if (functionCallName == "[") + functionCallName = "[]"; //fudge the lookup of brackets because only one is at children[1] (the other is at children[3]) + //std::cout << "scope lookup from expression or similar" << std::endl; std::vector*> transformedChildren; transformedChildren.push_back(lhs); transformedChildren.push_back(rhs); newNode = doFunction(scope, functionCallName, transformedChildren, templateTypeReplacements); if (newNode == NULL) { @@ -491,8 +505,8 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree // //Set the value of this function call if (newNode->getDataRef()->valueType == NULL && rhs->getDataRef()->valueType) newNode->getDataRef()->valueType = rhs->getDataRef()->valueType; - else - newNode->getDataRef()->valueType = NULL; + //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); @@ -522,17 +536,6 @@ NodeTree* ASTTransformation::transform(NodeTree* from, NodeTree throw "LOOKUP ERROR: " + funcName; } - return function; - } else if (children.size() >= 4) { //Array brackets [] - funcName = "[]"; - std::vector*> transformedChildren; - transformedChildren.push_back(transform(children[0], scope, types, templateTypeReplacements, instantiateTemplates)); - transformedChildren.push_back(transform(children[2], scope, types, templateTypeReplacements, instantiateTemplates)); - NodeTree* function = doFunction(scope, funcName, transformedChildren, templateTypeReplacements); - 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, templateTypeReplacements, instantiateTemplates); //Just a promoted child, so do it instead diff --git a/src/Parser.cpp b/src/Parser.cpp index 524b888..4a87119 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -179,11 +179,21 @@ bool Parser::isNullableHelper(Symbol token, std::set done) { done.insert(token); if (tokenNullable.find(token) != tokenNullable.end()) return tokenNullable[token]; - //Note that we only have to check the first token on the right side, as we would only get to the other tokens if it is nullable, which is what we're checking - for (std::vector::size_type i = 0; i < loadedGrammer.size(); i++) - if (token == loadedGrammer[i]->getLeftSide()) - if (isNullableHelper(loadedGrammer[i]->getRightSide()[0], done)) + + for (std::vector::size_type i = 0; i < loadedGrammer.size(); i++) { + if (token == loadedGrammer[i]->getLeftSide()) { + auto rightSide = loadedGrammer[i]->getRightSide(); + bool ruleNullable = true; + for (int j = 0; j < rightSide.size(); j++) { + if (!isNullableHelper(rightSide[j], done)) { + ruleNullable = false; + break; + } + } + if (ruleNullable) return true; + } + } return false; } diff --git a/src/RNGLRParser.cpp b/src/RNGLRParser.cpp index 8495042..400b4c4 100644 --- a/src/RNGLRParser.cpp +++ b/src/RNGLRParser.cpp @@ -109,7 +109,7 @@ NodeTree* RNGLRParser::parseInput(std::string inputString) { else std::cout << input[j].toString() << " "; std::cout << std::endl; - range = 3; + range = 1; std::cout << "\n\n\nThe states in the GSS at last frontiers:" << std::endl; for (int j = (i-range >= 0 ? i-range : 0); j < i; j++) { std::cout << "Frontier:" << j << " (would get): " << input[j].toString() << std::endl; diff --git a/stdlib/mem.krak b/stdlib/mem.krak index 13c4b2a..e52a85d 100644 --- a/stdlib/mem.krak +++ b/stdlib/mem.krak @@ -51,9 +51,9 @@ template T* new() { } template void delete(T* toDelete, int itemDestructCount) { - for (int i = 0; i < itemDestructCount; i++) + for (int i = 0; i < itemDestructCount; i++;) toDelete[i].destruct(); - delete(toDelete); + delete(toDelete); } template void delete(T* toDelete) {