-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Pip-Install-Party/officialblake-develop
add base logic for abstract syntax tree
- Loading branch information
Showing
9 changed files
with
310 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// *************************************************** | ||
// * CS460: Programming Assignment 5: Test Program 1 * | ||
// *************************************************** | ||
|
||
function int sum_of_first_n_squares (int n) | ||
{ | ||
int sum; | ||
|
||
sum = 0; | ||
if (n >= 1) | ||
{ | ||
sum = n * (n + 1) * (2 * n + 1) / 6; | ||
} | ||
return sum; | ||
} | ||
|
||
procedure main (void) | ||
{ | ||
int n; | ||
int sum; | ||
|
||
n = 100; | ||
sum = sum_of_first_n_squares (n); | ||
printf ("sum of the squares of the first %d numbers = %d\n", n, sum); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*************************************************** | ||
* CS460: Programming Assignment 5: Test Program 3 * | ||
***************************************************/ | ||
|
||
char announcement[2048]; | ||
|
||
|
||
procedure main (void) | ||
{ | ||
char name[100]; | ||
|
||
name = 'Robert\x0'; | ||
announcement = "You've got mail!\x0"; | ||
display_announcement (name); | ||
} | ||
|
||
|
||
function bool empty_string (char string[4096]) | ||
{ | ||
int i; | ||
int num_bytes_before_null; | ||
bool found_null; | ||
|
||
found_null = FALSE; | ||
num_bytes_before_null = 0; | ||
i = 0; | ||
while ((i < 4096) && (!found_null)) | ||
{ | ||
if (string[i] == '\x0') | ||
{ | ||
found_null = TRUE; | ||
} | ||
else | ||
{ | ||
num_bytes_before_null = num_bytes_before_null + 1; | ||
} | ||
i = i + 1; | ||
} | ||
return (num_bytes_before_null == 0); | ||
} | ||
|
||
|
||
procedure display_announcement (char name[512]) | ||
{ | ||
if (!empty_string(name)) | ||
{ | ||
printf ("Welcome, %s\n\n", name); | ||
if (!empty_string(announcement)) | ||
{ | ||
printf ("%s\n", announcement); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/*************************************************** | ||
* CS460: Programming Assignment 5: Test Program 4 * | ||
***************************************************/ | ||
|
||
char my_string[1024]; | ||
|
||
|
||
procedure main (void) | ||
{ | ||
result = TRUE; | ||
my_string[0] = '\x0'; | ||
number = 3; | ||
} | ||
|
||
|
||
int number; | ||
|
||
|
||
function bool random_long_parameter_list (int ensity, char ter, int rospective, int egrity, char latan, char coal, int elligent, bool lean, char treuse, char ming, int uitive) | ||
{ | ||
i = 1; | ||
j = 1000; | ||
k = 25; | ||
return TRUE; | ||
} | ||
|
||
bool result; | ||
|
||
procedure do_nothing (void) | ||
{ | ||
} | ||
|
||
int i, j, k; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// *************************************************** | ||
// * CS460: Programming Assignment 5: Test Program 5 * | ||
// *************************************************** | ||
|
||
|
||
|
||
// ******************************************************************************************* | ||
// * The fizzbuzz procedure outputs one of the following responses: * | ||
// * * | ||
// * If counter is divisible by three without remainder, display "Fizz". * | ||
// * If counter is divisible by five without remainder, display "Buzz". * | ||
// * If counter is divisible by both three and five without a remainder, display "Fizzbuzz". * | ||
// * If counter is NOT divisible by three or five, display the counter. * | ||
// ******************************************************************************************* | ||
procedure fizzbuzz (int counter) | ||
{ | ||
int state; | ||
|
||
state = 0; | ||
if ((counter % 3) == 0) | ||
{ | ||
state = 1; | ||
} | ||
if ((counter % 5) == 0) | ||
{ | ||
state = state * 2 + 2; | ||
} | ||
if (state == 1) | ||
{ | ||
printf ("Fizz"); | ||
} | ||
else | ||
{ | ||
if (state == 2) | ||
{ | ||
printf ("Buzz"); | ||
} | ||
else | ||
{ | ||
if (state == 4) | ||
{ | ||
printf ("Fizzbuzz"); | ||
} | ||
else | ||
{ | ||
printf ("%d", counter); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
procedure main (void) | ||
{ | ||
int counter; | ||
|
||
counter = 1; | ||
while (counter <= 100) | ||
{ | ||
fizzbuzz (counter); | ||
counter = counter + 1; | ||
if (counter <= 100) | ||
{ | ||
printf (", "); | ||
} | ||
else | ||
{ | ||
printf ("\n"); | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "tree.h" | ||
|
||
// Prints the abstract syntax tree to the provided output stream | ||
void Tree::printTree(Token* head){ | ||
if (head == nullptr){ | ||
return; | ||
} | ||
if (head->getValue() == "{") { | ||
std::cout << "BEGIN BLOCK"; | ||
} else if (head->getValue() == "}") { | ||
std::cout << "END BLOCK"; | ||
} else if (head->getValue() == "procedure" || head->getValue() == "function") { | ||
std::cout << "DECLARATION"; | ||
} else if (contains(head->getValue())) { | ||
std::cout << "DECLARATION"; | ||
} else if (head->getType() == "IDENTIFIER") { | ||
if (head->getSibling() != nullptr && head->getSibling()->getValue() == "=") { | ||
std::cout << "ASSIGNMENT" << " ----> " << head->getValue(); | ||
// This needs to break out to handleAssignment(); | ||
// head = handleAssignment(); | ||
} | ||
} | ||
if (head->getSibling() != nullptr) { | ||
head = head->getSibling(); | ||
std::cout << " ----> "; | ||
} else if (head->getChild() != nullptr) { | ||
head = head->getChild(); | ||
std::cout << "\n|\n|\n|\n|\nv "; | ||
} | ||
return printTree(head); | ||
} | ||
|
||
bool contains(std::string type){ | ||
for (const auto& reserved : varTypes) { | ||
if (type == reserved) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef TREE_H | ||
#define TREE_H | ||
#include <iostream> | ||
#include "token.h" | ||
|
||
const std::vector<std::string> varTypes = {"int", "void", "char", "bool", "string", "short", "long"}; | ||
|
||
class Tree { | ||
private: | ||
void printTree(Token*); | ||
bool contains(std::string); | ||
public: | ||
Tree(Token* head) { printTree(head); } | ||
~Tree(); | ||
}; | ||
|
||
#endif // TREE_H |