diff --git a/src/parser.cpp b/src/parser.cpp index 19736cd..c553891 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4,6 +4,8 @@ #include "parser.h" #include +#include +#include obelisk::Parser::Parser() { @@ -208,3 +210,164 @@ std::unique_ptr obelisk::Parser::parseExtern() getNextToken(); return parsePrototype(); } + +std::unique_ptr obelisk::Parser::parseAction() +{ + //action(is "dangerous" then "avoid" or "ignore"); + getNextToken(); + if (getCurrentToken() != '(') + { + // TODO: throw an error + } +} + +std::unique_ptr 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::Parser::parseFact() +{ + std::stack syntax; + + getNextToken(); + if (getCurrentToken() != '(') + { + // TODO: throw an error + } + + syntax.push('('); + + // (" + + bool getEntity {true}; + std::vector leftEntities; + std::vector 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() +{ +} diff --git a/src/parser.h b/src/parser.h index d89cec8..8738ee1 100644 --- a/src/parser.h +++ b/src/parser.h @@ -32,6 +32,9 @@ namespace obelisk std::unique_ptr parseDefinition(); std::unique_ptr parseTopLevelExpression(); std::unique_ptr parseExtern(); + std::unique_ptr parseAction(); + std::unique_ptr parseRule(); + std::unique_ptr parseFact(); public: Parser(); @@ -45,6 +48,9 @@ namespace obelisk void handleDefinition(); void handleExtern(); void handleTopLevelExpression(); + void handleAction(); + void handleRule(); + void handleFact(); }; } // namespace obelisk