statement builders finished

This commit is contained in:
ProgramSnail 2023-07-23 19:40:27 +03:00
parent 64a91299ff
commit 4470454838
19 changed files with 682 additions and 255 deletions

View file

@ -5,6 +5,9 @@
namespace builders {
// returns Modifier::NONE for incorrecnt input
nodes::Modifier build_modifier(parser::ParseTree::Node parser_node);
nodes::Node build_node(parser::ParseTree::Node parser_node);
// --- literals
@ -25,6 +28,8 @@ nodes::Literal build_null_literal(parser::ParseTree::Node parser_node);
// --- identifiers
nodes::Identifier build_identifier(parser::ParseTree::Node parser_node);
nodes::Identifier build_simple_name(parser::ParseTree::Node parser_node);
nodes::Identifier build_simple_type(parser::ParseTree::Node parser_node);

View file

@ -7,7 +7,14 @@
namespace nodes {
enum class ReferenceType { REF, IN, OUT, NONE };
enum class Modifier {
OUT, // -> x
IN, // <- x
REF, // <> x
OR_FALSE, // x?
OR_PANIC, // x!
NONE,
};
class Node {
public:

View file

@ -9,8 +9,8 @@
namespace builders {
nodes::SymbolDocs build_symbol_docs(
parser::ParseTree::Node description_parser_node,
const std::vector<parser::ParseTree::Node> &annotation_parser_nodes,
const std::unordered_set<std::string> &annotations);
std::optional<parser::ParseTree::Node> description_parser_node,
const std::vector<parser::ParseTree::Node> &annotation_parser_nodes = {},
const std::unordered_set<std::string> &annotations = {});
} // namespace builders

View file

@ -8,6 +8,8 @@ namespace nodes {
class SymbolDocs {
public:
SymbolDocs() {}
SymbolDocs(std::string &&description)
: description_(std::move(description)) {}
@ -22,9 +24,19 @@ public:
return false;
}
std::string *get_description() { return &description_; }
std::optional<std::string *> get_description() {
if (description_.has_value()) {
return &description_.value();
}
return std::nullopt;
}
const std::string *get_description() const { return &description_; }
std::optional<const std::string *> get_description() const {
if (description_.has_value()) {
return &description_.value();
}
return std::nullopt;
}
std::optional<std::string *>
get_annotation_info(const std::string &annotation) {
@ -45,7 +57,7 @@ public:
}
private:
std::string description_;
std::optional<std::string> description_;
std::unordered_map<std::string, std::string> annotations_info_;
};

View file

@ -1,6 +1,8 @@
#pragma once
#include "basic_nodes.hpp"
#include "tree_sitter_wrapper.hpp"
#include <iostream>
namespace error_handling {

View file

@ -8,85 +8,86 @@ namespace builders {
// --- flow control
nodes::ExpressionProxy
build_expression(parser::ParseTree::Node parse_node,
build_expression(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Match build_match(parser::ParseTree::Node parse_node,
nodes::Match build_match(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Condition build_condition(parser::ParseTree::Node parse_node,
nodes::Condition build_condition(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Loop build_loop(parser::ParseTree::Node parse_node,
nodes::Loop build_loop(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
// --- operators
nodes::NameExpression
build_comma_expression(parser::ParseTree::Node parse_node,
build_comma_expression(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::NameExpression
build_operator_expression(parser::ParseTree::Node parse_node,
build_operator_expression(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
// --- continers
nodes::Container build_block(parser::ParseTree::Node parse_node,
nodes::Container build_block(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Container build_array(parser::ParseTree::Node parse_node,
nodes::Container build_array(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
// --- modifiers
nodes::Return build_return(parser::ParseTree::Node parse_node,
nodes::Return build_return(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::NameDefinition build_name_definition(parser::ParseTree::Node parse_node);
nodes::NameDefinition
build_name_definition(parser::ParseTree::Node parser_node);
nodes::Access build_array_access(parser::ParseTree::Node parse_node,
nodes::Access build_array_access(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Access build_tuple_access(parser::ParseTree::Node parse_node,
nodes::Access build_tuple_access(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::LoopControl build_loop_control(parser::ParseTree::Node parse_node);
nodes::LoopControl build_loop_control(parser::ParseTree::Node parser_node);
nodes::ModifierExpression
build_reference_expression(parser::ParseTree::Node parse_node,
build_reference_expression(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::ModifierExpression
build_suffix_expression(parser::ParseTree::Node parse_node,
build_suffix_expression(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
// --- other
nodes::NameExpression
build_name_expression(parser::ParseTree::Node parse_node,
build_name_expression(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Constructor
build_constructor(parser::ParseTree::Node parse_node,
build_constructor(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Lambda build_lambda(parser::ParseTree::Node parse_node,
nodes::Lambda build_lambda(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);

View file

@ -344,14 +344,6 @@ private:
class ModifierExpression : public Node {
public:
enum Modifier {
OUT, // -> x
IN, // <- x
REF, // <> x
OR_FALSE, // x?
OR_PANIC, // x!
};
ModifierExpression(Node node, Modifier modifier, ExpressionProxy expression)
: Node(node), modifier_(modifier), expression_(expression) {}
@ -372,15 +364,16 @@ private:
class NameExpression : public Node {
public:
template <typename T>
NameExpression(Node node, T &&name)
NameExpression(Node node, Identifier &&name)
: Node(node), name_(std::forward<T>(name)) {}
template <typename T, typename U, typename V>
NameExpression(Node node, T &&name, U &&arguments, V &&prefix,
bool is_point_call = false)
: Node(node), name_(std::forward<T>(name)),
arguments_(std::forward<U>(arguments)),
prefix_(std::forward<V>(prefix)), is_point_call_(is_point_call) {}
NameExpression(
Node node, Identifier &&name,
std::vector<std::pair<std::optional<std::string>, ExpressionProxy>>
&&arguments,
std::optional<const Type> &&prefix, bool is_point_call = false)
: Node(node), name_(std::move(name)), arguments_(std::move(arguments)),
prefix_(std::move(prefix)), is_point_call_(is_point_call) {}
std::string *get_name() { return name_.get(); }
@ -436,12 +429,6 @@ private:
bool is_point_call_ = false; // x.f ... or f x ...
};
// explicit instantiation
template NameExpression::NameExpression(
Node, Identifier &&,
std::vector<std::pair<std::optional<std::string>, ExpressionProxy>> &&,
std::optional<const Type> &&, bool);
class Constructor : public Node {
public:
Constructor(

View file

@ -6,35 +6,33 @@
namespace builders {
// IN PROGRESS: return type, etc.
void build_source_file(parser::ParseTree::Node parse_node,
void build_source_file(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
// IN PROGRESS: return type, etc.
void build_statement(parser::ParseTree::Node parse_node,
void build_statement(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Import build_import(parser::ParseTree::Node parse_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::Import build_import(parser::ParseTree::Node parser_node);
nodes::Constraint build_constraint(parser::ParseTree::Node parse_node,
nodes::Constraint build_constraint(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::TypeDefinition
build_type_definition(parser::ParseTree::Node parse_node,
build_type_definition(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::FunctionDefinition
build_function_definition(parser::ParseTree::Node parse_node,
build_function_definition(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::TypeclassDefinition
build_typeclass_definition(parser::ParseTree::Node parse_node,
build_typeclass_definition(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);

View file

@ -57,25 +57,27 @@ public:
FunctionDefinition(Node node, SymbolDocs &&docs,
std::vector<Constraint> &&constraints,
ModifierType modifier, const Identifier &name,
std::vector<std::string> &&annotations,
std::vector<std::optional<std::string>> &&annotations,
std::vector<Identifier> &&arguments,
std::vector<ReferenceType> &&reference_types,
std::vector<TypeProxy> &&types)
std::vector<Modifier> &&reference_types,
std::vector<std::optional<TypeProxy>> &&types,
std::optional<ExpressionProxy> expression)
: Node(node), docs_(std::move(docs)),
constraints_(std::move(constraints)), modifier_(modifier), name_(name),
annotations_(std::move(annotations)), arguments_(std::move(arguments)),
reference_types_(std::move(reference_types)), types_(std::move(types)) {
}
reference_types_(std::move(reference_types)), types_(std::move(types)),
expression_(expression) {}
private:
SymbolDocs docs_;
std::vector<Constraint> constraints_;
ModifierType modifier_;
Identifier name_;
std::vector<std::string> annotations_;
std::vector<std::optional<std::string>> annotations_;
std::vector<Identifier> arguments_;
std::vector<ReferenceType> reference_types_;
std::vector<TypeProxy> types_;
std::vector<Modifier> reference_types_;
std::vector<std::optional<TypeProxy>> types_;
std::optional<ExpressionProxy> expression_;
// std::vector<bool> optional_arguments_; // ??
}; // IN PROGRESS
@ -83,7 +85,8 @@ class TypeDefinition : public Node {
public:
TypeDefinition(Node node, SymbolDocs &&docs, bool is_on_heap,
const Identifier &name, std::vector<Identifier> &&arguments,
VariantType &&type, std::vector<FunctionDefinition> &&methods)
std::optional<VariantType> &&type,
std::vector<FunctionDefinition> &&methods)
: Node(node), docs_(std::move(docs)), is_on_heap_(is_on_heap),
name_(name), arguments_(std::move(arguments)), type_(std::move(type)),
methods_(std::move(methods)) {}
@ -93,7 +96,7 @@ private:
bool is_on_heap_;
Identifier name_;
std::vector<Identifier> arguments_;
VariantType type_; // TupleType is VariantType with one variant
std::optional<VariantType> type_; // TupleType is VariantType with one variant
std::vector<FunctionDefinition> methods_;
}; // IN PROGRESS

View file

@ -111,7 +111,7 @@ public:
fields_.reserve(fields.size());
for (auto &field : fields) {
if (field.first.has_value()) {
annotation_ids_[field.first.value()] = fields_.size();
annotation_fields_[field.first.value()] = fields_.size();
}
fields_.push_back(field.second);
}
@ -124,15 +124,26 @@ public:
const Type *get(size_t id) const { return fields_.at(id).get(); }
Type *get(const std::string &annotation) {
return fields_.at(annotation_ids_.at(annotation)).get();
return fields_.at(annotation_fields_.at(annotation)).get();
}
const Type *get(const std::string &annotation) const {
return fields_.at(annotation_ids_.at(annotation)).get();
return fields_.at(annotation_fields_.at(annotation)).get();
}
std::vector<std::string> get_all_annotations() {
std::vector<std::string> annotations;
annotations.reserve(annotation_fields_.size());
for (auto &annotation_with_field : annotation_fields_) {
annotations.push_back(annotation_with_field.first);
}
return annotations;
}
private:
std::unordered_map<std::string, size_t> annotation_ids_; // Annotations
std::unordered_map<std::string, size_t> annotation_fields_;
std::vector<TypeProxy> fields_;
};