mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-24 07:48:46 +00:00
combine functions for statements part done
This commit is contained in:
parent
437c9692ec
commit
263b58a17c
7 changed files with 515 additions and 120 deletions
|
|
@ -22,9 +22,11 @@ public:
|
|||
std::pair<size_t, size_t> end_position)
|
||||
: start_position_(start_position), end_position_(end_position) {}
|
||||
|
||||
std::pair<size_t, size_t> get_start_position() { return start_position_; }
|
||||
std::pair<size_t, size_t> get_start_position() const {
|
||||
return start_position_;
|
||||
}
|
||||
|
||||
std::pair<size_t, size_t> get_end_position() { return end_position_; }
|
||||
std::pair<size_t, size_t> get_end_position() const { return end_position_; }
|
||||
|
||||
protected:
|
||||
std::pair<size_t, size_t> start_position_;
|
||||
|
|
|
|||
|
|
@ -17,14 +17,13 @@ inline void print_position(std::ostream &out,
|
|||
}
|
||||
|
||||
inline void
|
||||
handle_internal_error(const std::string &message, const std::string &place,
|
||||
std::optional<nodes::Node> node = std::nullopt) {
|
||||
std::cerr << "\x1b[1;31mInternal Error:\x1b[0m " << message << " at "
|
||||
<< place;
|
||||
handle_internal_error(const std::string &message,
|
||||
std::optional<const nodes::Node *> node = std::nullopt) {
|
||||
std::cerr << "\x1b[1;31mInternal Error:\x1b[0m " << message;
|
||||
if (node.has_value()) {
|
||||
std::cerr << ", at ";
|
||||
print_position(std::cerr, node.value().get_start_position(),
|
||||
node.value().get_end_position());
|
||||
std::cerr << " at ";
|
||||
print_position(std::cerr, node.value()->get_start_position(),
|
||||
node.value()->get_end_position());
|
||||
}
|
||||
std::cerr << ".\n";
|
||||
exit(1);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include "statement_nodes.hpp"
|
||||
#include "tree_sitter_wrapper.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace names {
|
||||
|
|
@ -9,72 +13,85 @@ namespace names {
|
|||
// IN PROGRESS
|
||||
class NameTree {
|
||||
public:
|
||||
struct Node {};
|
||||
NameTree() {
|
||||
nodes_.emplace_back(); // root
|
||||
}
|
||||
|
||||
NameTree() {}
|
||||
bool insert(const std::string &path, nodes::Statement &&statement);
|
||||
|
||||
bool insert_path(const std::vector<std::string> &path, Node node) {}
|
||||
std::optional<nodes::Statement *> find(const std::string &path);
|
||||
|
||||
std::optional<const nodes::Statement *> find(const std::string &path) const;
|
||||
|
||||
private:
|
||||
struct Node {
|
||||
public:
|
||||
std::optional<nodes::Statement *> get_statement() {
|
||||
if (statement_.has_value()) {
|
||||
return &statement_.value();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<const nodes::Statement *> get_statement() const {
|
||||
if (statement_.has_value()) {
|
||||
return &statement_.value();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool set_statement(nodes::Statement &&statement) {
|
||||
if (statement_.has_value()) {
|
||||
return false;
|
||||
}
|
||||
statement_ = std::move(statement);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<size_t> find(size_t name_id) const {
|
||||
auto name_iter = children_.find(name_id);
|
||||
|
||||
if (name_iter == children_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return name_iter->second;
|
||||
}
|
||||
|
||||
bool insert(size_t name_id, size_t node_id) {
|
||||
if (children_.count(name_id) != 0) {
|
||||
return false;
|
||||
}
|
||||
children_[name_id] = node_id;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, size_t> children_;
|
||||
std::optional<nodes::Statement> statement_;
|
||||
};
|
||||
|
||||
size_t get_root() const { return 0; }
|
||||
|
||||
size_t add_node(size_t current_node, const std::vector<size_t> &name_ids,
|
||||
size_t current_name = 0);
|
||||
|
||||
std::optional<size_t> find_node(size_t current_node,
|
||||
const std::vector<size_t> &name_ids,
|
||||
size_t current_name = 0) const;
|
||||
|
||||
std::vector<size_t> slice_path_to_name_ids(const std::string &path);
|
||||
|
||||
std::optional<std::vector<size_t>>
|
||||
slice_path_to_existing_name_ids(const std::string &path) const;
|
||||
|
||||
size_t add_name_id(const std::string &name);
|
||||
|
||||
std::optional<size_t> find_name_id(const std::string &name) const;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, size_t> name_to_id_;
|
||||
std::vector<Node> nodes_;
|
||||
};
|
||||
|
||||
class NameStorage;
|
||||
|
||||
class NameProxy {
|
||||
friend NameStorage;
|
||||
|
||||
public:
|
||||
NameProxy(NameStorage *name_storage, size_t id)
|
||||
: name_storage_(name_storage), id_(id) {}
|
||||
|
||||
std::string *get();
|
||||
|
||||
const std::string *get() const;
|
||||
|
||||
bool operator==(const NameProxy &other) const {
|
||||
return name_storage_ == other.name_storage_ && id_ == other.id_;
|
||||
}
|
||||
|
||||
bool operator<(const NameProxy &other) const {
|
||||
return name_storage_ < other.name_storage_ ||
|
||||
(name_storage_ == other.name_storage_ && id_ < other.id_);
|
||||
}
|
||||
|
||||
private:
|
||||
NameStorage *name_storage_;
|
||||
size_t id_;
|
||||
};
|
||||
|
||||
class NameStorage {
|
||||
friend NameProxy;
|
||||
|
||||
public:
|
||||
NameProxy add_expression(const std::string &name) {
|
||||
storage_.push_back(name);
|
||||
return NameProxy(this, storage_.size() - 1);
|
||||
}
|
||||
|
||||
NameProxy add_expression(std::string &&name) {
|
||||
storage_.push_back(std::move(name));
|
||||
return NameProxy(this, storage_.size() - 1);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string *get_expression(size_t id) { return &storage_.at(id); }
|
||||
|
||||
const std::string *get_expression(size_t id) const {
|
||||
return &storage_.at(id);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::string> storage_;
|
||||
};
|
||||
|
||||
std::vector<std::string> string_to_path(const std::string &str) {
|
||||
std::vector<std::string> path;
|
||||
for (;;) {
|
||||
}
|
||||
return path;
|
||||
} // IN PROGRESS
|
||||
|
||||
} // namespace names
|
||||
|
|
|
|||
|
|
@ -11,6 +11,19 @@
|
|||
|
||||
namespace nodes {
|
||||
|
||||
enum class CombineResult {
|
||||
DIFFERENT_NAME_ERROR,
|
||||
DIFFERNENT_MODIFIER_ERROR,
|
||||
MORE_THEN_ONE_DOCS_ERROR,
|
||||
MORE_THEN_ONE_CONSTRAINTS_ERROR,
|
||||
MORE_THEN_ONE_DEFINITION_BODY_ERROR,
|
||||
ARGUMENTS_ERROR,
|
||||
DIFFERENT_STATEMENT_TYPES,
|
||||
STATEMENTS_CANT_BE_COMBINED_ERROR,
|
||||
GENERIC_ERROR,
|
||||
OK,
|
||||
};
|
||||
|
||||
// IN PROGRESS: add another constructors ??
|
||||
class Import : public Node {
|
||||
public:
|
||||
|
|
@ -88,8 +101,42 @@ public:
|
|||
|
||||
//
|
||||
|
||||
// add type with same annotation and same before_modifier
|
||||
bool update_type_from(const Argument &other_argument) {
|
||||
if (type_.has_value() || !other_argument.type_.has_value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (annotation_.has_value() &&
|
||||
(!other_argument.annotation_.has_value() ||
|
||||
annotation_.value() != other_argument.annotation_.value())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (before_modifier_ != other_argument.before_modifier_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (after_modifier_ != other_argument.after_modifier_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
annotation_ = other_argument.annotation_;
|
||||
type_ = other_argument.type_;
|
||||
before_modifier_ = other_argument.before_modifier_;
|
||||
after_modifier_ = other_argument.after_modifier_;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// check, that argument modifiers are none or same to different from type
|
||||
// modifiers
|
||||
bool add_type(const std::optional<std::string> &annotation, TypeProxy type,
|
||||
nodes::Modifier before_modifier = Modifier::NONE) {
|
||||
if (type_.has_value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (annotation_.has_value() &&
|
||||
(!annotation.has_value() ||
|
||||
annotation_.value() != annotation.value())) {
|
||||
|
|
@ -258,7 +305,9 @@ public:
|
|||
|
||||
//
|
||||
|
||||
bool combine(FunctionDefinition &&other_function_definition);
|
||||
bool is_same_to(const FunctionDefinition &other_function_definition) const;
|
||||
|
||||
CombineResult combine(FunctionDefinition &&other_function_definition);
|
||||
|
||||
private:
|
||||
SymbolDocs docs_;
|
||||
|
|
@ -278,7 +327,11 @@ public:
|
|||
std::vector<FunctionDefinition> &&methods)
|
||||
: Node(node), docs_(std::move(docs)), is_on_heap_(is_on_heap),
|
||||
name_(name), arguments_(std::move(arguments)), type_(std::move(type)),
|
||||
methods_(std::move(methods)) {}
|
||||
methods_(std::move(methods)) {
|
||||
for (size_t i = 0; i < methods.size(); ++i) {
|
||||
methods_by_name_[*methods_[i].get_name()->get()] = i;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
|
|
@ -332,7 +385,9 @@ public:
|
|||
|
||||
//
|
||||
|
||||
bool combine(TypeDefinition &&other_type_definition);
|
||||
bool is_same_to(const TypeDefinition &other_type_definition) const;
|
||||
|
||||
CombineResult combine(TypeDefinition &&other_type_definition);
|
||||
|
||||
private:
|
||||
SymbolDocs docs_;
|
||||
|
|
@ -340,6 +395,8 @@ private:
|
|||
Identifier name_;
|
||||
std::vector<Identifier> arguments_;
|
||||
std::optional<VariantType> type_; // TupleType is VariantType with one variant
|
||||
std::unordered_map<std::string, size_t>
|
||||
methods_by_name_; // methods can't be overloaded ??
|
||||
std::vector<FunctionDefinition> methods_;
|
||||
};
|
||||
|
||||
|
|
@ -350,7 +407,11 @@ public:
|
|||
std::vector<FunctionDefinition> &&methods)
|
||||
: Node(node), docs_(std::move(docs)), name_(name),
|
||||
base_typeclasses_(std::move(base_typeclasses)),
|
||||
methods_(std::move(methods)) {}
|
||||
methods_(std::move(methods)) {
|
||||
for (size_t i = 0; i < methods.size(); ++i) {
|
||||
methods_by_name_[*methods_[i].get_name()->get()] = i;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
|
|
@ -388,12 +449,16 @@ public:
|
|||
|
||||
//
|
||||
|
||||
bool combine(TypeclassDefinition &&other_typeclass_definition);
|
||||
bool is_same_to(const TypeclassDefinition &other_typeclass_definition) const;
|
||||
|
||||
CombineResult combine(TypeclassDefinition &&other_typeclass_definition);
|
||||
|
||||
private:
|
||||
SymbolDocs docs_;
|
||||
Identifier name_;
|
||||
std::vector<Identifier> base_typeclasses_;
|
||||
std::unordered_map<std::string, size_t>
|
||||
methods_by_name_; // methods can't be overloaded ??
|
||||
std::vector<FunctionDefinition> methods_;
|
||||
};
|
||||
|
||||
|
|
@ -420,6 +485,10 @@ public:
|
|||
|
||||
auto get_any() const { return &expression_; }
|
||||
|
||||
bool is_same_to(const Statement &other_statement) const;
|
||||
|
||||
CombineResult combine(Statement &&other_statement);
|
||||
|
||||
private:
|
||||
std::variant<Import, TypeDefinition, FunctionDefinition, TypeclassDefinition,
|
||||
EmptyLines>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue