obelisk/src/obelisk.cpp

136 lines
3.3 KiB
C++
Raw Normal View History

#include "knowledge_base.h"
2022-10-17 22:26:36 -03:00
#include "lexer.h"
#include "obelisk.h"
#include "parser.h"
2023-02-08 09:36:43 -03:00
#include "version.h"
2022-10-17 22:26:36 -03:00
#include <iomanip>
2022-10-17 22:26:36 -03:00
#include <iostream>
#include <limits>
#include <memory>
2022-10-17 22:26:36 -03:00
2022-12-09 23:26:37 -03:00
static int obelisk::mainLoop()
2022-10-17 22:26:36 -03:00
{
auto parser = std::unique_ptr<obelisk::Parser> {new obelisk::Parser()};
2022-11-26 00:32:06 -03:00
std::unique_ptr<obelisk::KnowledgeBase> kb;
try
{
2022-12-13 17:35:14 -03:00
kb = std::unique_ptr<obelisk::KnowledgeBase> {new obelisk::KnowledgeBase("cromer.kb")};
2022-11-26 00:32:06 -03:00
}
catch (obelisk::KnowledgeBaseException& exception)
{
std::cout << exception.what() << std::endl;
return EXIT_FAILURE;
}
2022-10-17 22:26:36 -03:00
// Prime the first token.
fprintf(stderr, "ready> ");
try
{
parser->getNextToken();
}
catch (obelisk::LexerException& exception)
{
std::cout << "Error: " << exception.what() << std::endl;
return EXIT_FAILURE;
}
2022-10-17 22:26:36 -03:00
while (true)
{
fprintf(stderr, "ready> ");
switch (parser->getCurrentToken())
{
case obelisk::Lexer::kTokenEof :
2022-11-26 00:32:06 -03:00
return EXIT_SUCCESS;
2022-10-17 22:26:36 -03:00
case ';' : // ignore top-level semicolons.
2022-12-13 17:35:14 -03:00
std::cout << "Identifier: " << parser->getLexer()->getIdentifier() << std::endl;
std::cout << "Num: " << parser->getLexer()->getNumberValue() << std::endl;
try
{
parser->getNextToken();
}
catch (obelisk::LexerException& exception)
{
std::cout << "Error: " << exception.what() << std::endl;
return EXIT_FAILURE;
}
2022-10-17 22:26:36 -03:00
break;
case obelisk::Lexer::kTokenFact :
2022-11-26 00:32:06 -03:00
parser->handleFact(kb);
break;
case obelisk::Lexer::kTokenRule :
2022-11-26 00:32:06 -03:00
// parser->handleRule();
break;
case obelisk::Lexer::kTokenAction :
2022-11-26 00:32:06 -03:00
// parser->handleAction();
break;
2022-10-17 22:26:36 -03:00
default :
parser->getNextToken();
break;
}
}
2022-11-26 00:32:06 -03:00
return EXIT_SUCCESS;
2022-10-17 22:26:36 -03:00
}
2023-02-08 09:36:43 -03:00
void obelisk::showUsage()
{
std::cout << obelisk::usageMessage << std::endl;
}
2022-10-17 22:26:36 -03:00
int main(int argc, char** argv)
{
2023-02-08 09:36:43 -03:00
std::vector<std::string> sourceFiles;
std::string kbFile = "obelisk.kb";
while (true)
2022-10-17 22:26:36 -03:00
{
2023-02-08 09:36:43 -03:00
int option_index = 0;
switch (getopt_long(argc, argv, "k:hv", obelisk::long_options, &option_index))
{
case 'k' :
kbFile = std::string(optarg);
continue;
case 'h' :
obelisk::showUsage();
return EXIT_SUCCESS;
break;
case 'v' :
std::cout << "obelisk " << obelisk::version << std::endl;
return EXIT_SUCCESS;
break;
default :
obelisk::showUsage();
return EXIT_FAILURE;
break;
case -1 :
break;
}
break;
2022-10-17 22:26:36 -03:00
}
2023-02-08 09:36:43 -03:00
if (optind < argc)
{
2023-02-08 09:36:43 -03:00
while (optind < argc)
{
sourceFiles.push_back(argv[optind++]);
}
}
2023-02-08 09:36:43 -03:00
if (sourceFiles.size() == 0)
{
2023-02-08 09:36:43 -03:00
obelisk::showUsage();
return EXIT_FAILURE;
2023-02-08 09:36:43 -03:00
}
std::cout << sourceFiles[0] << std::endl;
std::cout << kbFile << std::endl;
return 0;
2022-12-09 23:26:37 -03:00
return obelisk::mainLoop();
2022-10-17 22:26:36 -03:00
}