mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2026-01-25 13:07:13 +00:00
optional variable deffinition added, annotations removed
This commit is contained in:
parent
7ce8513ad0
commit
de7c1e062f
20 changed files with 178 additions and 57 deletions
|
|
@ -99,9 +99,10 @@ private:
|
||||||
|
|
||||||
void Visit(PartitionName* node) override;
|
void Visit(PartitionName* node) override;
|
||||||
void Visit(NameExpression* node) override;
|
void Visit(NameExpression* node) override;
|
||||||
|
void Visit(OptionalName* node) override;
|
||||||
void Visit(TupleName* node) override;
|
void Visit(TupleName* node) override;
|
||||||
void Visit(VariantName* node) override;
|
void Visit(VariantName* node) override;
|
||||||
void Visit(AnnotatedName* node) override;
|
void Visit(Name* node) override;
|
||||||
|
|
||||||
void Visit(AnyName& node) override; // variant
|
void Visit(AnyName& node) override; // variant
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,9 +108,10 @@ private:
|
||||||
|
|
||||||
// // void Visit(PartitionName* node) override;
|
// // void Visit(PartitionName* node) override;
|
||||||
void Visit(NameExpression* node) override;
|
void Visit(NameExpression* node) override;
|
||||||
|
void Visit(OptionalName* node) override;
|
||||||
void Visit(TupleName* node) override;
|
void Visit(TupleName* node) override;
|
||||||
void Visit(VariantName* node) override;
|
void Visit(VariantName* node) override;
|
||||||
void Visit(AnnotatedName* node) override;
|
void Visit(Name* node) override;
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
||||||
|
|
@ -190,8 +191,12 @@ private:
|
||||||
// local types store types graph ids
|
// local types store types graph ids
|
||||||
|
|
||||||
utils::IdType current_value_;
|
utils::IdType current_value_;
|
||||||
|
|
||||||
std::optional<LoopControlExpression> active_loop_control_expression_;
|
std::optional<LoopControlExpression> active_loop_control_expression_;
|
||||||
|
|
||||||
std::optional<utils::IsConstModifier> is_const_definition_;
|
std::optional<utils::IsConstModifier> is_const_definition_;
|
||||||
|
bool is_definition_failed_ = false;
|
||||||
|
|
||||||
bool case_matched_;
|
bool case_matched_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,9 +84,10 @@ private:
|
||||||
|
|
||||||
// // void Visit(PartitionName* node) override;
|
// // void Visit(PartitionName* node) override;
|
||||||
// // void Visit(NameExpression* node) override;
|
// // void Visit(NameExpression* node) override;
|
||||||
|
// // void Visit(OptionalName* node) override;
|
||||||
// // void Visit(TupleName* node) override;
|
// // void Visit(TupleName* node) override;
|
||||||
// // void Visit(VariantName* node) override;
|
// // void Visit(VariantName* node) override;
|
||||||
// // void Visit(AnnotatedName* node) override;
|
// // void Visit(Name* node) override;
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -192,13 +192,15 @@ struct PartitionName {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct NameExpression;
|
struct NameExpression;
|
||||||
|
struct OptionalName;
|
||||||
struct TupleName;
|
struct TupleName;
|
||||||
struct VariantName;
|
struct VariantName;
|
||||||
struct AnnotatedName;
|
struct Name;
|
||||||
|
|
||||||
// // ScopedAnyName <-> AnyName
|
// // ScopedAnyName <-> AnyName
|
||||||
using AnyName = std::variant<
|
using AnyName = std::variant<
|
||||||
std::unique_ptr<AnnotatedName>,
|
std::unique_ptr<Name>,
|
||||||
|
std::unique_ptr<OptionalName>,
|
||||||
std::unique_ptr<TupleName>,
|
std::unique_ptr<TupleName>,
|
||||||
std::unique_ptr<VariantName>>;
|
std::unique_ptr<VariantName>>;
|
||||||
|
|
||||||
|
|
@ -599,6 +601,12 @@ struct NameExpression {
|
||||||
std::vector<NameIdentifier> names;
|
std::vector<NameIdentifier> names;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct OptionalName {
|
||||||
|
BaseNode base;
|
||||||
|
|
||||||
|
ScopedAnyName name;
|
||||||
|
};
|
||||||
|
|
||||||
struct TupleName {
|
struct TupleName {
|
||||||
BaseNode base;
|
BaseNode base;
|
||||||
|
|
||||||
|
|
@ -611,11 +619,10 @@ struct VariantName {
|
||||||
std::vector<ScopedAnyName> names;
|
std::vector<ScopedAnyName> names;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AnnotatedName {
|
struct Name {
|
||||||
BaseNode base;
|
BaseNode base;
|
||||||
|
|
||||||
NameIdentifier name;
|
NameIdentifier name;
|
||||||
std::optional<ScopedAnyType> type;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// ----------------- Type, typeclass, etc. -----------------
|
// ----------------- Type, typeclass, etc. -----------------
|
||||||
|
|
|
||||||
|
|
@ -93,9 +93,10 @@ private:
|
||||||
|
|
||||||
// // void Visit(PartitionName* node) override;
|
// // void Visit(PartitionName* node) override;
|
||||||
// // void Visit(NameExpression* node) override;
|
// // void Visit(NameExpression* node) override;
|
||||||
|
// // void Visit(OptionalName* node) override;
|
||||||
// // void Visit(TupleName* node) override;
|
// // void Visit(TupleName* node) override;
|
||||||
// // void Visit(VariantName* node) override;
|
// // void Visit(VariantName* node) override;
|
||||||
// // void Visit(AnnotatedName* node) override;
|
// // void Visit(Name* node) override;
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,9 +84,10 @@ const std::string LoopControlExpression = "loop_control_expression";
|
||||||
|
|
||||||
const std::string PartitionName = "partition_name";
|
const std::string PartitionName = "partition_name";
|
||||||
const std::string NameExpression = "name_expression";
|
const std::string NameExpression = "name_expression";
|
||||||
|
const std::string OptionalName = "optional_name";
|
||||||
const std::string TupleName = "tuple_name";
|
const std::string TupleName = "tuple_name";
|
||||||
const std::string VariantName = "variant_name";
|
const std::string VariantName = "variant_name";
|
||||||
const std::string AnnotatedName = "annotated_name";
|
const std::string Name = "name";
|
||||||
const std::string AnyName = "any_name";
|
const std::string AnyName = "any_name";
|
||||||
const std::string ScopedAnyName = "scoped_any_name";
|
const std::string ScopedAnyName = "scoped_any_name";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -82,9 +82,10 @@ private:
|
||||||
|
|
||||||
void Visit(PartitionName* node) override;
|
void Visit(PartitionName* node) override;
|
||||||
void Visit(NameExpression* node) override;
|
void Visit(NameExpression* node) override;
|
||||||
|
void Visit(OptionalName* node) override;
|
||||||
void Visit(TupleName* node) override;
|
void Visit(TupleName* node) override;
|
||||||
void Visit(VariantName* node) override;
|
void Visit(VariantName* node) override;
|
||||||
void Visit(AnnotatedName* node) override;
|
void Visit(Name* node) override;
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,9 +103,10 @@ private:
|
||||||
|
|
||||||
void Visit(PartitionName* node) override;
|
void Visit(PartitionName* node) override;
|
||||||
void Visit(NameExpression* node) override;
|
void Visit(NameExpression* node) override;
|
||||||
|
void Visit(OptionalName* node) override;
|
||||||
void Visit(TupleName* node) override;
|
void Visit(TupleName* node) override;
|
||||||
void Visit(VariantName* node) override;
|
void Visit(VariantName* node) override;
|
||||||
void Visit(AnnotatedName* node) override;
|
void Visit(Name* node) override;
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
||||||
|
|
@ -261,6 +262,7 @@ private:
|
||||||
std::unordered_map<info::type::InternalType, utils::IdType> internal_to_abstract_type_;
|
std::unordered_map<info::type::InternalType, utils::IdType> internal_to_abstract_type_;
|
||||||
|
|
||||||
std::optional<utils::IsConstModifier> is_const_definition_;
|
std::optional<utils::IsConstModifier> is_const_definition_;
|
||||||
|
bool definition_can_fail_ = false; // for optional patterns
|
||||||
|
|
||||||
bool is_in_statement_ = false;
|
bool is_in_statement_ = false;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -84,9 +84,10 @@ private:
|
||||||
|
|
||||||
void Visit(PartitionName* node) override;
|
void Visit(PartitionName* node) override;
|
||||||
void Visit(NameExpression* node) override;
|
void Visit(NameExpression* node) override;
|
||||||
|
void Visit(OptionalName* node) override;
|
||||||
void Visit(TupleName* node) override;
|
void Visit(TupleName* node) override;
|
||||||
void Visit(VariantName* node) override;
|
void Visit(VariantName* node) override;
|
||||||
void Visit(AnnotatedName* node) override;
|
void Visit(Name* node) override;
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -230,6 +230,11 @@ public:
|
||||||
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
const std::unordered_set<utils::IdType>& type_namespaces) const;
|
||||||
|
|
||||||
std::string ToString() const;
|
std::string ToString() const;
|
||||||
|
|
||||||
|
utils::IdType GetType() const {
|
||||||
|
return type_;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
utils::IdType type_;
|
utils::IdType type_;
|
||||||
TypeManager* type_manager_ = nullptr;
|
TypeManager* type_manager_ = nullptr;
|
||||||
|
|
|
||||||
|
|
@ -96,9 +96,10 @@ protected:
|
||||||
|
|
||||||
virtual void Visit(PartitionName* node);
|
virtual void Visit(PartitionName* node);
|
||||||
virtual void Visit(NameExpression* node);
|
virtual void Visit(NameExpression* node);
|
||||||
|
virtual void Visit(OptionalName* node);
|
||||||
virtual void Visit(TupleName* node);
|
virtual void Visit(TupleName* node);
|
||||||
virtual void Visit(VariantName* node);
|
virtual void Visit(VariantName* node);
|
||||||
virtual void Visit(AnnotatedName* node);
|
virtual void Visit(Name* node);
|
||||||
|
|
||||||
virtual void Visit(AnyName& node); // variant
|
virtual void Visit(AnyName& node); // variant
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit ab391d2d8d5251422abc94b80a51438faddb19c7
|
Subproject commit 7e17d1b2ed47065ec4ac537748da454a9f81a89e
|
||||||
|
|
@ -1202,6 +1202,17 @@ void BuildVisitor::Visit(NameExpression* node) {
|
||||||
current_node_ = parse_node;
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BuildVisitor::Visit(OptionalName* node) {
|
||||||
|
SetPosition(node->base, current_node_);
|
||||||
|
|
||||||
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.ChildByFieldName("name");
|
||||||
|
Visit(node->name);
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(TupleName* node) {
|
void BuildVisitor::Visit(TupleName* node) {
|
||||||
SetPosition(node->base, current_node_);
|
SetPosition(node->base, current_node_);
|
||||||
|
|
||||||
|
|
@ -1213,7 +1224,7 @@ void BuildVisitor::Visit(TupleName* node) {
|
||||||
|
|
||||||
for (size_t i = 0; i < names_count; ++i) {
|
for (size_t i = 0; i < names_count; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i);
|
||||||
node->names[i] = std::make_unique<AnnotatedName>();
|
node->names[i] = std::make_unique<Name>();
|
||||||
Visit(node->names[i]);
|
Visit(node->names[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1231,27 +1242,21 @@ void BuildVisitor::Visit(VariantName* node) {
|
||||||
|
|
||||||
for (size_t i = 0; i < names_count; ++i) {
|
for (size_t i = 0; i < names_count; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i);
|
||||||
node->names[i] =std::make_unique<AnnotatedName>();
|
node->names[i] =std::make_unique<Name>();
|
||||||
Visit(node->names[i]);
|
Visit(node->names[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
current_node_ = parse_node;
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(AnnotatedName* node) {
|
void BuildVisitor::Visit(Name* node) {
|
||||||
SetPosition(node->base, current_node_);
|
SetPosition(node->base, current_node_);
|
||||||
|
|
||||||
auto parse_node = current_node_;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
node->name = parse_node.ChildByFieldName("name").GetValue();
|
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||||
|
|
||||||
if (parse_node.NamedChildCount() > 1) {
|
current_node_ = parse_node;
|
||||||
current_node_ = parse_node.ChildByFieldName("type");
|
|
||||||
node->type.emplace();
|
|
||||||
Visit(node->type.value());
|
|
||||||
|
|
||||||
current_node_ = parse_node;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(AnyName& node) {
|
void BuildVisitor::Visit(AnyName& node) {
|
||||||
|
|
@ -1261,9 +1266,12 @@ void BuildVisitor::Visit(AnyName& node) {
|
||||||
|
|
||||||
std::string current_node_type = current_node_.GetType();
|
std::string current_node_type = current_node_.GetType();
|
||||||
|
|
||||||
if (current_node_type == parser::tokens::AnnotatedName) {
|
if (current_node_type == parser::tokens::Name) {
|
||||||
node = std::make_unique<AnnotatedName>();
|
node = std::make_unique<Name>();
|
||||||
Visit(std::get<std::unique_ptr<AnnotatedName>>(node).get());
|
Visit(std::get<std::unique_ptr<Name>>(node).get());
|
||||||
|
} else if (current_node_type == parser::tokens::OptionalName) {
|
||||||
|
node = std::make_unique<OptionalName>();
|
||||||
|
Visit(std::get<std::unique_ptr<OptionalName>>(node).get());
|
||||||
} else if (current_node_type == parser::tokens::TupleName) {
|
} else if (current_node_type == parser::tokens::TupleName) {
|
||||||
node = std::make_unique<TupleName>();
|
node = std::make_unique<TupleName>();
|
||||||
Visit(std::get<std::unique_ptr<TupleName>>(node).get());
|
Visit(std::get<std::unique_ptr<TupleName>>(node).get());
|
||||||
|
|
|
||||||
|
|
@ -55,14 +55,19 @@ void ExecuteVisitor::Visit(VariableDefinitionStatement* node) {
|
||||||
|
|
||||||
Visitor::Visit(node->value);
|
Visitor::Visit(node->value);
|
||||||
|
|
||||||
|
is_definition_failed_ = false;
|
||||||
is_const_definition_ = node->modifier;
|
is_const_definition_ = node->modifier;
|
||||||
Visitor::Visit(node->name); // current_type_ passed from value
|
Visitor::Visit(node->name); // current_type_ passed from value
|
||||||
is_const_definition_ = std::nullopt;
|
is_const_definition_ = std::nullopt;
|
||||||
|
|
||||||
if (node->in_expression.has_value()) {
|
if (node->in_expression.has_value()) {
|
||||||
Visitor::Visit(node->in_expression.value());
|
if (!is_definition_failed_) {
|
||||||
|
Visitor::Visit(node->in_expression.value());
|
||||||
|
}
|
||||||
context_manager_.ExitContext();
|
context_manager_.ExitContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is_definition_failed_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExecuteVisitor::Visit(FunctionDeclaration* node) {
|
void ExecuteVisitor::Visit(FunctionDeclaration* node) {
|
||||||
|
|
@ -754,6 +759,25 @@ void ExecuteVisitor::Visit(NameExpression* node) { // TODO: check
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ExecuteVisitor::Visit(OptionalName* node) {
|
||||||
|
utils::IdType value = current_value_;
|
||||||
|
|
||||||
|
std::optional<info::value::OptionalValue*> maybe_optional_value = context_manager_.GetValue<info::value::OptionalValue>(value);
|
||||||
|
|
||||||
|
if (!maybe_optional_value.has_value()) {
|
||||||
|
error_handling::HandleRuntimeError("Mismatched value types in optional variable definition", node->base);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maybe_optional_value.value()->value.has_value()) {
|
||||||
|
current_value_ = maybe_optional_value.value()->value.value();
|
||||||
|
Visitor::Visit(node->name);
|
||||||
|
} else {
|
||||||
|
is_definition_failed_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_value_ = value;
|
||||||
|
}
|
||||||
|
|
||||||
void ExecuteVisitor::Visit(TupleName* node) {
|
void ExecuteVisitor::Visit(TupleName* node) {
|
||||||
utils::IdType value = current_value_;
|
utils::IdType value = current_value_;
|
||||||
|
|
||||||
|
|
@ -817,7 +841,7 @@ void ExecuteVisitor::Visit(VariantName* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move, etc.
|
// TODO: move, etc.
|
||||||
void ExecuteVisitor::Visit(AnnotatedName* node) {
|
void ExecuteVisitor::Visit(Name* node) {
|
||||||
if (!is_const_definition_.has_value()) {
|
if (!is_const_definition_.has_value()) {
|
||||||
error_handling::HandleInternalError("No value in is_const_definition_",
|
error_handling::HandleInternalError("No value in is_const_definition_",
|
||||||
"TypeCheckVisitor.AnnotatedName",
|
"TypeCheckVisitor.AnnotatedName",
|
||||||
|
|
|
||||||
|
|
@ -577,6 +577,12 @@ void PrintVisitor::Visit(NameExpression* node) {
|
||||||
out_ << ')';
|
out_ << ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PrintVisitor::Visit(OptionalName* node) {
|
||||||
|
out_ << "[OptionalName] (";
|
||||||
|
Visitor::Visit(node->name);
|
||||||
|
out_ << ')';
|
||||||
|
}
|
||||||
|
|
||||||
void PrintVisitor::Visit(TupleName* node) {
|
void PrintVisitor::Visit(TupleName* node) {
|
||||||
out_ << "[TupleName] (";
|
out_ << "[TupleName] (";
|
||||||
for (auto& name : node->names) {
|
for (auto& name : node->names) {
|
||||||
|
|
@ -595,15 +601,10 @@ void PrintVisitor::Visit(VariantName* node) {
|
||||||
out_ << ')';
|
out_ << ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintVisitor::Visit(AnnotatedName* node) {
|
void PrintVisitor::Visit(Name* node) {
|
||||||
out_ << "[AnnotatedName ";
|
out_ << "[Name ";
|
||||||
Visit(&node->name);
|
Visit(&node->name);
|
||||||
out_ << ']';
|
out_ << ']';
|
||||||
if (node->type.has_value()) {
|
|
||||||
out_ << " : (";
|
|
||||||
Visitor::Visit(node->type.value());
|
|
||||||
out_ << ')';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,7 @@ void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
||||||
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
|
||||||
|
|
||||||
|
definition_can_fail_ = false;
|
||||||
is_const_definition_ = node->modifier;
|
is_const_definition_ = node->modifier;
|
||||||
Visitor::Visit(node->name);
|
Visitor::Visit(node->name);
|
||||||
is_const_definition_ = std::nullopt;
|
is_const_definition_ = std::nullopt;
|
||||||
|
|
@ -117,10 +118,16 @@ void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
||||||
if (node->in_expression.has_value()) {
|
if (node->in_expression.has_value()) {
|
||||||
Visitor::Visit(node->in_expression.value());
|
Visitor::Visit(node->in_expression.value());
|
||||||
context_manager_.ExitContext();
|
context_manager_.ExitContext();
|
||||||
|
} else {
|
||||||
|
if (definition_can_fail_) {
|
||||||
|
error_handling::HandleTypecheckError("Can't make definition that can fail (use \'in\' for associated block)", node->base);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
current_type_ = internal_to_abstract_type_.at(info::type::InternalType::Unit);
|
current_type_ = internal_to_abstract_type_.at(info::type::InternalType::Unit);
|
||||||
|
|
||||||
|
definition_can_fail_ = false;
|
||||||
|
|
||||||
node->base.type_ = current_type_;
|
node->base.type_ = current_type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1110,6 +1117,43 @@ void TypeCheckVisitor::Visit(NameExpression* node) {
|
||||||
node->base.type_ = current_type_;
|
node->base.type_ = current_type_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TypeCheckVisitor::Visit(OptionalName* node) {
|
||||||
|
utils::IdType type = current_type_;
|
||||||
|
|
||||||
|
std::optional<info::type::DefinedType*> maybe_defined_type_value = context_manager_.GetValue<info::type::DefinedType>(type);
|
||||||
|
if (maybe_defined_type_value.has_value()) {
|
||||||
|
type = maybe_defined_type_value.value()->GetType();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto maybe_type_value = context_manager_.GetValue<info::type::OptionalType>(type);
|
||||||
|
if (!maybe_type_value.has_value()) {
|
||||||
|
error_handling::HandleTypecheckError("Mismatched types in optional variable definition", node->base);
|
||||||
|
}
|
||||||
|
info::type::OptionalType* type_value = maybe_type_value.value();
|
||||||
|
|
||||||
|
if (!is_const_definition_.has_value()) {
|
||||||
|
error_handling::HandleInternalError("No value in is_const_definition_",
|
||||||
|
"TypeCheckVisitor.OptionalName",
|
||||||
|
&node->base);
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::ValueType value_type = context_manager_.GetValueType(type);
|
||||||
|
|
||||||
|
current_type_ = type_value->GetType();
|
||||||
|
|
||||||
|
if (value_type == utils::ValueType::Tmp) { // TODO: instead of recusive modification ??
|
||||||
|
current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
Visitor::Visit(node->name);
|
||||||
|
|
||||||
|
current_type_ = type;
|
||||||
|
|
||||||
|
definition_can_fail_ = true;
|
||||||
|
|
||||||
|
node->base.type_ = current_type_;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: move, etc.
|
// TODO: move, etc.
|
||||||
void TypeCheckVisitor::Visit(TupleName* node) {
|
void TypeCheckVisitor::Visit(TupleName* node) {
|
||||||
utils::IdType type = current_type_;
|
utils::IdType type = current_type_;
|
||||||
|
|
@ -1204,7 +1248,7 @@ void TypeCheckVisitor::Visit(VariantName* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: move, etc.
|
// TODO: move, etc.
|
||||||
void TypeCheckVisitor::Visit(AnnotatedName* node) {
|
void TypeCheckVisitor::Visit(Name* node) {
|
||||||
utils::IdType type = current_type_;
|
utils::IdType type = current_type_;
|
||||||
|
|
||||||
if (!is_const_definition_.has_value()) {
|
if (!is_const_definition_.has_value()) {
|
||||||
|
|
@ -1217,12 +1261,14 @@ void TypeCheckVisitor::Visit(AnnotatedName* node) {
|
||||||
if (!context_manager_.DefineVariable(node->name, type)) {
|
if (!context_manager_.DefineVariable(node->name, type)) {
|
||||||
error_handling::HandleTypecheckError("Variable name already present in context", node->base);
|
error_handling::HandleTypecheckError("Variable name already present in context", node->base);
|
||||||
}
|
}
|
||||||
if (node->type.has_value()) { // TODO: check
|
|
||||||
Visitor::Visit(node->type.value());
|
// --------- FOR ANNOTATIONS ---------
|
||||||
if (!context_manager_.EqualValues(type, current_type_)) {
|
// if (node->type.has_value()) {
|
||||||
error_handling::HandleTypecheckError("Wrong type annotation in annotated name", node->base);
|
// Visitor::Visit(node->type.value());
|
||||||
}
|
// if (!context_manager_.EqualValues(type, current_type_)) {
|
||||||
}
|
// error_handling::HandleTypecheckError("Wrong type annotation in annotated name", node->base);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
node->base.type_ = current_type_;
|
node->base.type_ = current_type_;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -801,6 +801,18 @@ void TypedPrintVisitor::Visit(NameExpression* node) {
|
||||||
out_ << ')';
|
out_ << ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TypedPrintVisitor::Visit(OptionalName* node) {
|
||||||
|
out_ << "[OptionalName : ";
|
||||||
|
|
||||||
|
if (node->base.type_.has_value()) {
|
||||||
|
out_ << context_manager_.GetAnyValue(node->base.type_.value())->GetTypeName();
|
||||||
|
}
|
||||||
|
|
||||||
|
out_ << "] (";
|
||||||
|
Visitor::Visit(node->name);
|
||||||
|
out_ << ')';
|
||||||
|
}
|
||||||
|
|
||||||
void TypedPrintVisitor::Visit(TupleName* node) {
|
void TypedPrintVisitor::Visit(TupleName* node) {
|
||||||
out_ << "[TupleName : ";
|
out_ << "[TupleName : ";
|
||||||
|
|
||||||
|
|
@ -831,8 +843,8 @@ void TypedPrintVisitor::Visit(VariantName* node) {
|
||||||
out_ << ')';
|
out_ << ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypedPrintVisitor::Visit(AnnotatedName* node) {
|
void TypedPrintVisitor::Visit(Name* node) {
|
||||||
out_ << "[AnnotatedName : (";
|
out_ << "[Name : (";
|
||||||
|
|
||||||
if (node->base.type_.has_value()) {
|
if (node->base.type_.has_value()) {
|
||||||
out_ << context_manager_.GetAnyValue(node->base.type_.value())->GetTypeName();
|
out_ << context_manager_.GetAnyValue(node->base.type_.value())->GetTypeName();
|
||||||
|
|
@ -841,11 +853,6 @@ void TypedPrintVisitor::Visit(AnnotatedName* node) {
|
||||||
out_ << ") ";
|
out_ << ") ";
|
||||||
Visit(&node->name);
|
Visit(&node->name);
|
||||||
out_ << ']';
|
out_ << ']';
|
||||||
if (node->type.has_value()) {
|
|
||||||
out_ << " : (";
|
|
||||||
Visitor::Visit(node->type.value());
|
|
||||||
out_ << ')';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
|
||||||
|
|
@ -222,12 +222,15 @@ void Visitor::Visit(SuperExpression& node) {
|
||||||
void Visitor::Visit(AnyName& node) {
|
void Visitor::Visit(AnyName& node) {
|
||||||
switch (node.index()) {
|
switch (node.index()) {
|
||||||
case 0:
|
case 0:
|
||||||
Visit(std::get<std::unique_ptr<AnnotatedName>>(node).get());
|
Visit(std::get<std::unique_ptr<Name>>(node).get());
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
Visit(std::get<std::unique_ptr<TupleName>>(node).get());
|
Visit(std::get<std::unique_ptr<OptionalName>>(node).get());
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
|
Visit(std::get<std::unique_ptr<TupleName>>(node).get());
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
Visit(std::get<std::unique_ptr<VariantName>>(node).get());
|
Visit(std::get<std::unique_ptr<VariantName>>(node).get());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
@ -561,6 +564,10 @@ void Visitor::Visit(NameExpression* node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Visitor::Visit(OptionalName* node) {
|
||||||
|
Visit(node->name);
|
||||||
|
}
|
||||||
|
|
||||||
void Visitor::Visit(TupleName* node) {
|
void Visitor::Visit(TupleName* node) {
|
||||||
for (auto& name : node->names) {
|
for (auto& name : node->names) {
|
||||||
Visit(name);
|
Visit(name);
|
||||||
|
|
@ -573,11 +580,8 @@ void Visitor::Visit(VariantName* node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Visitor::Visit(AnnotatedName* node) {
|
void Visitor::Visit(Name* node) {
|
||||||
Visit(&node->name);
|
Visit(&node->name);
|
||||||
if (node->type.has_value()) {
|
|
||||||
Visit(node->type.value());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type, typeclass, etc. -----------------
|
// Type, typeclass, etc. -----------------
|
||||||
|
|
|
||||||
|
|
@ -278,6 +278,9 @@ def scan-three = & \io..scan: & \io..scan: & \io..scan:
|
||||||
exec main {
|
exec main {
|
||||||
var n = scan-anything:[int]
|
var n = scan-anything:[int]
|
||||||
var x = (for _ in 0--n do scan-int:) // $array[int] & 0
|
var x = (for _ in 0--n do scan-int:) // $array[int] & 0
|
||||||
|
|
||||||
|
var k? = if n < 2 then n in print-anything:[string] "n < 2"
|
||||||
|
|
||||||
; print-anything:[int] n
|
; print-anything:[int] n
|
||||||
|
|
||||||
var & a & b & c = scan-three-t:
|
var & a & b & c = scan-three-t:
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ def test-variables : a = {
|
||||||
|
|
||||||
var z = 543.32 in {
|
var z = 543.32 in {
|
||||||
for x in 1--10 do print: 111
|
for x in 1--10 do print: 111
|
||||||
if scan:[int] < 11 then do-something-another: z "ccc"
|
if scan:[int] < 11 then do-something-another: z "aaa"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (& t? & _ & t3)? = maybe-something: in do-something-func: t1 t3
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue