Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Implementation of Programming Languages / Transpiler / src / LLVMVisitor / LLVMVisitor.cpp
diff options
context:
space:
mode:
Commitd8eb50a7960edd35c74885d0ba7897f35c1b1eed (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-01-19 17:43:29 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-01-19 17:43:29 +0200
Files2de79b2847c730bd1bfbb0f6e02ef57aac1ada40 /Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp
uni-projects-d8eb50a7960edd35c74885d0ba7897f35c1b1eed.tar
uni-projects-d8eb50a7960edd35c74885d0ba7897f35c1b1eed.tar.gz
uni-projects-d8eb50a7960edd35c74885d0ba7897f35c1b1eed.zip
Parent07dfcf732fa7d88c196ad158ffa0326f757156b9 (diff)
[IPL/T] Values now have a return value which can be parsed easily
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;
}