2022-10-25 17:08:36 -03:00
|
|
|
#include "knowledge_base.h"
|
2022-10-17 22:26:36 -03:00
|
|
|
#include "lexer.h"
|
|
|
|
#include "obelisk.h"
|
|
|
|
#include "parser.h"
|
|
|
|
|
2022-10-25 17:08:36 -03:00
|
|
|
#include <iomanip>
|
2022-10-17 22:26:36 -03:00
|
|
|
#include <iostream>
|
2022-10-25 17:08:36 -03:00
|
|
|
#include <limits>
|
2022-11-04 11:20:34 -03:00
|
|
|
#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
|
|
|
{
|
2022-11-04 11:20:34 -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> ");
|
2022-11-30 22:44:35 -03:00
|
|
|
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;
|
2022-11-30 22:44:35 -03:00
|
|
|
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;
|
2022-11-04 11:20:34 -03:00
|
|
|
case obelisk::Lexer::kTokenFact :
|
2022-11-26 00:32:06 -03:00
|
|
|
parser->handleFact(kb);
|
2022-11-04 11:20:34 -03:00
|
|
|
break;
|
|
|
|
case obelisk::Lexer::kTokenRule :
|
2022-11-26 00:32:06 -03:00
|
|
|
// parser->handleRule();
|
2022-11-04 11:20:34 -03:00
|
|
|
break;
|
|
|
|
case obelisk::Lexer::kTokenAction :
|
2022-11-26 00:32:06 -03:00
|
|
|
// parser->handleAction();
|
2022-11-04 11:20:34 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-04 11:20:34 -03:00
|
|
|
// 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
|
2022-10-25 17:08:36 -03:00
|
|
|
{
|
2022-11-04 11:20:34 -03:00
|
|
|
float first;
|
|
|
|
float second;
|
|
|
|
double var = 0.123456789012345;
|
|
|
|
|
2022-10-25 17:08:36 -03:00
|
|
|
obelisk::KnowledgeBase* kb = new obelisk::KnowledgeBase("cromer.kb");
|
2022-11-04 11:20:34 -03:00
|
|
|
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-12-09 23:26:37 -03:00
|
|
|
return obelisk::mainLoop();
|
2022-10-17 22:26:36 -03:00
|
|
|
}
|