obelisk/src/obelisk.cpp

114 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"
#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-11-26 00:32:06 -03:00
static int 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
{
kb = std::unique_ptr<obelisk::KnowledgeBase> {
new obelisk::KnowledgeBase("cromer.kb")};
}
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.
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
}
int main(int argc, char** argv)
{
for (int i = 1; i < argc; i++)
{
std::cout << argv[i] << std::endl;
}
// This can be used to store a double as 2 floats in the database, then restore it back to a double.
// Inspired by Godot's double precision on the GPU to render large worlds.
/*try
{
float first;
float second;
double var = 0.123456789012345;
obelisk::KnowledgeBase* kb = new obelisk::KnowledgeBase("cromer.kb");
kb->getFloat(first, second, var);
std::cout << std::setprecision(std::numeric_limits<double>::digits10)
<< "Double: " << var << std::endl
<< "First: " << first << std::endl
<< "Second: " << second << std::endl;
var = 0.0;
kb->getDouble(var, first, second);
std::cout << std::setprecision(std::numeric_limits<double>::digits10)
<< "Double: " << var << std::endl
<< "First: " << first << std::endl
<< "Second: " << second << std::endl;
}
catch (obelisk::KnowledgeBaseException& exception)
{
return EXIT_FAILURE;
}*/
2022-11-26 00:32:06 -03:00
return mainLoop();
2022-10-17 22:26:36 -03:00
}