mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-15 11:28:46 +00:00
type structure change, mostly done
This commit is contained in:
parent
522dd16f79
commit
a7c1e3f658
9 changed files with 183 additions and 66 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "basic_nodes.hpp"
|
||||
#include "builtin_types.hpp"
|
||||
#include "doc_nodes.hpp"
|
||||
#include "expression_nodes.hpp"
|
||||
#include "type_nodes.hpp"
|
||||
|
|
@ -92,7 +93,8 @@ public:
|
|||
Modifier before_modifier = Modifier::NONE)
|
||||
: annotation_(annotation), type_(type),
|
||||
before_modifier_(before_modifier),
|
||||
after_modifier_(type.get()->get_modifier()) {}
|
||||
after_modifier_(
|
||||
builtin::builtin_to_modifier(type.get()->to_builtin())) {}
|
||||
|
||||
//
|
||||
|
||||
|
|
@ -143,15 +145,18 @@ public:
|
|||
return false;
|
||||
}
|
||||
|
||||
auto type_after_modifier =
|
||||
builtin::builtin_to_modifier(type.get()->to_builtin());
|
||||
|
||||
if (after_modifier_ != Modifier::NONE &&
|
||||
after_modifier_ != type.get()->get_modifier()) {
|
||||
after_modifier_ != type_after_modifier) {
|
||||
return false;
|
||||
}
|
||||
|
||||
annotation_ = annotation;
|
||||
type_ = type;
|
||||
before_modifier_ = before_modifier;
|
||||
after_modifier_ = type.get()->get_modifier();
|
||||
after_modifier_ = type_after_modifier;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -335,7 +340,7 @@ public:
|
|||
TypeDefinition(Node node, SymbolDocs &&docs, bool is_on_heap,
|
||||
const Identifier &name, std::vector<Identifier> &&typeclasses,
|
||||
std::vector<Identifier> &&arguments,
|
||||
std::optional<TypeProxy> &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)) {}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "basic_nodes.hpp"
|
||||
#include "builtin_types.hpp"
|
||||
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
|
@ -34,21 +36,28 @@ private:
|
|||
// can't have both optional and result modifiers ??
|
||||
class Type : public Node {
|
||||
public:
|
||||
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, Identifier &&identifier, bool is_on_heap = false,
|
||||
const std::optional<std::string> &annotation = std::nullopt)
|
||||
: Node(node), name_(std::move(identifier)), is_on_heap_(is_on_heap),
|
||||
annotation_(annotation) {}
|
||||
|
||||
Type(Node node, const Identifier &identifier, bool is_on_heap = false)
|
||||
: Node(node), name_(identifier), is_on_heap_(is_on_heap) {}
|
||||
Type(Node node, const Identifier &identifier, bool is_on_heap = false,
|
||||
const std::optional<std::string> &annotation = std::nullopt)
|
||||
: Node(node), name_(identifier), is_on_heap_(is_on_heap),
|
||||
annotation_(annotation) {}
|
||||
|
||||
Type(Node node, Identifier &&identifier, std::vector<TypeProxy> &¶meters,
|
||||
bool is_on_heap = false)
|
||||
bool is_on_heap = false,
|
||||
const std::optional<std::string> &annotation = std::nullopt)
|
||||
: Node(node), name_(std::move(identifier)),
|
||||
parameters_(std::move(parameters)), is_on_heap_(is_on_heap) {}
|
||||
parameters_(std::move(parameters)), is_on_heap_(is_on_heap),
|
||||
annotation_(annotation) {}
|
||||
|
||||
Type(Node node, const Identifier &identifier,
|
||||
std::vector<TypeProxy> &¶meters, bool is_on_heap = false)
|
||||
std::vector<TypeProxy> &¶meters, bool is_on_heap = false,
|
||||
const std::optional<std::string> &annotation = std::nullopt)
|
||||
: Node(node), name_(identifier), parameters_(std::move(parameters)),
|
||||
is_on_heap_(is_on_heap) {}
|
||||
is_on_heap_(is_on_heap), annotation_(annotation) {}
|
||||
|
||||
//
|
||||
|
||||
|
|
@ -74,6 +83,42 @@ public:
|
|||
|
||||
//
|
||||
|
||||
std::optional<std::string *> get_annotation() {
|
||||
if (annotation_.has_value()) {
|
||||
return &annotation_.value();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<const std::string *> get_annotation() const {
|
||||
if (annotation_.has_value()) {
|
||||
return &annotation_.value();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void set_annotation(std::string &&annotation) {
|
||||
annotation_ = std::move(annotation);
|
||||
}
|
||||
|
||||
void set_annotation(const std::string &annotation) {
|
||||
annotation_ = annotation;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void collect_annotations_recursively(
|
||||
std::insert_iterator<T> insert_iterator) const {
|
||||
if (annotation_.has_value()) {
|
||||
insert_iterator = annotation_.value();
|
||||
}
|
||||
|
||||
for (auto ¶meter : parameters_) {
|
||||
parameter.get()->collect_annotations_recursively(insert_iterator);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
bool operator==(const Type &other_type) const {
|
||||
if (name_ != other_type.name_ || is_on_heap_ != other_type.is_on_heap_ ||
|
||||
parameters_.size() != other_type.parameters_.size()) {
|
||||
|
|
@ -93,11 +138,33 @@ public:
|
|||
return !(*this == other_type);
|
||||
}
|
||||
|
||||
// is parameters count check necessary ??
|
||||
builtin::BuiltinType to_builtin() {
|
||||
if (*name_.get() == builtin::TUPLE_IDENTIFIER && parameters_.size() > 0) {
|
||||
return builtin::BuiltinType::TUPLE;
|
||||
} else if (*name_.get() == builtin::VARIANT_IDENTIFIER &&
|
||||
parameters_.size() > 0) {
|
||||
return builtin::BuiltinType::VARIANT;
|
||||
} else if (*name_.get() == builtin::ARRAY_IDENTIFIER &&
|
||||
parameters_.size() == 1) {
|
||||
return builtin::BuiltinType::ARRAY;
|
||||
} else if (*name_.get() == builtin::OPTIONAL_IDENTIFIER &&
|
||||
parameters_.size() == 1) {
|
||||
return builtin::BuiltinType::OPTIONAL;
|
||||
} else if (*name_.get() == builtin::RESULT_IDENTIFIER &&
|
||||
parameters_.size() == 1) {
|
||||
return builtin::BuiltinType::RESULT;
|
||||
} else {
|
||||
return builtin::BuiltinType::NONE;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Identifier name_;
|
||||
std::vector<TypeProxy> parameters_;
|
||||
// or use allocator ??
|
||||
bool is_on_heap_ = false;
|
||||
std::optional<std::string> annotation_;
|
||||
};
|
||||
|
||||
class TypeStorage {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue