typeclass tree start

This commit is contained in:
ProgramSnail 2023-05-07 15:17:37 +03:00
parent c1dec6a0d1
commit 173d50672a
15 changed files with 1475 additions and 35 deletions

Binary file not shown.

View file

@ -50,9 +50,9 @@ void LinkSymbolsVisitor::Visit(TypeExpression* node) { // TODO: check
node->type_id_ = namespace_visitor_.FindType(path, node->type.type);
node->constructor_id_ = namespace_visitor_.FindConstructor(path, node->type.type);
if (!node->type_id_.has_value() && !node->constructor_id_.has_value()) {
error_handling::HandleTypecheckError("Type or constructor not found", node->base);
}
// if (!node->type_id_.has_value() && !node->constructor_id_.has_value()) { // TODO: check, that not bastract types
// error_handling::HandleTypecheckError("Type or constructor not found", node->base);
// }
if (node->constructor_id_.has_value()) {
utils::IdType constructor_type_id = namespace_visitor_.GetGlobalInfo()->GetConstructorInfo(node->constructor_id_.value()).type_id;

View file

@ -4,9 +4,15 @@
// for clangd
#include "../include/parse_tree.hpp"
#include "../include/global_info.hpp"
#include "../include/type_info_contexts.hpp"
#include "../include/interpreter_tree.hpp"
#include "../include/build_visitor.hpp"
#include "../include/print_visitor.hpp"
#include "../include/find_symbols_visitor.hpp"
#include "../include/link_symbols_visitor.hpp"
#include "../include/type_check_visitor.hpp"
#include "../include/typed_print_visitor.hpp"
#include "../include/error_handling.hpp"
int main(int argc, char** argv) { // TODO, only test version
@ -37,9 +43,19 @@ int main(int argc, char** argv) { // TODO, only test version
std::unique_ptr<interpreter::tokens::SourceFile> source_file =
std::make_unique<interpreter::tokens::SourceFile>();
info::GlobalInfo global_info;
info::TypeInfoContextManager context_manager;
interpreter::BuildVisitor build_visitor(parse_tree);
interpreter::PrintVisitor print_visitor(std::cout);
// interpreter::PrintVisitor print_visitor(std::cout);
interpreter::FindSymbolsVisitor find_symbols_visitor(global_info);
interpreter::LinkSymbolsVisitor link_symbols_visitor(global_info);
interpreter::TypeCheckVisitor type_check_visitor(global_info, context_manager);
interpreter::TypedPrintVisitor typed_print_visitor(std::cout, context_manager);
build_visitor.VisitSourceFile(source_file.get());
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());
typed_print_visitor.VisitSourceFile(source_file.get());
}

View file

@ -19,6 +19,8 @@ void TypeCheckVisitor::Visit(SourceFile* node) {
Visitor::Visit(statement);
}
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
// Namespaces, partitions -----------------
@ -28,11 +30,15 @@ void TypeCheckVisitor::Visit(PartitionSources* node) {
Visitor::Visit(statement);
}
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(Partition* node) {
Visit(&node->scope);
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(NamespaceSources* node) {
@ -40,6 +46,8 @@ void TypeCheckVisitor::Visit(NamespaceSources* node) {
Visitor::Visit(statement);
}
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for class: const and var
@ -78,6 +86,8 @@ void TypeCheckVisitor::Visit(Namespace* node) { // TODO: two var namespces for c
namespace_visitor_.ExitNamespace();
context_manager_.ExitContext();
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
// Definitions -----------------
@ -107,6 +117,8 @@ void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
// Visit(node->value.get());
//
// context_manager_.ExitContext();
//
// node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
@ -127,6 +139,8 @@ void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
@ -138,6 +152,8 @@ void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
if (!was_in_statement) {
is_in_statement_ = false;
}
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
@ -190,6 +206,8 @@ void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
@ -198,6 +216,8 @@ void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
@ -219,6 +239,8 @@ void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
@ -227,20 +249,28 @@ void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
is_in_statement_ = false;
node->base.type_ = current_type_;
}
// Definition parts
void TypeCheckVisitor::Visit(FunctionDefinition* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(TypeDefinition* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(AnyAnnotatedType* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Unit, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
// Flow control -----------------
@ -313,6 +343,8 @@ void TypeCheckVisitor::Visit(TypeConstructorPattern* node) { // TODO: match name
Visitor::Visit(*type_info.value);
current_type_ = TypeInContext(current_type_, context);
current_type_ = context_manager_.ToModifiedType(current_type_, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(MatchCase* node) {
@ -339,6 +371,8 @@ void TypeCheckVisitor::Visit(MatchCase* node) {
Visitor::Visit(node->statement.value());
}
// current_type_ from statement is current_type_ for MatchCase
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(Match* node) { // TODO: move value to match
@ -376,6 +410,8 @@ void TypeCheckVisitor::Visit(Match* node) { // TODO: move value to match
}
current_type_ = type;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(Condition* node) {
@ -413,6 +449,8 @@ void TypeCheckVisitor::Visit(Condition* node) {
info::type::OptionalType(type, context_manager_.GetTypeManager()),
utils::ValueType::Tmp); // ??
}
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(DoWhileLoop* node) {
@ -443,6 +481,8 @@ void TypeCheckVisitor::Visit(WhileLoop* node) {
current_type_ = context_manager_.AddType(
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(ForLoop* node) {
@ -479,6 +519,8 @@ void TypeCheckVisitor::Visit(LoopLoop* node) {
current_type_ = context_manager_.AddType(
info::type::ArrayType(0, current_type_, context_manager_.GetTypeManager()),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
// Statements, expressions, blocks, etc. -----------------
@ -511,11 +553,15 @@ void TypeCheckVisitor::Visit(Block* node) {
}
current_type_ = type;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(ScopedStatement* node) {
Visitor::Visit(node->statement);
// current_type_ is type of statement
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum
@ -524,7 +570,7 @@ void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum
// Operators
// TODO
// TODO: better structure
void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
// TODO: Check, that type is not abstract ??
auto maybe_operator_id = namespace_visitor_.FindFunction(std::nullopt, node->operator_name);
@ -632,8 +678,11 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
}
Visitor::Visit(*operator_info.declaration.value().argument_types.back());
node->base.type_ = current_type_;
}
// TODO: can be method ??
void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) {
// TODO: Check, that type is not abstract ??
auto maybe_operator_id = namespace_visitor_.FindFunction({}, node->operator_name);
@ -652,7 +701,7 @@ void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) {
error_handling::HandleTypecheckError("Operator definition not found", node->base);
}
if (operator_info.argument_count != 1) {
if (operator_info.argument_count != 2) { // 1 + return type
error_handling::HandleTypecheckError("Operator wrong argument count", node->base);
}
@ -667,6 +716,10 @@ void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) {
if (!context_manager_.AddTypeRequirement(current_type_, expression_type)) {
error_handling::HandleTypecheckError("Operator expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types.back());
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(ReferenceExpression* node) {
@ -693,6 +746,8 @@ void TypeCheckVisitor::Visit(AccessExpression* node) {
}
current_type_ = maybe_type_value.value()->GetElementsType();
node->base.type_ = current_type_;
}
// Other Expressions
@ -701,6 +756,7 @@ void TypeCheckVisitor::Visit(AccessExpression* node) {
// TODO: builtin functions/methods
// TODO: alias types, abstract types, etc.
// TODO: deduce function parameter types
// TODO: find functions in typeclasses
void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
std::optional<utils::IdType> maybe_function_id;
std::unordered_map<std::string, utils::IdType> context;
@ -830,6 +886,8 @@ void TypeCheckVisitor::Visit(TupleExpression* node) {
current_type_ = context_manager_.AddType(
info::type::TupleType(std::nullopt, fields, context_manager_.GetTypeManager()),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
// ??
@ -847,11 +905,15 @@ void TypeCheckVisitor::Visit(VariantExpression* node) {
current_type_ = context_manager_.AddType(info::type::VariantType(std::nullopt, constructors, -1),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(ReturnExpression* node) {
Visitor::Visit(node->expression);
returned_type_ = current_type_;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(TypeConstructorParameter* node) {} // Handeled in TypeConstructor visit
@ -931,11 +993,15 @@ void TypeCheckVisitor::Visit(TypeConstructor* node) {
}
current_type_ = TypeInContext(current_type_, context);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(LambdaFunction* node) { // TODO
error_handling::HandleInternalError("Unimplemented (unsolved type deduction problems)",
"TypeCheckVisitor.LambdaFunction");
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(ArrayExpression* node) {
@ -959,6 +1025,8 @@ void TypeCheckVisitor::Visit(ArrayExpression* node) {
current_type_ = context_manager_.AddType(
info::type::ArrayType(node->elements.size(), elements_type, context_manager_.GetTypeManager()),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
// Name
@ -993,6 +1061,8 @@ void TypeCheckVisitor::Visit(NameExpression* node) {
}
current_type_ = context_manager_.ToModifiedType(current_type_, variable_value_type);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(TupleName* node) {
@ -1034,6 +1104,8 @@ void TypeCheckVisitor::Visit(TupleName* node) {
}
current_type_ = type;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(VariantName* node) {
@ -1084,6 +1156,8 @@ void TypeCheckVisitor::Visit(VariantName* node) {
}
current_type_ = type;
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(AnnotatedName* node) {
@ -1109,6 +1183,8 @@ void TypeCheckVisitor::Visit(AnnotatedName* node) {
error_handling::HandleTypecheckError("Wrong type annotation in annotated name", node->base);
}
}
node->base.type_ = current_type_;
}
// Type, typeclass, etc. -----------------
@ -1129,6 +1205,8 @@ void TypeCheckVisitor::Visit(FunctionType* node) {
current_type_ = context_manager_.AddType(
info::type::FunctionType(argument_types, return_type, context_manager_.GetTypeManager()),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(TupleType* node) {
@ -1143,6 +1221,8 @@ void TypeCheckVisitor::Visit(TupleType* node) {
current_type_ = context_manager_.AddType(
info::type::TupleType(node->type, fields, context_manager_.GetTypeManager()),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(VariantType* node) {
@ -1167,6 +1247,8 @@ void TypeCheckVisitor::Visit(VariantType* node) {
current_type_ = context_manager_.AddType(info::type::VariantType(node->type, constructors, -1),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
// TODO handle local abstract types, abstract types, aliases, etc.
@ -1194,6 +1276,8 @@ void TypeCheckVisitor::Visit(TypeExpression* node) {
info::type::ArrayType(node->array_size.value(), current_type_, context_manager_.GetTypeManager()),
utils::ValueType::Tmp);
}
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(ExtendedScopedAnyType* node) {
@ -1202,6 +1286,8 @@ void TypeCheckVisitor::Visit(ExtendedScopedAnyType* node) {
current_type_ = context_manager_.AddType(
info::type::ReferenceToType(node->references, current_type_, context_manager_.GetTypeManager()),
utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
// Typeclass
@ -1210,6 +1296,8 @@ void TypeCheckVisitor::Visit(ParametrizedTypeclass* node) { // TODO ??
for (auto& parameter : node->parameters) {
Visit(parameter.get());
}
node->base.type_ = current_type_;
}
// Typeclass & Type -----------------
@ -1220,18 +1308,26 @@ void TypeCheckVisitor::Visit(ParametrizedType* node) {} // Handled in TypeExpres
void TypeCheckVisitor::Visit(FloatNumberLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Float, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(NumberLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Int, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(StringLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::String, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
void TypeCheckVisitor::Visit(CharLiteral* node) {
current_type_ = context_manager_.AddType(info::type::InternalType::Char, utils::ValueType::Tmp);
node->base.type_ = current_type_;
}
//

1061
src/typed_print_visitor.cpp Normal file

File diff suppressed because it is too large Load diff

View file

@ -436,6 +436,36 @@ std::optional<utils::IdType> Type::GetFieldType(const std::string& name) const {
return std::nullopt;
}
std::string Type::GetTypeName() const {
size_t index = type_.index();
switch (index) {
case 0:
return "AbstractType";
case 1:
return "DefinedType";
case 2:
return "Builtin";
case 3:
return "TupleType";
case 4:
return "VariantType";
case 5:
return "ReferenceToType";
case 6:
return "FunctionType";
case 7:
return "ArrayType";
case 8:
return "OptionalType";
default:
// error
break;
}
return ""; // ??
}
//
template<typename T>