Skip to content

Commit

Permalink
Tweaked it into working on CLANG on Windows again (VS still not).
Browse files Browse the repository at this point in the history
  • Loading branch information
FlatAssembler committed Aug 19, 2021
1 parent 19edac8 commit 8e70e2f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
5 changes: 3 additions & 2 deletions TreeNode.cpp
Expand Up @@ -437,8 +437,9 @@ class TreeNode {
static std::vector<TreeNode>
parseVariableDeclaration(std::vector<TreeNode> 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
};
Expand Down
9 changes: 6 additions & 3 deletions TreeRootNode.cpp
Expand Up @@ -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);
Expand Down
35 changes: 20 additions & 15 deletions compiler.cpp
Expand Up @@ -12,8 +12,12 @@
#include "semanticAnalyzer.cpp"
#include <ciso646> // 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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() == '[') {
Expand All @@ -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))
Expand All @@ -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() == '[') {
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion semanticAnalyzer.cpp
Expand Up @@ -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)
Expand Down

0 comments on commit 8e70e2f

Please sign in to comment.