diff --git a/code/compiler/03/ast.cpp b/code/compiler/03/ast.cpp new file mode 100644 index 0000000..734a54a --- /dev/null +++ b/code/compiler/03/ast.cpp @@ -0,0 +1 @@ +#include "ast.hpp" diff --git a/code/compiler/03/ast.hpp b/code/compiler/03/ast.hpp index 70c2af9..e1629cd 100644 --- a/code/compiler/03/ast.hpp +++ b/code/compiler/03/ast.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include "type.hpp" struct ast { virtual ~ast() = default; diff --git a/code/compiler/03/type.cpp b/code/compiler/03/type.cpp new file mode 100644 index 0000000..206a9aa --- /dev/null +++ b/code/compiler/03/type.cpp @@ -0,0 +1,25 @@ +#include "type.hpp" +#include +#include + +std::string type_mgr::new_type_name() { + std::ostringstream oss; + int temp = last_id++; + + do { + oss << (char) ('a' + (temp % 26)); + temp /= 26; + } while(temp); + std::string str = oss.str(); + + std::reverse(str.begin(), str.end()); + return str; +}; + +type_ptr type_mgr::new_type() { + return type_ptr(new type_var(new_type_name())); +} + +type_ptr type_mgr::new_arrow_type() { + return type_ptr(new type_arr(new_type(), new_type())); +} diff --git a/code/compiler/03/type.hpp b/code/compiler/03/type.hpp new file mode 100644 index 0000000..752d778 --- /dev/null +++ b/code/compiler/03/type.hpp @@ -0,0 +1,39 @@ +#pragma once +#include +#include + +struct type { + virtual ~type() = default; +}; + +using type_ptr = std::shared_ptr; + +struct type_var : public type { + std::string name; + + type_var(std::string n) + : name(std::move(n)) {} +}; + +struct type_id : public type { + int id; + + type_id(int i) + : id(i) {} +}; + +struct type_arr : public type { + type_ptr left; + type_ptr right; + + type_arr(type_ptr l, type_ptr r) + : left(std::move(l)), right(std::move(r)) {} +}; + +struct type_mgr { + int last_id = 0; + + std::string new_type_name(); + type_ptr new_type(); + type_ptr new_arrow_type(); +};