Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Implementation of Programming Languages / Transpiler / src / LLVMVisitor / LLVMVisitor.cpp
diff options
context:
space:
mode:
Commitc42d553c7df6ae7073488cb971ea984932a86801 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-01-19 13:55:46 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-01-19 13:55:46 +0200
Files455f05ed4b0f375fa81011e64f603889b315f3e0 /Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp
uni-projects-c42d553c7df6ae7073488cb971ea984932a86801.tar
uni-projects-c42d553c7df6ae7073488cb971ea984932a86801.tar.gz
uni-projects-c42d553c7df6ae7073488cb971ea984932a86801.zip
Parentbfa6fffc85772c17219437ab99c0cc59c3056f88 (diff)
[IPL/T] Moved function associativity to a seperate function
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp
index 9ca6f8f..d0d8de3 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp
@@ -1,3 +1,4 @@
+#include <utility>
#include "LLVMVisitor.h"
LLVMVisitor::LLVMVisitor(const char* fileName) : outFile(std::ofstream(fileName)), reg() {
@@ -17,22 +18,30 @@ std::string LLVMVisitor::Visit(const Float& vfloat) {
return valRegister;
}
-std::string LLVMVisitor::f_add(const BuiltInFunction& bfun, unsigned i) {
+void f_add(std::string reg, std::string first, std::string second, std::ofstream& of) {
+ of << reg << " = add i32 " << first << ", " << second << "\n";
+}
+
+void f_sub(std::string reg, std::string first, std::string second, std::ofstream& of) {
+ of << reg << " = sub i32 " << first << ", " << second << "\n";
+}
+
+std::string LLVMVisitor::rightAssoc(BiFout f, const BuiltInFunction& bfun, unsigned i) {
std::string leftArg = bfun.get_args()[i]->Visit(*this);
std::string rightArg;
- if (bfun.get_args().size() - 2 == i) {
+ if (bfun.get_args().size() - 2 == i)
rightArg = bfun.get_args()[i+1]->Visit(*this);
- }
- else {
- rightArg = f_add(bfun, i+1);
- }
+ else
+ rightArg = rightAssoc(f, bfun, i+1);
+
std::string valRegister = *(reg++);
- outFile << valRegister << " = add i32 " << leftArg << ", " << rightArg << "\n";
+ f(valRegister, leftArg, rightArg, outFile);
return valRegister;
}
std::string LLVMVisitor::Visit(const BuiltInFunction& bfun) {
- if (bfun.get_name() == "+")
- return f_add(bfun);
+ if (bfun.get_name() == "+") return rightAssoc(f_add, bfun);
+ if (bfun.get_name() == "-") return rightAssoc(f_sub, bfun);
+
return "";
}