diff options
[IPL/T] Values now have a return value which can be parsed easily
4 files changed, 26 insertions, 12 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 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; } 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 6229b7a..55cc86e 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 @@ -9,9 +9,10 @@ class LLVMVisitor { std::ofstream outFile; - Register reg; + Register greg; + Register lreg; - typedef void (&BiFout)(std::string reg, std::string first, std::string second, std::ofstream& of); + typedef void (&BiFout)(std::string reg, Value::T_Primitive ty, std::string first, std::string second, std::ofstream& of); std::string rightAssoc(BiFout f, const BuiltInFunction& bfun, unsigned i = 0); public: diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.cpp index f352295..873d4e0 100644 --- a/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.cpp +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.cpp @@ -1,6 +1,6 @@ #include "Register.h" -Register::Register() : counter(1) {} +Register::Register(std::string prepend) : prepend(prepend), counter(1) {} Register& Register::operator++() { ++counter; @@ -14,5 +14,5 @@ Register Register::operator++(int) { } std::string Register::operator*() { - return "%" + std::to_string(counter); + return prepend + std::to_string(counter); } diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.h b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.h index 70f0619..768cfbc 100644 --- a/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.h +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.h @@ -4,10 +4,11 @@ #include <string> class Register { + std::string prepend; unsigned counter; public: - Register(); + Register(std::string prepend); Register& operator++(); Register operator++(int); std::string operator*(); |
