mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-06 06:58:46 +00:00
type node added (for type deduction)
This commit is contained in:
parent
a7c1e3f658
commit
8bce645431
6 changed files with 92 additions and 248 deletions
|
|
@ -43,7 +43,6 @@ set(
|
||||||
|
|
||||||
add_executable(lang src/main.cpp
|
add_executable(lang src/main.cpp
|
||||||
src/name_tree.cpp
|
src/name_tree.cpp
|
||||||
src/types.cpp
|
|
||||||
|
|
||||||
${NODES}
|
${NODES}
|
||||||
${BUILDERS}
|
${BUILDERS}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,22 @@
|
||||||
|
|
||||||
namespace nodes {
|
namespace nodes {
|
||||||
|
|
||||||
|
class TypedNode : public Node {
|
||||||
|
public:
|
||||||
|
TypedNode(Node node) : Node(node) {}
|
||||||
|
|
||||||
|
void set_expression_type(TypeProxy expression_type) {
|
||||||
|
expression_type_ = expression_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<TypeProxy> get_expression_type() const {
|
||||||
|
return expression_type_;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::optional<TypeProxy> expression_type_;
|
||||||
|
};
|
||||||
|
|
||||||
class Expression;
|
class Expression;
|
||||||
class ExpressionStorage;
|
class ExpressionStorage;
|
||||||
|
|
||||||
|
|
@ -53,9 +69,9 @@ proxy_to_expr_optional(const std::optional<nodes::ExpressionProxy> &proxy) {
|
||||||
|
|
||||||
// --- flow control
|
// --- flow control
|
||||||
|
|
||||||
class Match : public Node {
|
class Match : public TypedNode {
|
||||||
public:
|
public:
|
||||||
class Case : public Node {
|
class Case : public TypedNode {
|
||||||
public:
|
public:
|
||||||
enum CaseType {
|
enum CaseType {
|
||||||
PATTERN_VALUE,
|
PATTERN_VALUE,
|
||||||
|
|
@ -65,7 +81,7 @@ public:
|
||||||
Case(Node node, CaseType case_type, ExpressionProxy value,
|
Case(Node node, CaseType case_type, ExpressionProxy value,
|
||||||
std::optional<ExpressionProxy> condition = std::nullopt,
|
std::optional<ExpressionProxy> condition = std::nullopt,
|
||||||
std::optional<ExpressionProxy> expression = std::nullopt)
|
std::optional<ExpressionProxy> expression = std::nullopt)
|
||||||
: Node(node), case_type_(case_type), value_(value),
|
: TypedNode(node), case_type_(case_type), value_(value),
|
||||||
condition_(condition), expression_(expression) {}
|
condition_(condition), expression_(expression) {}
|
||||||
|
|
||||||
CaseType case_type() const { return case_type_; }
|
CaseType case_type() const { return case_type_; }
|
||||||
|
|
@ -98,10 +114,10 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
Match(Node node, ExpressionProxy value, std::vector<Case> &&cases)
|
Match(Node node, ExpressionProxy value, std::vector<Case> &&cases)
|
||||||
: Node(node), value_(value), cases_(std::move(cases)) {}
|
: TypedNode(node), value_(value), cases_(std::move(cases)) {}
|
||||||
|
|
||||||
Match(Node node, ExpressionProxy value, const std::vector<Case> &cases)
|
Match(Node node, ExpressionProxy value, const std::vector<Case> &cases)
|
||||||
: Node(node), value_(value), cases_(cases) {}
|
: TypedNode(node), value_(value), cases_(cases) {}
|
||||||
|
|
||||||
Expression *get_value() { return value_.get(); }
|
Expression *get_value() { return value_.get(); }
|
||||||
|
|
||||||
|
|
@ -154,7 +170,7 @@ private:
|
||||||
std::optional<ExpressionProxy> else_case_;
|
std::optional<ExpressionProxy> else_case_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Loop : public Node {
|
class Loop : public TypedNode {
|
||||||
public:
|
public:
|
||||||
enum LoopType {
|
enum LoopType {
|
||||||
LOOP,
|
LOOP,
|
||||||
|
|
@ -164,20 +180,20 @@ public:
|
||||||
|
|
||||||
// LOOP
|
// LOOP
|
||||||
Loop(Node node, ExpressionProxy expression)
|
Loop(Node node, ExpressionProxy expression)
|
||||||
: Node(node), type_(LOOP), expression_(expression) {}
|
: TypedNode(node), loop_type_(LOOP), expression_(expression) {}
|
||||||
|
|
||||||
// WHILE
|
// WHILE
|
||||||
Loop(Node node, ExpressionProxy condition, ExpressionProxy expression)
|
Loop(Node node, ExpressionProxy condition, ExpressionProxy expression)
|
||||||
: Node(node), type_(WHILE), expression_(expression),
|
: TypedNode(node), loop_type_(WHILE), expression_(expression),
|
||||||
condition_(condition) {}
|
condition_(condition) {}
|
||||||
|
|
||||||
// FOR
|
// FOR
|
||||||
Loop(Node node, ExpressionProxy variable, ExpressionProxy interval,
|
Loop(Node node, ExpressionProxy variable, ExpressionProxy interval,
|
||||||
ExpressionProxy expression)
|
ExpressionProxy expression)
|
||||||
: Node(node), type_(FOR), expression_(expression), variable_(variable),
|
: TypedNode(node), loop_type_(FOR), expression_(expression),
|
||||||
interval_(interval) {}
|
variable_(variable), interval_(interval) {}
|
||||||
|
|
||||||
LoopType get_type() const { return type_; }
|
LoopType get_type() const { return loop_type_; }
|
||||||
|
|
||||||
Expression *get_expression() { return expression_.get(); }
|
Expression *get_expression() { return expression_.get(); }
|
||||||
|
|
||||||
|
|
@ -208,7 +224,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LoopType type_;
|
LoopType loop_type_;
|
||||||
|
|
||||||
ExpressionProxy expression_;
|
ExpressionProxy expression_;
|
||||||
|
|
||||||
|
|
@ -219,7 +235,7 @@ private:
|
||||||
|
|
||||||
// --- containers
|
// --- containers
|
||||||
|
|
||||||
class Container : public Node {
|
class Container : public TypedNode {
|
||||||
public:
|
public:
|
||||||
enum ContainerType {
|
enum ContainerType {
|
||||||
BLOCK,
|
BLOCK,
|
||||||
|
|
@ -228,13 +244,14 @@ public:
|
||||||
|
|
||||||
Container(Node node, ContainerType type,
|
Container(Node node, ContainerType type,
|
||||||
std::vector<ExpressionProxy> &&expressions)
|
std::vector<ExpressionProxy> &&expressions)
|
||||||
: Node(node), type_(type), expressions_(std::move(expressions)) {}
|
: TypedNode(node), container_type_(type),
|
||||||
|
expressions_(std::move(expressions)) {}
|
||||||
|
|
||||||
Container(Node node, ContainerType type,
|
Container(Node node, ContainerType type,
|
||||||
const std::vector<ExpressionProxy> &expressions)
|
const std::vector<ExpressionProxy> &expressions)
|
||||||
: Node(node), type_(type), expressions_(expressions) {}
|
: TypedNode(node), container_type_(type), expressions_(expressions) {}
|
||||||
|
|
||||||
ContainerType get_type() const { return type_; }
|
ContainerType get_type() const { return container_type_; }
|
||||||
|
|
||||||
size_t expressions_size() const { return expressions_.size(); }
|
size_t expressions_size() const { return expressions_.size(); }
|
||||||
|
|
||||||
|
|
@ -245,7 +262,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ContainerType type_;
|
ContainerType container_type_;
|
||||||
|
|
||||||
std::vector<ExpressionProxy> expressions_;
|
std::vector<ExpressionProxy> expressions_;
|
||||||
};
|
};
|
||||||
|
|
@ -260,21 +277,21 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
Return(Node node, ReturnType type, ExpressionProxy expression)
|
Return(Node node, ReturnType type, ExpressionProxy expression)
|
||||||
: Node(node), type_(type), expression_(expression) {}
|
: Node(node), return_type_(type), expression_(expression) {}
|
||||||
|
|
||||||
ReturnType get_type() const { return type_; }
|
ReturnType get_type() const { return return_type_; }
|
||||||
|
|
||||||
Expression *get_expression() { return expression_.get(); }
|
Expression *get_expression() { return expression_.get(); }
|
||||||
|
|
||||||
const Expression *get_expression() const { return expression_.get(); }
|
const Expression *get_expression() const { return expression_.get(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ReturnType type_;
|
ReturnType return_type_;
|
||||||
|
|
||||||
ExpressionProxy expression_;
|
ExpressionProxy expression_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class NameDefinition : public Node {
|
class NameDefinition : public TypedNode {
|
||||||
public:
|
public:
|
||||||
enum Modifier {
|
enum Modifier {
|
||||||
LET, // %
|
LET, // %
|
||||||
|
|
@ -282,10 +299,10 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
NameDefinition(Node node, Modifier modifier, Identifier &&name)
|
NameDefinition(Node node, Modifier modifier, Identifier &&name)
|
||||||
: Node(node), modifier_(modifier), name_(std::move(name)) {}
|
: TypedNode(node), modifier_(modifier), name_(std::move(name)) {}
|
||||||
|
|
||||||
NameDefinition(Node node, Modifier modifier, const Identifier &name)
|
NameDefinition(Node node, Modifier modifier, const Identifier &name)
|
||||||
: Node(node), modifier_(modifier), name_(name) {}
|
: TypedNode(node), modifier_(modifier), name_(name) {}
|
||||||
|
|
||||||
Modifier get_modifier() const { return modifier_; }
|
Modifier get_modifier() const { return modifier_; }
|
||||||
|
|
||||||
|
|
@ -299,7 +316,7 @@ private:
|
||||||
Identifier name_;
|
Identifier name_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Access : public Node {
|
class Access : public TypedNode {
|
||||||
public:
|
public:
|
||||||
enum AccessType {
|
enum AccessType {
|
||||||
ARRAY,
|
ARRAY,
|
||||||
|
|
@ -308,9 +325,9 @@ public:
|
||||||
|
|
||||||
Access(Node node, AccessType type, ExpressionProxy value,
|
Access(Node node, AccessType type, ExpressionProxy value,
|
||||||
ExpressionProxy index)
|
ExpressionProxy index)
|
||||||
: Node(node), type_(type), value_(value), index_(index) {}
|
: TypedNode(node), access_type_(type), value_(value), index_(index) {}
|
||||||
|
|
||||||
AccessType get_type() const { return type_; }
|
AccessType get_type() const { return access_type_; }
|
||||||
|
|
||||||
Expression *get_value() { return value_.get(); }
|
Expression *get_value() { return value_.get(); }
|
||||||
|
|
||||||
|
|
@ -321,31 +338,32 @@ public:
|
||||||
const Expression *get_index() const { return index_.get(); }
|
const Expression *get_index() const { return index_.get(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
AccessType type_;
|
AccessType access_type_;
|
||||||
|
|
||||||
ExpressionProxy value_;
|
ExpressionProxy value_;
|
||||||
ExpressionProxy index_;
|
ExpressionProxy index_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LoopControl : public Node {
|
class LoopControl : public TypedNode {
|
||||||
public:
|
public:
|
||||||
enum LoopControlType {
|
enum LoopControlType {
|
||||||
BREAK,
|
BREAK,
|
||||||
CONTINUE,
|
CONTINUE,
|
||||||
};
|
};
|
||||||
|
|
||||||
LoopControl(Node node, LoopControlType type) : Node(node), type_(type) {}
|
LoopControl(Node node, LoopControlType type)
|
||||||
|
: TypedNode(node), loop_control_type_(type) {}
|
||||||
|
|
||||||
LoopControlType get_type() const { return type_; }
|
LoopControlType get_type() const { return loop_control_type_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LoopControlType type_;
|
LoopControlType loop_control_type_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ModifierExpression : public Node {
|
class ModifierExpression : public TypedNode {
|
||||||
public:
|
public:
|
||||||
ModifierExpression(Node node, Modifier modifier, ExpressionProxy expression)
|
ModifierExpression(Node node, Modifier modifier, ExpressionProxy expression)
|
||||||
: Node(node), modifier_(modifier), expression_(expression) {}
|
: TypedNode(node), modifier_(modifier), expression_(expression) {}
|
||||||
|
|
||||||
Modifier get_modifier() const { return modifier_; }
|
Modifier get_modifier() const { return modifier_; }
|
||||||
|
|
||||||
|
|
@ -361,12 +379,13 @@ private:
|
||||||
|
|
||||||
// --- other
|
// --- other
|
||||||
|
|
||||||
class NameExpression : public Node {
|
class NameExpression : public TypedNode {
|
||||||
public:
|
public:
|
||||||
NameExpression(Node node, Identifier &&name)
|
NameExpression(Node node, Identifier &&name)
|
||||||
: Node(node), name_(std::move(name)) {}
|
: TypedNode(node), name_(std::move(name)) {}
|
||||||
|
|
||||||
NameExpression(Node node, const Identifier &name) : Node(node), name_(name) {}
|
NameExpression(Node node, const Identifier &name)
|
||||||
|
: TypedNode(node), name_(name) {}
|
||||||
|
|
||||||
NameExpression(
|
NameExpression(
|
||||||
Node node, Identifier &&name,
|
Node node, Identifier &&name,
|
||||||
|
|
@ -374,9 +393,9 @@ public:
|
||||||
&&arguments,
|
&&arguments,
|
||||||
std::optional<TypeProxy> &&prefix, bool is_point_call = false,
|
std::optional<TypeProxy> &&prefix, bool is_point_call = false,
|
||||||
bool is_operator_call = false)
|
bool is_operator_call = false)
|
||||||
: Node(node), name_(std::move(name)), arguments_(std::move(arguments)),
|
: TypedNode(node), name_(std::move(name)),
|
||||||
prefix_(std::move(prefix)), is_point_call_(is_point_call),
|
arguments_(std::move(arguments)), prefix_(std::move(prefix)),
|
||||||
is_operator_call_(is_operator_call) {}
|
is_point_call_(is_point_call), is_operator_call_(is_operator_call) {}
|
||||||
|
|
||||||
Identifier *get_name() { return &name_; }
|
Identifier *get_name() { return &name_; }
|
||||||
|
|
||||||
|
|
@ -435,23 +454,24 @@ private:
|
||||||
bool is_operator_call_ = false; // ... operator ...
|
bool is_operator_call_ = false; // ... operator ...
|
||||||
};
|
};
|
||||||
|
|
||||||
class Constructor : public Node {
|
class Constructor : public TypedNode {
|
||||||
public:
|
public:
|
||||||
Constructor(
|
Constructor(
|
||||||
Node node, TypeProxy type,
|
Node node, TypeProxy type,
|
||||||
std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
|
std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
|
||||||
&&arguments)
|
&&arguments)
|
||||||
: Node(node), type_(type), arguments_(std::move(arguments)) {}
|
: TypedNode(node), constructor_type_(type),
|
||||||
|
arguments_(std::move(arguments)) {}
|
||||||
|
|
||||||
Constructor(
|
Constructor(
|
||||||
Node node, TypeProxy type,
|
Node node, TypeProxy type,
|
||||||
const std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
|
const std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
|
||||||
&arguments)
|
&arguments)
|
||||||
: Node(node), type_(type), arguments_(arguments) {}
|
: TypedNode(node), constructor_type_(type), arguments_(arguments) {}
|
||||||
|
|
||||||
Type *get_type() { return type_.get(); }
|
Type *get_type() { return constructor_type_.get(); }
|
||||||
|
|
||||||
const Type *get_type() const { return type_.get(); }
|
const Type *get_type() const { return constructor_type_.get(); }
|
||||||
|
|
||||||
size_t arguments_size() const { return arguments_.size(); }
|
size_t arguments_size() const { return arguments_.size(); }
|
||||||
|
|
||||||
|
|
@ -478,20 +498,21 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TypeProxy type_;
|
TypeProxy constructor_type_;
|
||||||
std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
|
std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
|
||||||
arguments_;
|
arguments_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Lambda : public Node {
|
class Lambda : public TypedNode {
|
||||||
public:
|
public:
|
||||||
Lambda(Node node, std::vector<Identifier> &&arguments,
|
Lambda(Node node, std::vector<Identifier> &&arguments,
|
||||||
ExpressionProxy expression)
|
ExpressionProxy expression)
|
||||||
: Node(node), arguments_(std::move(arguments)), expression_(expression) {}
|
: TypedNode(node), arguments_(std::move(arguments)),
|
||||||
|
expression_(expression) {}
|
||||||
|
|
||||||
Lambda(Node node, const std::vector<Identifier> &arguments,
|
Lambda(Node node, const std::vector<Identifier> &arguments,
|
||||||
ExpressionProxy expression)
|
ExpressionProxy expression)
|
||||||
: Node(node), arguments_(arguments), expression_(expression) {}
|
: TypedNode(node), arguments_(arguments), expression_(expression) {}
|
||||||
|
|
||||||
size_t arguments_size() const { return arguments_.size(); }
|
size_t arguments_size() const { return arguments_.size(); }
|
||||||
|
|
||||||
|
|
@ -550,7 +571,7 @@ private:
|
||||||
|
|
||||||
// --- modifiers
|
// --- modifiers
|
||||||
Return, NameDefinition,
|
Return, NameDefinition,
|
||||||
// TupleAccess - Access with nmber Literal index
|
// TupleAccess - Access with number Literal index
|
||||||
Access, LoopControl,
|
Access, LoopControl,
|
||||||
// Reference or SuffixExpression is ModifierExpression
|
// Reference or SuffixExpression is ModifierExpression
|
||||||
ModifierExpression,
|
ModifierExpression,
|
||||||
|
|
|
||||||
|
|
@ -119,6 +119,28 @@ public:
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
|
bool operator<(const Type &other_type) const {
|
||||||
|
if (name_ != other_type.name_) {
|
||||||
|
return *name_.get() < *other_type.name_.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_on_heap_ != other_type.is_on_heap_) {
|
||||||
|
return is_on_heap_ < other_type.is_on_heap_;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parameters_.size() != other_type.parameters_.size()) {
|
||||||
|
return parameters_.size() < other_type.parameters_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < parameters_.size(); ++i) {
|
||||||
|
if (*parameters_[i].get() != *other_type.parameters_[i].get()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool operator==(const Type &other_type) const {
|
bool operator==(const Type &other_type) const {
|
||||||
if (name_ != other_type.name_ || is_on_heap_ != other_type.is_on_heap_ ||
|
if (name_ != other_type.name_ || is_on_heap_ != other_type.is_on_heap_ ||
|
||||||
parameters_.size() != other_type.parameters_.size()) {
|
parameters_.size() != other_type.parameters_.size()) {
|
||||||
|
|
|
||||||
|
|
@ -1,188 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "basic_nodes.hpp"
|
|
||||||
#include "name_tree.hpp"
|
|
||||||
#include "statement_nodes.hpp"
|
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace types {
|
|
||||||
|
|
||||||
class Type;
|
|
||||||
class TypeStorage;
|
|
||||||
|
|
||||||
class TypeProxy {
|
|
||||||
friend TypeStorage;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Type *get();
|
|
||||||
|
|
||||||
const Type *get() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
TypeProxy(TypeStorage &type_storage, size_t id)
|
|
||||||
: type_storage_(&type_storage), id_(id) {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
TypeStorage *type_storage_;
|
|
||||||
size_t id_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Defined {
|
|
||||||
public:
|
|
||||||
Defined(names::StatementProxy<nodes::TypeDefinition> definition)
|
|
||||||
: definition_(definition) {}
|
|
||||||
|
|
||||||
nodes::TypeDefinition *get_definition() { return definition_.get(); }
|
|
||||||
|
|
||||||
const nodes::TypeDefinition *get_definition() const {
|
|
||||||
return definition_.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
names::StatementProxy<nodes::TypeDefinition> definition_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Abstract {
|
|
||||||
public:
|
|
||||||
Abstract(std::optional<std::string> name,
|
|
||||||
std::vector<std::string> typeclasses)
|
|
||||||
: name_(name), typeclasses_(typeclasses) {}
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
std::optional<std::string *> get_name() {
|
|
||||||
if (name_.has_value()) {
|
|
||||||
return &name_.value();
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<const std::string *> get_name() const {
|
|
||||||
if (name_.has_value()) {
|
|
||||||
return &name_.value();
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
size_t typeclasses_size() const { return typeclasses_.size(); }
|
|
||||||
|
|
||||||
std::string *get_typeclass(size_t id) { return &typeclasses_.at(id); }
|
|
||||||
|
|
||||||
const std::string *get_typeclass(size_t id) const {
|
|
||||||
return &typeclasses_.at(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::optional<std::string> name_;
|
|
||||||
std::vector<std::string> typeclasses_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Modified {
|
|
||||||
public:
|
|
||||||
Modified(nodes::Modifier modifier, TypeProxy type)
|
|
||||||
: modifier_(modifier), type_(type) {}
|
|
||||||
|
|
||||||
nodes::Modifier get_modifier() const { return modifier_; }
|
|
||||||
|
|
||||||
Type *get_type() { return type_.get(); }
|
|
||||||
|
|
||||||
const Type *get_type() const { return type_.get(); }
|
|
||||||
|
|
||||||
const TypeProxy &get_type_proxy() const { return type_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
nodes::Modifier modifier_;
|
|
||||||
TypeProxy type_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Container {
|
|
||||||
public:
|
|
||||||
enum ContainerType {
|
|
||||||
OR,
|
|
||||||
AND,
|
|
||||||
};
|
|
||||||
|
|
||||||
Container(ContainerType type, std::vector<TypeProxy> &&fields)
|
|
||||||
: type_(type), fields_(std::move(fields)) {}
|
|
||||||
|
|
||||||
Container(ContainerType type, const std::vector<TypeProxy> &fields)
|
|
||||||
: type_(type), fields_(std::move(fields)) {}
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
ContainerType get_type() const { return type_; }
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
size_t fields_size() const { return fields_.size(); }
|
|
||||||
|
|
||||||
Type *get_field(size_t id) { return fields_.at(id).get(); }
|
|
||||||
|
|
||||||
const Type *get_field(size_t id) const { return fields_.at(id).get(); }
|
|
||||||
|
|
||||||
const TypeProxy &get_field_proxy(size_t id) const { return fields_.at(id); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
ContainerType type_;
|
|
||||||
std::vector<TypeProxy> fields_; // or constructors
|
|
||||||
};
|
|
||||||
|
|
||||||
class Type {
|
|
||||||
public:
|
|
||||||
Type(const Type &) = default;
|
|
||||||
Type(Type &&) = default;
|
|
||||||
Type &operator=(const Type &) = default;
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
explicit Type(T &&type) : type_(std::forward<T>(type)) {}
|
|
||||||
|
|
||||||
template <typename T> std::optional<T *> get() {
|
|
||||||
if (std::holds_alternative<T>(type_)) {
|
|
||||||
return &std::get<T>(type_);
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> std::optional<const T *> get() const {
|
|
||||||
if (std::holds_alternative<T>(type_)) {
|
|
||||||
return &std::get<T>(type_);
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto get_any() { return &type_; }
|
|
||||||
|
|
||||||
auto get_any() const { return &type_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::variant<Defined, Abstract, Modified, Container> type_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TypeStorage {
|
|
||||||
friend TypeProxy;
|
|
||||||
|
|
||||||
public:
|
|
||||||
TypeProxy add_type(const Type &type) {
|
|
||||||
storage_.push_back(type);
|
|
||||||
return TypeProxy(*this, storage_.size() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
TypeProxy add_type(Type &&type) {
|
|
||||||
storage_.push_back(std::move(type));
|
|
||||||
return TypeProxy(*this, storage_.size() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Type *get_type(size_t id) { return &storage_.at(id); }
|
|
||||||
|
|
||||||
const Type *get_type(size_t id) const { return &storage_.at(id); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<Type> storage_;
|
|
||||||
};
|
|
||||||
|
|
||||||
}; // namespace types
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
#include "error_handling.hpp"
|
#include "error_handling.hpp"
|
||||||
#include "tokens.hpp"
|
#include "tokens.hpp"
|
||||||
#include "type_nodes.hpp"
|
#include "type_nodes.hpp"
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace builders {
|
namespace builders {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
#include "types.hpp"
|
|
||||||
|
|
||||||
namespace types {
|
|
||||||
|
|
||||||
Type *TypeProxy::get() { return type_storage_->get_type(id_); }
|
|
||||||
|
|
||||||
const Type *TypeProxy::get() const { return type_storage_->get_type(id_); }
|
|
||||||
|
|
||||||
} // namespace types
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue