diff --git a/Tests/Program2/programming_assignment_2-test_file_7.c b/Tests/Program2/programming_assignment_2-test_file_7.c new file mode 100644 index 0000000..8ce2604 --- /dev/null +++ b/Tests/Program2/programming_assignment_2-test_file_7.c @@ -0,0 +1,61 @@ +// *************************************************** +// * CS460: Programming Assignment 5: Test Program 2 * +// *************************************************** + + + +// *********************************************************************************** +// * Hex digit converts a single character into its non-negative integer equivalent. * +// * * +// * Hex digit returns -1 upon error * +// *********************************************************************************** +function int hexdigit2int (char hex_digit) +{ + int i, digit; + + digit = -1; + if ((hex_digit >= '0') && (hex_digit <= '9')) + { + digit = hex_digit - '0'; + } + else + { + if ((hex_digit >= 'a') && (hex_digit <= 'f')) + { + digit = hex_digit - 'a' + 10; + } + else + { + if ((hex_digit >= 'A') && (hex_digit <= 'F')) + { + digit = hex_digit - 'A' + 10; + } + } + } + return digit; +} + + + +procedure main (void) +{ + char hexnum[9]; + int i, digit, number; + + number = 0; + hexnum = "feed\x0"; + digit = 0; + for (i = 0; (i < 4) && (digit > -1); i = i + 1) + { + digit = hexdigit2int (hexnum[i]); + if (digit > -1) + { + number = number * 16 + digit; + } + } + if (digit > -1) + { + printf ("Hex: 0x%s is %d decimal\n", hexnum, number); + } +} + diff --git a/testFiles.h b/testFiles.h index c198abc..e6fdb98 100644 --- a/testFiles.h +++ b/testFiles.h @@ -20,7 +20,8 @@ const std::filesystem::path a2Tests[] = { "Tests/Program2/programming_assignment_2-test_file_3.c", "Tests/Program2/programming_assignment_2-test_file_4.c", "Tests/Program2/programming_assignment_2-test_file_5.c", - "Tests/Program2/programming_assignment_2-test_file_6.c" + "Tests/Program2/programming_assignment_2-test_file_6.c", + "Tests/Program2/programming_assignment_2-test_file_7.c" }; // Assignment 3 diff --git a/tokenizer.cpp b/tokenizer.cpp index 6d00ff6..c754e82 100644 --- a/tokenizer.cpp +++ b/tokenizer.cpp @@ -228,6 +228,8 @@ void Tokenizer::state5(std::istringstream &inputStream, int &lineCount) { // Add the accumulated string as a CHARACTER token, may need to be renamed in the future. Token token("CHARACTER", strLiteral, lineCount); tokenList.push_back(token); + Token endingQuote("SINGLE_QUOTE", "\'", lineCount); + tokenList.push_back(endingQuote); } } diff --git a/tree.cpp b/tree.cpp index b6ff975..f14f23d 100644 --- a/tree.cpp +++ b/tree.cpp @@ -13,7 +13,14 @@ void Tree::printTree(Token* head, Token* prevToken){ } else if (head->getValue() == "}") { ignore = false; std::cout << "END BLOCK"; - } else if (head->getValue() == "procedure" || head->getValue() == "function") { + } else if(head->getValue() == "else"){ + ignore = false; + std::cout << "ELSE"; + } else if(head->getValue() == "return"){ + ignore = false; + std::cout << "RETURN"; + head = head->getSibling(); + }else if (head->getValue() == "procedure" || head->getValue() == "function") { std::cout << "DECLARATION\n|\n|\n|\n|\nv\n"; while(head->getSibling() != nullptr) { head = head->getSibling(); @@ -26,7 +33,19 @@ void Tree::printTree(Token* head, Token* prevToken){ head = handleAssignment(head); prevToken = nullptr; - } else if (contains(varTypes, prevToken->getValue())) { + } else if (head->getValue() == "for"){ + short forCount = 0; + ignore = false; + while (forCount < 3){ + std::cout << "FOR EXPRESSION " << forCount+1 << " ----> "; + head = head->getSibling(); + head = handleAssignment(head); + prevToken = nullptr; + std::cout << "\n|\n|\n|\n|\nv\n "; + forCount++; + } + + }else if (contains(varTypes, prevToken->getValue())) { ignore = false; std::cout << "DECLARATION"; if (head->getSibling() != nullptr && head->getSibling()->getValue() == ",") { @@ -45,7 +64,9 @@ void Tree::printTree(Token* head, Token* prevToken){ head = handleAssignment(head); prevToken = nullptr; } - } + } else if (head->getSibling() == nullptr && head->getChild() != nullptr && head->getValue() == ";"){ + std::cout << "\n|\n|\n|\n|\nv\n "; + } if (head->getSibling() != nullptr) { if (!ignore){ std::cout << " ----> "; @@ -72,10 +93,12 @@ bool Tree::contains(const std::vector reserved, std::string type){ Token* Tree::handleAssignment(Token* head) { std::vector equationAsVec; - equationAsVec.push_back(head); + if (head->getValue() != "(") { + equationAsVec.push_back(head); + } head = head->getSibling(); - while(contains(equationOperators, head->getValue()) || head->getType() == "IDENTIFIER" || head->getType() == "INTEGER" || head->getType() == "CHARACTER") { + while(head->getValue() != ";" &&(contains(equationOperators, head->getValue()) || head->getType() == "IDENTIFIER" || head->getType() == "INTEGER" || head->getType() == "CHARACTER" || head->getType() == "STRING" || head->getType() == "DOUBLE_QUOTE")) { equationAsVec.push_back(head); if (head ->getSibling() != nullptr) { head = head->getSibling(); @@ -119,12 +142,17 @@ std::vector Tree::infixToPostfix(const std::vector infix) { std::stack operators; std::vector postfix; + // std::cout << "PRINTING: "; + // for (Token* t : infix) { + // std::cout << t->getValue(); + // } + for (Token* t : infix) { std::string tokenType = t->getType(); std::string tokenValue = t->getValue(); // If it's an operand, add it to the postfix output - if (tokenType == "INTEGER" || tokenType == "STRING" || tokenType == "CHARACTER" || tokenType == "IDENTIFIER") { + if (tokenType == "INTEGER" || tokenType == "STRING" || tokenType == "CHARACTER" || tokenType == "IDENTIFIER" || tokenType == "SINGLE_QUOTE" || tokenType == "DOUBLE_QUOTE") { postfix.push_back(t); } // If it's a left parenthesis, push it onto the stack