Skip to content

Commit

Permalink
Attempted to fix the Stack Overflow error using static fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
FlatAssembler committed Aug 19, 2021
1 parent e057114 commit 19edac8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
20 changes: 15 additions & 5 deletions TreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ int Levenstein_distance(std::string A, std::string B) {
#define USING_LEVENSTEIN_DISTANCE

class TreeNode {
enum Associativity { left, right };
enum class Associativity { left, right };
static std::vector<TreeNode>
applyBinaryOperators(std::vector<TreeNode> input,
std::vector<std::string> operators,
Expand Down Expand Up @@ -178,12 +178,15 @@ class TreeNode {
}

public:
std::map<std::string, int> basicDataTypeSizes;
std::map<std::string, AssemblyCode::AssemblyType>
// HappySkeptic suggested me to try to make shared parts of TreeNode static to
// save on stack memory:
// https://atheistforums.org/thread-63150-post-2053689.html#pid2053689
static std::map<std::string, int> basicDataTypeSizes;
static std::map<std::string, AssemblyCode::AssemblyType>
mappingOfAECTypesToWebAssemblyTypes;
std::map<AssemblyCode::AssemblyType, std::string>
static std::map<AssemblyCode::AssemblyType, std::string>
stringRepresentationOfWebAssemblyType;
std::set<std::string> AECkeywords;
static std::set<std::string> AECkeywords;
std::vector<TreeNode> children;
std::string text;
int lineNumber, columnNumber;
Expand Down Expand Up @@ -475,3 +478,10 @@ std::string convertInlineAssemblyToAssembly(TreeNode inlineAssemblyNode) {
inlineAssembly = temporaryString;
return inlineAssembly;
}

std::map<std::string, int> TreeNode::basicDataTypeSizes;
std::map<std::string, AssemblyCode::AssemblyType>
TreeNode::mappingOfAECTypesToWebAssemblyTypes;
std::map<AssemblyCode::AssemblyType, std::string>
TreeNode::stringRepresentationOfWebAssemblyType;
std::set<std::string> TreeNode::AECkeywords;
17 changes: 14 additions & 3 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <algorithm>
#include <ciso646> // Necessary for Microsoft C++ Compiler.

// TODO: Refactor the parser so that it is more legible. Perhaps we can start by
// moving functions that deal with the details, such as "applyBinaryOperators",
// to the end instead of to beginning.

std::vector<TreeNode>
TreeNode::applyBinaryOperators(std::vector<TreeNode> input,
std::vector<std::string> operators,
Expand Down Expand Up @@ -60,14 +64,14 @@ TreeNode::applyBinaryOperators(std::vector<TreeNode> input,
input[i].children.push_back(input.at(i + 1));
input.erase(input.begin() + i + 1);
input.erase(input.begin() + i - 1);
i += associativity == left ? -1 : 0;
i += associativity == Associativity::left ? -1 : 0;
}
};
// https://discord.com/channels/172018499005317120/258361499519549440/811664251789639740
if (associativity == left)
if (associativity == Associativity::left)
for (int i = 0; i < int(input.size()); i++)
loop_body(i);
else if (associativity == right)
else if (associativity == Associativity::right)
for (int i = input.size() - 1; i >= 0; i--)
loop_body(i);
else {
Expand Down Expand Up @@ -893,6 +897,13 @@ std::vector<TreeNode> TreeNode::parse(std::vector<TreeNode> input) {
}
auto iteratorPointingToTheEndStructureToken =
std::find_if(input.begin() + i + 1, input.end(), [](TreeNode node) {
if (node.text == "Structure")
std::cerr << "Line " << node.lineNumber << ", Column "
<< node.columnNumber
<< ", Parser error: The parser does not yet support "
"nested structures, so the syntax tree given to the "
"semantic analyzer will probably be corrupt!"
<< std::endl;
return node.text == "EndStructure";
}); // TODO: Deal with nested structures. As it is now,
// "iteratorPointingToTheEndStructureToken" will point to an
Expand Down
4 changes: 3 additions & 1 deletion semanticAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ std::string TreeNode::getType(const CompilationContext context) const {
"Line " + std::to_string(child.lineNumber) + ", Column " +
std::to_string(child.columnNumber) +
": Sorry about that, but this compiler does "
"not yet support named function arguments!");
"not yet support named function arguments!"); // TODO: Implement
// named function
// arguments.
return potentialFunction->returnType;
}
if (text.back() == '(' and
Expand Down

0 comments on commit 19edac8

Please sign in to comment.