hoolang/HooCore.cpp

53 lines
1.3 KiB
C++

#include "HooCore.hpp"
#include "HooLexer.h"
#include "HooParser.h"
#include "Visitor.hpp"
std::any HooCore::compile(std::string &input, std::string &moduleName)
{
try
{
antlr4::tree::ParseTree *tree = parse(input);
if (tree == nullptr)
{
std::cout << "Parse failed" << std::endl;
}
Visitor *visitor = new Visitor(moduleName);
auto result = visitor->visit(tree);
return result;
}
catch (const std::exception &e)
{
std::cerr << "Compilation Error: " << e.what() << std::endl;
return nullptr;
}
catch (...)
{
std::cerr << "Compilation Error: Unknown exception" << std::endl;
return nullptr;
}
}
antlr4::tree::ParseTree *HooCore::parse(std::string &input)
{
try
{
antlr4::ANTLRInputStream input_stream(input);
HooLexer lexer(&input_stream);
antlr4::CommonTokenStream tokens(&lexer);
HooParser parser(&tokens);
antlr4::tree::ParseTree *tree = parser.unit();
return tree;
}
catch (const std::exception &e)
{
std::cerr << "Parse Error: " << e.what() << std::endl;
return nullptr;
}
catch (...)
{
std::cerr << "Parse Error: Unknown exception" << std::endl;
return nullptr;
}
}