diff --git a/TreeNode.cpp b/TreeNode.cpp index e95751b..91b65c3 100644 --- a/TreeNode.cpp +++ b/TreeNode.cpp @@ -437,8 +437,9 @@ class TreeNode { static std::vector parseVariableDeclaration(std::vector input); virtual AssemblyCode compile(CompilationContext context) const; - virtual AssemblyCode compileAPointer(CompilationContext context) const; - virtual std::string getType(CompilationContext context) const; // Integer32... + virtual AssemblyCode compileAPointer(const CompilationContext &context) const; + virtual std::string + getType(const CompilationContext &context) const; // Integer32... virtual ~TreeNode() = default; // https://discord.com/channels/172018499005317120/172018499005317120/809830734256406569 }; diff --git a/TreeRootNode.cpp b/TreeRootNode.cpp index 6d26232..9cad705 100644 --- a/TreeRootNode.cpp +++ b/TreeRootNode.cpp @@ -930,10 +930,13 @@ In the meantime, you can try modifying your program to use ")" "()"); // So that the compiler doesn't throw a bunch of warnings about // the control reaching the end of a non-void function. } - AssemblyCode compileAPointer(CompilationContext context) const override { - context.stackSizeOfThisFunction = 0; + AssemblyCode + compileAPointer(const CompilationContext &context) const override { std::cerr << "Internal compiler error: Some part of the compiler attempted " - "to get the assembly of the pointer of a module, which " + "to get the assembly of the pointer of a module, when " + "compiling a function named\"" + << context.currentFunctionName + << "\", which " "doesn't make sense. Quitting now!" << std::endl; exit(1); diff --git a/compiler.cpp b/compiler.cpp index e2d29b5..1c8ad3c 100644 --- a/compiler.cpp +++ b/compiler.cpp @@ -12,8 +12,12 @@ #include "semanticAnalyzer.cpp" #include // Necessary for Microsoft C++ Compiler. -AssemblyCode convertToInteger32(const TreeNode node, - const CompilationContext context) { +AssemblyCode convertToInteger32( + const TreeNode & + node, // HappySkeptic suggested me to use constant references to avoid + // Stack Overflow: + // https://atheistforums.org/thread-63150-post-2054368.html#pid2054368 + const CompilationContext &context) { auto originalCode = node.compile(context); const AssemblyCode::AssemblyType i32 = AssemblyCode::AssemblyType::i32, i64 = AssemblyCode::AssemblyType::i64, @@ -54,8 +58,8 @@ AssemblyCode convertToInteger32(const TreeNode node, return AssemblyCode("()"); } -AssemblyCode convertToInteger64(const TreeNode node, - const CompilationContext context) { +AssemblyCode convertToInteger64(const TreeNode &node, + const CompilationContext &context) { auto originalCode = node.compile(context); const AssemblyCode::AssemblyType i32 = AssemblyCode::AssemblyType::i32, i64 = AssemblyCode::AssemblyType::i64, @@ -99,8 +103,8 @@ AssemblyCode convertToInteger64(const TreeNode node, return AssemblyCode("()"); } -AssemblyCode convertToDecimal32(const TreeNode node, - const CompilationContext context) { +AssemblyCode convertToDecimal32(const TreeNode &node, + const CompilationContext &context) { auto originalCode = node.compile(context); const AssemblyCode::AssemblyType i32 = AssemblyCode::AssemblyType::i32, i64 = AssemblyCode::AssemblyType::i64, @@ -144,8 +148,8 @@ AssemblyCode convertToDecimal32(const TreeNode node, return AssemblyCode("()"); } -AssemblyCode convertToDecimal64(const TreeNode node, - const CompilationContext context) { +AssemblyCode convertToDecimal64(const TreeNode &node, + const CompilationContext &context) { auto originalCode = node.compile(context); const AssemblyCode::AssemblyType i32 = AssemblyCode::AssemblyType::i32, i64 = AssemblyCode::AssemblyType::i64, @@ -185,8 +189,8 @@ AssemblyCode convertToDecimal64(const TreeNode node, return AssemblyCode("()"); } -AssemblyCode convertTo(const TreeNode node, const std::string type, - const CompilationContext context) { +AssemblyCode convertTo(const TreeNode &node, const std::string &type, + const CompilationContext &context) { if (type == "Character" or type == "Integer16" or type == "Integer32" or isPointerType(type)) // When, in JavaScript Virtual Machine, you can't // push types of less than 4 bytes (32 bits) onto @@ -1287,13 +1291,14 @@ AssemblyCode TreeNode::compile(CompilationContext context) const { return AssemblyCode(assembly, returnType); } -AssemblyCode TreeNode::compileAPointer(CompilationContext context) const { +AssemblyCode +TreeNode::compileAPointer(const CompilationContext &context) const { if (text == "ValueAt(") return children[0].compile(context); if (context.localVariables.count(text) and text.back() != '[') return AssemblyCode( "(i32.sub\n\t(global.get $stack_pointer)\n\t(i32.const " + - std::to_string(context.localVariables[text]) + ") ;;" + text + + std::to_string(context.localVariables.at(text)) + ") ;;" + text + "\n)", AssemblyCode::AssemblyType::i32); if (context.localVariables.count(text) and text.back() == '[') { @@ -1309,7 +1314,7 @@ AssemblyCode TreeNode::compileAPointer(CompilationContext context) const { return AssemblyCode( "(i32.add\n\t(i32.sub\n\t\t(global.get " "$stack_pointer)\n\t\t(i32.const " + - std::to_string(context.localVariables[text]) + ") ;;" + text + + std::to_string(context.localVariables.at(text)) + ") ;;" + text + "\n\t)\n\t(i32.mul\n\t\t(i32.const " + std::to_string(basicDataTypeSizes.count(getType(context)) ? basicDataTypeSizes.at(getType(context)) @@ -1321,7 +1326,7 @@ AssemblyCode TreeNode::compileAPointer(CompilationContext context) const { } if (context.globalVariables.count(text) and text.back() != '[') return AssemblyCode("(i32.const " + - std::to_string(context.globalVariables[text]) + + std::to_string(context.globalVariables.at(text)) + ") ;;" + text, AssemblyCode::AssemblyType::i32); if (context.globalVariables.count(text) and text.back() == '[') { @@ -1336,7 +1341,7 @@ AssemblyCode TreeNode::compileAPointer(CompilationContext context) const { } return AssemblyCode( "(i32.add\n\t(i32.const " + - std::to_string(context.globalVariables[text]) + ") ;;" + text + + std::to_string(context.globalVariables.at(text)) + ") ;;" + text + "\n\t(i32.mul\n\t\t(i32.const " + std::to_string(basicDataTypeSizes.count(getType(context)) ? basicDataTypeSizes.at(getType(context)) diff --git a/semanticAnalyzer.cpp b/semanticAnalyzer.cpp index 3ac6966..fc7b931 100644 --- a/semanticAnalyzer.cpp +++ b/semanticAnalyzer.cpp @@ -41,7 +41,7 @@ std::string getStrongerType(const int lineNumber, const int columnNumber, return firstType; } -std::string TreeNode::getType(const CompilationContext context) const { +std::string TreeNode::getType(const CompilationContext &context) const { if (text == "nan") // Not-a-number, to signal a numeric error. return "Decimal32"; if (text == "asm(" and children.size() == 1)