printing fixes

This commit is contained in:
ProgramSnail 2023-07-25 21:33:57 +03:00
parent 0bb72e0b10
commit 469cb3581f
23 changed files with 318 additions and 151 deletions

View file

@ -12,13 +12,22 @@ include_directories(include
add_executable(lang src/main.cpp add_executable(lang src/main.cpp
src/type_nodes.cpp src/type_nodes.cpp
src/expression_nodes.cpp src/expression_nodes.cpp
src/basic_builders.cpp src/basic_builders.cpp
src/type_builders.cpp src/type_builders.cpp
src/doc_builders.cpp src/doc_builders.cpp
src/expression_builders.cpp src/expression_builders.cpp
src/statement_builders.cpp src/statement_builders.cpp
src/basic_printers.cpp
src/type_printers.cpp
src/doc_printers.cpp
src/expression_printers.cpp
src/statement_printers.cpp
include/tree_sitter_wrapper.hpp include/tree_sitter_wrapper.hpp
deps/tree-sitter-lang/src/tree_sitter/parser.h deps/tree-sitter-lang/src/tree_sitter/parser.h
deps/tree-sitter-lang/src/parser.c deps/tree-sitter-lang/src/parser.c

@ -1 +1 @@
Subproject commit 18d84f5f28da3722749477b3bfa58e1dba397bf5 Subproject commit 1c19b471990acb91cc0ccc9426d988debab8cb27

View file

@ -44,6 +44,8 @@
<RegExpr String="^\: [^\n]*" attribute="Documentation" context="#stay"/> <RegExpr String="^\: [^\n]*" attribute="Documentation" context="#stay"/>
<RegExpr String="^\@[^\n]*" attribute="Documentation" context="#stay"/> <RegExpr String="^\@[^\n]*" attribute="Documentation" context="#stay"/>
<RegExpr String="^#![^\n]*" attribute="Comment" context="#stay"/>
<RegExpr String="\@[a-z_][a-z0-9_]*(?![a-z0-9_])" attribute="Annotation" context="#stay"/> <RegExpr String="\@[a-z_][a-z0-9_]*(?![a-z0-9_])" attribute="Annotation" context="#stay"/>
<RegExpr String="(\:\=)|(\=\:)|\%|\\|\$|(\:\:)|(\-\>)|(&lt;\-)|(&lt;\>)|^" attribute="Keyword" context="#stay"/> <RegExpr String="(\:\=)|(\=\:)|\%|\\|\$|(\:\:)|(\-\>)|(&lt;\-)|(&lt;\>)|^" attribute="Keyword" context="#stay"/>

View file

@ -47,4 +47,8 @@ nodes::Identifier build_operator(parser::ParseTree::Node parser_node);
nodes::Identifier build_placeholder(parser::ParseTree::Node parser_node); nodes::Identifier build_placeholder(parser::ParseTree::Node parser_node);
// --- empty lines
nodes::EmptyLines build_empty_lines(parser::ParseTree::Node parser_node);
} // namespace builders } // namespace builders

View file

@ -53,15 +53,9 @@ public:
return std::nullopt; return std::nullopt;
} }
std::variant<double, long long, std::string, char, bool, unit, null> * auto get_any() { return &value_; }
get_any() {
return &value_;
}
const std::variant<double, long long, std::string, char, bool, unit, null> * auto get_any() const { return &value_; }
get_any() const {
return &value_;
}
private: private:
std::variant<double, long long, std::string, char, bool, unit, null> value_; std::variant<double, long long, std::string, char, bool, unit, null> value_;
@ -86,7 +80,7 @@ public:
Identifier(Node node, IdentifierType type, const std::string &value) Identifier(Node node, IdentifierType type, const std::string &value)
: Node(node), type_(type), value_(value) {} : Node(node), type_(type), value_(value) {}
IdentifierType get_type() { return type_; } IdentifierType get_type() const { return type_; }
std::string *get() { return &value_; } std::string *get() { return &value_; }
@ -97,4 +91,14 @@ private:
std::string value_; std::string value_;
}; };
class EmptyLines : public Node {
public:
EmptyLines(Node node, size_t size) : Node(node), size_(size) {}
size_t size() const { return size_; }
private:
size_t size_;
};
} // namespace nodes } // namespace nodes

View file

@ -11,7 +11,7 @@ public:
bool print_words_instead_of_symbols) bool print_words_instead_of_symbols)
: output_(output), tab_width_(tab_width), width_limit_(width_limit), : output_(output), tab_width_(tab_width), width_limit_(width_limit),
print_words_instead_of_symbols_(print_words_instead_of_symbols), print_words_instead_of_symbols_(print_words_instead_of_symbols),
current_position_(0), current_indentation_level_(0) {} current_position_(0), indentation_level_(0) {}
void print_converted(const std::string &value) { void print_converted(const std::string &value) {
for (auto &ch : value) { for (auto &ch : value) {
@ -34,7 +34,7 @@ public:
void new_indent_line() { void new_indent_line() {
end_line(); end_line();
print_spaces(current_indentation_level_ * tab_width_); print_spaces(indentation_level_);
} }
void new_line(size_t indentation) { void new_line(size_t indentation) {
@ -42,9 +42,9 @@ public:
print_spaces(indentation); print_spaces(indentation);
} }
void indent() { current_indentation_level_ += tab_width_; } void indent() { indentation_level_ += tab_width_; }
void deindent() { current_indentation_level_ -= tab_width_; } void deindent() { indentation_level_ -= tab_width_; }
void tab() { print_spaces(tab_width_); } void tab() { print_spaces(tab_width_); }
@ -66,12 +66,10 @@ public:
size_t get_current_position() const { return current_position_; } size_t get_current_position() const { return current_position_; }
size_t get_current_indentation_level() const { size_t get_indentation_level() const { return indentation_level_; }
return current_indentation_level_;
}
size_t set_current_indentation_level(size_t indentation_level) { void set_indentation_level(size_t indentation_level) {
current_indentation_level_ = indentation_level; indentation_level_ = indentation_level;
} }
private: private:
@ -117,7 +115,7 @@ private:
bool print_words_instead_of_symbols_ = false; bool print_words_instead_of_symbols_ = false;
size_t current_position_ = 0; size_t current_position_ = 0;
size_t current_indentation_level_ = 0; size_t indentation_level_ = 0;
}; };
void print_literal(const nodes::Literal &literal, Printer &printer); void print_literal(const nodes::Literal &literal, Printer &printer);
@ -126,4 +124,6 @@ void print_identifier(const nodes::Identifier &identifier, Printer &printer);
void print_annotation(const std::string &annotation, Printer &printer); void print_annotation(const std::string &annotation, Printer &printer);
void print_empty_lines(const nodes::EmptyLines &empty_lines, Printer &printer);
} // namespace printers } // namespace printers

View file

@ -3,6 +3,7 @@
#include <optional> #include <optional>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector>
namespace nodes { namespace nodes {
@ -16,8 +17,9 @@ public:
SymbolDocs(const std::string &description) : description_(description) {} SymbolDocs(const std::string &description) : description_(description) {}
bool add_annotation_info(const std::string &annotation, std::string &&info) { bool add_annotation_info(const std::string &annotation, std::string &&info) {
if (annotations_info_.count(annotation) == 0) { if (annotations_info_ids_.count(annotation) == 0) {
annotations_info_[annotation] = std::move(info); annotations_info_ids_[annotation] = annotations_info_.size();
annotations_info_.emplace_back(annotation, std::move(info));
return true; return true;
} }
return false; return false;
@ -25,13 +27,16 @@ public:
bool add_annotation_info(const std::string &annotation, bool add_annotation_info(const std::string &annotation,
const std::string &info) { const std::string &info) {
if (annotations_info_.count(annotation) == 0) { if (annotations_info_ids_.count(annotation) == 0) {
annotations_info_[annotation] = info; annotations_info_ids_[annotation] = annotations_info_.size();
annotations_info_.emplace_back(annotation, std::move(info));
return true; return true;
} }
return false; return false;
} }
//
std::optional<std::string *> get_description() { std::optional<std::string *> get_description() {
if (description_.has_value()) { if (description_.has_value()) {
return &description_.value(); return &description_.value();
@ -46,27 +51,52 @@ public:
return std::nullopt; return std::nullopt;
} }
//
std::optional<std::string *> std::optional<std::string *>
get_annotation_info(const std::string &annotation) { get_annotation_info(const std::string &annotation) {
auto info_iterator = annotations_info_.find(annotation); auto info_iterator = annotations_info_ids_.find(annotation);
if (info_iterator != annotations_info_.end()) { if (info_iterator != annotations_info_ids_.end()) {
return &info_iterator->second; return &annotations_info_[info_iterator->second].second;
} }
return std::nullopt; return std::nullopt;
} }
std::optional<const std::string *> std::optional<const std::string *>
get_annotation_info(const std::string &annotation) const { get_annotation_info(const std::string &annotation) const {
auto info_iterator = annotations_info_.find(annotation); auto info_iterator = annotations_info_ids_.find(annotation);
if (info_iterator != annotations_info_.end()) { if (info_iterator != annotations_info_ids_.end()) {
return &info_iterator->second; return &annotations_info_[info_iterator->second].second;
} }
return std::nullopt; return std::nullopt;
} }
//
size_t get_annotations_info_size() const { return annotations_info_.size(); }
std::string *get_annotation(size_t id) {
return &annotations_info_[id].first;
}
const std::string *get_annotation(size_t id) const {
return &annotations_info_[id].first;
}
std::string *get_annotation_info(size_t id) {
return &annotations_info_[id].second;
}
const std::string *get_annotation_info(size_t id) const {
return &annotations_info_[id].second;
}
//
private: private:
std::optional<std::string> description_; std::optional<std::string> description_;
std::unordered_map<std::string, std::string> annotations_info_; std::vector<std::pair<std::string, std::string>> annotations_info_;
std::unordered_map<std::string, size_t> annotations_info_ids_;
}; };
} // namespace nodes } // namespace nodes

View file

@ -167,13 +167,13 @@ public:
: Node(node), type_(LOOP), expression_(expression) {} : Node(node), type_(LOOP), expression_(expression) {}
// WHILE // WHILE
Loop(Node node, ExpressionProxy expression, ExpressionProxy condition) Loop(Node node, ExpressionProxy condition, ExpressionProxy expression)
: Node(node), type_(WHILE), expression_(expression), : Node(node), type_(WHILE), expression_(expression),
condition_(condition) {} condition_(condition) {}
// FOR // FOR
Loop(Node node, ExpressionProxy expression, ExpressionProxy variable, Loop(Node node, ExpressionProxy variable, ExpressionProxy interval,
ExpressionProxy interval) ExpressionProxy expression)
: Node(node), type_(FOR), expression_(expression), variable_(variable), : Node(node), type_(FOR), expression_(expression), variable_(variable),
interval_(interval) {} interval_(interval) {}
@ -528,19 +528,9 @@ public:
return std::nullopt; return std::nullopt;
} }
std::variant<Match, Condition, Loop, Container, Return, NameDefinition, auto get_any() { return &expression_; }
Access, LoopControl, ModifierExpression, NameExpression,
Constructor, Lambda, Literal> *
get_any() {
return &expression_;
}
const std::variant<Match, Condition, Loop, Container, Return, NameDefinition, auto get_any() const { return &expression_; }
Access, LoopControl, ModifierExpression, NameExpression,
Constructor, Lambda, Literal> *
get_any() const {
return &expression_;
}
bool is_scoped() const { return is_scoped_; } bool is_scoped() const { return is_scoped_; }
@ -570,7 +560,10 @@ private:
NameExpression, Constructor, Lambda, NameExpression, Constructor, Lambda,
// --- literal // --- literal
Literal Literal,
// --- empty lines
EmptyLines
> >
expression_; expression_;

View file

@ -75,13 +75,16 @@ public:
std::vector<TypeProxy> &&types, std::vector<TypeProxy> &&types,
std::vector<bool> &&optional_arguments, std::vector<bool> &&optional_arguments,
std::vector<bool> &&result_arguments, std::vector<bool> &&result_arguments,
bool is_annotations_same_to_names,
std::optional<ExpressionProxy> expression) std::optional<ExpressionProxy> expression)
: Node(node), docs_(std::move(docs)), : Node(node), docs_(std::move(docs)),
constraints_(std::move(constraints)), modifier_(modifier), name_(name), constraints_(std::move(constraints)), modifier_(modifier), name_(name),
annotations_(std::move(annotations)), arguments_(std::move(arguments)), 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)),
optional_arguments_(optional_arguments), optional_arguments_(optional_arguments),
result_arguments_(result_arguments), expression_(expression) {} result_arguments_(result_arguments),
is_annotations_same_to_names_(is_annotations_same_to_names),
expression_(expression) {}
// //
@ -91,7 +94,7 @@ public:
// //
size_t get_constraints_size() const { return arguments_.size(); } size_t get_constraints_size() const { return constraints_.size(); }
Constraint *get_constraint(size_t id) { return &constraints_.at(id); } Constraint *get_constraint(size_t id) { return &constraints_.at(id); }
@ -159,6 +162,12 @@ public:
// //
bool is_annotations_same_to_names() const {
return is_annotations_same_to_names_;
}
//
std::optional<Expression *> get_expression() { std::optional<Expression *> get_expression() {
if (expression_.has_value()) { if (expression_.has_value()) {
return expression_.value().get(); return expression_.value().get();
@ -184,6 +193,7 @@ private:
std::vector<TypeProxy> types_; std::vector<TypeProxy> types_;
std::vector<bool> optional_arguments_; std::vector<bool> optional_arguments_;
std::vector<bool> result_arguments_; std::vector<bool> result_arguments_;
bool is_annotations_same_to_names_;
std::optional<ExpressionProxy> expression_; std::optional<ExpressionProxy> expression_;
}; // refactor ?? }; // refactor ??

View file

@ -5,9 +5,9 @@
namespace printers { namespace printers {
void print_source_file(Printer &printer); // TODO // void print_source_file(Printer &printer); // TODO
void print_statement(Printer &printer); // TODO // void print_statement(Printer &printer); // TODO
void print_import(const nodes::Import &statement, Printer &printer); void print_import(const nodes::Import &statement, Printer &printer);

View file

@ -62,6 +62,8 @@ enum class Type {
DEFINITION_INFO, DEFINITION_INFO,
ANNOTATION_INFO, ANNOTATION_INFO,
EMPTY_LINES,
// --- tokens // --- tokens
PLACEHOLDER, PLACEHOLDER,
@ -146,6 +148,8 @@ const static std::string TYPE = "type";
const static std::string DEFINITION_INFO = "definition_info"; const static std::string DEFINITION_INFO = "definition_info";
const static std::string ANNOTATION_INFO = "annotation_info"; const static std::string ANNOTATION_INFO = "annotation_info";
const static std::string EMPTY_LINES = "empty_lines";
// --- tokens // --- tokens
const static std::string PLACEHOLDER = "placeholder"; const static std::string PLACEHOLDER = "placeholder";
@ -228,6 +232,8 @@ inline Type string_to_type(const std::string &str) {
return Type::DEFINITION_INFO; return Type::DEFINITION_INFO;
} else if (str == ANNOTATION_INFO) { } else if (str == ANNOTATION_INFO) {
return Type::ANNOTATION_INFO; return Type::ANNOTATION_INFO;
} else if (str == EMPTY_LINES) {
return Type::EMPTY_LINES;
} else if (str == PLACEHOLDER) { } else if (str == PLACEHOLDER) {
return Type::PLACEHOLDER; return Type::PLACEHOLDER;
} else if (str == SIMPLE_NAME_IDENTIFIER) { } else if (str == SIMPLE_NAME_IDENTIFIER) {

View file

@ -63,6 +63,16 @@ public:
return source_->substr(start, end - start); return source_->substr(start, end - start);
} }
size_t get_value_length() const { // from source
if (is_null()) {
error_handling::handle_general_error(
"Null parsing node method called (get_value_length)");
}
size_t start = ts_node_start_byte(node_);
size_t end = ts_node_end_byte(node_);
return end - start;
}
bool is_null() const { return ts_node_is_null(node_); } bool is_null() const { return ts_node_is_null(node_); }
bool is_named() const { bool is_named() const {

View file

@ -224,12 +224,14 @@ std::string build_annotation(parser::ParseTree::Node parser_node) {
nodes::Identifier build_operator(parser::ParseTree::Node parser_node) { nodes::Identifier build_operator(parser::ParseTree::Node parser_node) {
std::string identifier = parser_node.get_value(); std::string identifier = parser_node.get_value();
if (identifier.size() > 0 && identifier.front() != '.') { // --- points needed for proper printing
// for not point only identifiers // if (identifier.size() > 0 && identifier.front() != '.') {
while (identifier.size() > 0 && identifier.back() == '.') { // // for not point only identifiers
identifier.pop_back(); // while (identifier.size() > 0 && identifier.back() == '.') {
} // identifier.pop_back();
} // }
// }
// ---
return nodes::Identifier(build_node(parser_node), nodes::Identifier::OPERATOR, return nodes::Identifier(build_node(parser_node), nodes::Identifier::OPERATOR,
identifier); identifier);
@ -240,4 +242,11 @@ nodes::Identifier build_placeholder(parser::ParseTree::Node parser_node) {
nodes::Identifier::PLACEHOLDER, "_"); nodes::Identifier::PLACEHOLDER, "_");
} }
// --- empty lines
nodes::EmptyLines build_empty_lines(parser::ParseTree::Node parser_node) {
return nodes::EmptyLines(build_node(parser_node),
parser_node.get_value_length() - 1);
}
} // namespace builders } // namespace builders

View file

@ -53,4 +53,10 @@ void print_annotation(const std::string &annotation, Printer &printer) {
printer.print(annotation); printer.print(annotation);
} }
void print_empty_lines(const nodes::EmptyLines &empty_lines, Printer &printer) {
for (size_t i = 0; i < empty_lines.size(); ++i) {
printer.new_indent_line();
}
}
} // namespace printers } // namespace printers

View file

@ -18,8 +18,8 @@ nodes::SymbolDocs build_symbol_docs(
// remove newline delimeters (": " at each new line // remove newline delimeters (": " at each new line
size_t j = 0; size_t j = 0;
for (size_t i = 0; i < description.value().size(); ++i, ++j) { for (size_t i = 2; i < description.value().size(); ++i, ++j) {
if (j != i) { if (j != i) { // always true
description.value()[j] = description.value()[i]; description.value()[j] = description.value()[i];
} }

View file

@ -1,4 +1,5 @@
#include "doc_printers.hpp" #include "doc_printers.hpp"
#include "basic_printers.hpp"
#include "utils.hpp" #include "utils.hpp"
namespace printers { namespace printers {
@ -6,8 +7,27 @@ namespace printers {
// TODO // TODO
void print_docs(const nodes::SymbolDocs &docs, Printer &printer) { void print_docs(const nodes::SymbolDocs &docs, Printer &printer) {
auto description = docs.get_description();
error_handling::handle_general_error("Docs printing unimplemented yet"); if (description.has_value()) {
printer.print(": ");
for (auto &ch : *description.value()) {
if (ch == '\n') {
printer.new_indent_line();
printer.print(": ");
} else {
printer.print(std::string(1, ch));
}
}
printer.new_indent_line();
}
for (size_t i = 0; i < docs.get_annotations_info_size(); ++i) {
print_annotation(*docs.get_annotation(i), printer);
printer.space();
printer.print(*docs.get_annotation_info(i));
printer.new_indent_line();
}
} // IN PROGRESS } // IN PROGRESS

View file

@ -16,9 +16,10 @@ build_expression(parser::ParseTree::Node parser_node,
nodes::TypeStorage &type_storage) { nodes::TypeStorage &type_storage) {
tokens::Type type = tokens::string_to_type(parser_node.get_type()); tokens::Type type = tokens::string_to_type(parser_node.get_type());
auto maybe_parenthesis = parser_node.next_sibling(); auto maybe_parenthesis = parser_node.previous_sibling();
bool is_scoped = bool is_scoped =
(!maybe_parenthesis.is_null() && maybe_parenthesis.get_value() == "("); (!maybe_parenthesis.is_null() && !maybe_parenthesis.is_named() &&
maybe_parenthesis.get_value() == "(");
switch (type) { switch (type) {
// --- flow control // --- flow control
@ -119,6 +120,9 @@ build_expression(parser::ParseTree::Node parser_node,
case tokens::Type::NULL_LITERAL: case tokens::Type::NULL_LITERAL:
return expression_storage.add_expression( return expression_storage.add_expression(
nodes::Expression(build_null_literal(parser_node), is_scoped)); nodes::Expression(build_null_literal(parser_node), is_scoped));
case tokens::Type::EMPTY_LINES:
return expression_storage.add_expression(
nodes::Expression(build_empty_lines(parser_node), is_scoped));
default: default:
error_handling::handle_parsing_error("Unexprected expression node type", error_handling::handle_parsing_error("Unexprected expression node type",
parser_node); parser_node);
@ -271,7 +275,7 @@ build_comma_expression(parser::ParseTree::Node parser_node,
return nodes::NameExpression( return nodes::NameExpression(
build_node(parser_node), build_node(parser_node),
nodes::Identifier( nodes::Identifier(
build_node(parser_node), // can't find more precise location' build_node(parser_node), // can't find more precise location
nodes::Identifier::SIMPLE_NAME, ","), nodes::Identifier::SIMPLE_NAME, ","),
std::move(arguments), std::nullopt, false, true); std::move(arguments), std::nullopt, false, true);
} }
@ -308,7 +312,7 @@ build_container(parser::ParseTree::Node parser_node,
nodes::Container::ContainerType container_type) { nodes::Container::ContainerType container_type) {
std::vector<nodes::ExpressionProxy> expressions; std::vector<nodes::ExpressionProxy> expressions;
auto current_node = parser_node.nth_named_child(0).next_named_sibling(); auto current_node = parser_node.nth_named_child(0);
while (!current_node.is_null()) { while (!current_node.is_null()) {
expressions.push_back( expressions.push_back(

View file

@ -2,6 +2,7 @@
#include "basic_nodes.hpp" #include "basic_nodes.hpp"
#include "basic_printers.hpp" #include "basic_printers.hpp"
#include "expression_nodes.hpp" #include "expression_nodes.hpp"
#include "tokens.hpp"
#include "type_printers.hpp" #include "type_printers.hpp"
namespace printers { namespace printers {
@ -60,6 +61,10 @@ void print_expression(const nodes::Expression &expression,
case 12: // Literal case 12: // Literal
print_literal(*expression.get<nodes::Literal>().value(), printer); print_literal(*expression.get<nodes::Literal>().value(), printer);
break; break;
// --- empty lines
case 13: // EmptyLines
print_empty_lines(*expression.get<nodes::EmptyLines>().value(), printer);
break;
} }
if (expression.is_scoped()) { if (expression.is_scoped()) {
@ -100,9 +105,9 @@ void print_match(const nodes::Match &expression, printers::Printer &printer) {
printer.space(); printer.space();
size_t previous_indentation_level = printer.get_current_indentation_level(); size_t previous_indentation_level = printer.get_indentation_level();
printer.set_current_indentation_level(printer.current_position()); printer.set_indentation_level(printer.current_position());
for (size_t i = 0; i < expression.cases_size(); ++i) { for (size_t i = 0; i < expression.cases_size(); ++i) {
print_case(*expression.get_case(i), printer); print_case(*expression.get_case(i), printer);
@ -112,7 +117,7 @@ void print_match(const nodes::Match &expression, printers::Printer &printer) {
} }
} }
printer.set_current_indentation_level(previous_indentation_level); printer.set_indentation_level(previous_indentation_level);
} // IN PROGRESS } // IN PROGRESS
@ -123,17 +128,19 @@ void print_condition(const nodes::Condition &expression,
if (i == 0) { if (i == 0) {
printer.print(printer.print_words_instead_of_symbols() ? "if " : "?? "); printer.print(printer.print_words_instead_of_symbols() ? "if " : "?? ");
} else { } else {
printer.new_indent_line();
printer.print(printer.print_words_instead_of_symbols() ? "elif " : "!! "); printer.print(printer.print_words_instead_of_symbols() ? "elif " : "!! ");
} }
print_expression(*expression.get_case(i).first, printer); print_expression(*expression.get_case(i).first, printer);
printer.print(printer.print_words_instead_of_symbols() ? "do " : "=> "); printer.print(printer.print_words_instead_of_symbols() ? " do " : " => ");
print_expression(*expression.get_case(i).second, printer); print_expression(*expression.get_case(i).second, printer);
} }
if (expression.get_else_case().has_value()) { if (expression.get_else_case().has_value()) {
printer.new_indent_line();
printer.print(printer.print_words_instead_of_symbols() ? "else " : "!!=> "); printer.print(printer.print_words_instead_of_symbols() ? "else " : "!!=> ");
print_expression(*expression.get_else_case().value(), printer); print_expression(*expression.get_else_case().value(), printer);
} }
@ -163,7 +170,6 @@ void print_loop(const nodes::Loop &expression, printers::Printer &printer) {
printer.print(printer.print_words_instead_of_symbols() ? "do " : "=> "); printer.print(printer.print_words_instead_of_symbols() ? "do " : "=> ");
print_expression(*expression.get_expression(), printer); print_expression(*expression.get_expression(), printer);
printer.print(";");
} // IN PROGRESS } // IN PROGRESS
// --- containers // --- containers
@ -177,16 +183,23 @@ void print_container(const nodes::Container &expression,
} else { } else {
printer.print("{"); printer.print("{");
printer.indent(); printer.indent();
printer.new_indent_line();
} }
for (size_t i = 0; i < expression.expressions_size(); ++i) { for (size_t i = 0; i < expression.expressions_size(); ++i) {
bool is_empty_lines_expression = expression.get_expression(i)->get<nodes::EmptyLines>().has_value();
if (!is_array && !is_empty_lines_expression) {
printer.new_indent_line();
}
print_expression(*expression.get_expression(i), printer); print_expression(*expression.get_expression(i), printer);
if (is_array) { if (is_array) {
printer.space(); if (i + 1 < expression.expressions_size()) {
printer.space();
}
} else { } else {
printer.print(";"); if (!is_empty_lines_expression) {
printer.new_indent_line(); printer.print(";");
}
} }
} }
@ -194,8 +207,8 @@ void print_container(const nodes::Container &expression,
printer.print("]]"); printer.print("]]");
} else { } else {
printer.deindent(); printer.deindent();
printer.print("}");
printer.new_indent_line(); printer.new_indent_line();
printer.print("}");
} }
} // IN PROGRESS } // IN PROGRESS
@ -234,10 +247,10 @@ void print_name_definition(const nodes::NameDefinition &expression,
} else { } else {
switch (expression.get_modifier()) { switch (expression.get_modifier()) {
case nodes::NameDefinition::LET: case nodes::NameDefinition::LET:
printer.print("% "); printer.print("%");
break; break;
case nodes::NameDefinition::VAR: case nodes::NameDefinition::VAR:
printer.print("$ "); printer.print("$");
break; break;
default: default:
break; break;
@ -343,7 +356,7 @@ void print_name_expression(const nodes::NameExpression &expression,
print_expression(*expression.get_argument_value(0), printer); print_expression(*expression.get_argument_value(0), printer);
if (expression.is_point_call()) { if (expression.is_point_call()) {
printer.print("."); printer.print(".");
} else { } else if (*expression.get_name()->get() != ",") {
printer.space(); printer.space();
} }
} else if (expression.get_prefix().has_value()) { } else if (expression.get_prefix().has_value()) {
@ -351,10 +364,27 @@ void print_name_expression(const nodes::NameExpression &expression,
printer.print("."); printer.print(".");
} }
// TODO: properly print operator name //
bool operator_called_as_function =
(!expression.is_operator_call() &&
expression.get_name()->get_type() == nodes::Identifier::OPERATOR);
if (operator_called_as_function) {
printer.print("( ");
}
print_identifier(*expression.get_name(), printer); print_identifier(*expression.get_name(), printer);
for (size_t i = 0; i < expression.arguments_size(); ++i) { if (operator_called_as_function) {
printer.print(" )");
}
//
for (size_t i =
expression.is_operator_call() || expression.is_point_call() ? 1 : 0;
i < expression.arguments_size(); ++i) {
printer.space(); printer.space();
if (expression.get_argument_annotation(i).has_value()) { if (expression.get_argument_annotation(i).has_value()) {
@ -384,7 +414,7 @@ void print_constructor(const nodes::Constructor &expression,
} // IN PROGRESS } // IN PROGRESS
void print_lambda(const nodes::Lambda &expression, printers::Printer &printer) { void print_lambda(const nodes::Lambda &expression, printers::Printer &printer) {
printer.print(printer.print_words_instead_of_symbols() ? "lambda " : "\\ "); printer.print(printer.print_words_instead_of_symbols() ? "lambda " : "\\");
for (size_t i = 0; i < expression.arguments_size(); ++i) { for (size_t i = 0; i < expression.arguments_size(); ++i) {
print_identifier(*expression.get_argument(i), printer); print_identifier(*expression.get_argument(i), printer);

View file

@ -2,11 +2,13 @@
#include "basic_builders.hpp" #include "basic_builders.hpp"
#include "basic_nodes.hpp" #include "basic_nodes.hpp"
#include "basic_printers.hpp"
#include "doc_builders.hpp" #include "doc_builders.hpp"
#include "doc_nodes.hpp" #include "doc_nodes.hpp"
#include "error_handling.hpp" #include "error_handling.hpp"
#include "expression_builders.hpp" #include "expression_builders.hpp"
#include "statement_nodes.hpp" #include "statement_nodes.hpp"
#include "statement_printers.hpp"
#include "tokens.hpp" #include "tokens.hpp"
#include "tree_sitter_wrapper.hpp" #include "tree_sitter_wrapper.hpp"
#include "type_builders.hpp" #include "type_builders.hpp"
@ -37,18 +39,42 @@ void build_statement(parser::ParseTree::Node parser_node,
nodes::TypeStorage &type_storage) { nodes::TypeStorage &type_storage) {
tokens::Type type = tokens::string_to_type(parser_node.get_type()); tokens::Type type = tokens::string_to_type(parser_node.get_type());
printers::Printer printer(std::cout, 2, 80, false);
switch (type) { switch (type) {
case tokens::Type::IMPORT: case tokens::Type::IMPORT:
build_import(parser_node); build_import(parser_node);
printers::print_import(build_import(parser_node),
printer); // TODO TEMPORARY
printer.new_indent_line();
return; return;
case tokens::Type::TYPE_DEFINITION: case tokens::Type::TYPE_DEFINITION:
build_type_definition(parser_node, expression_storage, type_storage); build_type_definition(parser_node, expression_storage, type_storage);
printers::print_type_definition(
build_type_definition(parser_node, expression_storage, type_storage),
printer); // TODO TEMPORARY
printer.new_indent_line();
return; return;
case tokens::Type::FUNCTION_DEFINITION: case tokens::Type::FUNCTION_DEFINITION:
build_function_definition(parser_node, expression_storage, type_storage); build_function_definition(parser_node, expression_storage, type_storage);
printers::print_function_definition(
build_function_definition(parser_node, expression_storage,
type_storage),
printer); // TODO TEMPORARY
printer.new_indent_line();
return; return;
case tokens::Type::TYPECLASS_DEFINITION: case tokens::Type::TYPECLASS_DEFINITION:
build_typeclass_definition(parser_node, expression_storage, type_storage); build_typeclass_definition(parser_node, expression_storage, type_storage);
printers::print_typeclass_definition(
build_typeclass_definition(parser_node, expression_storage,
type_storage),
printer); // TODO TEMPORARY
printer.new_indent_line();
return;
case tokens::Type::EMPTY_LINES:
build_empty_lines(parser_node);
printers::print_empty_lines(build_empty_lines(parser_node),
printer); // TODO TEMPORARY
return; return;
default: default:
error_handling::handle_parsing_error("Unexprected statement node type", error_handling::handle_parsing_error("Unexprected statement node type",
@ -219,7 +245,7 @@ build_function_definition(parser::ParseTree::Node parser_node,
nodes::FunctionDefinition::STATIC; nodes::FunctionDefinition::STATIC;
current_node = name_node.previous_sibling(); current_node = name_node.previous_sibling();
if (!current_node.is_null() && current_node.is_named()) { if (!current_node.is_null() && !current_node.is_named()) {
std::string modifier_str = current_node.get_value(); std::string modifier_str = current_node.get_value();
if (modifier_str == "%" || modifier_str == "let") { if (modifier_str == "%" || modifier_str == "let") {
modifier = nodes::FunctionDefinition::LET; modifier = nodes::FunctionDefinition::LET;
@ -270,11 +296,11 @@ build_function_definition(parser::ParseTree::Node parser_node,
argument_reference_types.push_back(last_reference_type); argument_reference_types.push_back(last_reference_type);
arguments.push_back(build_identifier(current_node)); arguments.push_back(build_identifier(current_node));
optional_arguments.push_back(!current_node.next_sibling().is_null() && optional_arguments.push_back(!current_node.next_sibling().is_null() &&
current_node.next_sibling().is_named() && !current_node.next_sibling().is_named() &&
current_node.next_sibling().get_value() == current_node.next_sibling().get_value() ==
"?"); "?");
result_arguments.push_back(!current_node.next_sibling().is_null() && result_arguments.push_back(!current_node.next_sibling().is_null() &&
current_node.next_sibling().is_named() && !current_node.next_sibling().is_named() &&
current_node.next_sibling().get_value() == current_node.next_sibling().get_value() ==
"!"); "!");
if (optional_arguments.back() || result_arguments.back()) { if (optional_arguments.back() || result_arguments.back()) {
@ -306,7 +332,10 @@ build_function_definition(parser::ParseTree::Node parser_node,
std::vector<std::optional<std::string>> *annotations = nullptr; std::vector<std::optional<std::string>> *annotations = nullptr;
std::vector<nodes::Modifier> *reference_types = nullptr; std::vector<nodes::Modifier> *reference_types = nullptr;
if (!at_least_one_argument_annotation_found) { bool is_annotations_same_to_names =
(!at_least_one_argument_annotation_found && types.empty());
if (is_annotations_same_to_names) {
for (size_t i = 0; i < argument_annotations.size(); ++i) { for (size_t i = 0; i < argument_annotations.size(); ++i) {
std::string argument_annotation = *arguments[i].get(); std::string argument_annotation = *arguments[i].get();
argument_annotations[i] = argument_annotations[i] =
@ -346,6 +375,7 @@ build_function_definition(parser::ParseTree::Node parser_node,
std::move(*annotations), std::move(arguments), std::move(*annotations), std::move(arguments),
std::move(*reference_types), std::move(types), std::move(*reference_types), std::move(types),
std::move(optional_arguments), std::move(result_arguments), std::move(optional_arguments), std::move(result_arguments),
is_annotations_same_to_names,
expression_node.has_value() expression_node.has_value()
? build_expression(expression_node.value(), expression_storage, ? build_expression(expression_node.value(), expression_storage,
type_storage) type_storage)

View file

@ -28,9 +28,6 @@ void print_import(const nodes::Import &statement, Printer &printer) {
} }
} }
printer.print(";"); printer.print(";");
printer.new_indent_line();
printer.new_indent_line();
} // IN PROGRESS } // IN PROGRESS
void print_constraint(const nodes::Constraint &statement, Printer &printer) { void print_constraint(const nodes::Constraint &statement, Printer &printer) {
@ -39,6 +36,28 @@ void print_constraint(const nodes::Constraint &statement, Printer &printer) {
printer.print(";"); printer.print(";");
} }
template <typename T>
void print_statement_methods(const T &statement, Printer &printer) {
if (statement.get_methods_size() > 0) {
printer.print(" {");
printer.indent();
for (size_t i = 0; i < statement.get_methods_size(); ++i) {
printer.new_indent_line();
print_function_definition(*statement.get_method(i), printer);
if (i + 1 < statement.get_methods_size()) {
printer.new_indent_line();
}
}
printer.deindent();
printer.new_indent_line();
printer.print("}");
} else {
printer.print(";");
}
}
void print_type_definition(const nodes::TypeDefinition &statement, void print_type_definition(const nodes::TypeDefinition &statement,
Printer &printer) { Printer &printer) {
print_docs(*statement.get_docs(), printer); // TODO print_docs(*statement.get_docs(), printer); // TODO
@ -57,33 +76,16 @@ void print_type_definition(const nodes::TypeDefinition &statement,
if (statement.get_type().has_value()) { if (statement.get_type().has_value()) {
printer.print(" = "); printer.print(" = ");
size_t previous_indentation_level = printer.get_current_indentation_level(); size_t previous_indentation_level = printer.get_indentation_level();
printer.set_current_indentation_level(printer.get_current_position()); printer.set_indentation_level(printer.get_current_position() - 2);
print_variant_type(*statement.get_type().value(), printer); print_variant_type(*statement.get_type().value(), printer);
printer.set_current_indentation_level(previous_indentation_level); printer.set_indentation_level(previous_indentation_level);
}
// TODO: same to typeclass, make separated function
if (statement.get_methods_size() > 0) {
printer.print(" {");
printer.indent();
for (size_t i = 0; i < statement.get_methods_size(); ++i) {
printer.new_indent_line();
print_function_definition(*statement.get_method(i), printer);
printer.new_indent_line();
}
printer.deindent();
printer.new_indent_line();
printer.print("} ");
} else {
printer.print(";");
} }
print_statement_methods(statement, printer);
} // IN PROGRESS } // IN PROGRESS
void print_function_definition(const nodes::FunctionDefinition &statement, void print_function_definition(const nodes::FunctionDefinition &statement,
@ -99,22 +101,36 @@ void print_function_definition(const nodes::FunctionDefinition &statement,
case nodes::FunctionDefinition::STATIC: case nodes::FunctionDefinition::STATIC:
break; break;
case nodes::FunctionDefinition::LET: case nodes::FunctionDefinition::LET:
printer.print(printer.print_words_instead_of_symbols() ? "let " : "% "); printer.print(printer.print_words_instead_of_symbols() ? "let " : "%");
break; break;
case nodes::FunctionDefinition::VAR: case nodes::FunctionDefinition::VAR:
printer.print(printer.print_words_instead_of_symbols() ? "var " : "$ "); printer.print(printer.print_words_instead_of_symbols() ? "var " : "$");
break; break;
default: // remove ?? default: // remove ??
break; break;
} }
bool operator_called_as_function =
(statement.get_name()->get_type() == nodes::Identifier::OPERATOR);
if (operator_called_as_function) {
printer.print("( ");
}
print_identifier(*statement.get_name(), printer); print_identifier(*statement.get_name(), printer);
if (operator_called_as_function) {
printer.print(" )");
}
// TODO: same fragments with function type, maybe better make separated // TODO: same fragments with function type, maybe better make separated
// function // function
for (size_t i = 0; i < statement.get_arguments_size(); ++i) { for (size_t i = 0; i < statement.get_arguments_size(); ++i) {
printer.space();
if (statement.get_argument_types_size() == 0) { if (statement.get_argument_types_size() == 0) {
if (statement.get_argument_annotation(i).has_value()) { if (!statement.is_annotations_same_to_names() &&
statement.get_argument_annotation(i).has_value()) {
print_annotation(*statement.get_argument_annotation(i).value(), print_annotation(*statement.get_argument_annotation(i).value(),
printer); printer);
printer.space(); printer.space();
@ -154,7 +170,7 @@ void print_function_definition(const nodes::FunctionDefinition &statement,
} }
} }
if (statement.get_argument_types_size() > 0) { // TODO if (statement.get_argument_types_size() > 0) {
printer.print(" :"); printer.print(" :");
for (size_t i = 0; i < statement.get_argument_types_size(); ++i) { for (size_t i = 0; i < statement.get_argument_types_size(); ++i) {
printer.space(); printer.space();
@ -183,22 +199,18 @@ void print_function_definition(const nodes::FunctionDefinition &statement,
} }
print_type(*statement.get_argument_type(i), printer); print_type(*statement.get_argument_type(i), printer);
// TODO: can't be optional and result in same time
if (statement.is_argument_optional(i)) {
printer.print("?");
}
if (statement.is_argument_result(i)) {
printer.print("!");
}
} }
} }
if (statement.get_expression().has_value()) { if (statement.get_expression().has_value()) {
printer.print(" = "); printer.print(" = ");
print_expression(*statement.get_expression().value(), printer); print_expression(*statement.get_expression().value(), printer);
if (!statement.get_expression()
.value()
->get<nodes::Container>()
.has_value()) {
printer.print(";");
}
} else { } else {
printer.print(";"); printer.print(";");
} }
@ -218,25 +230,7 @@ void print_typeclass_definition(const nodes::TypeclassDefinition &statement,
} }
} }
if (statement.get_methods_size() > 0) { print_statement_methods(statement, printer);
printer.print(" {");
printer.indent();
for (size_t i = 0; i < statement.get_methods_size(); ++i) {
printer.new_indent_line();
print_function_definition(*statement.get_method(i), printer);
printer.new_indent_line();
}
printer.deindent();
printer.new_indent_line();
printer.print("} ");
} else {
printer.print(";");
}
printer.new_indent_line();
printer.new_indent_line();
} // IN PROGRESS } // IN PROGRESS
} // namespace printers } // namespace printers

View file

@ -19,12 +19,16 @@ void print_type(const nodes::Type &type, printers::Printer &printer) {
printer.print("!"); printer.print("!");
} }
printer.print("["); if (type.get_parametrs_size() > 0) {
for (size_t i = 0; i < type.get_parametrs_size(); ++i) { printer.print("[");
print_type(*type.get_parameter(i), printer); for (size_t i = 0; i < type.get_parametrs_size(); ++i) {
printer.space(); print_type(*type.get_parameter(i), printer);
if (i + 1 < type.get_parametrs_size()) {
printer.space();
}
}
printer.print("]");
} }
printer.print("]");
} }
void print_tuple_type(const nodes::TupleType &type, void print_tuple_type(const nodes::TupleType &type,
@ -37,6 +41,7 @@ void print_tuple_type(const nodes::TupleType &type,
if (annotation.has_value()) { if (annotation.has_value()) {
print_annotation(*annotation.value(), printer); print_annotation(*annotation.value(), printer);
printer.space();
} }
print_type(*type.get(i), printer); print_type(*type.get(i), printer);
@ -46,7 +51,7 @@ void print_tuple_type(const nodes::TupleType &type,
void print_variant_type(const nodes::VariantType &type, void print_variant_type(const nodes::VariantType &type,
printers::Printer &printer) { printers::Printer &printer) {
for (size_t i = 0; i < type.size(); ++i) { for (size_t i = 0; i < type.size(); ++i) {
if (type.size() > 1 || i > 0) { if (i > 0) {
printer.print("| "); printer.print("| ");
} }

View file

@ -85,7 +85,7 @@ func
'a 'a
'b 'b
'c 'c
: A B C -> D : A B C -> D;
func 'a 'b 'c func 'a 'b 'c
: :
@ -127,7 +127,7 @@ func = {
// operators, containers, modifiers // operators, containers, modifiers
func { func = {
x + y; x + y;
@ -167,10 +167,11 @@ func {
<> x; <> x;
x?; x?;
}
// other // other
func { func = {
x x
.func a b c; .func a b c;

View file

@ -90,7 +90,7 @@ func_2 = {
maybe_something!; maybe_something!;
// open optional: if null then return default value (operator) // open optional: if null then return default value (operator)
%x := maybe_something ?| value %x := maybe_something ?| value;
%y := Fruit @apple (); %y := Fruit @apple ();