diff options
| Commit | c42d553c7df6ae7073488cb971ea984932a86801 (patch) | |
|---|---|---|
| Author | 2023-01-19 13:55:46 +0200 | |
| Committer | 2023-01-19 13:55:46 +0200 | |
| Files | 455f05ed4b0f375fa81011e64f603889b315f3e0 | |
| uni-projects-c42d553c7df6ae7073488cb971ea984932a86801.tar uni-projects-c42d553c7df6ae7073488cb971ea984932a86801.tar.gz uni-projects-c42d553c7df6ae7073488cb971ea984932a86801.zip | ||
| Parent | bfa6fffc85772c17219437ab99c0cc59c3056f88 (diff) | |
[IPL/T] Moved function associativity to a seperate function
3 files changed, 25 insertions, 11 deletions
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 ""; } diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.h b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.h index 78719da..f8fea5e 100644 --- a/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.h +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.h @@ -11,7 +11,8 @@ class LLVMVisitor { std::ofstream outFile; Register reg; - std::string f_add(const BuiltInFunction& bfun, unsigned i = 0); + typedef void (&BiFout)(std::string reg, std::string first, std::string second, std::ofstream& of); + std::string rightAssoc(BiFout f, const BuiltInFunction& bfun, unsigned i = 0); public: std::string Visit(const Integer& vint); diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp index fefe544..a154c56 100644 --- a/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp @@ -1,12 +1,16 @@ #include <iostream> #include <deque> #include "LLVMVisitor/LLVMVisitor.h" +#include "Value/Function/BuiltInFunction.h" int main() { LLVMVisitor visitor("testout.ll"); std::deque<Value*> nodes = { new BuiltInFunction("+", { - new Integer(1), new Integer(2), new Integer(123) }) }; + new Integer(1), + new Integer(2), + new BuiltInFunction("-", { new Integer(5), new Integer(10) }), + new Integer(123) }) }; for (Value* n : nodes) { n->Visit(visitor); } |
