mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 15:08:45 +00:00
big part of type_chack_visitor done
This commit is contained in:
parent
adccf6feec
commit
f7080ba856
7 changed files with 427 additions and 171 deletions
|
|
@ -131,13 +131,15 @@ private:
|
||||||
utils::IdType TypeInContext(utils::IdType type,
|
utils::IdType TypeInContext(utils::IdType type,
|
||||||
const std::unordered_map<std::string, utils::IdType>& context);
|
const std::unordered_map<std::string, utils::IdType>& context);
|
||||||
|
|
||||||
|
void CheckPattern(Pattern& node, const BaseNode& base_node);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||||
info::TypeInfoContextManager& context_manager_;
|
info::TypeInfoContextManager& context_manager_;
|
||||||
|
|
||||||
utils::IdType current_type_;
|
utils::IdType current_type_;
|
||||||
std::optional<utils::IdType> returned_type_;
|
std::optional<utils::IdType> returned_type_;
|
||||||
std::optional<bool> is_const_definition_;
|
std::optional<utils::IsConstModifier> is_const_definition_;
|
||||||
|
|
||||||
bool is_in_statement_ = false;
|
bool is_in_statement_ = false;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,16 @@
|
||||||
|
|
||||||
namespace info {
|
namespace info {
|
||||||
|
|
||||||
// TODO: remember about typespointers
|
// TODO: remember about type pointers
|
||||||
class TypeInfoContextManager {
|
class TypeInfoContextManager {
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
utils::IdType AddType(const T&& type) {
|
utils::IdType AddType(T&& type, utils::ValueType value_type) {
|
||||||
return type_manager_.AddType(std::forward(type));
|
return type_manager_.AddType(std::forward(type), value_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::IdType AddAnyType(type::Type&& type, utils::ValueType value_type) {
|
||||||
|
return type_manager_.AddType(std::move(type), value_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
@ -28,6 +32,10 @@ public:
|
||||||
return type_manager_.GetAnyType(type_id);
|
return type_manager_.GetAnyType(type_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils::ValueType GetValueType(utils::IdType type_id) {
|
||||||
|
return type_manager_.GetValueType(type_id);
|
||||||
|
}
|
||||||
|
|
||||||
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
|
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
|
||||||
return type_manager_.AddTypeRequirement(type, requrement);
|
return type_manager_.AddTypeRequirement(type, requrement);
|
||||||
}
|
}
|
||||||
|
|
@ -36,6 +44,10 @@ public:
|
||||||
return type_manager_.EqualTypes(first_type, second_type);
|
return type_manager_.EqualTypes(first_type, second_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils::IdType ToModifiedType(utils::IdType type_id, utils::ValueType new_value_type) {
|
||||||
|
return AddAnyType(type::Type(*GetAnyType(type_id)), new_value_type);
|
||||||
|
}
|
||||||
|
|
||||||
type::TypeManager* GetTypeManager() {
|
type::TypeManager* GetTypeManager() {
|
||||||
return &type_manager_;
|
return &type_manager_;
|
||||||
}
|
}
|
||||||
|
|
@ -56,10 +68,9 @@ public:
|
||||||
contexts_.clear();
|
contexts_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: variable modifiers
|
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
||||||
bool DefineVariable(const std::string& name, utils::IdType type_id, bool is_const) {
|
|
||||||
// check in previous contexts ??
|
// check in previous contexts ??
|
||||||
return contexts_.back().DefineVariable(name, type_id, is_const);
|
return contexts_.back().DefineVariable(name, type_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
|
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
|
||||||
|
|
@ -80,14 +91,14 @@ public:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnterVariableContext(const std::string& name,
|
void EnterVariableContext(const std::string& name, utils::IdType type_id) {
|
||||||
utils::IdType type_id, bool is_const) { // for variable namespaces, for loops
|
// for variable namespaces, for loops
|
||||||
contexts_.push_back(false);
|
contexts_.emplace_back();
|
||||||
|
|
||||||
DefineVariable(name, type_id, is_const);
|
DefineVariable(name, type_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::pair<utils::IdType, bool>> GetVariableInfo(const std::string& name) {
|
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
|
||||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||||
auto maybe_type = contexts_[i].GetVariableInfo(name);
|
auto maybe_type = contexts_[i].GetVariableInfo(name);
|
||||||
if (maybe_type.has_value()) {
|
if (maybe_type.has_value()) {
|
||||||
|
|
@ -110,13 +121,13 @@ public:
|
||||||
private:
|
private:
|
||||||
class Context {
|
class Context {
|
||||||
public:
|
public:
|
||||||
Context(bool hide_previous) : hide_previous_(hide_previous) {}
|
Context() = default;
|
||||||
|
|
||||||
bool DefineVariable(const std::string& name, utils::IdType type_id, bool is_const) {
|
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
||||||
if (variables_.count(name) > 0) {
|
if (variables_.count(name) > 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
variables_[name] = {type_id, is_const};
|
variables_[name] = type_id;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,7 +143,7 @@ private:
|
||||||
return variables_.erase(name);
|
return variables_.erase(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::pair<utils::IdType, bool>> GetVariableInfo(const std::string& name) {
|
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
|
||||||
auto variable_iter = variables_.find(name);
|
auto variable_iter = variables_.find(name);
|
||||||
|
|
||||||
if (variable_iter == variables_.end()) {
|
if (variable_iter == variables_.end()) {
|
||||||
|
|
@ -151,11 +162,8 @@ private:
|
||||||
|
|
||||||
return local_abstract_type_iter->second;
|
return local_abstract_type_iter->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsFirst() { return hide_previous_; }
|
|
||||||
private:
|
private:
|
||||||
bool hide_previous_;
|
std::unordered_map<std::string, utils::IdType> variables_;
|
||||||
std::unordered_map<std::string, std::pair<utils::IdType, bool>> variables_;
|
|
||||||
std::unordered_map<std::string, utils::IdType> local_abstract_types_;
|
std::unordered_map<std::string, utils::IdType> local_abstract_types_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,9 @@ public:
|
||||||
bool Same(const AbstractType& type) const;
|
bool Same(const AbstractType& type) const;
|
||||||
bool operator<(const AbstractType& type) const;
|
bool operator<(const AbstractType& type) const;
|
||||||
bool operator>(const AbstractType& type) const;
|
bool operator>(const AbstractType& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
utils::AbstractTypeModifier modifier_;
|
utils::AbstractTypeModifier modifier_;
|
||||||
std::string name_;
|
std::string name_;
|
||||||
|
|
@ -50,6 +53,8 @@ public:
|
||||||
bool operator<(const DefinedType& type) const;
|
bool operator<(const DefinedType& type) const;
|
||||||
bool operator>(const DefinedType& type) const;
|
bool operator>(const DefinedType& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
utils::IdType GetTypeId() const {
|
utils::IdType GetTypeId() const {
|
||||||
return type_id_;
|
return type_id_;
|
||||||
}
|
}
|
||||||
|
|
@ -86,6 +91,8 @@ public:
|
||||||
bool operator<(const TupleType& type) const;
|
bool operator<(const TupleType& type) const;
|
||||||
bool operator>(const TupleType& type) const;
|
bool operator>(const TupleType& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
const std::vector<std::pair<std::optional<std::string>, utils::IdType>>& GetFields() const {
|
const std::vector<std::pair<std::optional<std::string>, utils::IdType>>& GetFields() const {
|
||||||
return fields_;
|
return fields_;
|
||||||
}
|
}
|
||||||
|
|
@ -100,21 +107,29 @@ class VariantType {
|
||||||
public:
|
public:
|
||||||
VariantType() = default;
|
VariantType() = default;
|
||||||
VariantType(const std::optional<std::string>& name,
|
VariantType(const std::optional<std::string>& name,
|
||||||
const std::vector<TupleType>& constructors)
|
const std::vector<TupleType>& constructors,
|
||||||
: name_(name), constructors_(constructors){}
|
std::optional<size_t> current_constructor)
|
||||||
|
: name_(name), constructors_(constructors), current_constructor_(current_constructor) {}
|
||||||
|
|
||||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||||
bool Same(const VariantType& type) const;
|
bool Same(const VariantType& type) const;
|
||||||
bool operator<(const VariantType& type) const;
|
bool operator<(const VariantType& type) const;
|
||||||
bool operator>(const VariantType& type) const;
|
bool operator>(const VariantType& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
const std::vector<TupleType>& GetConstructors() const {
|
const std::vector<TupleType>& GetConstructors() const {
|
||||||
return constructors_;
|
return constructors_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const void SetCurrentConstructor(size_t constructor) {
|
||||||
|
current_constructor_ = constructor;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::optional<std::string> name_;
|
std::optional<std::string> name_;
|
||||||
std::vector<TupleType> constructors_;
|
std::vector<TupleType> constructors_;
|
||||||
|
std::optional<size_t> current_constructor_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class OptionalType {
|
class OptionalType {
|
||||||
|
|
@ -129,6 +144,8 @@ public:
|
||||||
bool operator<(const OptionalType& type) const;
|
bool operator<(const OptionalType& type) const;
|
||||||
bool operator>(const OptionalType& type) const;
|
bool operator>(const OptionalType& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
utils::IdType type_;
|
utils::IdType type_;
|
||||||
TypeManager* type_manager_ = nullptr;
|
TypeManager* type_manager_ = nullptr;
|
||||||
|
|
@ -147,6 +164,8 @@ public:
|
||||||
bool operator<(const ReferenceToType& type) const;
|
bool operator<(const ReferenceToType& type) const;
|
||||||
bool operator>(const ReferenceToType& type) const;
|
bool operator>(const ReferenceToType& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<utils::ReferenceModifier> references_;
|
std::vector<utils::ReferenceModifier> references_;
|
||||||
utils::IdType type_;
|
utils::IdType type_;
|
||||||
|
|
@ -168,6 +187,8 @@ public:
|
||||||
bool operator<(const FunctionType& type) const;
|
bool operator<(const FunctionType& type) const;
|
||||||
bool operator>(const FunctionType& type) const;
|
bool operator>(const FunctionType& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<utils::IdType> argument_types_;
|
std::vector<utils::IdType> argument_types_;
|
||||||
utils::IdType return_type_;
|
utils::IdType return_type_;
|
||||||
|
|
@ -187,6 +208,8 @@ public:
|
||||||
bool operator<(const ArrayType& type) const;
|
bool operator<(const ArrayType& type) const;
|
||||||
bool operator>(const ArrayType& type) const;
|
bool operator>(const ArrayType& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
utils::IdType GetElementsType() {
|
utils::IdType GetElementsType() {
|
||||||
return elements_type_;
|
return elements_type_;
|
||||||
}
|
}
|
||||||
|
|
@ -199,6 +222,8 @@ private:
|
||||||
|
|
||||||
class Type {
|
class Type {
|
||||||
public:
|
public:
|
||||||
|
Type() = default;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
explicit Type(T&& type) : type_(std::forward(type)) {}
|
explicit Type(T&& type) : type_(std::forward(type)) {}
|
||||||
|
|
||||||
|
|
@ -207,6 +232,8 @@ public:
|
||||||
bool operator<(const Type& type) const; // TODO: rule exceptions
|
bool operator<(const Type& type) const; // TODO: rule exceptions
|
||||||
bool operator>(const Type& type) const;
|
bool operator>(const Type& type) const;
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GetFieldType(const std::string& name) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::variant<AbstractType,
|
std::variant<AbstractType,
|
||||||
DefinedType,
|
DefinedType,
|
||||||
|
|
@ -215,24 +242,29 @@ private:
|
||||||
VariantType,
|
VariantType,
|
||||||
ReferenceToType,
|
ReferenceToType,
|
||||||
FunctionType,
|
FunctionType,
|
||||||
ArrayType> type_;
|
ArrayType,
|
||||||
|
OptionalType> type_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TypeManager {
|
class TypeManager {
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T>
|
||||||
utils::IdType AddType(const T&& type);
|
utils::IdType AddType(T&& type, utils::ValueType value_type);
|
||||||
|
|
||||||
|
utils::IdType AddAnyType(Type&& type, utils::ValueType value_type);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::optional<T*> GetType(utils::IdType type_id);
|
std::optional<T*> GetType(utils::IdType type_id);
|
||||||
|
|
||||||
Type* GetAnyType(utils::IdType type_id);
|
Type* GetAnyType(utils::IdType type_id);
|
||||||
|
|
||||||
|
utils::ValueType GetValueType(utils::IdType type_id);
|
||||||
|
|
||||||
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement);
|
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement);
|
||||||
bool EqualTypes(utils::IdType first_type, utils::IdType second_type);
|
bool EqualTypes(utils::IdType first_type, utils::IdType second_type);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<info::type::Type> types_;
|
std::vector<std::pair<info::type::Type, utils::ValueType>> types_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace info::type
|
} // namespace info::type
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
|
@ -17,6 +18,18 @@ enum class AliasModifier { Alias = 0, Type = 1, Let = 2 };
|
||||||
enum class AbstractTypeModifier { Basic = 0, Abstract = 1 };
|
enum class AbstractTypeModifier { Basic = 0, Abstract = 1 };
|
||||||
enum class FunctionTypeModifier { Function = 0, Operator = 1 };
|
enum class FunctionTypeModifier { Function = 0, Operator = 1 };
|
||||||
|
|
||||||
|
enum class ValueType { Const = 0, Var = 1, Tmp = 2 };
|
||||||
|
|
||||||
|
inline ValueType IsConstModifierToValueType(IsConstModifier modifier) {
|
||||||
|
switch (modifier) {
|
||||||
|
case IsConstModifier::Const:
|
||||||
|
return ValueType::Const;
|
||||||
|
case IsConstModifier::Var:
|
||||||
|
return ValueType::Var;
|
||||||
|
}
|
||||||
|
// unreachable
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Storage {
|
class Storage {
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ int main(int argc, char** argv) { // TODO, only test version
|
||||||
parser::ParseTree parse_tree(source);
|
parser::ParseTree parse_tree(source);
|
||||||
|
|
||||||
if (!parse_tree.IsProperlyParsed()) {
|
if (!parse_tree.IsProperlyParsed()) {
|
||||||
error_handling::HandleParsingError("There are some parsing errors in file.", {0, 0});
|
error_handling::HandleParsingError("There are some parsing errors in file.", {0, 0}, {0, 0});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<interpreter::tokens::SourceFile> source_file =
|
std::unique_ptr<interpreter::tokens::SourceFile> source_file =
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include <asm-generic/errno.h>
|
#include <asm-generic/errno.h>
|
||||||
|
#include <cstdarg>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
@ -17,7 +18,7 @@ void TypeCheckVisitor::Visit(SourceFile* node) {
|
||||||
for (auto& statement : node->statements) {
|
for (auto& statement : node->statements) {
|
||||||
Visitor::Visit(statement);
|
Visitor::Visit(statement);
|
||||||
}
|
}
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Namespaces, partitions -----------------
|
// Namespaces, partitions -----------------
|
||||||
|
|
@ -26,33 +27,34 @@ void TypeCheckVisitor::Visit(PartitionSources* node) {
|
||||||
for (auto& statement : node->statements) {
|
for (auto& statement : node->statements) {
|
||||||
Visitor::Visit(statement);
|
Visitor::Visit(statement);
|
||||||
}
|
}
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(Partition* node) {
|
void TypeCheckVisitor::Visit(Partition* node) {
|
||||||
Visit(&node->scope);
|
Visit(&node->scope);
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(NamespaceSources* node) {
|
void TypeCheckVisitor::Visit(NamespaceSources* node) {
|
||||||
for (auto& statement : node->statements) {
|
for (auto& statement : node->statements) {
|
||||||
Visitor::Visit(statement);
|
Visitor::Visit(statement);
|
||||||
}
|
}
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for class: const and var
|
void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for class: const and var
|
||||||
if (node->modifier.has_value()) {
|
if (node->modifier.has_value()) {
|
||||||
bool is_const = node->modifier.value() == utils::IsConstModifier::Const;
|
bool is_const = (node->modifier.value() == utils::IsConstModifier::Const);
|
||||||
|
|
||||||
if (node->link_typeclass_id_.has_value()) { // TODO: think about typeclass
|
if (node->link_typeclass_id_.has_value()) { // TODO: think about typeclass
|
||||||
|
|
||||||
std::vector<utils::IdType> requirements {node->link_typeclass_id_.value()};
|
std::vector<utils::IdType> requirements {node->link_typeclass_id_.value()};
|
||||||
utils::IdType abstract_type = context_manager_.AddType(info::type::AbstractType(utils::AbstractTypeModifier::Abstract, node->type, requirements));
|
utils::IdType abstract_type = context_manager_.AddType(
|
||||||
|
info::type::AbstractType(utils::AbstractTypeModifier::Abstract, node->type, requirements),
|
||||||
|
IsConstModifierToValueType(node->modifier.value()));
|
||||||
|
|
||||||
context_manager_.EnterVariableContext("self",
|
context_manager_.EnterVariableContext("self", // TODO: different name ??
|
||||||
abstract_type,
|
abstract_type);
|
||||||
is_const); // TODO: different name ??
|
|
||||||
context_manager_.DefineLocalAbstractType(node->type, abstract_type);
|
context_manager_.DefineLocalAbstractType(node->type, abstract_type);
|
||||||
} else if (node->link_type_id_.has_value()) {
|
} else if (node->link_type_id_.has_value()) {
|
||||||
Visitor::Visit(*namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->link_type_id_.value()).value().value); // handle error?
|
Visitor::Visit(*namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(node->link_type_id_.value()).value().value); // handle error?
|
||||||
|
|
@ -60,8 +62,8 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c
|
||||||
context_manager_.EnterVariableContext(
|
context_manager_.EnterVariableContext(
|
||||||
"self", // TODO: different name ??
|
"self", // TODO: different name ??
|
||||||
context_manager_.AddType(
|
context_manager_.AddType(
|
||||||
info::type::DefinedType(node->link_type_id_.value(), current_type_, context_manager_.GetTypeManager())),
|
info::type::DefinedType(node->link_type_id_.value(), current_type_, context_manager_.GetTypeManager()),
|
||||||
is_const);
|
IsConstModifierToValueType(node->modifier.value())));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (node->link_typeclass_id_.has_value()) { // ??
|
if (node->link_typeclass_id_.has_value()) { // ??
|
||||||
|
|
@ -75,7 +77,7 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c
|
||||||
|
|
||||||
namespace_visitor_.ExitNamespace();
|
namespace_visitor_.ExitNamespace();
|
||||||
context_manager_.ExitContext();
|
context_manager_.ExitContext();
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Definitions -----------------
|
// Definitions -----------------
|
||||||
|
|
@ -85,36 +87,44 @@ void TypeCheckVisitor::Visit(ImportStatement* node) {}
|
||||||
// TODO
|
// TODO
|
||||||
// + TODO: let bindings for abstract types
|
// + TODO: let bindings for abstract types
|
||||||
void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
|
||||||
if (!is_in_statement_) { // do nothing if alias is not applied
|
error_handling::HandleInternalError("Unimplemented",
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
"TypeCheckVisitor.AliasDefinitionStatement");
|
||||||
return;
|
// if (!is_in_statement_) { // do nothing if alias is not applied
|
||||||
|
// current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// context_manager_.EnterContext();
|
||||||
|
//
|
||||||
|
// for (auto& parameter : node->parameters) {
|
||||||
|
// current_type_ = context_manager_.AddType(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
|
||||||
|
// parameter,
|
||||||
|
// /*TODO*/{}),
|
||||||
|
// utils::ValueType::Tmp);
|
||||||
|
// context_manager_.DefineLocalAbstractType(parameter, current_type_);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Visit(node->value.get());
|
||||||
|
//
|
||||||
|
// context_manager_.ExitContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
context_manager_.EnterContext();
|
|
||||||
|
|
||||||
for (auto& parameter : node->parameters) {
|
|
||||||
current_type_ = context_manager_.AddType(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
|
|
||||||
parameter,
|
|
||||||
/*TODO*/{}));
|
|
||||||
context_manager_.DefineLocalAbstractType(parameter, current_type_);
|
|
||||||
}
|
|
||||||
|
|
||||||
Visit(node->value.get());
|
|
||||||
|
|
||||||
context_manager_.ExitContext();
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: remove variable on move
|
|
||||||
void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
||||||
|
// TODO: remove variable on move
|
||||||
is_in_statement_ = true;
|
is_in_statement_ = true;
|
||||||
|
|
||||||
Visitor::Visit(node->value);
|
Visitor::Visit(node->value);
|
||||||
// current_type from value automatically passed to name definitions
|
// current_type from value automatically passed to name definitions
|
||||||
is_const_definition_ = (node->modifier == utils::IsConstModifier::Const);
|
if (node->assignment_modifier == utils::AssignmentModifier::Assign) {
|
||||||
|
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp);
|
||||||
|
// TODO: make type tmp recursively ??
|
||||||
|
}
|
||||||
|
|
||||||
|
is_const_definition_ = node->modifier;
|
||||||
Visitor::Visit(node->name);
|
Visitor::Visit(node->name);
|
||||||
is_const_definition_ = std::nullopt;
|
is_const_definition_ = std::nullopt;
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
|
|
||||||
is_in_statement_ = false;
|
is_in_statement_ = false;
|
||||||
}
|
}
|
||||||
|
|
@ -123,7 +133,7 @@ void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
|
||||||
bool was_in_statement = is_in_statement_;
|
bool was_in_statement = is_in_statement_;
|
||||||
is_in_statement_ = true;
|
is_in_statement_ = true;
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
|
|
||||||
if (!was_in_statement) {
|
if (!was_in_statement) {
|
||||||
is_in_statement_ = false;
|
is_in_statement_ = false;
|
||||||
|
|
@ -152,7 +162,8 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||||
// TODO: add recursive typeclasses from typeclass tree
|
// TODO: add recursive typeclasses from typeclass tree
|
||||||
current_type_ = context_manager_.AddType(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
|
current_type_ = context_manager_.AddType(info::type::AbstractType(utils::AbstractTypeModifier::Abstract,
|
||||||
parameter.type,
|
parameter.type,
|
||||||
requirements));
|
requirements),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
if (!context_manager_.DefineLocalAbstractType(parameter.type, current_type_)) {
|
if (!context_manager_.DefineLocalAbstractType(parameter.type, current_type_)) {
|
||||||
error_handling::HandleTypecheckError("Can't define function parameter type: abstract type redefinition", node->base);
|
error_handling::HandleTypecheckError("Can't define function parameter type: abstract type redefinition", node->base);
|
||||||
}
|
}
|
||||||
|
|
@ -160,7 +171,8 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||||
|
|
||||||
for (size_t i = 0; i < node->definition->arguments.size(); ++i) {
|
for (size_t i = 0; i < node->definition->arguments.size(); ++i) {
|
||||||
Visitor::Visit(*declaration.argument_types[i]);
|
Visitor::Visit(*declaration.argument_types[i]);
|
||||||
if (!context_manager_.DefineVariable(node->definition->arguments[i].name, current_type_, true)) { // TODO: watch to reference
|
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Const); // TODO: var function arguments??
|
||||||
|
if (!context_manager_.DefineVariable(node->definition->arguments[i].name, current_type_)) { // TODO: watch to reference
|
||||||
error_handling::HandleTypecheckError("Can't define function argument variable: name redefinition", node->base);
|
error_handling::HandleTypecheckError("Can't define function argument variable: name redefinition", node->base);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -175,7 +187,7 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||||
|
|
||||||
context_manager_.ExitContext();
|
context_manager_.ExitContext();
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
|
|
||||||
is_in_statement_ = false;
|
is_in_statement_ = false;
|
||||||
}
|
}
|
||||||
|
|
@ -183,7 +195,7 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||||
void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
|
||||||
is_in_statement_ = true;
|
is_in_statement_ = true;
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
|
|
||||||
is_in_statement_ = false;
|
is_in_statement_ = false;
|
||||||
}
|
}
|
||||||
|
|
@ -198,12 +210,13 @@ void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
||||||
}
|
}
|
||||||
// TODO: add recursive typeclasses from typeclass tree
|
// TODO: add recursive typeclasses from typeclass tree
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::AbstractType(node->modifier, node->type->type, requirements));
|
current_type_ = context_manager_.AddType(info::type::AbstractType(node->modifier, node->type->type, requirements),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
if (!context_manager_.DefineLocalAbstractType(node->type->type, current_type_)) {
|
if (!context_manager_.DefineLocalAbstractType(node->type->type, current_type_)) {
|
||||||
error_handling::HandleTypecheckError("Can't define basic/bastract type: abstract type redefinition", node->base);
|
error_handling::HandleTypecheckError("Can't define basic/bastract type: abstract type redefinition", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
|
|
||||||
is_in_statement_ = false;
|
is_in_statement_ = false;
|
||||||
}
|
}
|
||||||
|
|
@ -211,7 +224,7 @@ void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
||||||
void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
|
||||||
is_in_statement_ = true;
|
is_in_statement_ = true;
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
|
|
||||||
is_in_statement_ = false;
|
is_in_statement_ = false;
|
||||||
}
|
}
|
||||||
|
|
@ -219,40 +232,30 @@ void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
|
||||||
// Definition parts
|
// Definition parts
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(FunctionDefinition* node) {
|
void TypeCheckVisitor::Visit(FunctionDefinition* node) {
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(TypeDefinition* node) {
|
void TypeCheckVisitor::Visit(TypeDefinition* node) {
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(AnyAnnotatedType* node) {
|
void TypeCheckVisitor::Visit(AnyAnnotatedType* node) {
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flow control -----------------
|
// Flow control -----------------
|
||||||
|
|
||||||
// TODO <----- | current position |
|
void TypeCheckVisitor::Visit(TypeConstructorPatternParameter* node) {} // Handled in TypeConstructorPattern
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
void Visitor::Visit(TypeConstructorPatternParameter* node) {} // Handled in TypeConstructorPattern
|
void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match named constructor
|
||||||
|
|
||||||
// TODO
|
|
||||||
void Visitor::Visit(TypeConstructorPattern* node) {
|
|
||||||
Visit(node->constructor.get());
|
|
||||||
for (auto& parameter : node->parameters) {
|
|
||||||
Visit(¶meter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
|
||||||
if (!node->constructor->constructor_id_.has_value()) {
|
if (!node->constructor->constructor_id_.has_value()) {
|
||||||
error_handling::HandleTypecheckError("Type constructor name not found", node->base);
|
error_handling::HandleTypecheckError("Type constructor pattern name not found", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!node->constructor->type_id_.has_value()) {
|
if (!node->constructor->type_id_.has_value()) {
|
||||||
error_handling::HandleInternalError("Type constructor without type", "TypeCheckVisitor.TypeConstructor");
|
error_handling::HandleInternalError("Type constructor pattern without type",
|
||||||
|
"TypeCheckVisitor.TypeConstructorPattern");
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::IdType constructor_id = node->constructor->constructor_id_.value();
|
utils::IdType constructor_id = node->constructor->constructor_id_.value();
|
||||||
|
|
@ -269,7 +272,8 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
||||||
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type_id);
|
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type_id);
|
||||||
|
|
||||||
if (!maybe_type_info.has_value()) { // TODO
|
if (!maybe_type_info.has_value()) { // TODO
|
||||||
error_handling::HandleInternalError("Implemented only for AnyType", "TypeCheckVisitor.LambdaFunction");
|
error_handling::HandleInternalError("Implemented only for AnyType",
|
||||||
|
"TypeCheckVisitor.TypeConstructorPattern");
|
||||||
}
|
}
|
||||||
|
|
||||||
const info::definition::AnyType& type_info = maybe_type_info.value();
|
const info::definition::AnyType& type_info = maybe_type_info.value();
|
||||||
|
|
@ -279,74 +283,55 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
||||||
TupleType* constructor_fields = constructor_info.constructor_tuple_node.value();
|
TupleType* constructor_fields = constructor_info.constructor_tuple_node.value();
|
||||||
|
|
||||||
if (constructor_fields->entities.size() != node->parameters.size()) {
|
if (constructor_fields->entities.size() != node->parameters.size()) {
|
||||||
error_handling::HandleTypecheckError("Type constructor parameters count mismatch", node->base);
|
error_handling::HandleTypecheckError("Type constructor pattern parameters count mismatch", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < node->parameters.size(); ++i) {
|
for (size_t i = 0; i < node->parameters.size(); ++i) {
|
||||||
// TODO: remove variable on move
|
if (node->parameters[i].name.has_value()) { // TODO: needed??
|
||||||
if (node->parameters[i].name.has_value()) {
|
|
||||||
if (!constructor_fields->entities[i].first.has_value()
|
if (!constructor_fields->entities[i].first.has_value()
|
||||||
|| constructor_fields->entities[i].first.value().name != node->parameters[i].name.value().name) {
|
|| constructor_fields->entities[i].first.value().name != node->parameters[i].name.value().name) {
|
||||||
error_handling::HandleTypecheckError("Type constructor: name of parameter and name in constructor don't match each other", node->base);
|
error_handling::HandleTypecheckError("Type constructor pattern: name of parameter and name in constructor don't match each other", node->base);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (constructor_fields->entities[i].first.has_value()) {
|
if (constructor_fields->entities[i].first.has_value()) {
|
||||||
error_handling::HandleTypecheckError("Type constructor: unnamed pprameter corresponds named field", node->base);
|
error_handling::HandleTypecheckError("Type constructor pattern: unnamed parameter corresponds named field", node->base);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Visitor::Visit(constructor_fields->entities[i].second.get());
|
Visitor::Visit(constructor_fields->entities[i].second.get());
|
||||||
utils::IdType parameter_type = TypeInContext(current_type_, context);
|
current_type_ = TypeInContext(current_type_, context);
|
||||||
|
|
||||||
Visitor::Visit(node->parameters[i].value);
|
CheckPattern(node->parameters[i].value, node->base); // current_type_ passed to parameter
|
||||||
|
|
||||||
context_manager_.EqualTypes(TypeInContext(parameter_type, context), current_type_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (node->parameters.size() > 0) {
|
if (node->parameters.size() > 0) {
|
||||||
error_handling::HandleTypecheckError("Parameters for untyped type constructor", node->base);
|
error_handling::HandleTypecheckError("Parameters for untyped type constructor pattern", node->base);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Visitor::Visit(*type_info.value);
|
Visitor::Visit(*type_info.value);
|
||||||
current_type_ = TypeInContext(current_type_, context);
|
current_type_ = TypeInContext(current_type_, context);
|
||||||
|
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(MatchCase* node) {
|
void TypeCheckVisitor::Visit(MatchCase* node) {
|
||||||
// value type passed as current_type_
|
// value type passed as current_type_
|
||||||
is_const_definition_ = true; // TODO: or var??
|
is_const_definition_ = utils::IsConstModifier::Const; // TODO: or var??
|
||||||
Visitor::Visit(node->value);
|
Visitor::Visit(node->value);
|
||||||
|
|
||||||
utils::IdType value_type = current_type_;
|
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp);
|
||||||
|
|
||||||
switch (node->value.index()) {
|
CheckPattern(node->value, node->base);
|
||||||
case 0:
|
|
||||||
context_manager_.DefineVariable(std::get<std::unique_ptr<ExtendedName>>(node->value)->name,
|
|
||||||
current_type_,
|
|
||||||
is_const_definition_.value());
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
Visitor::Visit(*std::get<std::unique_ptr<Literal>>(node->value));
|
|
||||||
if (context_manager_.EqualTypes(current_type_, value_type)) {
|
|
||||||
error_handling::HandleTypecheckError("Literal and value have different types", node->base);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
Visit(std::get<std::unique_ptr<TypeConstructorPattern>>(node->value).get());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// error
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
is_const_definition_ = std::nullopt;
|
is_const_definition_ = std::nullopt;
|
||||||
|
|
||||||
if (node->condition.has_value()) {
|
if (node->condition.has_value()) {
|
||||||
Visitor::Visit(node->condition.value());
|
Visitor::Visit(node->condition.value());
|
||||||
}
|
}
|
||||||
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool), current_type_)) {
|
if (!context_manager_.EqualTypes(
|
||||||
|
context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp),
|
||||||
|
current_type_)) {
|
||||||
error_handling::HandleTypecheckError("Match case condition is not bool expression", node->base);
|
error_handling::HandleTypecheckError("Match case condition is not bool expression", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -356,7 +341,7 @@ void TypeCheckVisitor::Visit(MatchCase* node) {
|
||||||
// current_type_ from statement is current_type_ for MatchCase
|
// current_type_ from statement is current_type_ for MatchCase
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(Match* node) {
|
void TypeCheckVisitor::Visit(Match* node) { // TODO: move value to match
|
||||||
// TODO: types can be different in block statement
|
// TODO: types can be different in block statement
|
||||||
utils::IdType type;
|
utils::IdType type;
|
||||||
|
|
||||||
|
|
@ -367,7 +352,10 @@ void TypeCheckVisitor::Visit(Match* node) {
|
||||||
|
|
||||||
for (size_t i = 0; i < node->matches.size(); ++i) {
|
for (size_t i = 0; i < node->matches.size(); ++i) {
|
||||||
current_type_ = value_type;
|
current_type_ = value_type;
|
||||||
|
|
||||||
|
context_manager_.EnterContext();
|
||||||
Visit(&node->matches[i]);
|
Visit(&node->matches[i]);
|
||||||
|
context_manager_.ExitContext();
|
||||||
|
|
||||||
if (!node->matches[i].statement.has_value()) {
|
if (!node->matches[i].statement.has_value()) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -384,7 +372,7 @@ void TypeCheckVisitor::Visit(Match* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_type_found) {
|
if (!is_type_found) {
|
||||||
type = context_manager_.AddType(info::type::InternalType::Unit);
|
type = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = type;
|
current_type_ = type;
|
||||||
|
|
@ -396,11 +384,13 @@ void TypeCheckVisitor::Visit(Condition* node) {
|
||||||
|
|
||||||
for (size_t i = 0; i < node->conditions.size(); ++i) {
|
for (size_t i = 0; i < node->conditions.size(); ++i) {
|
||||||
Visitor::Visit(node->conditions[i]);
|
Visitor::Visit(node->conditions[i]);
|
||||||
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool), current_type_)) {
|
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) {
|
||||||
error_handling::HandleTypecheckError("Condition statement condition is not bool expression", node->base);
|
error_handling::HandleTypecheckError("Condition statement condition is not bool expression", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context_manager_.EnterContext();
|
||||||
Visitor::Visit(node->statements[i]);
|
Visitor::Visit(node->statements[i]);
|
||||||
|
context_manager_.ExitContext();
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
type = current_type_;
|
type = current_type_;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -411,39 +401,48 @@ void TypeCheckVisitor::Visit(Condition* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node->statements.size() > node->conditions.size()) {
|
if (node->statements.size() > node->conditions.size()) {
|
||||||
|
context_manager_.EnterContext();
|
||||||
Visitor::Visit(node->statements[node->conditions.size()]);
|
Visitor::Visit(node->statements[node->conditions.size()]);
|
||||||
|
context_manager_.ExitContext();
|
||||||
if (!context_manager_.EqualTypes(type, current_type_)) {
|
if (!context_manager_.EqualTypes(type, current_type_)) {
|
||||||
error_handling::HandleTypecheckError("Condition statement else have different type from other cases", node->base);
|
error_handling::HandleTypecheckError("Condition statement else have different type from other cases", node->base);
|
||||||
}
|
}
|
||||||
current_type_ = type;
|
current_type_ = type;
|
||||||
} else {
|
} else {
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::OptionalType(type, context_manager_.GetTypeManager()));
|
info::type::OptionalType(type, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp); // ??
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(DoWhileLoop* node) {
|
void TypeCheckVisitor::Visit(DoWhileLoop* node) {
|
||||||
Visitor::Visit(node->condition);
|
Visitor::Visit(node->condition);
|
||||||
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool), current_type_)) {
|
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) {
|
||||||
error_handling::HandleTypecheckError("Do while loop statement condition is not bool expression", node->base);
|
error_handling::HandleTypecheckError("Do while loop statement condition is not bool expression", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context_manager_.EnterContext();
|
||||||
Visitor::Visit(node->statement);
|
Visitor::Visit(node->statement);
|
||||||
|
context_manager_.ExitContext();
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()));
|
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(WhileLoop* node) {
|
void TypeCheckVisitor::Visit(WhileLoop* node) {
|
||||||
Visitor::Visit(node->condition);
|
Visitor::Visit(node->condition);
|
||||||
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool), current_type_)) {
|
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Bool, utils::ValueType::Tmp), current_type_)) {
|
||||||
error_handling::HandleTypecheckError("While loop statement condition is not bool expression", node->base);
|
error_handling::HandleTypecheckError("While loop statement condition is not bool expression", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
context_manager_.EnterContext();
|
||||||
Visitor::Visit(node->statement);
|
Visitor::Visit(node->statement);
|
||||||
|
context_manager_.ExitContext();
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()));
|
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(ForLoop* node) {
|
void TypeCheckVisitor::Visit(ForLoop* node) {
|
||||||
|
|
@ -454,22 +453,32 @@ void TypeCheckVisitor::Visit(ForLoop* node) {
|
||||||
error_handling::HandleTypecheckError("For loop interval type mismatch", node->base);
|
error_handling::HandleTypecheckError("For loop interval type mismatch", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(maybe_interval_type.value()->GetElementsType());
|
context_manager_.EnterContext();
|
||||||
is_const_definition_ = (node->modifier == utils::IsConstModifier::Const);
|
|
||||||
|
current_type_ = context_manager_.AddType(maybe_interval_type.value()->GetElementsType(),
|
||||||
|
utils::IsConstModifierToValueType(node->variable_modifier));
|
||||||
|
|
||||||
|
is_const_definition_ = node->variable_modifier;
|
||||||
Visitor::Visit(node->variable); // type passed to variable definition throught current_type_
|
Visitor::Visit(node->variable); // type passed to variable definition throught current_type_
|
||||||
is_const_definition_ = std::nullopt;
|
is_const_definition_ = std::nullopt;
|
||||||
|
|
||||||
Visitor::Visit(node->statement);
|
Visitor::Visit(node->statement);
|
||||||
|
|
||||||
|
context_manager_.ExitContext();
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()));
|
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(LoopLoop* node) {
|
void TypeCheckVisitor::Visit(LoopLoop* node) {
|
||||||
|
context_manager_.EnterContext();
|
||||||
Visitor::Visit(node->statement);
|
Visitor::Visit(node->statement);
|
||||||
|
context_manager_.ExitContext();
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()));
|
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Statements, expressions, blocks, etc. -----------------
|
// Statements, expressions, blocks, etc. -----------------
|
||||||
|
|
@ -480,6 +489,7 @@ void TypeCheckVisitor::Visit(Block* node) {
|
||||||
|
|
||||||
bool is_type_found = false;
|
bool is_type_found = false;
|
||||||
|
|
||||||
|
context_manager_.EnterContext();
|
||||||
for (auto& statement : node->statements) {
|
for (auto& statement : node->statements) {
|
||||||
returned_type_ = std::nullopt;
|
returned_type_ = std::nullopt;
|
||||||
Visitor::Visit(statement);
|
Visitor::Visit(statement);
|
||||||
|
|
@ -494,9 +504,10 @@ void TypeCheckVisitor::Visit(Block* node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
context_manager_.EnterContext();
|
||||||
|
|
||||||
if (!is_type_found) {
|
if (!is_type_found) {
|
||||||
type = context_manager_.AddType(info::type::InternalType::Unit);
|
type = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = type;
|
current_type_ = type;
|
||||||
|
|
@ -508,7 +519,7 @@ void TypeCheckVisitor::Visit(ScopedStatement* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum
|
void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Unit);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
|
|
@ -595,12 +606,13 @@ void TypeCheckVisitor::Visit(ReferenceExpression* node) {
|
||||||
Visit(node->expression.get());
|
Visit(node->expression.get());
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()));
|
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()),
|
||||||
|
context_manager_.GetValueType(current_type_)); // ??
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(AccessExpression* node) {
|
void TypeCheckVisitor::Visit(AccessExpression* node) {
|
||||||
Visitor::Visit(node->id);
|
Visitor::Visit(node->id);
|
||||||
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Int), current_type_)) {
|
if (!context_manager_.EqualTypes(context_manager_.AddType(info::type::InternalType::Int, utils::ValueType::Tmp), current_type_)) {
|
||||||
error_handling::HandleTypecheckError("Access index has wrong type (not Int)", node->base);
|
error_handling::HandleTypecheckError("Access index has wrong type (not Int)", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -652,11 +664,18 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
||||||
"TypeCheckVisitor.FunctionCallExpresssion");
|
"TypeCheckVisitor.FunctionCallExpresssion");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: better decision ??
|
||||||
std::optional<utils::IdType> maybe_const_function_id = maybe_type_info.value().parent_namespace->const_namespaces.at(maybe_type_info->type.type).functions[node->name.name];
|
std::optional<utils::IdType> maybe_const_function_id = maybe_type_info.value().parent_namespace->const_namespaces.at(maybe_type_info->type.type).functions[node->name.name];
|
||||||
|
|
||||||
// TODO: better decision ??
|
utils::ValueType expression_value_type = context_manager_.GetValueType(current_type_);
|
||||||
if (/*is var expression*/) {
|
|
||||||
|
if (expression_value_type == utils::ValueType::Tmp) {
|
||||||
|
error_handling::HandleInternalError("Expression value type is ValueType::Type",
|
||||||
|
"TypeCheckVisitor.FunctionCallExpression");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expression_value_type == utils::ValueType::Var || expression_value_type == utils::ValueType::Tmp) {
|
||||||
|
// TODO: choose expression value types
|
||||||
maybe_function_id = maybe_type_info.value().parent_namespace->var_namespaces.at(maybe_type_info->type.type).functions[node->name.name];
|
maybe_function_id = maybe_type_info.value().parent_namespace->var_namespaces.at(maybe_type_info->type.type).functions[node->name.name];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -742,7 +761,8 @@ void TypeCheckVisitor::Visit(TupleExpression* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::TupleType(std::nullopt, fields, context_manager_.GetTypeManager()));
|
info::type::TupleType(std::nullopt, fields, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ??
|
// ??
|
||||||
|
|
@ -758,7 +778,8 @@ void TypeCheckVisitor::Visit(VariantExpression* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::VariantType(std::nullopt, constructors));
|
current_type_ = context_manager_.AddType(info::type::VariantType(std::nullopt, constructors, -1),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(ReturnExpression* node) {
|
void TypeCheckVisitor::Visit(ReturnExpression* node) {
|
||||||
|
|
@ -766,7 +787,7 @@ void TypeCheckVisitor::Visit(ReturnExpression* node) {
|
||||||
returned_type_ = current_type_;
|
returned_type_ = current_type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Visitor::Visit(TypeConstructorParameter* node) {} // Handeled in TypeConstructor visit
|
void TypeCheckVisitor::Visit(TypeConstructorParameter* node) {} // Handeled in TypeConstructor visit
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
||||||
if (!node->constructor->constructor_id_.has_value()) {
|
if (!node->constructor->constructor_id_.has_value()) {
|
||||||
|
|
@ -791,7 +812,7 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
||||||
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type_id);
|
namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(type_id);
|
||||||
|
|
||||||
if (!maybe_type_info.has_value()) { // TODO
|
if (!maybe_type_info.has_value()) { // TODO
|
||||||
error_handling::HandleInternalError("Implemented only for AnyType", "TypeCheckVisitor.LambdaFunction");
|
error_handling::HandleInternalError("Implemented only for AnyType", "TypeCheckVisitor.TypeConstructor");
|
||||||
}
|
}
|
||||||
|
|
||||||
const info::definition::AnyType& type_info = maybe_type_info.value();
|
const info::definition::AnyType& type_info = maybe_type_info.value();
|
||||||
|
|
@ -813,7 +834,7 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (constructor_fields->entities[i].first.has_value()) {
|
if (constructor_fields->entities[i].first.has_value()) {
|
||||||
error_handling::HandleTypecheckError("Type constructor: unnamed pprameter corresponds named field", node->base);
|
error_handling::HandleTypecheckError("Type constructor: unnamed parameter corresponds named field", node->base);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -822,7 +843,9 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
||||||
|
|
||||||
Visitor::Visit(node->parameters[i].value);
|
Visitor::Visit(node->parameters[i].value);
|
||||||
|
|
||||||
context_manager_.EqualTypes(TypeInContext(parameter_type, context), current_type_);
|
if (!context_manager_.EqualTypes(parameter_type, current_type_)) {
|
||||||
|
error_handling::HandleTypecheckError("Type constructor: wrong parameter type", node->base);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -832,6 +855,14 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Visitor::Visit(*type_info.value);
|
Visitor::Visit(*type_info.value);
|
||||||
|
|
||||||
|
std::optional<info::type::VariantType*> maybe_variant_type =
|
||||||
|
context_manager_.GetType<info::type::VariantType>(current_type_);
|
||||||
|
|
||||||
|
if (maybe_type_info.has_value()) {
|
||||||
|
maybe_variant_type.value()->SetCurrentConstructor(constructor_info.order);
|
||||||
|
}
|
||||||
|
|
||||||
current_type_ = TypeInContext(current_type_, context);
|
current_type_ = TypeInContext(current_type_, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -859,29 +890,44 @@ void TypeCheckVisitor::Visit(ArrayExpression* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::ArrayType(node->elements.size(), elements_type, context_manager_.GetTypeManager()));
|
info::type::ArrayType(node->elements.size(), elements_type, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name
|
// Name
|
||||||
|
|
||||||
// TODO
|
|
||||||
void TypeCheckVisitor::Visit(NameExpression* node) {
|
void TypeCheckVisitor::Visit(NameExpression* node) {
|
||||||
// TODO: move, etc.
|
// TODO: move, etc.
|
||||||
if (node->names.size() == 0) {
|
if (node->names.size() == 0) {
|
||||||
error_handling::HandleInternalError("Name expression with zero names", "TypeCheckVisitor.NameExpression");
|
error_handling::HandleInternalError("Name expression with zero names", "TypeCheckVisitor.NameExpression");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto maybe_variable_info = context_manager_.GetVariableInfo(node->names[0].name);
|
auto maybe_variable_type = context_manager_.GetVariableInfo(node->names[0].name);
|
||||||
|
|
||||||
if (!maybe_variable_info.has_value()) {
|
if (!maybe_variable_type.has_value()) {
|
||||||
error_handling::HandleTypecheckError("Variable not found", node->names[0].base);
|
error_handling::HandleTypecheckError("Variable not found", node->names[0].base);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: get variable field
|
current_type_ = maybe_variable_type.value();
|
||||||
|
|
||||||
|
if (node->names.size() == 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils::ValueType variable_value_type = context_manager_.GetValueType(current_type_);
|
||||||
|
|
||||||
|
for (size_t i = 1; i < node->names.size(); ++i) {
|
||||||
|
std::optional<utils::IdType> maybe_type_value = context_manager_.GetAnyType(current_type_)->GetFieldType(node->names[i].name);
|
||||||
|
if (!maybe_type_value.has_value()) {
|
||||||
|
error_handling::HandleTypecheckError("Variable field not found", node->names[i].base);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_type_ = maybe_type_value.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
current_type_ = context_manager_.ToModifiedType(current_type_, variable_value_type);
|
||||||
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(TupleName* node) {
|
void TypeCheckVisitor::Visit(TupleName* node) {
|
||||||
utils::IdType type = current_type_;
|
utils::IdType type = current_type_;
|
||||||
|
|
||||||
|
|
@ -899,8 +945,24 @@ void TypeCheckVisitor::Visit(TupleName* node) {
|
||||||
if (maybe_type_value.value()->GetFields().size() != node->names.size()) {
|
if (maybe_type_value.value()->GetFields().size() != node->names.size()) {
|
||||||
error_handling::HandleTypecheckError("Mismatched field count in tuple variable definition", node->base);
|
error_handling::HandleTypecheckError("Mismatched field count in tuple variable definition", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!is_const_definition_.has_value()) {
|
||||||
|
error_handling::HandleInternalError("No value in is_const_definition_", "TypeCheckVisitor.TupleName");
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::ValueType value_type = context_manager_.GetValueType(type);
|
||||||
|
if (value_type == utils::ValueType::Const
|
||||||
|
&& is_const_definition_.value() == utils::IsConstModifier::Var) {
|
||||||
|
error_handling::HandleTypecheckError("TupleName: value type expression not match from variable definition modifier", node->base);
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < node->names.size(); ++i) {
|
for (size_t i = 0; i < node->names.size(); ++i) {
|
||||||
current_type_ = maybe_type_value.value()->GetFields()[i].second;
|
current_type_ = maybe_type_value.value()->GetFields()[i].second;
|
||||||
|
|
||||||
|
if (value_type == utils::ValueType::Tmp) { // TODO: ??
|
||||||
|
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp);
|
||||||
|
}
|
||||||
|
|
||||||
Visitor::Visit(node->names[i]);
|
Visitor::Visit(node->names[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -925,14 +987,30 @@ void TypeCheckVisitor::Visit(VariantName* node) {
|
||||||
error_handling::HandleTypecheckError("Mismatched variant count in variant variable definition", node->base);
|
error_handling::HandleTypecheckError("Mismatched variant count in variant variable definition", node->base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!is_const_definition_.has_value()) {
|
||||||
|
error_handling::HandleInternalError("No value in is_const_definition_", "TypeCheckVisitor.VariantName");
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::ValueType value_type = context_manager_.GetValueType(type);
|
||||||
|
if (value_type == utils::ValueType::Const
|
||||||
|
&& is_const_definition_.value() == utils::IsConstModifier::Var) {
|
||||||
|
error_handling::HandleTypecheckError("VariantName: value type of type and value type are diifferent", node->base);
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < node->names.size(); ++i) {
|
for (size_t i = 0; i < node->names.size(); ++i) {
|
||||||
if (maybe_type_value.value()->GetConstructors()[i].GetFields().size() == 0) {
|
if (maybe_type_value.value()->GetConstructors()[i].GetFields().size() == 0) {
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::OptionalType(context_manager_.AddType(info::type::InternalType::Unit), context_manager_.GetTypeManager()));
|
info::type::OptionalType(context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp), context_manager_.GetTypeManager()),
|
||||||
|
utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ??
|
||||||
} else {
|
} else {
|
||||||
info::type::TupleType constructor_type = maybe_type_value.value()->GetConstructors()[i];
|
info::type::TupleType constructor_type_value = maybe_type_value.value()->GetConstructors()[i];
|
||||||
|
|
||||||
|
utils::IdType constructor_type =
|
||||||
|
context_manager_.AddType(std::move(constructor_type_value),
|
||||||
|
utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ??
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::OptionalType(context_manager_.AddType(std::move(constructor_type)), context_manager_.GetTypeManager()));
|
info::type::OptionalType(constructor_type, context_manager_.GetTypeManager()),
|
||||||
|
utils::IsConstModifierToValueType(is_const_definition_.value())); // TODO ??
|
||||||
}
|
}
|
||||||
|
|
||||||
Visitor::Visit(node->names[i]);
|
Visitor::Visit(node->names[i]);
|
||||||
|
|
@ -948,10 +1026,21 @@ void TypeCheckVisitor::Visit(AnnotatedName* node) {
|
||||||
error_handling::HandleInternalError("No value in is_const_definition_", "TypeCheckVisitor.AnnotatedName");
|
error_handling::HandleInternalError("No value in is_const_definition_", "TypeCheckVisitor.AnnotatedName");
|
||||||
}
|
}
|
||||||
|
|
||||||
context_manager_.DefineVariable(node->name, type, is_const_definition_.value());
|
utils::ValueType value_type = context_manager_.GetValueType(type);
|
||||||
|
if (value_type == utils::ValueType::Const
|
||||||
|
&& is_const_definition_.value() == utils::IsConstModifier::Var) {
|
||||||
|
error_handling::HandleTypecheckError("TupleName: value type expression not match from variable definition modifier", node->base);
|
||||||
|
}
|
||||||
|
|
||||||
|
type = context_manager_.ToModifiedType(type, utils::IsConstModifierToValueType(is_const_definition_.value()));
|
||||||
|
if (!context_manager_.DefineVariable(node->name, type)) {
|
||||||
|
error_handling::HandleTypecheckError("Variable name already present in context", node->base);
|
||||||
|
}
|
||||||
if (node->type.has_value()) {
|
if (node->type.has_value()) {
|
||||||
Visitor::Visit(node->type.value());
|
Visitor::Visit(node->type.value());
|
||||||
context_manager_.EqualTypes(type, current_type_);
|
if (!context_manager_.EqualTypes(type, current_type_)) { // TODO ??
|
||||||
|
error_handling::HandleTypecheckError("Wrong type annotation in annotated name", node->base);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -971,7 +1060,8 @@ void TypeCheckVisitor::Visit(FunctionType* node) {
|
||||||
argument_types.pop_back();
|
argument_types.pop_back();
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::FunctionType(argument_types, return_type, context_manager_.GetTypeManager()));
|
info::type::FunctionType(argument_types, return_type, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(TupleType* node) {
|
void TypeCheckVisitor::Visit(TupleType* node) {
|
||||||
|
|
@ -984,7 +1074,8 @@ void TypeCheckVisitor::Visit(TupleType* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::TupleType(node->type, fields, context_manager_.GetTypeManager()));
|
info::type::TupleType(node->type, fields, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(VariantType* node) {
|
void TypeCheckVisitor::Visit(VariantType* node) {
|
||||||
|
|
@ -1007,7 +1098,8 @@ void TypeCheckVisitor::Visit(VariantType* node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(info::type::VariantType(node->type, constructors));
|
current_type_ = context_manager_.AddType(info::type::VariantType(node->type, constructors, -1),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO handle local abstract types, abstract types, aliases, etc.
|
// TODO handle local abstract types, abstract types, aliases, etc.
|
||||||
|
|
@ -1032,7 +1124,8 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
|
||||||
|
|
||||||
if (node->array_size.has_value()) {
|
if (node->array_size.has_value()) {
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::ArrayType(node->array_size.value(), current_type_, context_manager_.GetTypeManager()));
|
info::type::ArrayType(node->array_size.value(), current_type_, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1040,13 +1133,13 @@ void TypeCheckVisitor::Visit(ExtendedScopedAnyType* node) {
|
||||||
Visitor::Visit(node->type);
|
Visitor::Visit(node->type);
|
||||||
|
|
||||||
current_type_ = context_manager_.AddType(
|
current_type_ = context_manager_.AddType(
|
||||||
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()));
|
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()),
|
||||||
|
utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Typeclass
|
// Typeclass
|
||||||
|
|
||||||
void Visitor::Visit(ParametrizedTypeclass* node) { // TODO ??
|
void TypeCheckVisitor::Visit(ParametrizedTypeclass* node) { // TODO ??
|
||||||
Visit(&node->typeclass);
|
|
||||||
for (auto& parameter : node->parameters) {
|
for (auto& parameter : node->parameters) {
|
||||||
Visit(parameter.get());
|
Visit(parameter.get());
|
||||||
}
|
}
|
||||||
|
|
@ -1059,19 +1152,19 @@ void TypeCheckVisitor::Visit(ParametrizedType* node) {} // Handled in TypeExpres
|
||||||
// Identifiers, constants, etc. -----------------
|
// Identifiers, constants, etc. -----------------
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(FloatNumberLiteral* node) {
|
void TypeCheckVisitor::Visit(FloatNumberLiteral* node) {
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Float);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Float, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(NumberLiteral* node) {
|
void TypeCheckVisitor::Visit(NumberLiteral* node) {
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Int);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Int, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(StringLiteral* node) {
|
void TypeCheckVisitor::Visit(StringLiteral* node) {
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::String);
|
current_type_ = context_manager_.AddType(info::type::InternalType::String, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(CharLiteral* node) {
|
void TypeCheckVisitor::Visit(CharLiteral* node) {
|
||||||
current_type_ = context_manager_.AddType(info::type::InternalType::Char);
|
current_type_ = context_manager_.AddType(info::type::InternalType::Char, utils::ValueType::Tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
@ -1118,7 +1211,30 @@ utils::IdType TypeCheckVisitor::TypeInContext(utils::IdType type,
|
||||||
const std::unordered_map<std::string, utils::IdType>& context) {
|
const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
info::type::Type type_in_context = info::type::Type(context_manager_.GetAnyType(type));
|
info::type::Type type_in_context = info::type::Type(context_manager_.GetAnyType(type));
|
||||||
type_in_context.InContext(context);
|
type_in_context.InContext(context);
|
||||||
return context_manager_.AddType(std::move(type_in_context));
|
return context_manager_.AddType(std::move(type_in_context), utils::ValueType::Tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TypeCheckVisitor::CheckPattern(Pattern& node, const BaseNode& base_node) { // <-> ScopedPattern
|
||||||
|
utils::IdType value_type = current_type_;
|
||||||
|
|
||||||
|
switch (node.index()) {
|
||||||
|
case 0:
|
||||||
|
context_manager_.DefineVariable(std::get<std::unique_ptr<ExtendedName>>(node)->name,
|
||||||
|
value_type);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
Visitor::Visit(*std::get<std::unique_ptr<Literal>>(node));
|
||||||
|
if (context_manager_.EqualTypes(current_type_, value_type)) { // TODO: better solution ??
|
||||||
|
error_handling::HandleTypecheckError("Literal and value have different types", base_node);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
Visit(std::get<std::unique_ptr<TypeConstructorPattern>>(node).get());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// error
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace interpreter
|
} // namespace interpreter
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,10 @@ bool AbstractType::operator>(const AbstractType& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> AbstractType::GetFieldType(const std::string& name) const {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
std::optional<utils::IdType> DefinedType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
std::optional<utils::IdType> DefinedType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
|
|
@ -56,6 +60,10 @@ bool DefinedType::operator>(const DefinedType& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> DefinedType::GetFieldType(const std::string& name) const {
|
||||||
|
return type_manager_->GetAnyType(type_)->GetFieldType(name);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
std::optional<utils::IdType> TupleType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
std::optional<utils::IdType> TupleType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
|
|
@ -102,6 +110,15 @@ bool TupleType::operator>(const TupleType& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> TupleType::GetFieldType(const std::string& name) const {
|
||||||
|
for (size_t i = 0; i < fields_.size(); ++i) { // TODO: optimize??
|
||||||
|
if (fields_[i].first.has_value() && fields_[i].first.value() == name) {
|
||||||
|
return fields_[i].second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
std::optional<utils::IdType> VariantType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
std::optional<utils::IdType> VariantType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
|
|
@ -144,6 +161,13 @@ bool VariantType::operator>(const VariantType& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> VariantType::GetFieldType(const std::string& name) const {
|
||||||
|
if (current_constructor_.has_value()) {
|
||||||
|
return constructors_.at(current_constructor_.value()).GetFieldType(name);
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
std::optional<utils::IdType> OptionalType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
std::optional<utils::IdType> OptionalType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
|
|
@ -168,6 +192,10 @@ bool OptionalType::operator>(const OptionalType& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> OptionalType::GetFieldType(const std::string& name) const {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
std::optional<utils::IdType> ReferenceToType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
std::optional<utils::IdType> ReferenceToType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
|
|
@ -192,6 +220,10 @@ bool ReferenceToType::operator>(const ReferenceToType& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& name) const {
|
||||||
|
return type_manager_->GetAnyType(type_)->GetFieldType(name);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
std::optional<utils::IdType> FunctionType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
std::optional<utils::IdType> FunctionType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
|
|
@ -239,6 +271,10 @@ bool FunctionType::operator>(const FunctionType& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> FunctionType::GetFieldType(const std::string& name) const {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
std::optional<utils::IdType> ArrayType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
std::optional<utils::IdType> ArrayType::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
|
|
@ -264,6 +300,10 @@ bool ArrayType::operator>(const ArrayType& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> ArrayType::GetFieldType(const std::string& name) const {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
std::optional<utils::IdType> Type::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
std::optional<utils::IdType> Type::InContext(const std::unordered_map<std::string, utils::IdType>& context) {
|
||||||
|
|
@ -286,6 +326,8 @@ std::optional<utils::IdType> Type::InContext(const std::unordered_map<std::strin
|
||||||
return std::get<FunctionType>(type_).InContext(context);
|
return std::get<FunctionType>(type_).InContext(context);
|
||||||
case 7:
|
case 7:
|
||||||
return std::get<ArrayType>(type_).InContext(context);
|
return std::get<ArrayType>(type_).InContext(context);
|
||||||
|
case 8:
|
||||||
|
return std::get<OptionalType>(type_).InContext(context);
|
||||||
default:
|
default:
|
||||||
// error
|
// error
|
||||||
break;
|
break;
|
||||||
|
|
@ -316,6 +358,8 @@ bool Type::Same(const Type& type) const {
|
||||||
return std::get<FunctionType>(type_).Same(std::get<FunctionType>(type.type_));
|
return std::get<FunctionType>(type_).Same(std::get<FunctionType>(type.type_));
|
||||||
case 7:
|
case 7:
|
||||||
return std::get<ArrayType>(type_).Same(std::get<ArrayType>(type.type_));
|
return std::get<ArrayType>(type_).Same(std::get<ArrayType>(type.type_));
|
||||||
|
case 8:
|
||||||
|
return std::get<OptionalType>(type_).Same(std::get<OptionalType>(type.type_));
|
||||||
default:
|
default:
|
||||||
// error
|
// error
|
||||||
break;
|
break;
|
||||||
|
|
@ -347,6 +391,8 @@ bool Type::operator<(const Type& type) const {
|
||||||
return std::get<FunctionType>(type_) < std::get<FunctionType>(type.type_);
|
return std::get<FunctionType>(type_) < std::get<FunctionType>(type.type_);
|
||||||
case 7:
|
case 7:
|
||||||
return std::get<ArrayType>(type_) < std::get<ArrayType>(type.type_);
|
return std::get<ArrayType>(type_) < std::get<ArrayType>(type.type_);
|
||||||
|
case 8:
|
||||||
|
return std::get<OptionalType>(type_) < std::get<OptionalType>(type.type_);
|
||||||
default:
|
default:
|
||||||
// error
|
// error
|
||||||
break;
|
break;
|
||||||
|
|
@ -360,24 +406,63 @@ bool Type::operator>(const Type& type) const {
|
||||||
return type < *this;
|
return type < *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> Type::GetFieldType(const std::string& name) const {
|
||||||
|
size_t index = type_.index();
|
||||||
|
|
||||||
|
switch (index) {
|
||||||
|
case 0:
|
||||||
|
return std::get<AbstractType>(type_).GetFieldType(name);
|
||||||
|
case 1:
|
||||||
|
return std::get<DefinedType>(type_).GetFieldType(name);
|
||||||
|
case 2:
|
||||||
|
return std::nullopt;
|
||||||
|
case 3:
|
||||||
|
return std::get<TupleType>(type_).GetFieldType(name);
|
||||||
|
case 4:
|
||||||
|
return std::get<VariantType>(type_).GetFieldType(name);
|
||||||
|
case 5:
|
||||||
|
return std::get<ReferenceToType>(type_).GetFieldType(name);
|
||||||
|
case 6:
|
||||||
|
return std::get<FunctionType>(type_).GetFieldType(name);
|
||||||
|
case 7:
|
||||||
|
return std::get<ArrayType>(type_).GetFieldType(name);
|
||||||
|
case 8:
|
||||||
|
return std::get<OptionalType>(type_).GetFieldType(name);
|
||||||
|
default:
|
||||||
|
// error
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
utils::IdType TypeManager::AddType(const T&& type) {
|
utils::IdType TypeManager::AddType(T&& type, utils::ValueType value_type) {
|
||||||
types_.emplace_back(std::forward(type));
|
types_.emplace_back({std::forward(type), value_type});
|
||||||
|
return types_.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::IdType TypeManager::AddAnyType(Type&& type, utils::ValueType value_type) {
|
||||||
|
types_.push_back({std::move(type), value_type});
|
||||||
return types_.size() - 1;
|
return types_.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::optional<T*> TypeManager::GetType(utils::IdType type_id) {
|
std::optional<T*> TypeManager::GetType(utils::IdType type_id) {
|
||||||
if (!std::holds_alternative<T>(types_.at(type_id))) {
|
if (!std::holds_alternative<T>(types_.at(type_id).second)) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
return &std::get<T>(types_.at(type_id));
|
return &std::get<T>(types_.at(type_id).second);
|
||||||
}
|
}
|
||||||
|
|
||||||
Type* TypeManager::GetAnyType(utils::IdType type_id) {
|
Type* TypeManager::GetAnyType(utils::IdType type_id) {
|
||||||
return &types_.at(type_id);
|
return &types_.at(type_id).first;
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::ValueType TypeManager::GetValueType(utils::IdType type_id) {
|
||||||
|
return types_.at(type_id).second;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TypeManager::EqualTypes(utils::IdType first_type, utils::IdType second_type) {
|
bool TypeManager::EqualTypes(utils::IdType first_type, utils::IdType second_type) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue