diff options
[IPL/T] Values now have a return value which can be parsed easily
1 files changed, 19 insertions, 7 deletions
| -rw-r--r-- | Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp | 26 |
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 c12bd09..9727a85 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,7 +1,7 @@ #include <utility> #include "LLVMVisitor.h" -LLVMVisitor::LLVMVisitor(const char* fileName) : outFile(std::ofstream(fileName)), reg() { +LLVMVisitor::LLVMVisitor(const char* fileName) : outFile(std::ofstream(fileName)), greg("@v"), lreg("%v") { if (!outFile.is_open()) throw std::runtime_error("Couldn't open file for writing!"); outFile << "define i32 @main() {\n"; @@ -19,12 +19,24 @@ std::string LLVMVisitor::Visit(const Float& vfloat) { return std::to_string(vfloat.get_value()); } -void f_add(std::string reg, std::string first, std::string second, std::ofstream& of) { - of << reg << " = add i32 " << first << ", " << second << "\n"; +void f_add(std::string reg, Value::T_Primitive ty, std::string first, std::string second, std::ofstream& of) { + of << reg << " = "; + switch(ty) { + case Value::T_integer: of << "add i32"; break; + case Value::T_float: of << "fadd double"; break; + default: throw std::runtime_error("Invalid type for function!"); + } + of << " " << 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"; +void f_sub(std::string reg, Value::T_Primitive ret, std::string first, std::string second, std::ofstream& of) { + of << reg << " = "; + switch(ret) { + case Value::T_integer: of << "sub i32"; break; + case Value::T_float: of << "fsub double"; break; + default: throw std::runtime_error("Invalid type for function!"); + } + of << " " << first << ", " << second << "\n"; } std::string LLVMVisitor::rightAssoc(BiFout f, const BuiltInFunction& bfun, unsigned i) { @@ -35,8 +47,8 @@ std::string LLVMVisitor::rightAssoc(BiFout f, const BuiltInFunction& bfun, unsig else rightArg = rightAssoc(f, bfun, i+1); - std::string valRegister = *(reg++); - f(valRegister, leftArg, rightArg, outFile); + std::string valRegister = *(lreg++); + f(valRegister, bfun.RetType(), leftArg, rightArg, outFile); return valRegister; } |
