mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-26 08:48:45 +00:00
type structure change, part done
This commit is contained in:
parent
78c696b99a
commit
522dd16f79
13 changed files with 386 additions and 218 deletions
|
|
@ -335,7 +335,7 @@ public:
|
|||
TypeDefinition(Node node, SymbolDocs &&docs, bool is_on_heap,
|
||||
const Identifier &name, std::vector<Identifier> &&typeclasses,
|
||||
std::vector<Identifier> &&arguments,
|
||||
std::optional<VariantType> &&type)
|
||||
std::optional<TypeProxy> &type)
|
||||
: Node(node), docs_(std::move(docs)), is_on_heap_(is_on_heap),
|
||||
name_(name), typeclasses_(typeclasses),
|
||||
arguments_(std::move(arguments)), type_(std::move(type)) {}
|
||||
|
|
@ -366,16 +366,9 @@ public:
|
|||
|
||||
//
|
||||
|
||||
std::optional<VariantType *> get_type() {
|
||||
std::optional<TypeProxy> get_type() const {
|
||||
if (type_.has_value()) {
|
||||
return &type_.value();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<const VariantType *> get_type() const {
|
||||
if (type_.has_value()) {
|
||||
return &type_.value();
|
||||
return type_.value();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
|
@ -396,7 +389,7 @@ private:
|
|||
Identifier name_;
|
||||
std::vector<Identifier> typeclasses_;
|
||||
std::vector<Identifier> arguments_;
|
||||
std::optional<VariantType> type_; // TupleType is VariantType with one variant
|
||||
std::optional<TypeProxy> type_;
|
||||
};
|
||||
|
||||
class Statement {
|
||||
|
|
|
|||
|
|
@ -34,27 +34,21 @@ private:
|
|||
// can't have both optional and result modifiers ??
|
||||
class Type : public Node {
|
||||
public:
|
||||
Type(Node node, Identifier &&identifier, bool is_on_heap = false,
|
||||
Modifier modifier = Modifier::NONE)
|
||||
: Node(node), name_(std::move(identifier)), is_on_heap_(is_on_heap),
|
||||
modifier_(modifier) {}
|
||||
Type(Node node, Identifier &&identifier, bool is_on_heap = false)
|
||||
: Node(node), name_(std::move(identifier)), is_on_heap_(is_on_heap) {}
|
||||
|
||||
Type(Node node, const Identifier &identifier, bool is_on_heap = false,
|
||||
Modifier modifier = Modifier::NONE)
|
||||
: Node(node), name_(identifier), is_on_heap_(is_on_heap),
|
||||
modifier_(modifier) {}
|
||||
Type(Node node, const Identifier &identifier, bool is_on_heap = false)
|
||||
: Node(node), name_(identifier), is_on_heap_(is_on_heap) {}
|
||||
|
||||
Type(Node node, Identifier &&identifier, std::vector<TypeProxy> &¶meters,
|
||||
bool is_on_heap = false, Modifier modifier = Modifier::NONE)
|
||||
bool is_on_heap = false)
|
||||
: Node(node), name_(std::move(identifier)),
|
||||
parameters_(std::move(parameters)), is_on_heap_(is_on_heap),
|
||||
modifier_(modifier) {}
|
||||
parameters_(std::move(parameters)), is_on_heap_(is_on_heap) {}
|
||||
|
||||
Type(Node node, const Identifier &identifier,
|
||||
std::vector<TypeProxy> &¶meters, bool is_on_heap = false,
|
||||
Modifier modifier = Modifier::NONE)
|
||||
std::vector<TypeProxy> &¶meters, bool is_on_heap = false)
|
||||
: Node(node), name_(identifier), parameters_(std::move(parameters)),
|
||||
is_on_heap_(is_on_heap), modifier_(modifier) {}
|
||||
is_on_heap_(is_on_heap) {}
|
||||
|
||||
//
|
||||
|
||||
|
|
@ -76,13 +70,12 @@ public:
|
|||
|
||||
bool is_on_heap() const { return is_on_heap_; }
|
||||
|
||||
Modifier get_modifier() const { return modifier_; }
|
||||
void set_is_on_heap(bool is_on_heap) { is_on_heap_ = is_on_heap; }
|
||||
|
||||
//
|
||||
|
||||
bool operator==(const Type &other_type) const {
|
||||
if (name_ != other_type.name_ || is_on_heap_ != other_type.is_on_heap_ ||
|
||||
modifier_ != other_type.modifier_ ||
|
||||
parameters_.size() != other_type.parameters_.size()) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -105,7 +98,6 @@ private:
|
|||
std::vector<TypeProxy> parameters_;
|
||||
// or use allocator ??
|
||||
bool is_on_heap_ = false;
|
||||
Modifier modifier_ = Modifier::NONE; // optional, result or none
|
||||
};
|
||||
|
||||
class TypeStorage {
|
||||
|
|
@ -131,85 +123,86 @@ private:
|
|||
std::vector<Type> storage_;
|
||||
};
|
||||
|
||||
class TupleType : public Node {
|
||||
public:
|
||||
TupleType(Node node,
|
||||
const std::vector<std::pair<std::optional<std::string>, TypeProxy>>
|
||||
&fields)
|
||||
: Node(node) {
|
||||
annotations_.reserve(fields.size());
|
||||
fields_.reserve(fields.size());
|
||||
for (auto &field : fields) {
|
||||
if (field.first.has_value()) {
|
||||
annotation_fields_[field.first.value()] = fields_.size();
|
||||
}
|
||||
annotations_.push_back(field.first);
|
||||
fields_.push_back(field.second);
|
||||
}
|
||||
}
|
||||
|
||||
size_t size() const { return fields_.size(); }
|
||||
|
||||
std::optional<std::string *> get_annotation(size_t id) {
|
||||
if (annotations_.at(id).has_value()) {
|
||||
return &annotations_[id].value();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<const std::string *> get_annotation(size_t id) const {
|
||||
if (annotations_.at(id).has_value()) {
|
||||
return &annotations_[id].value();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Type *get(size_t id) { return fields_.at(id).get(); }
|
||||
|
||||
const Type *get(size_t id) const { return fields_.at(id).get(); }
|
||||
|
||||
Type *get(const std::string &annotation) {
|
||||
return fields_.at(annotation_fields_.at(annotation)).get();
|
||||
}
|
||||
|
||||
const Type *get(const std::string &annotation) const {
|
||||
return fields_.at(annotation_fields_.at(annotation)).get();
|
||||
}
|
||||
|
||||
std::vector<std::string> get_all_annotations() {
|
||||
std::vector<std::string> annotations;
|
||||
|
||||
annotations.reserve(annotation_fields_.size());
|
||||
for (auto &annotation_with_field : annotation_fields_) {
|
||||
annotations.push_back(annotation_with_field.first);
|
||||
}
|
||||
|
||||
return annotations;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, size_t> annotation_fields_;
|
||||
std::vector<TypeProxy> fields_;
|
||||
std::vector<std::optional<std::string>> annotations_;
|
||||
};
|
||||
|
||||
class VariantType : public Node {
|
||||
public:
|
||||
VariantType(Node node, std::vector<TupleType> &&constructors_)
|
||||
: Node(node), constructors_(std::move(constructors_)) {}
|
||||
|
||||
VariantType(Node node, const std::vector<TupleType> &constructors_)
|
||||
: Node(node), constructors_(constructors_) {}
|
||||
|
||||
size_t size() const { return constructors_.size(); }
|
||||
|
||||
TupleType *get(size_t id) { return &constructors_.at(id); }
|
||||
|
||||
const TupleType *get(size_t id) const { return &constructors_.at(id); }
|
||||
|
||||
private:
|
||||
// named constructors ??
|
||||
std::vector<TupleType> constructors_;
|
||||
};
|
||||
// class TupleType : public Node {
|
||||
// public:
|
||||
// TupleType(Node node,
|
||||
// const std::vector<std::pair<std::optional<std::string>,
|
||||
// TypeProxy>>
|
||||
// &fields)
|
||||
// : Node(node) {
|
||||
// annotations_.reserve(fields.size());
|
||||
// fields_.reserve(fields.size());
|
||||
// for (auto &field : fields) {
|
||||
// if (field.first.has_value()) {
|
||||
// annotation_fields_[field.first.value()] = fields_.size();
|
||||
// }
|
||||
// annotations_.push_back(field.first);
|
||||
// fields_.push_back(field.second);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// size_t size() const { return fields_.size(); }
|
||||
//
|
||||
// std::optional<std::string *> get_annotation(size_t id) {
|
||||
// if (annotations_.at(id).has_value()) {
|
||||
// return &annotations_[id].value();
|
||||
// }
|
||||
// return std::nullopt;
|
||||
// }
|
||||
//
|
||||
// std::optional<const std::string *> get_annotation(size_t id) const {
|
||||
// if (annotations_.at(id).has_value()) {
|
||||
// return &annotations_[id].value();
|
||||
// }
|
||||
// return std::nullopt;
|
||||
// }
|
||||
//
|
||||
// Type *get(size_t id) { return fields_.at(id).get(); }
|
||||
//
|
||||
// const Type *get(size_t id) const { return fields_.at(id).get(); }
|
||||
//
|
||||
// Type *get(const std::string &annotation) {
|
||||
// return fields_.at(annotation_fields_.at(annotation)).get();
|
||||
// }
|
||||
//
|
||||
// const Type *get(const std::string &annotation) const {
|
||||
// return fields_.at(annotation_fields_.at(annotation)).get();
|
||||
// }
|
||||
//
|
||||
// std::vector<std::string> get_all_annotations() {
|
||||
// std::vector<std::string> annotations;
|
||||
//
|
||||
// annotations.reserve(annotation_fields_.size());
|
||||
// for (auto &annotation_with_field : annotation_fields_) {
|
||||
// annotations.push_back(annotation_with_field.first);
|
||||
// }
|
||||
//
|
||||
// return annotations;
|
||||
// }
|
||||
//
|
||||
// private:
|
||||
// std::unordered_map<std::string, size_t> annotation_fields_;
|
||||
// std::vector<TypeProxy> fields_;
|
||||
// std::vector<std::optional<std::string>> annotations_;
|
||||
// };
|
||||
//
|
||||
// class VariantType : public Node {
|
||||
// public:
|
||||
// VariantType(Node node, std::vector<TupleType> &&constructors_)
|
||||
// : Node(node), constructors_(std::move(constructors_)) {}
|
||||
//
|
||||
// VariantType(Node node, const std::vector<TupleType> &constructors_)
|
||||
// : Node(node), constructors_(constructors_) {}
|
||||
//
|
||||
// size_t size() const { return constructors_.size(); }
|
||||
//
|
||||
// TupleType *get(size_t id) { return &constructors_.at(id); }
|
||||
//
|
||||
// const TupleType *get(size_t id) const { return &constructors_.at(id); }
|
||||
//
|
||||
// private:
|
||||
// // named constructors ??
|
||||
// std::vector<TupleType> constructors_;
|
||||
// };
|
||||
|
||||
} // namespace nodes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue