mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
namespace storage fix, namespace enter fix, maybe other fixes
This commit is contained in:
parent
4882d458f8
commit
4b4756b657
11 changed files with 250 additions and 174 deletions
|
|
@ -44,8 +44,9 @@ struct AnyType {
|
|||
Parameter type;
|
||||
std::vector<Parameter> parameters;
|
||||
interpreter::tokens::AnyType* value;
|
||||
utils::ClassModifier modifier;
|
||||
|
||||
Namespace* parent_namespace = nullptr;
|
||||
utils::IdType parent_namespace;
|
||||
};
|
||||
|
||||
struct Type {
|
||||
|
|
@ -92,11 +93,11 @@ struct Namespace {
|
|||
std::unordered_map<std::string, utils::IdType> types;
|
||||
std::unordered_map<std::string, utils::IdType> functions;
|
||||
std::unordered_map<std::string, utils::IdType> constructors;
|
||||
std::unordered_map<std::string, Namespace> namespaces;
|
||||
std::unordered_map<std::string, Namespace> var_namespaces;
|
||||
std::unordered_map<std::string, Namespace> const_namespaces;
|
||||
std::unordered_map<std::string, utils::IdType> namespaces;
|
||||
std::unordered_map<std::string, utils::IdType> var_namespaces;
|
||||
std::unordered_map<std::string, utils::IdType> const_namespaces;
|
||||
|
||||
Namespace* parent_namespace = nullptr;
|
||||
utils::IdType parent_namespace;
|
||||
|
||||
std::optional<utils::IsConstModifier> modifier; // modifier => variable namespace
|
||||
std::string type_name;
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@ namespace info {
|
|||
class GlobalInfo {
|
||||
friend class NamespaceVisitor;
|
||||
public:
|
||||
GlobalInfo() {
|
||||
namespaces_.emplace_back(); // global namespace
|
||||
}
|
||||
|
||||
struct PartitionInfo {
|
||||
std::vector<std::string> path;
|
||||
std::string name;
|
||||
|
|
@ -39,7 +43,8 @@ public:
|
|||
std::optional<utils::IsConstModifier> modifier,
|
||||
const interpreter::tokens::BaseNode& base_node);
|
||||
|
||||
void EnterNamespace(const std::string& name);
|
||||
void EnterNamespace(const std::string& name,
|
||||
std::optional<utils::IsConstModifier> modifier);
|
||||
|
||||
void ExitNamespace();
|
||||
|
||||
|
|
@ -70,7 +75,7 @@ public:
|
|||
utils::IdType AddPartition(const std::vector<std::string>& path, // including name
|
||||
interpreter::tokens::PartitionStatement* node);
|
||||
|
||||
std::optional<definition::Namespace*> FindNamespace(const std::optional<std::vector<std::string>>& path);
|
||||
std::optional<utils::IdType> FindNamespace(const std::optional<std::vector<std::string>>& path);
|
||||
|
||||
std::optional<utils::IdType> FindFunction(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& name);
|
||||
|
|
@ -104,25 +109,25 @@ public:
|
|||
return current_path_;
|
||||
}
|
||||
|
||||
definition::Namespace* GetCurrentNamespace() {
|
||||
utils::IdType GetCurrentNamespace() {
|
||||
return namespace_stack_.back();
|
||||
}
|
||||
private:
|
||||
NamespaceVisitor(GlobalInfo& global_info) : global_info_(global_info),
|
||||
namespace_stack_({&global_info.global_namespace_}) {}
|
||||
namespace_stack_ {global_info.GlobalNamespaceId} {}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> FindSomething(
|
||||
const std::optional<std::vector<std::string>>& path,
|
||||
std::function<std::optional<T>(definition::Namespace*)> search_func);
|
||||
std::function<std::optional<T>(utils::IdType)> search_func);
|
||||
|
||||
std::optional<definition::Namespace*> FindNamespaceIn(
|
||||
definition::Namespace* current_namespace,
|
||||
std::optional<utils::IdType> FindNamespaceIn(
|
||||
utils::IdType current_namespace,
|
||||
const std::vector<std::string>& path);
|
||||
private:
|
||||
GlobalInfo& global_info_;
|
||||
|
||||
std::vector<definition::Namespace*> namespace_stack_;
|
||||
std::vector<utils::IdType> namespace_stack_;
|
||||
std::vector<std::string> current_path_;
|
||||
};
|
||||
|
||||
|
|
@ -131,7 +136,7 @@ public:
|
|||
}
|
||||
|
||||
// remember about vector realloc
|
||||
const definition::Function& GetFunctionInfo(utils::IdType id) {
|
||||
definition::Function& GetFunctionInfo(utils::IdType id) {
|
||||
return functions_.at(id);
|
||||
}
|
||||
|
||||
|
|
@ -144,22 +149,27 @@ public:
|
|||
}
|
||||
|
||||
// remember about vector realloc
|
||||
const definition::Type& GetAnyTypeInfo(utils::IdType id) {
|
||||
definition::Type& GetAnyTypeInfo(utils::IdType id) {
|
||||
return types_.at(id);
|
||||
}
|
||||
|
||||
// remember about vector realloc
|
||||
const definition::Typeclass& GetTypeclassInfo(utils::IdType id) {
|
||||
definition::Typeclass& GetTypeclassInfo(utils::IdType id) {
|
||||
return typeclasses_.at(id);
|
||||
}
|
||||
|
||||
// remember about vector realloc
|
||||
const definition::Constructor& GetConstructorInfo(utils::IdType id) {
|
||||
definition::Constructor& GetConstructorInfo(utils::IdType id) {
|
||||
return constructors_.at(id);
|
||||
}
|
||||
|
||||
// remember about vector realloc
|
||||
const PartitionInfo& GetPartitionInfo(utils::IdType id) {
|
||||
definition::Namespace& GetNamespaceInfo(utils::IdType id) {
|
||||
return namespaces_.at(id);
|
||||
}
|
||||
|
||||
// remember about vector realloc
|
||||
PartitionInfo& GetPartitionInfo(utils::IdType id) {
|
||||
return partitions_.at(id);
|
||||
}
|
||||
|
||||
|
|
@ -181,6 +191,7 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
const utils::IdType GlobalNamespaceId = 0;
|
||||
|
||||
std::vector<definition::Function> functions_;
|
||||
std::vector<definition::Type> types_;
|
||||
|
|
@ -188,13 +199,14 @@ private:
|
|||
std::vector<definition::Typeclass> typeclasses_;
|
||||
std::vector<definition::Constructor> constructors_;
|
||||
|
||||
std::vector<definition::Namespace> namespaces_;
|
||||
|
||||
std::unordered_map<std::string, utils::IdType> name_to_typeclass_;
|
||||
std::unordered_map<std::string, utils::IdType> name_to_abstract_type_;
|
||||
|
||||
std::vector<PartitionInfo> partitions_;
|
||||
utils::Trie<std::string, utils::IdType> partitions_trie_;
|
||||
|
||||
definition::Namespace global_namespace_;
|
||||
std::vector<definition::Import> imports_;
|
||||
std::unordered_map<std::string, definition::Import> usages_;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#include "utils.hpp"
|
||||
#include "visitor.hpp"
|
||||
#include "global_info.hpp"
|
||||
#include <unordered_set>
|
||||
|
||||
// TODO: class fields and constructors can't be accessed not from associated namespace
|
||||
|
||||
|
|
@ -139,6 +140,7 @@ private:
|
|||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||
info::ContextManager<info::type::Type, info::type::TypeManager>& context_manager_;
|
||||
|
||||
std::unordered_set<utils::IdType> type_namespaces_;
|
||||
utils::IdType current_type_;
|
||||
std::optional<utils::IdType> returned_type_;
|
||||
std::optional<utils::IsConstModifier> is_const_definition_;
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@ public:
|
|||
bool operator<(const AbstractType& type) const;
|
||||
bool operator>(const AbstractType& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
private:
|
||||
utils::AbstractTypeModifier modifier_;
|
||||
|
|
@ -45,6 +46,7 @@ public:
|
|||
DefinedType() = default;
|
||||
DefinedType(utils::IdType type_id,
|
||||
utils::IdType type,
|
||||
utils::ClassModifier class_modifier,
|
||||
TypeManager* type_manager)
|
||||
: type_id_(type_id), type_(type), type_manager_(type_manager) {}
|
||||
|
||||
|
|
@ -53,7 +55,8 @@ public:
|
|||
bool operator<(const DefinedType& type) const;
|
||||
bool operator>(const DefinedType& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
utils::IdType GetTypeId() const {
|
||||
return type_id_;
|
||||
|
|
@ -63,9 +66,14 @@ public:
|
|||
return type_;
|
||||
}
|
||||
|
||||
utils::ClassModifier GetClassModifier() const {
|
||||
return class_modifier_;
|
||||
}
|
||||
|
||||
private:
|
||||
utils::IdType type_id_; // in defined types
|
||||
utils::IdType type_; // in types manager, created using context types (if specific type)
|
||||
utils::ClassModifier class_modifier_;
|
||||
TypeManager* type_manager_ = nullptr;
|
||||
};
|
||||
|
||||
|
|
@ -122,7 +130,8 @@ public:
|
|||
bool operator<(const TupleType& type) const;
|
||||
bool operator>(const TupleType& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
const std::vector<std::pair<std::optional<std::string>, utils::IdType>>& GetFields() const {
|
||||
return fields_;
|
||||
|
|
@ -147,7 +156,8 @@ public:
|
|||
bool operator<(const VariantType& type) const;
|
||||
bool operator>(const VariantType& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
const std::vector<TupleType>& GetConstructors() const {
|
||||
return constructors_;
|
||||
|
|
@ -175,7 +185,8 @@ public:
|
|||
bool operator<(const OptionalType& type) const;
|
||||
bool operator>(const OptionalType& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
private:
|
||||
utils::IdType type_;
|
||||
|
|
@ -195,7 +206,8 @@ public:
|
|||
bool operator<(const ReferenceToType& type) const;
|
||||
bool operator>(const ReferenceToType& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
private:
|
||||
std::vector<utils::ReferenceModifier> references_;
|
||||
|
|
@ -218,7 +230,8 @@ public:
|
|||
bool operator<(const FunctionType& type) const;
|
||||
bool operator>(const FunctionType& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
private:
|
||||
std::vector<utils::IdType> argument_types_;
|
||||
|
|
@ -239,7 +252,8 @@ public:
|
|||
bool operator<(const ArrayType& type) const;
|
||||
bool operator>(const ArrayType& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
utils::IdType GetElementsType() {
|
||||
return elements_type_;
|
||||
|
|
@ -263,7 +277,8 @@ public:
|
|||
bool operator<(const Type& type) const; // TODO: rule exceptions
|
||||
bool operator>(const Type& type) const;
|
||||
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||
std::optional<utils::IdType> GetFieldType(const std::string& name,
|
||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||
|
||||
std::string GetTypeName() const;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue