mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-30 02:38:30 +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
|
|
@ -8,10 +8,22 @@ namespace builders {
|
|||
nodes::TypeProxy build_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeStorage &type_storage);
|
||||
|
||||
nodes::TupleType build_tuple_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeProxy build_tuple_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeStorage &type_storage);
|
||||
|
||||
nodes::VariantType build_variant_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeProxy build_variant_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeStorage &type_storage);
|
||||
|
||||
nodes::TypeProxy build_array_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeStorage &type_storage);
|
||||
|
||||
nodes::TypeProxy build_reference_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeStorage &type_storage);
|
||||
|
||||
nodes::TypeProxy build_modified_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeStorage &type_storage);
|
||||
|
||||
nodes::TypeProxy build_simple_type(parser::ParseTree::Node parse_node,
|
||||
nodes::TypeStorage &type_storage);
|
||||
|
||||
} // namespace builders
|
||||
|
|
|
|||
17
include/builtin_identifiers.hpp
Normal file
17
include/builtin_identifiers.hpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace builtin {
|
||||
|
||||
const static std::string TUPLE_IDENTIFIER = "Tuple";
|
||||
|
||||
const static std::string VARIANT_IDENTIFIER = "Variant";
|
||||
|
||||
const static std::string ARRAY_IDENTIFIER = "Array";
|
||||
|
||||
const static std::string OPTIONAL_IDENTIFIER = "Optional";
|
||||
|
||||
const static std::string RESULT_IDENTIFIER = "Result";
|
||||
|
||||
} // namespace builtin
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -7,9 +7,10 @@ namespace printers {
|
|||
|
||||
void print_type(const nodes::Type &type, printers::Printer &printer);
|
||||
|
||||
void print_tuple_type(const nodes::TupleType &type, printers::Printer &printer);
|
||||
|
||||
void print_variant_type(const nodes::VariantType &type,
|
||||
printers::Printer &printer);
|
||||
// void print_tuple_type(const nodes::TupleType &type, printers::Printer
|
||||
// &printer);
|
||||
//
|
||||
// void print_variant_type(const nodes::VariantType &type,
|
||||
// printers::Printer &printer);
|
||||
|
||||
} // namespace printers
|
||||
|
|
|
|||
|
|
@ -54,7 +54,10 @@ enum class Type {
|
|||
|
||||
VARIANT_TYPE,
|
||||
TUPLE_TYPE,
|
||||
TYPE,
|
||||
ARRAY_TYPE,
|
||||
REFERENCE_TYPE,
|
||||
MODIFIED_TYPE,
|
||||
SIMPLE_TYPE,
|
||||
|
||||
// --- comments
|
||||
|
||||
|
|
@ -141,7 +144,10 @@ const static std::string LAMBDA = "lambda";
|
|||
|
||||
const static std::string VARIANT_TYPE = "variant_type";
|
||||
const static std::string TUPLE_TYPE = "tuple_type";
|
||||
const static std::string TYPE = "type";
|
||||
const static std::string ARRAY_TYPE = "array_type";
|
||||
const static std::string REFERENCE_TYPE = "reference_type";
|
||||
const static std::string MODIFIED_TYPE = "modified_type";
|
||||
const static std::string SIMPLE_TYPE = "simple_type";
|
||||
|
||||
// --- comments
|
||||
|
||||
|
|
@ -226,8 +232,14 @@ inline Type string_to_type(const std::string &str) {
|
|||
return Type::VARIANT_TYPE;
|
||||
} else if (str == TUPLE_TYPE) {
|
||||
return Type::TUPLE_TYPE;
|
||||
} else if (str == TYPE) {
|
||||
return Type::TYPE;
|
||||
} else if (str == ARRAY_TYPE) {
|
||||
return Type::ARRAY_TYPE;
|
||||
} else if (str == REFERENCE_TYPE) {
|
||||
return Type::REFERENCE_TYPE;
|
||||
} else if (str == MODIFIED_TYPE) {
|
||||
return Type::MODIFIED_TYPE;
|
||||
} else if (str == SIMPLE_TYPE) {
|
||||
return Type::SIMPLE_TYPE;
|
||||
} else if (str == DEFINITION_INFO) {
|
||||
return Type::DEFINITION_INFO;
|
||||
} else if (str == ANNOTATION_INFO) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "name_tree.hpp"
|
||||
#include "statement_nodes.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace types {
|
||||
|
|
@ -30,11 +31,8 @@ private:
|
|||
|
||||
class Defined {
|
||||
public:
|
||||
Defined(nodes::Modifier modifier,
|
||||
names::StatementProxy<nodes::TypeDefinition> definition)
|
||||
: modifier_(modifier), definition_(definition) {}
|
||||
|
||||
nodes::Modifier get_modifier() const { return modifier_; }
|
||||
Defined(names::StatementProxy<nodes::TypeDefinition> definition)
|
||||
: definition_(definition) {}
|
||||
|
||||
nodes::TypeDefinition *get_definition() { return definition_.get(); }
|
||||
|
||||
|
|
@ -43,10 +41,64 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
nodes::Modifier modifier_;
|
||||
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 {
|
||||
|
|
@ -72,6 +124,8 @@ public:
|
|||
|
||||
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
|
||||
|
|
@ -105,7 +159,7 @@ public:
|
|||
auto get_any() const { return &type_; }
|
||||
|
||||
private:
|
||||
std::variant<Defined, Container> type_;
|
||||
std::variant<Defined, Abstract, Modified, Container> type_;
|
||||
};
|
||||
|
||||
class TypeStorage {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue