Merge pull request 'parser' (#4) from parser into develop

Reviewed-on: #4
This commit is contained in:
Chris Cromer 2022-11-09 17:04:15 -03:00
commit 43f8f292ff
2 changed files with 169 additions and 0 deletions

View File

@ -4,6 +4,8 @@
#include "parser.h"
#include <memory>
#include <stack>
#include <vector>
obelisk::Parser::Parser()
{
@ -208,3 +210,164 @@ std::unique_ptr<obelisk::PrototypeAST> obelisk::Parser::parseExtern()
getNextToken();
return parsePrototype();
}
std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parseAction()
{
//action(is "dangerous" then "avoid" or "ignore");
getNextToken();
if (getCurrentToken() != '(')
{
// TODO: throw an error
}
}
std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parseRule()
{
//rule("player" can "die" if "enemy1" is "dangerous");
getNextToken();
if (getCurrentToken() != '(')
{
// TODO: throw an error
}
while (true) //left side of Rule
{
getNextToken();
if (getCurrentToken() != '"')
{
//TODO: throw an error
}
/*if (getCurrentToken() == ')') // TODO: break if not string and not "and"
{
// TODO: save the verb
break;
}*/
}
while (true) //right side of Ruke
{
getNextToken();
if (getCurrentToken() != '"')
{
//TODO: throw an error
}
if (getCurrentToken() == ')')
{
// TODO: save the verb
break;
}
}
}
// fact("chris cromer" and "martin" and "Isabella" can "program" and "speak english");
// fact("" and "martin")
std::unique_ptr<obelisk::ExpressionAST> obelisk::Parser::parseFact()
{
std::stack<char> syntax;
getNextToken();
if (getCurrentToken() != '(')
{
// TODO: throw an error
}
syntax.push('(');
// ("
bool getEntity {true};
std::vector<std::string> leftEntities;
std::vector<std::string> rightEntities;
std::string entityName {""};
std::string verb {""};
getNextToken();
while (true) //left side of fact
{
if (getEntity)
{
if (getCurrentToken() == '"')
{
if (syntax.top() != '"')
{
// open a double quote
syntax.push('"');
getNextToken();
}
else if (syntax.top() == '"')
{
// close a double quote
syntax.pop();
if (verb == "")
{
leftEntities.push_back(entityName);
}
else
{
rightEntities.push_back(entityName);
}
entityName = "";
getEntity = false;
getNextToken();
continue;
}
}
if (syntax.top() == '"')
{
if (entityName != "")
{
entityName += " ";
}
entityName += getLexer()->getIdentifier();
}
getNextToken();
}
else
{
if (getCurrentToken() == ')')
{
// TODO: throw an error if verb is empty
// TODO: throw an error if rightEntities has 0 elements
break;
}
if (getCurrentToken() == '"')
{
// TODO: throw and error because there is an unexpected double quote.
break;
}
if (getLexer()->getIdentifier() == "and")
{
getNextToken();
getEntity = true;
continue;
}
else
{
verb = getLexer()->getIdentifier();
getEntity = true;
continue;
}
}
}
return nullptr;
}
void obelisk::Parser::handleAction()
{
}
void obelisk::Parser::handleRule()
{
}
void obelisk::Parser::handleFact()
{
parseFact();
}
void obelisk::Parser::insertFact()
{
}

View File

@ -32,6 +32,9 @@ namespace obelisk
std::unique_ptr<obelisk::FunctionAST> parseDefinition();
std::unique_ptr<obelisk::FunctionAST> parseTopLevelExpression();
std::unique_ptr<obelisk::PrototypeAST> parseExtern();
std::unique_ptr<obelisk::ExpressionAST> parseAction();
std::unique_ptr<obelisk::ExpressionAST> parseRule();
std::unique_ptr<obelisk::ExpressionAST> parseFact();
public:
Parser();
@ -45,6 +48,9 @@ namespace obelisk
void handleDefinition();
void handleExtern();
void handleTopLevelExpression();
void handleAction();
void handleRule();
void handleFact();
};
} // namespace obelisk