develop #15

Merged
cromer merged 64 commits from develop into master 2023-02-23 01:11:48 -03:00
17 changed files with 51 additions and 128 deletions
Showing only changes of commit 1f2500a037 - Show all commits

View File

@ -63,7 +63,7 @@ BreakBeforeTernaryOperators: true
BreakConstructorInitializers: AfterColon BreakConstructorInitializers: AfterColon
BreakInheritanceList: AfterColon BreakInheritanceList: AfterColon
BreakStringLiterals: false BreakStringLiterals: false
ColumnLimit: 80 ColumnLimit: 120
CommentPragmas: "^ IWYU pragma:" CommentPragmas: "^ IWYU pragma:"
CompactNamespaces: false CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerAllOnOneLineOrOnePerLine: true

View File

@ -22,8 +22,7 @@ namespace obelisk
void setArgs(std::vector<std::unique_ptr<ExpressionAST>> args); void setArgs(std::vector<std::unique_ptr<ExpressionAST>> args);
public: public:
CallExpressionAST(const std::string &callee, CallExpressionAST(const std::string &callee, std::vector<std::unique_ptr<ExpressionAST>> args) :
std::vector<std::unique_ptr<ExpressionAST>> args) :
callee_(callee), callee_(callee),
args_(std::move(args)) args_(std::move(args))
{ {

View File

@ -17,8 +17,7 @@ llvm::Function *obelisk::FunctionAST::codegen()
return nullptr; return nullptr;
} }
llvm::BasicBlock *bB llvm::BasicBlock *bB = llvm::BasicBlock::Create(*TheContext, "entry", theFunction);
= llvm::BasicBlock::Create(*TheContext, "entry", theFunction);
Builder->SetInsertPoint(bB); Builder->SetInsertPoint(bB);
NamedValues.clear(); NamedValues.clear();

View File

@ -18,8 +18,7 @@ namespace obelisk
void setPrototype(std::unique_ptr<PrototypeAST> prototype); void setPrototype(std::unique_ptr<PrototypeAST> prototype);
public: public:
FunctionAST(std::unique_ptr<PrototypeAST> prototype, FunctionAST(std::unique_ptr<PrototypeAST> prototype, std::unique_ptr<ExpressionAST> body) :
std::unique_ptr<ExpressionAST> body) :
prototype_(std::move(prototype)), prototype_(std::move(prototype)),
body_(std::move(body)) body_(std::move(body))
{ {

View File

@ -3,17 +3,10 @@
llvm::Function *obelisk::PrototypeAST::codegen() llvm::Function *obelisk::PrototypeAST::codegen()
{ {
std::vector<llvm::Type *> doubles(args_.size(), std::vector<llvm::Type *> doubles(args_.size(), llvm::Type::getDoubleTy(*TheContext));
llvm::Type::getDoubleTy(*TheContext)); llvm::FunctionType *FT = llvm::FunctionType::get(llvm::Type::getDoubleTy(*TheContext), doubles, false);
llvm::FunctionType *FT
= llvm::FunctionType::get(llvm::Type::getDoubleTy(*TheContext),
doubles,
false);
llvm::Function *F = llvm::Function::Create(FT, llvm::Function *F = llvm::Function::Create(FT, llvm::Function::ExternalLinkage, name_, obelisk::TheModule.get());
llvm::Function::ExternalLinkage,
name_,
obelisk::TheModule.get());
unsigned idx = 0; unsigned idx = 0;
for (auto &arg : F->args()) for (auto &arg : F->args())

View File

@ -19,8 +19,7 @@ namespace obelisk
void setArgs(std::vector<std::string> args); void setArgs(std::vector<std::string> args);
public: public:
PrototypeAST(const std::string& name, PrototypeAST(const std::string& name, std::vector<std::string> args) :
std::vector<std::string> args) :
name_(name), name_(name),
args_(std::move(args)) args_(std::move(args))
{ {

View File

@ -50,11 +50,7 @@ obelisk::KnowledgeBase::~KnowledgeBase()
void obelisk::KnowledgeBase::enableForeignKeys() void obelisk::KnowledgeBase::enableForeignKeys()
{ {
char* errmsg; char* errmsg;
int result = sqlite3_exec(dbConnection_, int result = sqlite3_exec(dbConnection_, "PRAGMA foreign_keys = ON;", NULL, NULL, &errmsg);
"PRAGMA foreign_keys = ON;",
NULL,
NULL,
&errmsg);
if (result != SQLITE_OK) if (result != SQLITE_OK)
{ {
if (errmsg) if (errmsg)
@ -96,9 +92,7 @@ void obelisk::KnowledgeBase::addEntities(std::vector<obelisk::Entity>& entities)
catch (obelisk::DatabaseConstraintException& exception) catch (obelisk::DatabaseConstraintException& exception)
{ {
// ignore unique constraint error // ignore unique constraint error
if (std::strcmp(exception.what(), if (std::strcmp(exception.what(), "UNIQUE constraint failed: entity.name") != 0)
"UNIQUE constraint failed: entity.name")
!= 0)
{ {
throw; throw;
} }
@ -117,9 +111,7 @@ void obelisk::KnowledgeBase::addVerbs(std::vector<obelisk::Verb>& verbs)
catch (obelisk::DatabaseConstraintException& exception) catch (obelisk::DatabaseConstraintException& exception)
{ {
// ignore unique constraint error // ignore unique constraint error
if (std::strcmp(exception.what(), if (std::strcmp(exception.what(), "UNIQUE constraint failed: verb.name") != 0)
"UNIQUE constraint failed: verb.name")
!= 0)
{ {
throw; throw;
} }
@ -163,9 +155,7 @@ void obelisk::KnowledgeBase::getFact(obelisk::Fact& fact)
fact.selectFact(dbConnection_); fact.selectFact(dbConnection_);
} }
void obelisk::KnowledgeBase::getFloat(float& result1, void obelisk::KnowledgeBase::getFloat(float& result1, float& result2, double var)
float& result2,
double var)
{ {
result1 = (float) var; result1 = (float) var;
result2 = (float) (var - (double) result1); result2 = (float) (var - (double) result1);

View File

@ -72,8 +72,7 @@ namespace obelisk
* base as. * base as.
*/ */
KnowledgeBase(const char* filename) : KnowledgeBase(const char* filename) :
KnowledgeBase(filename, KnowledgeBase(filename, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)
{ {
} }

View File

@ -21,11 +21,7 @@ void obelisk::Entity::selectEntity(sqlite3* dbConnection)
sqlite3_stmt* ppStmt = nullptr; sqlite3_stmt* ppStmt = nullptr;
auto result = sqlite3_prepare_v2(dbConnection, auto result = sqlite3_prepare_v2(dbConnection, "SELECT id, name FROM entity WHERE name=?", -1, &ppStmt, nullptr);
"SELECT id, name FROM entity WHERE name=?",
-1,
&ppStmt,
nullptr);
if (result != SQLITE_OK) if (result != SQLITE_OK)
{ {
@ -89,18 +85,13 @@ void obelisk::Entity::insertEntity(sqlite3* dbConnection)
sqlite3_stmt* ppStmt = nullptr; sqlite3_stmt* ppStmt = nullptr;
auto result = sqlite3_prepare_v2(dbConnection, auto result = sqlite3_prepare_v2(dbConnection, "INSERT INTO entity (name) VALUES (?)", -1, &ppStmt, nullptr);
"INSERT INTO entity (name) VALUES (?)",
-1,
&ppStmt,
nullptr);
if (result != SQLITE_OK) if (result != SQLITE_OK)
{ {
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
} }
result result = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT);
= sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT);
switch (result) switch (result)
{ {
case SQLITE_OK : case SQLITE_OK :
@ -127,8 +118,7 @@ void obelisk::Entity::insertEntity(sqlite3* dbConnection)
sqlite3_set_last_insert_rowid(dbConnection, 0); sqlite3_set_last_insert_rowid(dbConnection, 0);
break; break;
case SQLITE_CONSTRAINT : case SQLITE_CONSTRAINT :
throw obelisk::DatabaseConstraintException( throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection));
sqlite3_errmsg(dbConnection));
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseBusyException(); throw obelisk::DatabaseBusyException();
break; break;

View File

@ -18,8 +18,7 @@ namespace obelisk
} }
DatabaseException(const int errorCode) : DatabaseException(const int errorCode) :
errorMessage_( errorMessage_("database error " + std::to_string(errorCode) + " ocurred")
"database error " + std::to_string(errorCode) + " ocurred")
{ {
} }
@ -71,8 +70,7 @@ namespace obelisk
public: public:
DatabaseBusyException() DatabaseBusyException()
{ {
setErrorMessage( setErrorMessage("database was busy and operation was not performed");
"database was busy and operation was not performed");
} }
}; };

View File

@ -208,8 +208,7 @@ void obelisk::Fact::insertFact(sqlite3* dbConnection)
sqlite3_set_last_insert_rowid(dbConnection, 0); sqlite3_set_last_insert_rowid(dbConnection, 0);
break; break;
case SQLITE_CONSTRAINT : case SQLITE_CONSTRAINT :
throw obelisk::DatabaseConstraintException( throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection));
sqlite3_errmsg(dbConnection));
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseBusyException(); throw obelisk::DatabaseBusyException();
break; break;

View File

@ -34,9 +34,7 @@ namespace obelisk
{ {
} }
Fact(obelisk::Entity leftEntity, Fact(obelisk::Entity leftEntity, obelisk::Entity rightEntity, obelisk::Verb verb) :
obelisk::Entity rightEntity,
obelisk::Verb verb) :
id_(0), id_(0),
leftEntity_(leftEntity), leftEntity_(leftEntity),
rightEntity_(rightEntity), rightEntity_(rightEntity),
@ -44,10 +42,7 @@ namespace obelisk
{ {
} }
Fact(int id, Fact(int id, obelisk::Entity leftEntity, obelisk::Entity rightEntity, obelisk::Verb verb) :
obelisk::Entity leftEntity,
obelisk::Entity rightEntity,
obelisk::Verb verb) :
id_(id), id_(id),
leftEntity_(leftEntity), leftEntity_(leftEntity),
rightEntity_(rightEntity), rightEntity_(rightEntity),

View File

@ -33,9 +33,7 @@ namespace obelisk
{ {
} }
SuggestAction(obelisk::Fact fact, SuggestAction(obelisk::Fact fact, obelisk::Action trueAction, obelisk::Action falseAction) :
obelisk::Action trueAction,
obelisk::Action falseAction) :
id_(0), id_(0),
fact_(fact), fact_(fact),
trueAction_(trueAction), trueAction_(trueAction),
@ -43,10 +41,7 @@ namespace obelisk
{ {
} }
SuggestAction(int id, SuggestAction(int id, obelisk::Fact fact, obelisk::Action trueAction, obelisk::Action falseAction) :
obelisk::Fact fact,
obelisk::Action trueAction,
obelisk::Action falseAction) :
id_(id), id_(id),
fact_(fact), fact_(fact),
trueAction_(trueAction), trueAction_(trueAction),

View File

@ -23,11 +23,7 @@ void obelisk::Verb::selectVerb(sqlite3* dbConnection)
sqlite3_stmt* ppStmt = nullptr; sqlite3_stmt* ppStmt = nullptr;
auto result = sqlite3_prepare_v2(dbConnection, auto result = sqlite3_prepare_v2(dbConnection, "SELECT id, name FROM verb WHERE name=?", -1, &ppStmt, nullptr);
"SELECT id, name FROM verb WHERE name=?",
-1,
&ppStmt,
nullptr);
if (result != SQLITE_OK) if (result != SQLITE_OK)
{ {
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -89,18 +85,13 @@ void obelisk::Verb::insertVerb(sqlite3* dbConnection)
sqlite3_stmt* ppStmt = nullptr; sqlite3_stmt* ppStmt = nullptr;
auto result = sqlite3_prepare_v2(dbConnection, auto result = sqlite3_prepare_v2(dbConnection, "INSERT INTO verb (name) VALUES (?)", -1, &ppStmt, nullptr);
"INSERT INTO verb (name) VALUES (?)",
-1,
&ppStmt,
nullptr);
if (result != SQLITE_OK) if (result != SQLITE_OK)
{ {
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
} }
result result = sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT);
= sqlite3_bind_text(ppStmt, 1, getName().c_str(), -1, SQLITE_TRANSIENT);
switch (result) switch (result)
{ {
case SQLITE_OK : case SQLITE_OK :
@ -127,8 +118,7 @@ void obelisk::Verb::insertVerb(sqlite3* dbConnection)
sqlite3_set_last_insert_rowid(dbConnection, 0); sqlite3_set_last_insert_rowid(dbConnection, 0);
break; break;
case SQLITE_CONSTRAINT : case SQLITE_CONSTRAINT :
throw obelisk::DatabaseConstraintException( throw obelisk::DatabaseConstraintException(sqlite3_errmsg(dbConnection));
sqlite3_errmsg(dbConnection));
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseBusyException(); throw obelisk::DatabaseBusyException();
break; break;

View File

@ -15,8 +15,7 @@ static int obelisk::mainLoop()
try try
{ {
kb = std::unique_ptr<obelisk::KnowledgeBase> { kb = std::unique_ptr<obelisk::KnowledgeBase> {new obelisk::KnowledgeBase("cromer.kb")};
new obelisk::KnowledgeBase("cromer.kb")};
} }
catch (obelisk::KnowledgeBaseException& exception) catch (obelisk::KnowledgeBaseException& exception)
{ {
@ -44,10 +43,8 @@ static int obelisk::mainLoop()
case obelisk::Lexer::kTokenEof : case obelisk::Lexer::kTokenEof :
return EXIT_SUCCESS; return EXIT_SUCCESS;
case ';' : // ignore top-level semicolons. case ';' : // ignore top-level semicolons.
std::cout << "Identifier: " std::cout << "Identifier: " << parser->getLexer()->getIdentifier() << std::endl;
<< parser->getLexer()->getIdentifier() << std::endl; std::cout << "Num: " << parser->getLexer()->getNumberValue() << std::endl;
std::cout << "Num: " << parser->getLexer()->getNumberValue()
<< std::endl;
try try
{ {
parser->getNextToken(); parser->getNextToken();

View File

@ -44,15 +44,13 @@ void obelisk::Parser::setCurrentToken(int currentToken)
currentToken_ = currentToken; currentToken_ = currentToken;
} }
std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::logError( std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::logError(const char* str)
const char* str)
{ {
fprintf(stderr, "Error: %s\n", str); fprintf(stderr, "Error: %s\n", str);
return nullptr; return nullptr;
} }
std::unique_ptr<obelisk::PrototypeAST> obelisk::Parser::logErrorPrototype( std::unique_ptr<obelisk::PrototypeAST> obelisk::Parser::logErrorPrototype(const char* str)
const char* str)
{ {
logError(str); logError(str);
return nullptr; return nullptr;
@ -86,14 +84,12 @@ std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parsePrimary()
std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parseNumberExpression() std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parseNumberExpression()
{ {
auto result = std::make_unique<obelisk::NumberExpressionAST>( auto result = std::make_unique<obelisk::NumberExpressionAST>(getLexer()->getNumberValue());
getLexer()->getNumberValue());
getNextToken(); getNextToken();
return result; return result;
} }
std::unique_ptr<obelisk::ExpressionAST> std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parseParenthesisExpression()
obelisk::Parser::parseParenthesisExpression()
{ {
getNextToken(); getNextToken();
auto v = parseExpression(); auto v = parseExpression();
@ -110,8 +106,7 @@ std::unique_ptr<obelisk::ExpressionAST>
return v; return v;
} }
std::unique_ptr<obelisk::ExpressionAST> std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parseIdentifierExpression()
obelisk::Parser::parseIdentifierExpression()
{ {
std::string idName = getLexer()->getIdentifier(); std::string idName = getLexer()->getIdentifier();
getNextToken(); getNextToken();
@ -181,8 +176,7 @@ std::unique_ptr<obelisk::PrototypeAST> obelisk::Parser::parsePrototype()
getNextToken(); getNextToken();
return std::make_unique<obelisk::PrototypeAST>(functionName, return std::make_unique<obelisk::PrototypeAST>(functionName, std::move(argNames));
std::move(argNames));
} }
std::unique_ptr<obelisk::FunctionAST> obelisk::Parser::parseDefinition() std::unique_ptr<obelisk::FunctionAST> obelisk::Parser::parseDefinition()
@ -196,8 +190,7 @@ std::unique_ptr<obelisk::FunctionAST> obelisk::Parser::parseDefinition()
if (auto expression = parseExpression()) if (auto expression = parseExpression())
{ {
return std::make_unique<FunctionAST>(std::move(prototype), return std::make_unique<FunctionAST>(std::move(prototype), std::move(expression));
std::move(expression));
} }
return nullptr; return nullptr;
@ -208,10 +201,8 @@ std::unique_ptr<obelisk::FunctionAST> obelisk::Parser::parseTopLevelExpression()
if (auto expression = parseExpression()) if (auto expression = parseExpression())
{ {
// Make an anonymous prototype // Make an anonymous prototype
auto prototype = std::make_unique<obelisk::PrototypeAST>("__anon_expr", auto prototype = std::make_unique<obelisk::PrototypeAST>("__anon_expr", std::vector<std::string>());
std::vector<std::string>()); return std::make_unique<obelisk::FunctionAST>(std::move(prototype), std::move(expression));
return std::make_unique<obelisk::FunctionAST>(std::move(prototype),
std::move(expression));
} }
return nullptr; return nullptr;
} }
@ -241,8 +232,7 @@ void obelisk::Parser::parseFact(std::vector<obelisk::Fact>& facts)
getNextToken(); getNextToken();
if (getCurrentToken() != '(') if (getCurrentToken() != '(')
{ {
throw obelisk::ParserException( throw obelisk::ParserException("expected '(' but got '" + std::to_string(getCurrentToken()) + "'");
"expected '(' but got '" + std::to_string(getCurrentToken()) + "'");
} }
syntax.push('('); syntax.push('(');
@ -308,14 +298,12 @@ void obelisk::Parser::parseFact(std::vector<obelisk::Fact>& facts)
if (leftEntities.size() == 0) if (leftEntities.size() == 0)
{ {
throw obelisk::ParserException( throw obelisk::ParserException("missing left side entities");
"missing left side entities");
} }
if (rightEntities.size() == 0) if (rightEntities.size() == 0)
{ {
throw obelisk::ParserException( throw obelisk::ParserException("missing right side entities");
"missing right side entities");
} }
break; break;
} }
@ -346,9 +334,8 @@ void obelisk::Parser::parseFact(std::vector<obelisk::Fact>& facts)
{ {
for (auto& rightEntity : rightEntities) for (auto& rightEntity : rightEntities)
{ {
facts.push_back(obelisk::Fact(obelisk::Entity(leftEntity), facts.push_back(
obelisk::Entity(rightEntity), obelisk::Fact(obelisk::Entity(leftEntity), obelisk::Entity(rightEntity), obelisk::Verb(verb)));
obelisk::Verb(verb)));
} }
} }
} }
@ -380,8 +367,7 @@ void obelisk::Parser::handleFact(std::unique_ptr<obelisk::KnowledgeBase>& kb)
kb->getEntity(entity); kb->getEntity(entity);
if (entity.getId() == 0) if (entity.getId() == 0)
{ {
throw obelisk::ParserException( throw obelisk::ParserException("left entity could not be inserted into the database");
"left entity could not be inserted into the database");
} }
else else
{ {
@ -399,8 +385,7 @@ void obelisk::Parser::handleFact(std::unique_ptr<obelisk::KnowledgeBase>& kb)
kb->getEntity(entity); kb->getEntity(entity);
if (entity.getId() == 0) if (entity.getId() == 0)
{ {
throw obelisk::ParserException( throw obelisk::ParserException("right entity could not be inserted into the database");
"right entity could not be inserted into the database");
} }
else else
{ {
@ -423,8 +408,7 @@ void obelisk::Parser::handleFact(std::unique_ptr<obelisk::KnowledgeBase>& kb)
kb->getVerb(verb); kb->getVerb(verb);
if (verb.getId() == 0) if (verb.getId() == 0)
{ {
throw obelisk::ParserException( throw obelisk::ParserException("verb could not be inserted into the database");
"verb could not be inserted into the database");
} }
else else
{ {
@ -447,8 +431,7 @@ void obelisk::Parser::handleFact(std::unique_ptr<obelisk::KnowledgeBase>& kb)
kb->getFact(fact); kb->getFact(fact);
if (fact.getId() == 0) if (fact.getId() == 0)
{ {
throw obelisk::ParserException( throw obelisk::ParserException("fact could not be inserted into the database");
"fact could not be inserted into the database");
} }
} }
} }

View File

@ -21,13 +21,11 @@ namespace obelisk
void setCurrentToken(int currentToken); void setCurrentToken(int currentToken);
std::unique_ptr<obelisk::ExpressionAST> logError(const char* str); std::unique_ptr<obelisk::ExpressionAST> logError(const char* str);
std::unique_ptr<obelisk::PrototypeAST> logErrorPrototype( std::unique_ptr<obelisk::PrototypeAST> logErrorPrototype(const char* str);
const char* str);
std::unique_ptr<obelisk::ExpressionAST> parseExpression(); std::unique_ptr<obelisk::ExpressionAST> parseExpression();
std::unique_ptr<obelisk::ExpressionAST> parseNumberExpression(); std::unique_ptr<obelisk::ExpressionAST> parseNumberExpression();
std::unique_ptr<obelisk::ExpressionAST> std::unique_ptr<obelisk::ExpressionAST> parseParenthesisExpression();
parseParenthesisExpression();
std::unique_ptr<obelisk::ExpressionAST> parseIdentifierExpression(); std::unique_ptr<obelisk::ExpressionAST> parseIdentifierExpression();
std::unique_ptr<obelisk::ExpressionAST> parsePrimary(); std::unique_ptr<obelisk::ExpressionAST> parsePrimary();
std::unique_ptr<obelisk::PrototypeAST> parsePrototype(); std::unique_ptr<obelisk::PrototypeAST> parsePrototype();