Support one hardcoded import

This commit is contained in:
2025-07-10 00:43:17 +02:00
parent 181b19eefe
commit f02b2c49e5
14 changed files with 210 additions and 111 deletions

View File

@ -17,31 +17,52 @@ along with GCC; see the file COPYING3. If not see
#include "elna/boot/dependency.h"
#include <fstream>
#include <sstream>
#include <string.h>
#include "elna/boot/driver.h"
#include "elna/boot/semantic.h"
#include "parser.hh"
namespace elna::boot
{
dependency_graph::dependency_graph()
source_path_error::source_path_error(const std::string& message, const char *path)
: error(path, { 0, 0 }), message(message)
{
}
dependency_graph::dependency_graph(error_list&& errors)
std::string source_path_error::what() const
{
std::stringstream output;
output << "Cannot open filename ";
output << this->path;
output << ": ";
output << this->message;
return output.str();
}
dependency::dependency()
{
}
dependency::dependency(error_list&& errors)
: m_errors(std::move(errors))
{
}
bool dependency_graph::has_errors() const
bool dependency::has_errors() const
{
return !errors().empty();
}
const error_list& dependency_graph::errors() const
const error_list& dependency::errors() const
{
return m_errors;
}
dependency_graph read_sources(std::istream& entry_point, const char *entry_path)
dependency parse_source(std::istream& entry_point, const char *entry_path)
{
driver parse_driver{ entry_path };
lexer tokenizer(entry_point);
@ -49,14 +70,52 @@ namespace elna::boot
if (parser())
{
return dependency_graph(std::move(parse_driver.errors()));
return dependency(std::move(parse_driver.errors()));
}
else
{
dependency_graph outcome;
outcome.modules.emplace_back(std::move(parse_driver.tree));
dependency outcome;
std::swap(outcome.tree, parse_driver.tree);
return outcome;
}
}
dependency read_sources(const char *entry_path, std::shared_ptr<symbol_table> info_table,
std::shared_ptr<symbol_table> imports)
{
std::ifstream entry_point{ entry_path, std::ios::in };
if (!entry_point)
{
error_list errors;
errors.push_back(std::make_unique<source_path_error>(strerror(errno), entry_path));
return dependency(std::move(errors));
}
auto outcome = parse_source(entry_point, entry_path);
declaration_visitor declaration_visitor(entry_path);
outcome.tree->accept(&declaration_visitor);
if (!declaration_visitor.errors().empty())
{
return dependency(std::move(declaration_visitor.errors()));
}
outcome.unresolved = declaration_visitor.unresolved;
outcome.bag = symbol_bag{ std::move(declaration_visitor.unresolved), info_table };
if (imports != nullptr)
{
outcome.bag.add_import(imports);
}
elna::boot::name_analysis_visitor name_analysis_visitor(entry_path, outcome.bag);
outcome.tree->accept(&name_analysis_visitor);
if (!name_analysis_visitor.errors().empty())
{
return dependency(std::move(declaration_visitor.errors()));
}
return outcome;
}
}