fixes, part of statement printers done

This commit is contained in:
ProgramSnail 2023-07-24 22:50:18 +03:00
parent bf49f4030c
commit c176d1b11d
4 changed files with 315 additions and 7 deletions

View file

@ -19,13 +19,25 @@ public:
: Node(node), import_name_(import_name), module_name_(module_name), : Node(node), import_name_(import_name), module_name_(module_name),
symbols_(std::move(symbols)) {} symbols_(std::move(symbols)) {}
//
Identifier *get_import_name() { return &import_name_; }
const Identifier *get_import_name() const { return &import_name_; }
//
Identifier *get_module_name() { return &module_name_; }
const Identifier *get_module_name() const { return &module_name_; }
//
size_t symbols_size() const { return symbols_.size(); } size_t symbols_size() const { return symbols_.size(); }
std::string *get_symbol(size_t id) { return symbols_.at(id).get(); } Identifier *get_symbol(size_t id) { return &symbols_.at(id); }
const std::string *get_symbol(size_t id) const { const Identifier *get_symbol(size_t id) const { return &symbols_.at(id); }
return symbols_.at(id).get();
}
private: private:
Identifier import_name_; Identifier import_name_;
@ -71,6 +83,106 @@ public:
optional_arguments_(optional_arguments), optional_arguments_(optional_arguments),
result_arguments_(result_arguments), expression_(expression) {} result_arguments_(result_arguments), expression_(expression) {}
//
SymbolDocs *get_docs() { return &docs_; }
const SymbolDocs *get_docs() const { return &docs_; }
//
size_t get_constraints_size() const { return arguments_.size(); }
Constraint *get_constraint(size_t id) { return &constraints_.at(id); }
const Constraint *get_constraint(size_t id) const {
return &constraints_.at(id);
}
//
ModifierType get_modifier() { return modifier_; }
//
Identifier *get_name() { return &name_; }
const Identifier *get_name() const { return &name_; }
//
std::optional<std::string *> get_argument_annotation(size_t id) {
if (annotations_.at(id).has_value()) {
return &annotations_[id].value();
}
return std::nullopt;
}
std::optional<const std::string *> get_argument_annotation(size_t id) const {
if (annotations_.at(id).has_value()) {
return &annotations_[id].value();
}
return std::nullopt;
}
//
size_t get_arguments_size() const { return arguments_.size(); }
Identifier *get_argument(size_t id) { return &arguments_.at(id); }
const Identifier *get_argument(size_t id) const { return &arguments_.at(id); }
//
Modifier get_argument_reference_type(size_t id) {
return reference_types_.at(id);
}
//
size_t get_argument_types_size() const { return types_.size(); }
std::optional<Type *> get_argument_type(size_t id) {
if (types_.at(id).has_value()) {
return types_[id].value().get();
}
return std::nullopt;
}
std::optional<const Type *> get_argument_type(size_t id) const {
if (types_.at(id).has_value()) {
return types_[id].value().get();
}
return std::nullopt;
}
//
bool is_argument_optional(size_t id) const {
return optional_arguments_.at(id);
}
//
bool is_argument_result(size_t id) const { return result_arguments_.at(id); }
//
std::optional<Expression *> get_expression() {
if (expression_.has_value()) {
return expression_.value().get();
}
return std::nullopt;
}
std::optional<const Expression *> get_expression() const {
if (expression_.has_value()) {
return expression_.value().get();
}
return std::nullopt;
}
private: private:
SymbolDocs docs_; SymbolDocs docs_;
std::vector<Constraint> constraints_; std::vector<Constraint> constraints_;
@ -83,7 +195,7 @@ private:
std::vector<bool> optional_arguments_; std::vector<bool> optional_arguments_;
std::vector<bool> result_arguments_; std::vector<bool> result_arguments_;
std::optional<ExpressionProxy> expression_; std::optional<ExpressionProxy> expression_;
}; // IN PROGRESS }; // refactor ??
class TypeDefinition : public Node { class TypeDefinition : public Node {
public: public:
@ -95,6 +207,56 @@ public:
name_(name), arguments_(std::move(arguments)), type_(std::move(type)), name_(name), arguments_(std::move(arguments)), type_(std::move(type)),
methods_(std::move(methods)) {} methods_(std::move(methods)) {}
//
SymbolDocs *get_docs() { return &docs_; }
const SymbolDocs *get_docs() const { return &docs_; }
//
bool is_on_heap() { return is_on_heap_; }
//
Identifier *get_name() { return &name_; }
const Identifier *get_name() const { return &name_; }
//
size_t get_arguments_size() const { return arguments_.size(); }
Identifier *get_argument(size_t id) { return &arguments_.at(id); }
const Identifier *get_argument(size_t id) const { return &arguments_.at(id); }
//
std::optional<VariantType *> get_type() {
if (type_.has_value()) {
return &type_.value();
}
return std::nullopt;
}
std::optional<const VariantType *> get_type() const {
if (type_.has_value()) {
return &type_.value();
}
return std::nullopt;
}
//
size_t get_methods_size() const { return methods_.size(); }
FunctionDefinition *get_method(size_t id) { return &methods_.at(id); }
const FunctionDefinition *get_method(size_t id) const {
return &methods_.at(id);
}
private: private:
SymbolDocs docs_; SymbolDocs docs_;
bool is_on_heap_; bool is_on_heap_;
@ -102,7 +264,7 @@ private:
std::vector<Identifier> arguments_; std::vector<Identifier> arguments_;
std::optional<VariantType> type_; // TupleType is VariantType with one variant std::optional<VariantType> type_; // TupleType is VariantType with one variant
std::vector<FunctionDefinition> methods_; std::vector<FunctionDefinition> methods_;
}; // IN PROGRESS };
class TypeclassDefinition : public Node { class TypeclassDefinition : public Node {
public: public:
@ -113,11 +275,45 @@ public:
base_typeclasses_(std::move(base_typeclasses)), base_typeclasses_(std::move(base_typeclasses)),
methods_(std::move(methods)) {} methods_(std::move(methods)) {}
//
SymbolDocs *get_docs() { return &docs_; }
const SymbolDocs *get_docs() const { return &docs_; }
//
Identifier *get_name() { return &name_; }
const Identifier *get_name() const { return &name_; }
//
size_t get_base_typeclasses_size() const { return base_typeclasses_.size(); }
Identifier *get_base_typeclass(size_t id) {
return &base_typeclasses_.at(id);
}
const Identifier *get_base_typeclass(size_t id) const {
return &base_typeclasses_.at(id);
}
//
size_t get_methods_size() const { return methods_.size(); }
FunctionDefinition *get_method(size_t id) { return &methods_.at(id); }
const FunctionDefinition *get_method(size_t id) const {
return &methods_.at(id);
}
private: private:
SymbolDocs docs_; SymbolDocs docs_;
Identifier name_; Identifier name_;
std::vector<Identifier> base_typeclasses_; std::vector<Identifier> base_typeclasses_;
std::vector<FunctionDefinition> methods_; std::vector<FunctionDefinition> methods_;
}; // IN PROGRESS };
} // namespace nodes } // namespace nodes

View file

@ -0,0 +1,25 @@
#pragma once
#include "basic_printers.hpp"
#include "statement_nodes.hpp"
namespace printers {
void print_source_file(Printer &printer); // TODO
void print_statement(Printer &printer); // TODO
void print_import(const nodes::Import &statement, Printer &printer);
void print_constraint(const nodes::Constraint &statement, Printer &printer);
void print_type_definition(const nodes::TypeDefinition &statement,
Printer &printer);
void print_function_definition(const nodes::FunctionDefinition &statement,
Printer &printer);
void print_typeclass_definition(const nodes::TypeclassDefinition &statement,
Printer &printer);
} // namespace printers

View file

@ -348,6 +348,7 @@ void print_name_expression(const nodes::NameExpression &expression,
printer.print("."); printer.print(".");
} }
// TODO: properly print operator name
print_identifier(*expression.get_name(), printer); print_identifier(*expression.get_name(), printer);
for (size_t i = 0; i < expression.arguments_size(); ++i) { for (size_t i = 0; i < expression.arguments_size(); ++i) {

View file

@ -0,0 +1,86 @@
#include "statement_printers.hpp"
#include "basic_printers.hpp"
#include "expression_printers.hpp"
namespace printers {
void print_import(const nodes::Import &statement, Printer &printer) {
printer.print(":: ");
print_identifier(*statement.get_import_name(), printer);
if (*statement.get_import_name()->get() !=
*statement.get_module_name()->get()) {
printer.print(" = ");
print_identifier(*statement.get_module_name(), printer);
}
if (statement.symbols_size() > 0) {
printer.print(" :");
for (size_t i = 0; i < statement.symbols_size(); ++i) {
printer.space();
// TODO: properly print operator identifier
print_identifier(*statement.get_symbol(i), printer);
}
}
printer.print(";");
printer.new_indent_line();
printer.new_indent_line();
} // IN PROGRESS
void print_constraint(const nodes::Constraint &statement, Printer &printer) {
printer.print("? ");
print_expression(*statement.get_expression(), printer);
printer.print(";");
}
void print_type_definition(const nodes::TypeDefinition &statement,
Printer &printer) {
print_docs(*statement.get_docs(), printer); // TODO
} // IN PROGRESS
void print_function_definition(const nodes::FunctionDefinition &statement,
Printer &printer) {
print_docs(*statement.get_docs(), printer); // TODO
} // IN PROGRESS
void print_typeclass_definition(const nodes::TypeclassDefinition &statement,
Printer &printer) {
print_docs(*statement.get_docs(), printer); // TODO
print_identifier(*statement.get_name(), printer);
if (statement.get_base_typeclasses_size() > 0) {
printer.print(" :");
for (size_t i = 0; i < statement.get_base_typeclasses_size(); ++i) {
printer.space();
print_identifier(*statement.get_base_typeclass(i), 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);
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
} // namespace printers
// TODO: print operator in ()