elna/cli/main.cc

56 lines
1.6 KiB
C++

#include <elna/source/driver.h>
#include "elna/source/semantic.h"
#include "parser.hh"
#include <sstream>
constexpr std::size_t pointer_size = 4;
int main()
{
elna::source::driver driver{ "-" };
std::istringstream inp(R"(
var x: Int;
proc f();
begin
end;
begin
end.
)");
elna::source::lexer lexer(inp);
yy::parser parser(lexer, driver);
if (auto result = parser())
{
for (const auto& error : driver.errors())
{
std::cerr << error->path().string() << ':'
<< error->line() << ':' << error->column()
<< ": error: " << error->what()
<< '.' << std::endl;
}
return result;
}
auto symbol_table = elna::source::add_builtin_symbols();
elna::source::name_analysis_visitor name_analysis_visitor{ symbol_table, "-", pointer_size };
elna::source::type_analysis_visitor type_analysis_visitor{ symbol_table, "-", pointer_size };
name_analysis_visitor.visit(driver.tree.get());
for (auto& definition : driver.tree->definitions())
{
if (auto const_definition = dynamic_cast<elna::source::constant_definition *>(definition.get()))
{
std::cout << "const " << const_definition->identifier() << " = "
<< const_definition->body().number() << std::endl;
}
else if (auto proc_definition = dynamic_cast<elna::source::procedure_definition *>(definition.get()))
{
std::cout << "const " << proc_definition->identifier() << "()" << std::endl;
}
}
return 0;
}