types for typecheck, sources manager

This commit is contained in:
ProgramSnail 2023-08-02 17:54:39 +03:00
parent 4714a05467
commit ef88e6af86
9 changed files with 353 additions and 104 deletions

View file

@ -1,14 +1,4 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include "basic_printers.hpp"
#include "error_handling.hpp"
#include "expression_nodes.hpp"
#include "name_tree.hpp"
#include "statement_builders.hpp"
#include "statement_printers.hpp"
#include "type_nodes.hpp"
#include "sources_manager.hpp"
int main(int argc, char **argv) {
if (argc < 2 || argc > 2) {
@ -19,31 +9,10 @@ int main(int argc, char **argv) {
std::string filename = argv[1];
std::ifstream in;
in.open(filename);
//
std::stringstream source_stream;
SourcesManager sources_manager;
source_stream << in.rdbuf();
in.close();
std::string source = source_stream.str();
parser::ParseTree parse_tree(source);
if (!parse_tree.is_properly_parsed()) {
error_handling::handle_parsing_error(
"There are some parsing errors in file", parse_tree.get_root());
}
nodes::ExpressionStorage expression_storage;
nodes::TypeStorage type_storage;
names::NameTree name_tree;
auto statements = builders::build_source_file(
parse_tree.get_root(), expression_storage, type_storage, name_tree);
printers::Printer printer(std::cout, 2, 80, true);
printers::print_source_file(statements, printer);
sources_manager.add_file(filename);
sources_manager.print(std::cout);
}

View file

@ -5,36 +5,51 @@
namespace names {
// --- AnyStatementProxy
nodes::Statement *AnyStatementProxy::get() {
return name_tree_->nodes_.at(id_).get_statement().value();
}
const nodes::Statement *AnyStatementProxy::get() const {
return name_tree_->nodes_.at(id_).get_statement().value();
}
// --- NameTree
// --- public
bool NameTree::insert(const std::string &path, nodes::Statement &&statement) {
std::optional<AnyStatementProxy>
NameTree::insert(const std::string &path, nodes::Statement &&statement) {
auto name_ids = slice_path_to_name_ids(path);
size_t node_id = add_node(get_root(), name_ids);
if (nodes_[node_id].get_statement().has_value()) {
return false;
return std::nullopt;
}
nodes_[node_id].set_statement(std::move(statement));
return true;
return AnyStatementProxy(*this, node_id);
}
nodes::CombineResult NameTree::insert_combine(const std::string &path,
nodes::Statement &&statement) {
std::pair<std::optional<AnyStatementProxy>, nodes::CombineResult>
NameTree::insert_combine(const std::string &path,
nodes::Statement &&statement) {
auto name_ids = slice_path_to_name_ids(path);
size_t node_id = add_node(get_root(), name_ids);
if (!nodes_[node_id].get_statement().has_value()) {
nodes_[node_id].set_statement(std::move(statement));
return nodes::CombineResult::OK;
return {AnyStatementProxy(*this, node_id), nodes::CombineResult::OK};
}
return nodes_[node_id].get_statement().value()->combine(std::move(statement));
return {std::nullopt, nodes_[node_id].get_statement().value()->combine(
std::move(statement))};
}
std::optional<nodes::Statement *> NameTree::find(const std::string &path) {
std::optional<AnyStatementProxy> NameTree::find(const std::string &path) {
auto name_ids = slice_path_to_existing_name_ids(path);
if (!name_ids.has_value()) {
@ -44,24 +59,7 @@ std::optional<nodes::Statement *> NameTree::find(const std::string &path) {
auto node_id = find_node(get_root(), name_ids.value());
if (node_id.has_value()) {
return nodes_[node_id.value()].get_statement();
}
return std::nullopt;
}
std::optional<const nodes::Statement *>
NameTree::find(const std::string &path) const {
auto name_ids = slice_path_to_existing_name_ids(path);
if (!name_ids.has_value()) {
return std::nullopt;
}
auto node_id = find_node(get_root(), name_ids.value());
if (node_id.has_value()) {
return nodes_[node_id.value()].get_statement();
return AnyStatementProxy(*this, node_id.value());
}
return std::nullopt;
@ -71,33 +69,34 @@ void NameTree::print(printers::Printer &printer) const {
nodes_[get_root()].print(nodes_, printer);
}
// TODO
void NameTree::add_statement_children_to_tree() {
for (auto &node : nodes_) {
if (!node.get_statement().has_value()) {
continue;
}
switch (node.get_statement().value()->get_any()->index()) {
case 0: // Import
// TODO: link imported symbols, if named import, otherwise link imported
// symbols to root
break;
case 1: // FunctionDefinition
// TODO: link to result type
break;
case 2: // TypeDefinition
// TODO: link methods + link children to types + connect to typeclasses
break;
case 3: // EmptyLines
break;
default:
error_handling::handle_general_error(
"Unexpected statement type in name tree");
break;
}
}
}
// // TODO
// void NameTree::add_statement_children_to_tree() {
// for (auto &node : nodes_) {
// if (!node.get_statement().has_value()) {
// continue;
// }
//
// switch (node.get_statement().value()->get_any()->index()) {
// case 0: // Import
// // TODO: link imported symbols, if named import, otherwise link
// imported
// // symbols to root
// break;
// case 1: // FunctionDefinition
// // TODO: link to result type
// break;
// case 2: // TypeDefinition
// // TODO: link methods + link children to types + connect to typeclasses
// break;
// case 3: // EmptyLines
// break;
// default:
// error_handling::handle_general_error(
// "Unexpected statement type in name tree");
// break;
// }
// }
// }
// --- private

View file

@ -96,11 +96,13 @@ build_statement(parser::ParseTree::Node parser_node,
}
if (statement_name.has_value()) {
// TODO: combine statements with same name
auto statement_copy = statement.value();
if (!name_tree.insert(statement_name.value(), std::move(statement_copy))) {
if (name_tree
.insert_combine(statement_name.value(), std::move(statement_copy))
.second != nodes::CombineResult::OK) {
// TODO: more detailed errors
error_handling::handle_parsing_error(
"Two or more statements in file have the same name", parser_node);
"Can't combine statements with same name", parser_node);
}
}

View file

@ -1,4 +1,4 @@
#include "../include/type_nodes.hpp"
#include "type_nodes.hpp"
namespace nodes {

9
src/types.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "types.hpp"
namespace types {
Type *TypeProxy::get() { return type_storage_->get_type(id_); }
const Type *TypeProxy::get() const { return type_storage_->get_type(id_); }
} // namespace types