From 06006fc601ab5f7bffa095b7279a61070e38ba54 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 19 Jan 2023 11:27:31 +0200 Subject: [IPL/T] Implemented Float type and added basic function structure as well as added addition function for two arguments --- .../Transpiler/src/LLVMVisitor/LLVMVisitor.cpp | 20 ++++++++++++++++++++ .../Transpiler/src/LLVMVisitor/LLVMVisitor.h | 6 ++++++ .../Transpiler/src/Value/Casting.h | 0 .../Transpiler/src/Value/Float.cpp | 12 ++++++++++++ .../Transpiler/src/Value/Float.h | 16 ++++++++++++++++ .../src/Value/Function/BuiltInFunction.cpp | 8 ++++++++ .../Transpiler/src/Value/Function/BuiltInFunction.h | 12 ++++++++++++ .../Transpiler/src/Value/Function/Function.cpp | 12 ++++++++++++ .../Transpiler/src/Value/Function/Function.h | 20 ++++++++++++++++++++ .../Transpiler/src/Value/Function/UserFunction.h | 0 .../Transpiler/src/main.cpp | 2 +- 11 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Year 2/Implementation of Programming Languages/Transpiler/src/Value/Casting.h create mode 100644 Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.cpp create mode 100644 Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.h create mode 100644 Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.cpp create mode 100644 Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.h create mode 100644 Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.cpp create mode 100644 Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.h create mode 100644 Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/UserFunction.h (limited to 'Year 2/Implementation of Programming Languages/Transpiler/src') 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 f68b80d..d3eea2d 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 @@ -10,3 +10,23 @@ std::string LLVMVisitor::Visit(const Integer& vint) { outFile << valRegister + " = " + std::to_string(vint.get_value()) + "\n"; return valRegister; } + +std::string LLVMVisitor::Visit(const Float& vfloat) { + std::string valRegister = *(reg++); + outFile << valRegister + " = " + std::to_string(vfloat.get_value()) + "\n"; + return valRegister; +} + +std::string LLVMVisitor::f_add(const BuiltInFunction& bfun) { + std::string leftArg = bfun.get_args()[0]->Visit(*this); + std::string rightArg = bfun.get_args()[1]->Visit(*this); + std::string valRegister = *(reg++); + outFile << valRegister << " = add i32 " << leftArg << ", " << rightArg << "\n"; + return valRegister; +} + +std::string LLVMVisitor::Visit(const BuiltInFunction& bfun) { + if (bfun.get_name() == "+") + return f_add(bfun); + return ""; +} 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 d52b5b3..8b49cba 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 @@ -4,13 +4,19 @@ #include #include "Register.h" #include "../Value/Integer.h" +#include "../Value/Float.h" +#include "../Value/Function/BuiltInFunction.h" class LLVMVisitor { std::ofstream outFile; Register reg; + std::string f_add(const BuiltInFunction& bfun); + public: std::string Visit(const Integer& vint); + std::string Visit(const Float& vfloat); + std::string Visit(const BuiltInFunction& bfun); LLVMVisitor(const char* fileName); }; diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Casting.h b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Casting.h new file mode 100644 index 0000000..e69de29 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 new file mode 100644 index 0000000..a286fa2 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.cpp @@ -0,0 +1,12 @@ +#include "Float.h" +#include "../LLVMVisitor/LLVMVisitor.h" + +std::string Float::Visit(LLVMVisitor& visitor) const { + return visitor.Visit(*this); +} + +const double& Float::get_value() const { + return this->value; +} + +Float::Float(double value) : value(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 new file mode 100644 index 0000000..22cd721 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Float.h @@ -0,0 +1,16 @@ +#ifndef VALUE_FLOAT +#define VALUE_FLOAT + +#include "Value.h" + +class Float : public Value { + double value; +public: + std::string Visit(LLVMVisitor& visitor) const override; + const double& get_value() const; + + Float(double value); + ~Float() override = default; +}; + +#endif 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 new file mode 100644 index 0000000..6d38aad --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.cpp @@ -0,0 +1,8 @@ +#include "BuiltInFunction.h" +#include "../../LLVMVisitor/LLVMVisitor.h" + +std::string BuiltInFunction::Visit(LLVMVisitor& visitor) const { + return visitor.Visit(*this); +} + +BuiltInFunction::BuiltInFunction(std::string name, std::deque 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 new file mode 100644 index 0000000..26fba3f --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/BuiltInFunction.h @@ -0,0 +1,12 @@ +#ifndef VALUE_ADDFUNCTION +#define VALUE_ADDFUNCTION + +#include "Function.h" + +class BuiltInFunction : public Function { +public: + std::string Visit(LLVMVisitor& visitor) const override; + BuiltInFunction(std::string name, std::deque args); +}; + +#endif diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.cpp new file mode 100644 index 0000000..9e35eab --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.cpp @@ -0,0 +1,12 @@ +#include "Function.h" +#include "../../LLVMVisitor/LLVMVisitor.h" + +const std::string& Function::get_name() const { + return name; +} + +const std::deque Function::get_args() const { + return args; +} + +Function::Function(std::string name, std::deque args) : name(name), args(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 new file mode 100644 index 0000000..549d15e --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/Function.h @@ -0,0 +1,20 @@ +#ifndef VALUE_FUNCTION +#define VALUE_FUNCTION + +#include +#include +#include "../Value.h" + +class Function : public Value { + std::string name; + std::deque args; + +public: + const std::string& get_name() const; + const std::deque get_args() const; + + Function(std::string name, std::deque args); + ~Function() override = default; +}; + +#endif diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/UserFunction.h b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Function/UserFunction.h new file mode 100644 index 0000000..e69de29 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 738f18b..088f4d0 100644 --- a/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp @@ -4,7 +4,7 @@ int main() { LLVMVisitor visitor("testout.ll"); - std::deque nodes = { new Integer(123) }; + std::deque nodes = { new BuiltInFunction("+", { new Integer(123), new Integer(567) }) }; for (Value* n : nodes) { n->Visit(visitor); } -- cgit v1.3.1-26-gb76a