diff --git a/include/statement_nodes.hpp b/include/statement_nodes.hpp index 9824122..b0ef122 100644 --- a/include/statement_nodes.hpp +++ b/include/statement_nodes.hpp @@ -19,13 +19,25 @@ public: : Node(node), import_name_(import_name), module_name_(module_name), 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(); } - 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 { - return symbols_.at(id).get(); - } + const Identifier *get_symbol(size_t id) const { return &symbols_.at(id); } private: Identifier import_name_; @@ -71,6 +83,106 @@ public: optional_arguments_(optional_arguments), 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 get_argument_annotation(size_t id) { + if (annotations_.at(id).has_value()) { + return &annotations_[id].value(); + } + return std::nullopt; + } + + std::optional 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 get_argument_type(size_t id) { + if (types_.at(id).has_value()) { + return types_[id].value().get(); + } + return std::nullopt; + } + + std::optional 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 get_expression() { + if (expression_.has_value()) { + return expression_.value().get(); + } + return std::nullopt; + } + + std::optional get_expression() const { + if (expression_.has_value()) { + return expression_.value().get(); + } + return std::nullopt; + } + private: SymbolDocs docs_; std::vector constraints_; @@ -83,7 +195,7 @@ private: std::vector optional_arguments_; std::vector result_arguments_; std::optional expression_; -}; // IN PROGRESS +}; // refactor ?? class TypeDefinition : public Node { public: @@ -95,6 +207,56 @@ public: name_(name), arguments_(std::move(arguments)), type_(std::move(type)), 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 get_type() { + if (type_.has_value()) { + return &type_.value(); + } + return std::nullopt; + } + + std::optional 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: SymbolDocs docs_; bool is_on_heap_; @@ -102,7 +264,7 @@ private: std::vector arguments_; std::optional type_; // TupleType is VariantType with one variant std::vector methods_; -}; // IN PROGRESS +}; class TypeclassDefinition : public Node { public: @@ -113,11 +275,45 @@ public: base_typeclasses_(std::move(base_typeclasses)), 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: SymbolDocs docs_; Identifier name_; std::vector base_typeclasses_; std::vector methods_; -}; // IN PROGRESS +}; } // namespace nodes diff --git a/include/statement_printers.hpp b/include/statement_printers.hpp new file mode 100644 index 0000000..29c3013 --- /dev/null +++ b/include/statement_printers.hpp @@ -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 diff --git a/src/expression_printers.cpp b/src/expression_printers.cpp index 08ed8ed..10dd1d3 100644 --- a/src/expression_printers.cpp +++ b/src/expression_printers.cpp @@ -348,6 +348,7 @@ void print_name_expression(const nodes::NameExpression &expression, printer.print("."); } + // TODO: properly print operator name print_identifier(*expression.get_name(), printer); for (size_t i = 0; i < expression.arguments_size(); ++i) { diff --git a/src/statement_printers.cpp b/src/statement_printers.cpp new file mode 100644 index 0000000..890e97a --- /dev/null +++ b/src/statement_printers.cpp @@ -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 ()