diff options
[IPL/T] Added current progress of implementing functions
1 files changed, 23 insertions, 2 deletions
| -rw-r--r-- | Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp | 25 |
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 9727a85..1fce314 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,14 +1,15 @@ #include <utility> +#include <vector> #include "LLVMVisitor.h" 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"; + // outFile << "define i32 @main() {\n"; } void LLVMVisitor::finalizeFile() { - outFile << "ret i32 0\n}\n"; + // outFile << "ret i32 0\n}\n"; } std::string LLVMVisitor::Visit(const Integer& vint) { @@ -19,6 +20,26 @@ std::string LLVMVisitor::Visit(const Float& vfloat) { return std::to_string(vfloat.get_value()); } +std::string LLVMVisitor::Visit(const Name& vname) { + return vname.get_value(); +} + +std::string LLVMVisitor::Visit(const Funcall& vfuncall) { + if (std::find(gfuncs.begin(), gfuncs.end(), vfuncall) == gfuncs.end()) + throw std::runtime_error("Function doesn't exist!"); + + std::vector<std::string> pargs; + for (Value* arg : vfuncall.get_args()) + pargs.push_back(arg->Visit(*this)); + + std::string valReg = *(lreg++); + outFile << valReg << " = call i32 " << vfuncall.get_funname() << "("; + for (std::string arg : pargs) + outFile << "i32 " << arg << ", "; + outFile << ")\n"; + return valReg; +} + void f_add(std::string reg, Value::T_Primitive ty, std::string first, std::string second, std::ofstream& of) { of << reg << " = "; switch(ty) { |
