diff options
[IPL/T] Moved function associativity to a seperate function
1 files changed, 18 insertions, 9 deletions
| -rw-r--r-- | Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp | 27 |
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 ""; } |
