fixes, new examples

This commit is contained in:
ProgramSnail 2023-05-23 11:54:15 +03:00
parent 823fa30fa8
commit 7f4266821c
15 changed files with 322 additions and 74 deletions

View file

@ -924,8 +924,7 @@ void BuildVisitor::Visit(ReferenceExpression* node) {
}
current_node_ = parse_node.ChildByFieldName("expression");
node->expression = std::make_unique<ScopedStatement>();
Visit(node->expression.get());
Visit(node->expression);
current_node_ = parse_node;
}
@ -1053,7 +1052,8 @@ void BuildVisitor::Visit(TypeConstructorParameter* node) {
size_t child_count = parse_node.NamedChildCount();
if (child_count > 1) {
node->name = parse_node.ChildByFieldName("name").GetValue();
current_node_ = parse_node.ChildByFieldName("name");
node->name = current_node_.GetValue();
std::string assignment_modifier = current_node_.NextSibling().GetValue();
if (assignment_modifier == "=") {

View file

@ -386,7 +386,7 @@ void ExecuteVisitor::Visit(LoopControlExpression& node) {
void ExecuteVisitor::Visit(ReferenceExpression* node) {
// TODO: check, that there is no references to "Tmp"??
Visit(node->expression.get());
Visitor::Visit(node->expression);
utils::ValueType value_type = context_manager_.GetValueType(current_value_);
@ -748,7 +748,7 @@ void ExecuteVisitor::Visit(TupleName* node) {
std::optional<info::value::TupleValue*> maybe_tuple_value = context_manager_.GetValue<info::value::TupleValue>(value);
if (maybe_tuple_value.has_value()) {
if (!maybe_tuple_value.has_value()) {
error_handling::HandleRuntimeError("Mismatched value types in tuple variable definition", node->base);
}

View file

@ -59,9 +59,17 @@ int main(int argc, char** argv) { // TODO, only test version
// std::cout << "\n---------------------------------- Untyped -------------------------------------\n\n";
// print_visitor.VisitSourceFile(source_file.get());
find_symbols_visitor.VisitSourceFile(source_file.get());
link_symbols_visitor.VisitSourceFile(source_file.get());
type_check_visitor.VisitSourceFile(source_file.get());
try {
find_symbols_visitor.VisitSourceFile(source_file.get());
} catch (...) { error_handling::HandleInternalError("find_symbols_visitor exception", "main", std::nullopt); }
try {
link_symbols_visitor.VisitSourceFile(source_file.get());
} catch (...) { error_handling::HandleInternalError("link_symbols_visitor exception", "main", std::nullopt); }
try {
type_check_visitor.VisitSourceFile(source_file.get());
} catch (...) { error_handling::HandleInternalError("type_check_visitor exception", "main", std::nullopt); }
std::optional<utils::IdType> maybe_main_partition_id = global_info.FindPartition({"main"});
@ -72,13 +80,15 @@ int main(int argc, char** argv) { // TODO, only test version
const info::GlobalInfo::PartitionInfo& main_partition =
global_info.GetPartitionInfo(maybe_main_partition_id.value());
std::cout << "\n---------------------------------- Execution -------------------------------------\n\n";
// std::cout << "\n---------------------------------- Execution -------------------------------------\n\n";
interpreter::ExecuteVisitor execute_visitor(global_info,
type_context_manager,
context_manager);
execute_visitor.ExecutePartition(main_partition.node);
try {
execute_visitor.ExecutePartition(main_partition.node);
} catch (...) { error_handling::HandleInternalError("execute_visitor exception", "main", std::nullopt); }
// std::cout << "\n---------------------------------- Typed -------------------------------------\n\n";
// typed_print_visitor.VisitSourceFile(source_file.get());

View file

@ -398,7 +398,7 @@ void PrintVisitor::Visit(ReferenceExpression* node) {
break;
}
out_ << "] (";
Visit(node->expression.get());
Visitor::Visit(node->expression);
out_ << ')';
}

View file

@ -58,13 +58,11 @@ void TypeCheckVisitor::Visit(Namespace* node) {
info::definition::AnyType* type_info = maybe_type_info.value();
Visitor::Visit(type_info->node->value);
utils::IdType type = context_manager_.AddValue(
info::type::DefinedType(node->link_type_id_.value(),
current_type_,
type_info->modifier,
context_manager_.GetValueManager()),
ClassInternalsModifierToValueType(node->modifier));
// make parameter local types
VisitDefinedType(type_info, {}, ClassInternalsModifierToValueType(node->modifier));
utils::IdType type = current_type_;
if (node->modifier != utils::ClassInternalsModifier::Static) {
context_manager_.DefineVariable(utils::ClassInternalVarName, type);
@ -248,7 +246,9 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
if (!returned_type_.has_value()) {
returned_type_ = current_type_;
}
if (!context_manager_.EqualValues(returned_type, returned_type_.value())) {
// error_handling::DebugPrint(context_manager_.GetAnyValue(returned_type)->ToString() + " : " + context_manager_.GetAnyValue(returned_type_.value())->ToString());
error_handling::HandleTypecheckError("Wrong function return type", node->base);
}
returned_type_ = std::nullopt;
@ -435,9 +435,7 @@ void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match name
}
}
Visitor::Visit(type_info.node->value);
current_type_ = TypeInContext(current_type_, context);
current_type_ = context_manager_.ToModifiedValue(current_type_, utils::ValueType::Tmp);
VisitDefinedType(&type_info, context, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
@ -696,7 +694,7 @@ void TypeCheckVisitor::Visit(LoopControlExpression&) { // enum
// Operators
void TypeCheckVisitor::Visit(ReferenceExpression* node) {
Visit(node->expression.get());
Visitor::Visit(node->expression);
current_type_ = context_manager_.AddValue(
info::type::ReferenceToType({node->reference},
@ -983,6 +981,8 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
}
}
// TODO: replace with VisitDefinedType ??
AddTypeParameterLocalTypes(&type_info);
Visitor::Visit(type_info.node->value);
std::optional<info::type::VariantType*> maybe_variant_type =
@ -1293,7 +1293,7 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
&node->base);
}
VisitDefinedType(maybe_type_info.value(), context);
VisitDefinedType(maybe_type_info.value(), context, utils::ValueType::Tmp);
} else {
error_handling::HandleTypecheckError("Type not found", node->base);
}
@ -1316,9 +1316,11 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
void TypeCheckVisitor::Visit(ExtendedScopedAnyType* node) {
Visitor::Visit(node->type);
current_type_ = context_manager_.AddValue(
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetValueManager()),
utils::ValueType::Tmp);
if (!node->references.empty()) {
current_type_ = context_manager_.AddValue(
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetValueManager()),
utils::ValueType::Tmp);
}
node->base.type_ = current_type_;
}
@ -1540,7 +1542,7 @@ std::optional<FunctionDeclaration*>
&node->base);
}
VisitDefinedType(maybe_type_info.value(), context);
VisitDefinedType(maybe_type_info.value(), context, utils::ValueType::Tmp);
maybe_function_declaration =
FindDefinedTypeFunctionAndUpdate(node,
maybe_type_info.value(),
@ -1580,7 +1582,7 @@ std::optional<FunctionDeclaration*> TypeCheckVisitor::FindFunctionAndUpdate(Func
&node->base);
}
VisitDefinedType(maybe_type_info.value(), {}); // TODO: context ??
VisitDefinedType(maybe_type_info.value(), {}, utils::ValueType::Tmp); // TODO: context ??
maybe_function_declaration =
FindDefinedTypeFunctionAndUpdate(node,
maybe_type_info.value(),

View file

@ -557,7 +557,7 @@ void TypedPrintVisitor::Visit(ReferenceExpression* node) {
break;
}
out_ << "] (";
Visit(node->expression.get());
Visitor::Visit(node->expression);
out_ << ')';
}

View file

@ -120,13 +120,13 @@ std::optional<utils::IdType> TupleType::GetFieldType(const std::string& name,
return std::nullopt;
}
std::string TupleType::ToString() {
std::string TupleType::ToString() const {
std::string result;
result += "(";
for (auto& field : fields_) {
result += "& ";
result += " & ";
result += type_manager_->GetAnyValue(field.second)->ToString();
}
@ -185,7 +185,7 @@ std::optional<utils::IdType> VariantType::GetFieldType(const std::string& name,
return std::nullopt;
}
std::string VariantType::ToString() {
std::string VariantType::ToString() const {
std::string result;
result += "(";
@ -229,7 +229,7 @@ std::optional<utils::IdType> OptionalType::GetFieldType(const std::string&,
return std::nullopt;
}
std::string OptionalType::ToString() {
std::string OptionalType::ToString() const {
return "Optional " + type_manager_->GetAnyValue(type_)->ToString();
}
@ -263,7 +263,7 @@ std::optional<utils::IdType> ReferenceToType::GetFieldType(const std::string& na
}
std::string ReferenceToType::ToString() {
std::string ReferenceToType::ToString() const {
std::string result;
for (auto& reference : references_) {
@ -337,7 +337,7 @@ std::optional<utils::IdType> FunctionType::GetFieldType(const std::string&,
return std::nullopt;
}
std::string FunctionType::ToString() {
std::string FunctionType::ToString() const {
std::string result;
result += "(";
@ -387,7 +387,7 @@ std::optional<utils::IdType> ArrayType::GetFieldType(const std::string&,
return std::nullopt;
}
std::string ArrayType::ToString() {
std::string ArrayType::ToString() const {
return "Array (" + std::to_string(size_) + ") " + type_manager_->GetAnyValue(elements_type_)->ToString();
}
@ -554,7 +554,7 @@ std::string Type::GetTypeName() const {
return ""; // ??
}
std::string Type::ToString() {
std::string Type::ToString() const {
size_t index = type_.index();
switch (index) {
@ -562,7 +562,7 @@ std::string Type::ToString() {
return std::get<AbstractType>(type_).ToString();
case 1:
return std::get<DefinedType>(type_).ToString();
case 2:
case 2: // ??
return ::info::type::ToString(std::get<InternalType>(type_));
case 3:
return std::get<TupleType>(type_).ToString();

View file

@ -466,7 +466,7 @@ void Visitor::Visit(LoopControlExpression&) {} // enum
// Operators
void Visitor::Visit(ReferenceExpression* node) {
Visit(node->expression.get());
Visit(node->expression);
}
void Visitor::Visit(AccessExpression* node) {