Summary Files Commits Branches @commit @diff @stats
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
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;
}
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*();
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.cpp
index a286fa2..91268d7 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.cpp
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.cpp
@@ -5,6 +5,10 @@ std::string Float::Visit(LLVMVisitor& visitor) const {
return visitor.Visit(*this);
}
+Value::T_Primitive Float::RetType() const {
+ return Value::T_float;
+}
+
const double& Float::get_value() const {
return this->value;
}
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.h b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.h
index 22cd721..0f47be1 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.h
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.h
@@ -7,6 +7,7 @@ class Float : public Value {
double value;
public:
std::string Visit(LLVMVisitor& visitor) const override;
+ T_Primitive RetType() const override;
const double& get_value() const;
Float(double value);
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.cpp
index 6d38aad..954c8da 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.cpp
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.cpp
@@ -5,4 +5,10 @@ std::string BuiltInFunction::Visit(LLVMVisitor& visitor) const {
return visitor.Visit(*this);
}
+Value::T_Primitive BuiltInFunction::RetType() const {
+ if (args.empty())
+ return T_void;
+ return args.front()->RetType();
+}
+
BuiltInFunction::BuiltInFunction(std::string name, std::deque<Value*> args) : Function(name, args) {}
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.h b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.h
index 26fba3f..c4ea301 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.h
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.h
@@ -6,6 +6,7 @@
class BuiltInFunction : public Function {
public:
std::string Visit(LLVMVisitor& visitor) const override;
+ T_Primitive RetType() const override;
BuiltInFunction(std::string name, std::deque<Value*> args);
};
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.h b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.h
index 549d15e..34e3ea1 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.h
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.h
@@ -6,6 +6,7 @@
#include "../Value.h"
class Function : public Value {
+protected:
std::string name;
std::deque<Value*> args;
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.cpp
index d7f70e3..01bbe54 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.cpp
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.cpp
@@ -5,6 +5,10 @@ std::string Integer::Visit(LLVMVisitor& visitor) const {
return visitor.Visit(*this);
}
+Value::T_Primitive Integer::RetType() const {
+ return Value::T_integer;
+}
+
const int& Integer::get_value() const {
return this->value;
}
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.h b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.h
index 3859d1d..4a84c31 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.h
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.h
@@ -7,6 +7,7 @@ class Integer : public Value {
int value;
public:
std::string Visit(LLVMVisitor& visitor) const override;
+ T_Primitive RetType() const override;
const int& get_value() const;
Integer(int value);
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Value.h b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Value.h
index 8488262..0147515 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Value.h
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Value.h
@@ -7,7 +7,13 @@ class LLVMVisitor;
class Value {
public:
+ enum T_Primitive {
+ T_void,
+ T_integer,
+ T_float,
+ };
virtual std::string Visit(LLVMVisitor& visitor) const = 0;
+ virtual T_Primitive RetType() const = 0;
virtual ~Value() {};
};
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp
index 7f829d3..7fc9bcf 100644
--- a/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp
+++ b/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp
@@ -7,10 +7,10 @@ int main() {
LLVMVisitor visitor("testout.ll");
std::deque<Value*> nodes = {
new BuiltInFunction("+", {
- new Integer(1),
- new Integer(2),
- new BuiltInFunction("-", { new Integer(5), new Integer(10) }),
- new Integer(123) }) };
+ new Float(1),
+ new Float(2),
+ new BuiltInFunction("-", { new Float(5), new Float(10) }),
+ new Float(123) }) };
for (Value* n : nodes) {
n->Visit(visitor);
}