diff options
| Commit | 8f41c57ff12fa72693016b75fec63bb86ae6560f (patch) | |
|---|---|---|
| Author | 2023-01-18 11:51:57 +0200 | |
| Committer | 2023-01-18 11:51:57 +0200 | |
| Files | b308172d90c6d70fe8268a960a7a8ded8466d75d | |
| uni-projects-8f41c57ff12fa72693016b75fec63bb86ae6560f.tar uni-projects-8f41c57ff12fa72693016b75fec63bb86ae6560f.tar.gz uni-projects-8f41c57ff12fa72693016b75fec63bb86ae6560f.zip | ||
| Parent | efbc9d2a15ac6d21caf9cb87e7e913e35f48e391 (diff) | |
[IPL/T] Implemented initial structure with Integer data type
10 files changed, 134 insertions, 0 deletions
diff --git a/Year 2/Implementation of Programming Languages/Transpiler/README.md b/Year 2/Implementation of Programming Languages/Transpiler/README.md new file mode 100644 index 0000000..a002f71 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/README.md @@ -0,0 +1,16 @@ +# Transpiler + +My course project will be a transpiler from Common Lisp to LLVM IR + +## What will be implemented + +Types: + +1. Numbers + - Integers: fixnum only + - Floating-point: double only + - Complex: real and imaginary parts being floating-point +2. Characters: only standard and semi-standard characters +3. Symbols: no plist +4. Cons ; Lists +5. 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 new file mode 100644 index 0000000..f68b80d --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.cpp @@ -0,0 +1,12 @@ +#include "LLVMVisitor.h" + +LLVMVisitor::LLVMVisitor(const char* fileName) : outFile(std::ofstream(fileName)), reg() { + if (!outFile.is_open()) + throw std::runtime_error("Couldn't open file for writing!"); +} + +std::string LLVMVisitor::Visit(const Integer& vint) { + std::string valRegister = *(reg++); + outFile << valRegister + " = " + std::to_string(vint.get_value()) + "\n"; + 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 new file mode 100644 index 0000000..d52b5b3 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/LLVMVisitor.h @@ -0,0 +1,18 @@ +#ifndef LLVMVISITOR +#define LLVMVISITOR + +#include <fstream> +#include "Register.h" +#include "../Value/Integer.h" + +class LLVMVisitor { + std::ofstream outFile; + Register reg; + +public: + std::string Visit(const Integer& vint); + + LLVMVisitor(const char* fileName); +}; + +#endif 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 new file mode 100644 index 0000000..b2311d9 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.cpp @@ -0,0 +1,18 @@ +#include "Register.h" + +Register::Register() : counter(0) {} + +Register& Register::operator++() { + ++counter; + return *this; +} + +Register Register::operator++(int) { + Register old = *this; + ++(*this); + return old; +} + +std::string Register::operator*() { + return "%" + 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 new file mode 100644 index 0000000..70f0619 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/LLVMVisitor/Register.h @@ -0,0 +1,16 @@ +#ifndef LLVMVISITOR_REGISTER +#define LLVMVISITOR_REGISTER + +#include <string> + +class Register { + unsigned counter; + +public: + Register(); + Register& operator++(); + Register operator++(int); + std::string operator*(); +}; + +#endif 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 new file mode 100644 index 0000000..d7f70e3 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.cpp @@ -0,0 +1,12 @@ +#include "Integer.h" +#include "../LLVMVisitor/LLVMVisitor.h" + +std::string Integer::Visit(LLVMVisitor& visitor) const { + return visitor.Visit(*this); +} + +const int& Integer::get_value() const { + return this->value; +} + +Integer::Integer(int value) : value(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 new file mode 100644 index 0000000..3859d1d --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Integer.h @@ -0,0 +1,16 @@ +#ifndef VALUE_INTEGER +#define VALUE_INTEGER + +#include "Value.h" + +class Integer : public Value { + int value; +public: + std::string Visit(LLVMVisitor& visitor) const override; + const int& get_value() const; + + Integer(int value); + ~Integer() override = default; +}; + +#endif 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 new file mode 100644 index 0000000..8488262 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/Value/Value.h @@ -0,0 +1,14 @@ +#ifndef VALUE +#define VALUE + +#include <string> + +class LLVMVisitor; + +class Value { +public: + virtual std::string Visit(LLVMVisitor& visitor) const = 0; + virtual ~Value() {}; +}; + +#endif diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp b/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp new file mode 100644 index 0000000..738f18b --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/main.cpp @@ -0,0 +1,11 @@ +#include <iostream> +#include <deque> +#include "LLVMVisitor/LLVMVisitor.h" + +int main() { + LLVMVisitor visitor("testout.ll"); + std::deque<Value*> nodes = { new Integer(123) }; + for (Value* n : nodes) { + n->Visit(visitor); + } +} diff --git a/Year 2/Implementation of Programming Languages/Transpiler/src/testout.ll b/Year 2/Implementation of Programming Languages/Transpiler/src/testout.ll new file mode 100644 index 0000000..c3f0a47 --- /dev/null +++ b/Year 2/Implementation of Programming Languages/Transpiler/src/testout.ll @@ -0,0 +1 @@ +%0 = 123 |
