Skip to content

update logic #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Tests/Program2/programming_assignment_2-test_file_7.c
Original file line number Diff line number Diff line change
@@ -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);
}
}

3 changes: 2 additions & 1 deletion testFiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
40 changes: 34 additions & 6 deletions tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() == ",") {
Expand All @@ -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 << " ----> ";
Expand All @@ -72,10 +93,12 @@ bool Tree::contains(const std::vector<std::string> reserved, std::string type){

Token* Tree::handleAssignment(Token* head) {
std::vector<Token*> 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();
Expand Down Expand Up @@ -119,12 +142,17 @@ std::vector<Token*> Tree::infixToPostfix(const std::vector<Token*> infix) {
std::stack<Token*> operators;
std::vector<Token*> 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
Expand Down