build_visitor fixed, going to test it

This commit is contained in:
ProgramSnail 2023-04-25 21:21:36 +03:00
parent 5bf0c1bf48
commit c34523bd4f
23 changed files with 45468 additions and 45273 deletions

View file

@ -20,8 +20,8 @@ add_executable(lang_interpreter src/main.cpp
src/visitor.cpp src/visitor.cpp
src/build_visitor.cpp src/build_visitor.cpp
src/print_visitor.cpp src/print_visitor.cpp
src/find_symbols_visitor.cpp #src/find_symbols_visitor.cpp
src/global_info.cpp #src/global_info.cpp
src/parser.c src/parser.c
include/parser.h include/parser.h
tree-sitter/lib/src/lib.c) tree-sitter/lib/src/lib.c)

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
// for clangd // for clangd
#include "interpreter_tree.hpp"
#include "visitor.hpp" #include "visitor.hpp"
#include "parse_tree.hpp" #include "parse_tree.hpp"
@ -47,6 +48,8 @@ private:
// Flow control ----------------- // Flow control -----------------
void Visit(TypeConstructorPatternParameter* node) override;
void Visit(TypeConstructorPattern* node) override;
void Visit(MatchCase* node) override; void Visit(MatchCase* node) override;
void Visit(Match* node) override; void Visit(Match* node) override;
void Visit(Condition* node) override; void Visit(Condition* node) override;
@ -55,6 +58,8 @@ private:
void Visit(ForLoop* node) override; void Visit(ForLoop* node) override;
void Visit(LoopLoop* node) override; void Visit(LoopLoop* node) override;
void Visit(PatternToken& node) override; // variant
void Visit(Pattern& node) override; // variant
void Visit(FlowControl& node) override; // variant void Visit(FlowControl& node) override; // variant
// Statements, expressions, blocks, etc. ----------------- // Statements, expressions, blocks, etc. -----------------
@ -87,6 +92,7 @@ private:
void Visit(TupleExpression* node) override; void Visit(TupleExpression* node) override;
void Visit(VariantExpression* node) override; void Visit(VariantExpression* node) override;
void Visit(ReturnExpression* node) override; void Visit(ReturnExpression* node) override;
void Visit(TypeConstructorParameter* node) override;
void Visit(TypeConstructor* node) override; void Visit(TypeConstructor* node) override;
void Visit(LambdaFunction* node) override; void Visit(LambdaFunction* node) override;
void Visit(ArrayExpression* node) override; void Visit(ArrayExpression* node) override;
@ -109,7 +115,6 @@ private:
void Visit(FunctionType* node) override; void Visit(FunctionType* node) override;
void Visit(TupleType* node) override; void Visit(TupleType* node) override;
void Visit(VariantType* node) override; void Visit(VariantType* node) override;
void Visit(ParametrizedType* node) override;
void Visit(TypeExpression* node) override; void Visit(TypeExpression* node) override;
void Visit(AnyType& node) override; // variant void Visit(AnyType& node) override; // variant
@ -118,14 +123,15 @@ private:
// Typeclass // Typeclass
void Visit(ParametrizedTypeclass* node) override;
void Visit(TypeclassExpression* node) override; void Visit(TypeclassExpression* node) override;
void Visit(ParametrizedTypeclass* node) override;
void Visit(TypeclassUsage& node) override; // variant void Visit(TypeclassSubExpression& node) override; // variant
// Typeclass & Type // Typeclass & Type
void Visit(TypeParameter& node) override; // variant void Visit(ParametrizedType* node) override;
void Visit(TypeSubExpression& node) override; // variant void Visit(TypeSubExpression& node) override; // variant
// Identifiers, constants, etc. ----------------- // Identifiers, constants, etc. -----------------

View file

@ -8,7 +8,6 @@
// for clangd // for clangd
#include "utils.hpp" #include "utils.hpp"
#include "types_info.hpp"
namespace interpreter::tokens { namespace interpreter::tokens {
@ -69,6 +68,7 @@ using AnyAnnotatedType = AnnotatedType;
// Flow control ----------------- // Flow control -----------------
struct TypeConstructorPatternParameter;
struct TypeConstructorPattern; struct TypeConstructorPattern;
struct Match; struct Match;
@ -167,6 +167,7 @@ struct FunctionCallExpression;
struct TupleExpression; struct TupleExpression;
struct VariantExpression; struct VariantExpression;
struct ReturnExpression; struct ReturnExpression;
struct TypeConstructorParameter;
struct TypeConstructor; struct TypeConstructor;
struct LambdaFunction; struct LambdaFunction;
struct ArrayExpression; struct ArrayExpression;
@ -376,9 +377,14 @@ struct AnnotatedAbstractType {
// ----------------- Flow control ----------------- // ----------------- Flow control -----------------
struct TypeConstructorPatternParameter {
std::optional<ExtendedName> name;
PatternToken value;
};
struct TypeConstructorPattern { struct TypeConstructorPattern {
TypeIdentifier constructor; TypeIdentifier constructor;
std::vector<std::pair<std::optional<ExtendedName>, PatternToken>> parameters; std::vector<TypeConstructorPatternParameter> parameters;
}; };
struct MatchCase { struct MatchCase {
@ -459,8 +465,8 @@ struct AccessExpression {
// Other Expressions ----------------- // Other Expressions -----------------
struct FunctionCallExpression { struct FunctionCallExpression {
std::variant<std::unique_ptr<SubExpressionToken>, std::optional<std::variant<std::unique_ptr<SubExpressionToken>,
std::unique_ptr<TypeExpression>> prefix; std::unique_ptr<TypeExpression>>> prefix;
ExtendedName name; ExtendedName name;
std::vector<FunctionArgument> arguments; std::vector<FunctionArgument> arguments;
}; };
@ -477,10 +483,16 @@ struct ReturnExpression {
Expression expression; Expression expression;
}; };
struct TypeConstructor { struct TypeConstructorParameter {
enum AssignmentModifier { Move, Assign }; enum AssignmentModifier { Move, Assign };
std::optional<ExtendedName> name;
std::optional<AssignmentModifier> asignment_modifier;
PatternToken value;
};
struct TypeConstructor {
std::unique_ptr<TypeExpression> constructor; std::unique_ptr<TypeExpression> constructor;
std::vector<std::pair<std::optional<std::pair<ExtendedName, AssignmentModifier>>, SubExpression>> parameters; std::vector<TypeConstructorParameter> parameters;
}; };
struct LambdaFunction { struct LambdaFunction {

View file

@ -37,6 +37,9 @@ const std::string AnnotatedType = "annotated_type";
// Flow control ----------------- // Flow control -----------------
const std::string TypeConstructorPattern = "type_constructor_pattern";
const std::string PatternToken = "pattern_token";
const std::string Pattern = "pattern";
const std::string MatchCase = "match_case"; const std::string MatchCase = "match_case";
const std::string Match = "match"; const std::string Match = "match";
const std::string Condition = "condition"; const std::string Condition = "condition";
@ -93,7 +96,6 @@ const std::string ScopedAnyName = "scoped_any_name";
const std::string FunctionType = "function_type"; const std::string FunctionType = "function_type";
const std::string TupleType = "tuple_type"; const std::string TupleType = "tuple_type";
const std::string VariantType = "variant_type"; const std::string VariantType = "variant_type";
const std::string ParametrizedType = "parametrized_type";
const std::string TypeExpression = "type_expression"; const std::string TypeExpression = "type_expression";
const std::string Constructor = "constructor"; const std::string Constructor = "constructor";
const std::string AnyType = "any_type"; const std::string AnyType = "any_type";
@ -102,13 +104,16 @@ const std::string ExtendedScopedAnyType = "extended_scoped_any_type";
// Typeclass // Typeclass
const std::string TypeclassUsage = "typeclass_usage";
const std::string ParametrizedTypeclass = "parametrized_typeclass";
const std::string TypeclassExpression = "typeclass_expression"; const std::string TypeclassExpression = "typeclass_expression";
const std::string ParametrizedTypeclass = "parametrized_typeclass";
const std::string TypeclassSubExpression = "typeclass_subexpression";
// Typeclass & Type // Typeclass & Type
const std::string TypeParameter = "type_parameter"; const std::string ParametrizedType = "parametrized_type";
const std::string TypeSubExpression = "type_subexpression"; const std::string TypeSubExpression = "type_subexpression";
// Identifiers, constants, etc. ----------------- // Identifiers, constants, etc. -----------------

View file

@ -41,6 +41,7 @@ private:
// Flow control ----------------- // Flow control -----------------
void Visit(TypeConstructorPatternParameter* node) override;
void Visit(TypeConstructorPattern* node) override; void Visit(TypeConstructorPattern* node) override;
void Visit(MatchCase* node) override; void Visit(MatchCase* node) override;
void Visit(Match* node) override; void Visit(Match* node) override;
@ -70,6 +71,7 @@ private:
void Visit(TupleExpression* node) override; void Visit(TupleExpression* node) override;
void Visit(VariantExpression* node) override; void Visit(VariantExpression* node) override;
void Visit(ReturnExpression* node) override; void Visit(ReturnExpression* node) override;
void Visit(TypeConstructorParameter* node) override;
void Visit(TypeConstructor* node) override; void Visit(TypeConstructor* node) override;
void Visit(LambdaFunction* node) override; void Visit(LambdaFunction* node) override;
void Visit(ArrayExpression* node) override; void Visit(ArrayExpression* node) override;

View file

@ -45,6 +45,7 @@ protected:
// Flow control ----------------- // Flow control -----------------
virtual void Visit(TypeConstructorPatternParameter* node);
virtual void Visit(TypeConstructorPattern* node); virtual void Visit(TypeConstructorPattern* node);
virtual void Visit(MatchCase* node); virtual void Visit(MatchCase* node);
virtual void Visit(Match* node); virtual void Visit(Match* node);
@ -79,7 +80,7 @@ protected:
virtual void Visit(ReferenceExpression* node); virtual void Visit(ReferenceExpression* node);
virtual void Visit(AccessExpression* node); virtual void Visit(AccessExpression* node);
// Simple Expressions // Other expressions
virtual void Visit(FunctionCallExpression* node); virtual void Visit(FunctionCallExpression* node);
@ -88,6 +89,7 @@ protected:
virtual void Visit(TupleExpression* node); virtual void Visit(TupleExpression* node);
virtual void Visit(VariantExpression* node); virtual void Visit(VariantExpression* node);
virtual void Visit(ReturnExpression* node); virtual void Visit(ReturnExpression* node);
virtual void Visit(TypeConstructorParameter* node);
virtual void Visit(TypeConstructor* node); virtual void Visit(TypeConstructor* node);
virtual void Visit(LambdaFunction* node); virtual void Visit(LambdaFunction* node);
virtual void Visit(ArrayExpression* node); virtual void Visit(ArrayExpression* node);

View file

@ -159,7 +159,7 @@ void BuildVisitor::Visit(AliasDefinitionStatement* node) {
} }
current_node_ = parse_node.ChildByFieldName("value"); current_node_ = parse_node.ChildByFieldName("value");
node->value = std::make_unique<ParametrizedType>(); node->value = std::make_unique<TypeExpression>();
Visit(node->value.get()); Visit(node->value.get());
current_node_ = parse_node; current_node_ = parse_node;
@ -194,7 +194,7 @@ void BuildVisitor::Visit(VariableDefinitionStatement* node) {
void BuildVisitor::Visit(FunctionDeclaration* node) { void BuildVisitor::Visit(FunctionDeclaration* node) {
auto parse_node = current_node_; auto parse_node = current_node_;
node->name = parse_node.ChildByFieldName("name").GetValue(); node->name.name = parse_node.ChildByFieldName("name").GetValue();
size_t child_count = parse_node.NamedChildCount(); size_t child_count = parse_node.NamedChildCount();
@ -330,7 +330,7 @@ void BuildVisitor::Visit(SourceStatement& node) {
void BuildVisitor::Visit(FunctionDefinition* node) { void BuildVisitor::Visit(FunctionDefinition* node) {
auto parse_node = current_node_; auto parse_node = current_node_;
node->name = parse_node.ChildByFieldName("name").GetValue(); node->name.name = parse_node.ChildByFieldName("name").GetValue();
if (parse_node.NthChild(0).GetValue() == "(") { if (parse_node.NthChild(0).GetValue() == "(") {
node->modifier = FunctionDefinition::Operator; node->modifier = FunctionDefinition::Operator;
@ -395,7 +395,7 @@ void BuildVisitor::Visit(AnyAnnotatedType* node) {
for (size_t i = 0; i + 1 < child_count; ++i) { for (size_t i = 0; i + 1 < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i + 1); current_node_ = parse_node.NthNamedChild(i + 1);
Visit(node->typeclasses[i]); Visit(node->typeclasses[i].get());
} }
} }
@ -404,6 +404,87 @@ void BuildVisitor::Visit(AnyAnnotatedType* node) {
// Flow control ----------------- // Flow control -----------------
void BuildVisitor::Visit(TypeConstructorPatternParameter* node) {
auto parse_node = current_node_;
size_t child_count = parse_node.NamedChildCount();
if (child_count > 1) {
current_node_ = parse_node.ChildByFieldName("name"),
node->name.emplace();
Visit(&node->name.value());
}
current_node_ = parse_node.ChildByFieldName("value"),
Visit(node->value);
current_node_ = parse_node;
}
void BuildVisitor::Visit(TypeConstructorPattern* node) {
auto parse_node = current_node_;
node->constructor = parse_node.ChildByFieldName("constructor").GetValue();
size_t child_count = parse_node.NamedChildCount();
if (child_count > 1) {
node->parameters.resize(child_count - 1);
for (size_t i = 0; i + 1 < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i + 1);
Visit(&node->parameters[i]);
}
}
current_node_ = parse_node;
}
void BuildVisitor::Visit(PatternToken& node) {
auto parse_node = current_node_;
current_node_ = parse_node.NthNamedChild(0);
std::string current_node_type = current_node_.GetType();
if (current_node_type == parser::tokens::ExtendedName) { // optimize ??
node = std::make_unique<ExtendedName>();
Visit(std::get<std::unique_ptr<ExtendedName>>(node).get());
} else if (current_node_type == parser::tokens::Literal) {
node = std::make_unique<Literal>();
Visit(*std::get<std::unique_ptr<Literal>>(node));
} else if (current_node_type == parser::tokens::TypeConstructorPattern) {
node = std::make_unique<TypeConstructorPattern>();
Visit(std::get<std::unique_ptr<TypeConstructorPattern>>(node).get());
} else {
// error
}
current_node_ = parse_node;
}
void BuildVisitor::Visit(Pattern& node) {
auto parse_node = current_node_;
current_node_ = parse_node.NthNamedChild(0);
std::string current_node_type = current_node_.GetType();
if (current_node_type == parser::tokens::TypeConstructorPattern) { // optimize ??
node = std::make_unique<TypeConstructorPattern>();
Visit(std::get<std::unique_ptr<TypeConstructorPattern>>(node).get());
} else if (current_node_type == parser::tokens::PatternToken) {
node = std::make_unique<PatternToken>();
Visit(*std::get<std::unique_ptr<PatternToken>>(node));
} else {
// error
}
current_node_ = parse_node;
}
void BuildVisitor::Visit(MatchCase* node) { void BuildVisitor::Visit(MatchCase* node) {
auto parse_node = current_node_; auto parse_node = current_node_;
@ -635,9 +716,6 @@ void BuildVisitor::Visit(SubExpression& node) {
} else if (current_node_type == parser::tokens::SubExpressionToken) { } else if (current_node_type == parser::tokens::SubExpressionToken) {
node = std::make_unique<SubExpressionToken>(); node = std::make_unique<SubExpressionToken>();
Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node)); Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node));
} else if (current_node_type == parser::tokens::ArrayExpression) {
node = std::make_unique<ArrayExpression>();
Visit(std::get<std::unique_ptr<ArrayExpression>>(node).get());
} else if (current_node_type == parser::tokens::ReferenceExpression) { } else if (current_node_type == parser::tokens::ReferenceExpression) {
node = std::make_unique<ReferenceExpression>(); node = std::make_unique<ReferenceExpression>();
Visit(std::get<std::unique_ptr<ReferenceExpression>>(node).get()); Visit(std::get<std::unique_ptr<ReferenceExpression>>(node).get());
@ -716,6 +794,9 @@ void BuildVisitor::Visit(SuperExpression& node) {
} else if (current_node_type == parser::tokens::VariantExpression) { } else if (current_node_type == parser::tokens::VariantExpression) {
node = std::make_unique<VariantExpression>(); node = std::make_unique<VariantExpression>();
Visit(std::get<std::unique_ptr<VariantExpression>>(node).get()); Visit(std::get<std::unique_ptr<VariantExpression>>(node).get());
} else if (current_node_type == parser::tokens::ArrayExpression) {
node = std::make_unique<ArrayExpression>();
Visit(std::get<std::unique_ptr<ArrayExpression>>(node).get());
} else if (current_node_type == parser::tokens::Expression) { } else if (current_node_type == parser::tokens::Expression) {
node = std::make_unique<Expression>(); node = std::make_unique<Expression>();
Visit(*std::get<std::unique_ptr<Expression>>(node)); Visit(*std::get<std::unique_ptr<Expression>>(node));
@ -823,20 +904,38 @@ void BuildVisitor::Visit(FunctionArgument& node) {
void BuildVisitor::Visit(FunctionCallExpression* node) { void BuildVisitor::Visit(FunctionCallExpression* node) {
auto parse_node = current_node_; auto parse_node = current_node_;
size_t excluded_child_count = 0;
current_node_ = parse_node.NthNamedChild(0);
std::string current_node_type = current_node_.GetType();
if (current_node_type == parser::tokens::SubExpressionToken) {
node->prefix = std::make_unique<SubExpressionToken>();
Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value()));
++excluded_child_count;
} else if (current_node_type == parser::tokens::TypeExpression) {
node->prefix = std::make_unique<TypeExpression>();
Visit(std::get<std::unique_ptr<TypeExpression>>(node->prefix.value()).get());
++excluded_child_count;
} else {
// no prefix
}
current_node_ = parse_node.ChildByFieldName("name"); current_node_ = parse_node.ChildByFieldName("name");
node->name = std::make_unique<NameExpression>(); Visit(&node->name);
Visit(node->name.get()); ++excluded_child_count;
size_t child_count = parse_node.NamedChildCount(); size_t child_count = parse_node.NamedChildCount();
// if (child_count > 1) { // always true (repeat1) if (child_count > excluded_child_count) { // always true (repeat1)
node->arguments.resize(child_count - 1); node->arguments.resize(child_count - excluded_child_count);
for (size_t i = 0; i + 1 < child_count; ++i) { for (size_t i = 0; i + excluded_child_count < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i + 1); current_node_ = parse_node.NthNamedChild(i + excluded_child_count);
Visit(node->arguments[i]); Visit(node->arguments[i]);
}
} }
// }
current_node_ = parse_node; current_node_ = parse_node;
} }
@ -880,31 +979,45 @@ void BuildVisitor::Visit(ReturnExpression* node) {
current_node_ = parse_node; current_node_ = parse_node;
} }
void BuildVisitor::Visit(TypeConstructorParameter* node) {
auto parse_node = current_node_;
size_t child_count = parse_node.NamedChildCount();
if (child_count > 1) {
current_node_ = parse_node.ChildByFieldName("name");
node->name.emplace();
Visit(&node->name.value());
std::string assignment_modifier = current_node_.NextSibling().GetValue();
if (assignment_modifier == "=") {
node->asignment_modifier = TypeConstructorParameter::Assign;
} else if (assignment_modifier == "<-") {
node->asignment_modifier = TypeConstructorParameter::Move;
}
}
current_node_ = parse_node.ChildByFieldName("value");
Visit(node->value);
current_node_ = parse_node;
}
void BuildVisitor::Visit(TypeConstructor* node) { void BuildVisitor::Visit(TypeConstructor* node) {
auto parse_node = current_node_; auto parse_node = current_node_;
current_node_ = parse_node.ChildByFieldName("type"); current_node_ = parse_node.ChildByFieldName("constructor");
node->type = std::make_unique<ParametrizedType>(); node->constructor = std::make_unique<TypeExpression>();
Visit(node->type.get()); Visit(node->constructor.get());
size_t parameter_count = (parse_node.NamedChildCount() - 1) / 2; size_t child_count = parse_node.NamedChildCount();
node->parameters.resize(parameter_count); if (child_count > 1) {
node->parameters.resize(child_count - 1);
for (size_t i = 0; i < parameter_count * 2; ++i) { for (size_t i = 0; i + 1 < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i + 1); current_node_ = parse_node.NthNamedChild(i + 1);
Visit(&node->parameters[i]);
if (i % 2 == 0) {
Visit(&std::get<0>(node->parameters[i / 2]));
std::string assignment_modifier = current_node_.NextSibling().GetValue();
if (assignment_modifier == "=") {
std::get<1>(node->parameters[i / 2]) = TypeConstructor::Assign;
} else if (assignment_modifier == "<-") {
std::get<1>(node->parameters[i / 2]) = TypeConstructor::Move;
}
} else {
Visit(std::get<2>(node->parameters[i / 2]));
} }
} }
@ -976,23 +1089,10 @@ void BuildVisitor::Visit(NameExpression* node) {
size_t child_count = parse_node.NamedChildCount(); size_t child_count = parse_node.NamedChildCount();
bool namespaces_ended = false; node->names.resize(child_count);
for (size_t i = 0; i < child_count; ++i) { for (size_t i = 0; i < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i); current_node_ = parse_node.NthNamedChild(i);
std::string current_node_type = current_node_.GetType(); Visit(&node->names[i]);
if (current_node_type != parser::tokens::TypeSubExpression) {
namespaces_ended = true;
}
if (!namespaces_ended) {
node->namespaces.emplace_back();
Visit(node->namespaces.back());
} else {
node->expressions.emplace_back();
Visit(&node->expressions.back());
}
} }
current_node_ = parse_node; current_node_ = parse_node;
@ -1037,7 +1137,7 @@ void BuildVisitor::Visit(AnnotatedName* node) {
if (parse_node.NamedChildCount() > 1) { if (parse_node.NamedChildCount() > 1) {
current_node_ = parse_node.ChildByFieldName("type"); current_node_ = parse_node.ChildByFieldName("type");
node->type = std::make_unique<ParametrizedType>(); node->type.emplace();
Visit(node->type.value()); Visit(node->type.value());
current_node_ = parse_node; current_node_ = parse_node;
@ -1156,27 +1256,6 @@ void BuildVisitor::Visit(VariantType* node) {
current_node_ = parse_node; current_node_ = parse_node;
} }
void BuildVisitor::Visit(ParametrizedType* node) {
auto parse_node = current_node_;
current_node_ = parse_node.ChildByFieldName("type_expression");
node->type_expression = std::make_unique<TypeExpression>();
Visit(node->type_expression.get());
size_t child_count = parse_node.NamedChildCount();
if (child_count > 1) {
node->parameters.resize(child_count - 1);
for (size_t i = 0; i + 1 < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i + 1);
Visit(node->parameters[i]);
}
}
current_node_ = parse_node;
}
void BuildVisitor::Visit(TypeExpression* node) { void BuildVisitor::Visit(TypeExpression* node) {
auto parse_node = current_node_; auto parse_node = current_node_;
@ -1185,7 +1264,7 @@ void BuildVisitor::Visit(TypeExpression* node) {
size_t child_count = parse_node.NamedChildCount(); size_t child_count = parse_node.NamedChildCount();
current_node_ = parse_node.ChildByFieldName("type"); current_node_ = parse_node.ChildByFieldName("type");
node->type = current_node_.GetValue(); Visit(node->type);
++excluded_child_count; ++excluded_child_count;
current_node_ = current_node_.NextSibling(); current_node_ = current_node_.NextSibling();
@ -1203,11 +1282,11 @@ void BuildVisitor::Visit(TypeExpression* node) {
} }
if (child_count > excluded_child_count) { if (child_count > excluded_child_count) {
node->namespaces.resize(child_count - excluded_child_count); node->path.resize(child_count - excluded_child_count);
for (size_t i = 0; i + excluded_child_count < child_count; ++i) { for (size_t i = 0; i + excluded_child_count < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i); current_node_ = parse_node.NthNamedChild(i);
Visit(node->namespaces[i]); Visit(node->path[i]);
} }
} }
@ -1222,8 +1301,8 @@ void BuildVisitor::Visit(AnyType& node) {
std::string current_node_type = current_node_.GetType(); std::string current_node_type = current_node_.GetType();
if (current_node_type == parser::tokens::ParametrizedType) { if (current_node_type == parser::tokens::ParametrizedType) {
node = std::make_unique<ParametrizedType>(); node = std::make_unique<TypeExpression>();
Visit(std::get<std::unique_ptr<ParametrizedType>>(node).get()); Visit(std::get<std::unique_ptr<TypeExpression>>(node).get());
} else if (current_node_type == parser::tokens::TupleType) { } else if (current_node_type == parser::tokens::TupleType) {
node = std::make_unique<TupleType>(); node = std::make_unique<TupleType>();
Visit(std::get<std::unique_ptr<TupleType>>(node).get()); Visit(std::get<std::unique_ptr<TupleType>>(node).get());
@ -1265,16 +1344,36 @@ void BuildVisitor::Visit(ExtendedScopedAnyType* node) {
// Typeclass // Typeclass
void BuildVisitor::Visit(TypeclassUsage& node) { void BuildVisitor::Visit(TypeclassExpression* node) {
auto parse_node = current_node_;
//
size_t child_count = parse_node.NamedChildCount();
//
// if (child_count > 1) {
// node->path.resize(child_count - 1);
//
// for (size_t i = 0; i + 1 < child_count; ++i) {
// current_node_ = parse_node.NthNamedChild(i);
// Visit(node->path[i]);
// }
// }
//
current_node_ = parse_node.ChildByFieldName("typeclass");
Visit(node->typeclass);
//
current_node_ = parse_node;
}
void BuildVisitor::Visit(TypeclassSubExpression& node) {
auto parse_node = current_node_; auto parse_node = current_node_;
current_node_ = parse_node.NthNamedChild(0); current_node_ = parse_node.NthNamedChild(0);
std::string current_node_type = current_node_.GetType(); std::string current_node_type = current_node_.GetType();
if (current_node_type == parser::tokens::TypeclassExpression) { if (current_node_type == parser::tokens::TypeclassIdentifier) {
node = std::make_unique<TypeclassExpression>(); node = std::make_unique<TypeclassIdentifier>();
Visit(std::get<std::unique_ptr<TypeclassExpression>>(node).get()); Visit(std::get<std::unique_ptr<TypeclassIdentifier>>(node).get());
} else if (current_node_type == parser::tokens::ParametrizedTypeclass) { } else if (current_node_type == parser::tokens::ParametrizedTypeclass) {
node = std::make_unique<ParametrizedTypeclass>(); node = std::make_unique<ParametrizedTypeclass>();
Visit(std::get<std::unique_ptr<ParametrizedTypeclass>>(node).get()); Visit(std::get<std::unique_ptr<ParametrizedTypeclass>>(node).get());
@ -1288,9 +1387,7 @@ void BuildVisitor::Visit(TypeclassUsage& node) {
void BuildVisitor::Visit(ParametrizedTypeclass* node) { void BuildVisitor::Visit(ParametrizedTypeclass* node) {
auto parse_node = current_node_; auto parse_node = current_node_;
current_node_ = parse_node.ChildByFieldName("typeclass_expression"); node->typeclass = parse_node.ChildByFieldName("typeclass").GetValue();
node->typeclass_expression = std::make_unique<TypeclassExpression>();
Visit(node->typeclass_expression.get());
size_t child_count = parse_node.NamedChildCount(); size_t child_count = parse_node.NamedChildCount();
@ -1299,54 +1396,16 @@ void BuildVisitor::Visit(ParametrizedTypeclass* node) {
for (size_t i = 0; i + 1 < child_count; ++i) { for (size_t i = 0; i + 1 < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i + 1); current_node_ = parse_node.NthNamedChild(i + 1);
Visit(node->parameters[i]); node->parameters[i] = std::make_unique<TypeExpression>();
Visit(node->parameters[i].get());
} }
} }
current_node_ = parse_node; current_node_ = parse_node;
} }
void BuildVisitor::Visit(TypeclassExpression* node) {
auto parse_node = current_node_;
size_t child_count = parse_node.NamedChildCount();
if (child_count > 1) {
node->namespaces.resize(child_count - 1);
for (size_t i = 0; i + 1 < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i);
Visit(node->namespaces[i]);
}
}
node->typeclass = parse_node.ChildByFieldName("typeclass").GetValue();
current_node_ = parse_node;
}
// Typeclass & Type ----------------- // Typeclass & Type -----------------
void BuildVisitor::Visit(TypeParameter& node) {
auto parse_node = current_node_;
current_node_ = parse_node.NthNamedChild(0);
std::string current_node_type = current_node_.GetType();
if (current_node_type == parser::tokens::TypeExpression) {
node = std::make_unique<TypeExpression>();
Visit(std::get<std::unique_ptr<TypeExpression>>(node).get());
} else if (current_node_type == parser::tokens::ParametrizedType) {
node = std::make_unique<ParametrizedType>();
Visit(std::get<std::unique_ptr<ParametrizedType>>(node).get());
} else {
// error
}
current_node_ = parse_node;
}
void BuildVisitor::Visit(TypeSubExpression& node) { void BuildVisitor::Visit(TypeSubExpression& node) {
auto parse_node = current_node_; auto parse_node = current_node_;
@ -1370,6 +1429,26 @@ void BuildVisitor::Visit(TypeSubExpression& node) {
current_node_ = parse_node; current_node_ = parse_node;
} }
void BuildVisitor::Visit(ParametrizedType* node) {
auto parse_node = current_node_;
node->type = parse_node.ChildByFieldName("type").GetValue();
size_t child_count = parse_node.NamedChildCount();
if (child_count > 1) {
node->parameters.resize(child_count - 1);
for (size_t i = 0; i + 1 < child_count; ++i) {
current_node_ = parse_node.NthNamedChild(i + 1);
node->parameters[i] = std::make_unique<TypeExpression>();
Visit(node->parameters[i].get());
}
}
current_node_ = parse_node;
}
// Identifiers, constants, etc. ----------------- // Identifiers, constants, etc. -----------------
void BuildVisitor::Visit(ExtendedName* node) { void BuildVisitor::Visit(ExtendedName* node) {

View file

@ -33,8 +33,8 @@ int main(int argc, char** argv) { // TODO, only test version
std::make_unique<interpreter::tokens::SourceFile>(); std::make_unique<interpreter::tokens::SourceFile>();
interpreter::BuildVisitor build_visitor(parse_tree); interpreter::BuildVisitor build_visitor(parse_tree);
interpreter::PrintVisitor print_visitor(std::cout); // interpreter::PrintVisitor print_visitor(std::cout);
build_visitor.VisitSourceFile(source_file.get()); build_visitor.VisitSourceFile(source_file.get());
print_visitor.VisitSourceFile(source_file.get()); // print_visitor.VisitSourceFile(source_file.get());
} }

90118
src/parser.c

File diff suppressed because it is too large Load diff

View file

@ -262,6 +262,16 @@ void PrintVisitor::Visit(AnyAnnotatedType* node) {
// Flow control ----------------- // Flow control -----------------
void PrintVisitor::Visit(TypeConstructorPatternParameter* node) {
out_ << "[TypeConstructorPatternParameter ";
if (node->name.has_value()) {
Visit(&node->name.value());
out_ << " = ";
}
Visitor::Visit(node->value);
out_ << "]";
}
void PrintVisitor::Visit(TypeConstructorPattern* node) { void PrintVisitor::Visit(TypeConstructorPattern* node) {
out_ << "[TypeConstructorPattern "; out_ << "[TypeConstructorPattern ";
Visit(&node->constructor); Visit(&node->constructor);
@ -274,11 +284,7 @@ void PrintVisitor::Visit(TypeConstructorPattern* node) {
is_first = false; is_first = false;
} }
out_ << '('; out_ << '(';
if (parameter.first.has_value()) { Visit(&parameter);
Visit(&parameter.first.value());
out_ << " = ";
}
Visitor::Visit(parameter.second);
} }
out_ << ")\n"; out_ << ")\n";
} }
@ -434,18 +440,20 @@ void PrintVisitor::Visit(AccessExpression* node) {
// Other Expressions // Other Expressions
void PrintVisitor::Visit(FunctionCallExpression* node) { void PrintVisitor::Visit(FunctionCallExpression* node) {
out_ << "[FunctionCall ("; out_ << "[FunctionCall ";
if (std::holds_alternative<std::unique_ptr<SubExpressionToken>>(node->prefix)) { if (node->prefix.has_value()) {
Visitor::Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix)); out_ << '(';
} else if (std::holds_alternative<std::unique_ptr<TypeExpression>>(node->prefix)) { if (std::holds_alternative<std::unique_ptr<SubExpressionToken>>(node->prefix.value())) {
Visit(std::get<std::unique_ptr<TypeExpression>>(node->prefix).get()); Visitor::Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value()));
} else { } else if (std::holds_alternative<std::unique_ptr<TypeExpression>>(node->prefix.value())) {
// error Visit(std::get<std::unique_ptr<TypeExpression>>(node->prefix.value()).get());
} else {
// error
}
out_ << ").";
} }
out_ << ").";
Visit(&node->name); Visit(&node->name);
out_ << "] ("; out_ << "] (";
@ -480,6 +488,23 @@ void PrintVisitor::Visit(ReturnExpression* node) {
out_ << ")\n"; out_ << ")\n";
} }
void PrintVisitor::Visit(TypeConstructorParameter* node) {
out_ << "[TypeConstructorParameter ";
if (node->name.has_value()) {
Visit(&node->name.value());
switch (node->asignment_modifier.value()) {
case TypeConstructorParameter::Assign:
out_ << " = ";
break;
case TypeConstructorParameter::Move:
out_ << " <- ";
break;
}
}
Visitor::Visit(node->value);
out_ << "]";
}
void PrintVisitor::Visit(TypeConstructor* node) { void PrintVisitor::Visit(TypeConstructor* node) {
out_ << "[TypeConstructor "; out_ << "[TypeConstructor ";
Visit(node->constructor.get()); Visit(node->constructor.get());
@ -492,18 +517,7 @@ void PrintVisitor::Visit(TypeConstructor* node) {
is_first = false; is_first = false;
} }
out_ << '('; out_ << '(';
if (parameter.first.has_value()) { Visit(&parameter);
Visit(&parameter.first.value().first);
switch (parameter.first.value().second) {
case TypeConstructor::Assign:
out_ << " = ";
break;
case TypeConstructor::Move:
out_ << " <- ";
break;
}
}
Visitor::Visit(parameter.second);
} }
out_ << ")\n"; out_ << ")\n";
} }

View file

@ -448,13 +448,17 @@ void Visitor::Visit(AnyAnnotatedType* node) {
// Flow control ----------------- // Flow control -----------------
void Visitor::Visit(TypeConstructorPatternParameter* node) {
if (node->name.has_value()) {
Visit(&node->name.value());
}
Visit(node->value);
}
void Visitor::Visit(TypeConstructorPattern* node) { void Visitor::Visit(TypeConstructorPattern* node) {
Visit(&node->constructor); Visit(&node->constructor);
for (auto& parameter : node->parameters) { for (auto& parameter : node->parameters) {
if (parameter.first.has_value()) { Visit(&parameter);
Visit(&parameter.first.value());
}
Visit(parameter.second);
} }
} }
@ -543,12 +547,14 @@ void Visitor::Visit(AccessExpression* node) {
// Other Expressions // Other Expressions
void Visitor::Visit(FunctionCallExpression* node) { void Visitor::Visit(FunctionCallExpression* node) {
if (std::holds_alternative<std::unique_ptr<SubExpressionToken>>(node->prefix)) { if (node->prefix.has_value()) {
Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix)); if (std::holds_alternative<std::unique_ptr<SubExpressionToken>>(node->prefix.value())) {
} else if (std::holds_alternative<std::unique_ptr<TypeExpression>>(node->prefix)) { Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node->prefix.value()));
Visit(std::get<std::unique_ptr<TypeExpression>>(node->prefix).get()); } else if (std::holds_alternative<std::unique_ptr<TypeExpression>>(node->prefix.value())) {
} else { Visit(std::get<std::unique_ptr<TypeExpression>>(node->prefix.value()).get());
// error } else {
// error
}
} }
Visit(&node->name); Visit(&node->name);
@ -573,13 +579,17 @@ void Visitor::Visit(ReturnExpression* node) {
Visit(node->expression); Visit(node->expression);
} }
void Visitor::Visit(TypeConstructorParameter* node) {
if (node->name.has_value()) {
Visit(&node->name.value());
}
Visit(node->value);
}
void Visitor::Visit(TypeConstructor* node) { void Visitor::Visit(TypeConstructor* node) {
Visit(node->constructor.get()); Visit(node->constructor.get());
for (auto& parameter : node->parameters) { for (auto& parameter : node->parameters) {
if (parameter.first.has_value()) { Visit(&parameter);
Visit(&parameter.first.value().first);
}
Visit(parameter.second);
} }
} }

View file

@ -1,21 +1,21 @@
decl test_arrays : Unit -> Unit decl test_arrays : Unit -> Unit
def test_arrays = { def test_arrays = {
var arr1 = [1; 2; 3] var arr1 = ,1 ,2 ,3
const arr2 = [] const arr2 = Int._array 32
var arr3 = [] var arr3 = String._array 11
const arr4 = 'a'..'z' const arr4 = 'a'..'z'
const n = 100 const n = 100
var @arr5 = @$Int_n var @arr5 <- Int.@_new_array 10
var @arr6 = @$Int_n var @arr6 <- String.@_new_array 10
var @arr6_pointer = @arr6 var ~arr6_reference = ~arr6
const elem1 = arr1.1 const elem1 = arr1:0
var elem2 = arr1.1 var elem2 = arr1:2
const ~ref1 = ~arr1.1 const ~ref1 = ~arr1:1
var ~ref2 = ~arr1.1 var ~ref2 = ~arr1:3
; ~arr1.1 = 123 ; arr1:1 = 123
; ref1 = arr1.2 // set value ; ref1 = arr1:2 // set value
; ~ref1 = ~ref2 // set reference ; ~ref1 = ~ref2 // set reference
} }

View file

@ -40,7 +40,7 @@ class Employee =
& salary : Int & salary : Int
| Programmer | Programmer
& skills : Float & skills : Float
& current_task : Optional Task & current_task : (Optional Task)
& salary : Int) & salary : Int)
@ -56,5 +56,4 @@ class Bag =
& weight_g : Int & weight_g : Int
& weight_g : Int & weight_g : Int
| Big) | Big)
& other_things : Array Something & other_things : (Array Something)

View file

@ -6,11 +6,11 @@ namespace Employee {
& name = "John" & name = "John"
& role = & role =
($Manager ($Manager
& name = "John" & "John"
& productivity = & productivity =
($Productivity.High ($Productivity::High
& duration = 10.3 & duration = 10.3
& sleep_on_work = ($Productivity.SleepOnWork.No)) & sleep_on_work = ($Productivity::SleepOnWork::No))
& salary = 123) & salary = 123)
} }

View file

@ -8,9 +8,9 @@ def flow_control_test = {
return {} return {}
} }
while (a > 0) && (!array.is_empty) do { while (a > 0) && (!array.is_empty ()) do {
; --a ; --a
; array.pop ; array->pop
} }
while x < 10 do while x < 10 do

View file

@ -5,39 +5,40 @@ decl fib : Int -> Int
def fib : n = def fib : n =
match n with match n with
| 0 | 1 -> 1 | 0 | 1 -> 1
| _ -> fib (n - 1) + fib n | n ? n > 1 -> fib (n - 1) + fib n
| _ -> error "n must be positive"
decl fact : Int -> Int decl fact : Int -> Int
def fact : n = def fact : n =
match n with match n with
| 0 -> 1 | 0 -> 1
| n -> n * fact (n - 1) | n ? n > 0 -> n * fact (n - 1)
| _ -> error "n must be positive"
decl find_prefix_hashes ('H : (#AccHash Char)) : String -> Array 'H decl find_prefix_hashes ('H : (#AccHash Char)) : String -> (Array 'H)
def find_prefix_hashes 'H : str = { def find_prefix_hashes 'H : str = {
var hashes = (Array 'H).new (str.size + 1) var hashes = (Array 'H).new (str.size () + 1)
; hashes.0 = 'H.of str.0 ; hashes:0 = 'H.of str:0
for i in 1..hashes.size do { for i in 1..hashes.size () do {
; hashes.i = hashes.(i - 1).clone ; hashes:i = hashes:(i - 1)
; hashes.i.append str.i ; hashes:i.append str:i
} }
return hashes return hashes
} }
alias Hash = AccHash Char alias Hash = (AccHash Char)
decl find_substring : String -> String -> Array Index decl find_substring : String -> String -> (Array Index)
def find_substring : str substr = { def find_substring : str substr = {
var result = (Array Index).empty ()
var result = (Array Index).empty
const str_hashes = find_prefix_hashes Hash str const str_hashes = find_prefix_hashes Hash str
const substr_hash = Hash.of substr const substr_hash = Hash.of substr
for i in 0..(str_hashes.size - substr.size) do { for i in 0..(str_hashes.size () - substr.size ()) do {
const part_hash = Hash.diff str_hashes.(i + substr.size) str_hashes.i const part_hash = Hash.diff str_hashes:(i + substr->size ()) str_hashes:i
if part_hash == substr_hash then { if part_hash == substr_hash then {
; result.push i ; result.push i
@ -48,8 +49,7 @@ def find_substring : str substr = {
} }
decl is_empty : Unit -> Bool decl is_empty : Unit -> Bool
def is_empty = def is_empty = 0
return 0
decl do_something : Unit -> Unit decl do_something : Unit -> Unit
def do_something = def do_something =

View file

@ -1,7 +1,7 @@
decl test_lambdas : Unit -> Unit decl test_lambdas : Unit -> Unit
def test_lambdas = { def test_lambdas = {
const lambda1 = \x -> x * x const lambda1 = \x -> x * x
const lambda2 = \x -> x.hash const lambda2 = \x -> x.hash ()
const lambda3 = \x y -> x + y const lambda3 = \x y -> x + y
const lambda4 = \x -> { const lambda4 = \x -> {

View file

@ -1,15 +1,15 @@
def fruit_cost : fruit = { def fruit_cost : fruit = {
return (match fruit with return (match fruit with
| $Banana -> 11 | $Banana -> 11
| $Apple | $Orange -> 7) | $Apple | $Orange -> 7)
} }
def amount_to_string : x is_zero_separated = { def amount_to_string : x is_zero_separated = {
const ans = match x with const ans = match x with
| 0 ? is_zero_separated -> "Zero" | 0 ? is_zero_separated () -> "Zero"
| 0 | 1 | 2 | 3 | 4 -> "Few" | 0 | 1 | 2 | 3 | 4 -> "Few"
| x ? (5..9).contains x -> "Several" | x ? (5..9).contains x -> "Several"
| x ? (10..19).contains x -> "Pack" | x ? (10..19).contains x -> "Pack"
| _ -> "Lots" | _ -> "Lots"
return ans return ans
} }

View file

@ -3,6 +3,6 @@ struct StructWithRef =
decl test_memory : Unit -> Unit decl test_memory : Unit -> Unit
def test_memory = { def test_memory = {
const @unique_ref1 <- @(5) const @unique_ref1 <- Int.@_new 5
var @unique_ref2 <- @(Array.of 1 2 3) var @unique_ref2 <- Array.@of 1 2 3
} }

View file

@ -6,18 +6,6 @@ partition INTERFACE { // or .interface.lang filename
decl something : Unit -> Unit decl something : Unit -> Unit
} }
partition CORE { // or .core.lang filename partition CODE { // or .core.lang filename
decl something : Unit -> Unit
}
partition LIB { // or .lib.lang filename
decl something : Unit -> Unit
}
partition MODULE { // or .module.lang filename
decl something : Unit -> Unit
}
partition EXE { // or .exe.lang filename
decl something : Unit -> Unit decl something : Unit -> Unit
} }

View file

@ -3,8 +3,5 @@ def test_tuples = {
var tuple1 = & "a" & 2 & "hello" var tuple1 = & "a" & 2 & "hello"
const & t1 & t2 & t3 = f x const & t1 & t2 & t3 = f x
; tuple1.0 = "b" ; tuple1:0 = "b"
} }

View file

@ -3,6 +3,6 @@ alias T1 = Int
abstract (T2 : #A #B #C) abstract (T2 : #A #B #C)
// Used to pre-compile module for some types // Used to pre-compile module for some types
let T2 = Int // let T2 = Int
let T2 = Float // let T2 = Float
let T2 = Complex // let T2 = Complex

View file

@ -3,16 +3,13 @@ def test_variants = {
var variant1 = | 'a' | 2 | "hello" var variant1 = | 'a' | 2 | "hello"
var | val | err = f x var | val | err = f x
; val -> "something" // open variant as value in expr ; val -?> "something" // open variant as value in expr
; val -!> "nothing" // open variant as None in expr ; val -!> "nothing" // open variant as None in expr
match variant1 with match variant1 with
| 'a' -> "something" | 'a' -> "something"
| 2 -> "something" | 2 -> "nothing"
| "hello" -> "something" | "hello" -> "nothing"
| a -> "Something"
| String.of str -> "something"
| Int.of i -> "someting"
| 11 -> "nothing" | 11 -> "nothing"
} }