From 2d8f697f736e14289ad1b1949882770aae36b46a Mon Sep 17 00:00:00 2001 From: Martin Araneda Date: Tue, 8 Nov 2022 22:27:26 -0300 Subject: [PATCH 1/2] changes to parser --- src/parser.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ src/parser.h | 6 +++ 2 files changed, 133 insertions(+) diff --git a/src/parser.cpp b/src/parser.cpp index 9c783b5..22b2ef7 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -3,6 +3,9 @@ #include "ast/variable_expression_ast.h" #include "parser.h" +#include +#include + obelisk::Parser::Parser() { lexer_ = new obelisk::Lexer(); @@ -223,3 +226,127 @@ 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::string entityName {""}; + getNextToken(); + while (true) //left side of fact + { + if (getEntity) + { + if (getCurrentToken() == '"') + { + if (syntax.top() != '"') + { + // open a double quote + syntax.push('"'); + } + else if (syntax.top() == '"') + { + // close a double quote + syntax.pop(); + leftEntities.push_back(entityName); + entityName = ""; + getEntity = false; + } + } + + if (syntax.top() == '"') + { + if (entityName != "") + { + entityName += " "; + } + entityName += getLexer()->getIdentifier(); + } + getNextToken(); + } + else + { + // and + } + + /*if (getCurrentToken() == ')') // TODO: break if not string and not "and" + { + // TODO: save the verb + break; + }*/ + } +} + +void handleAction() +{ +} + +void handleRule() +{ +} + +void handleFact() +{ +} diff --git a/src/parser.h b/src/parser.h index 2a00bbc..b2bf43b 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(); @@ -47,6 +50,9 @@ namespace obelisk void handleDefinition(); void handleExtern(); void handleTopLevelExpression(); + void handleAction(); + void handleRule(); + void handleFact(); }; } // namespace obelisk -- 2.30.2 From 18cd9c59332c98e22c4e18fa9ce6d8e823b82624 Mon Sep 17 00:00:00 2001 From: Martin Araneda Date: Wed, 9 Nov 2022 16:49:58 -0300 Subject: [PATCH 2/2] Finished parsing facts --- src/parser.cpp | 61 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index 22b2ef7..76be7a5 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -293,7 +293,9 @@ std::unique_ptr obelisk::Parser::parseFact() bool getEntity {true}; std::vector leftEntities; + std::vector rightEntities; std::string entityName {""}; + std::string verb {""}; getNextToken(); while (true) //left side of fact { @@ -305,14 +307,24 @@ std::unique_ptr obelisk::Parser::parseFact() { // open a double quote syntax.push('"'); + getNextToken(); } else if (syntax.top() == '"') { // close a double quote syntax.pop(); - leftEntities.push_back(entityName); + if (verb == "") + { + leftEntities.push_back(entityName); + } + else + { + rightEntities.push_back(entityName); + } entityName = ""; - getEntity = false; + getEntity = false; + getNextToken(); + continue; } } @@ -328,25 +340,50 @@ std::unique_ptr obelisk::Parser::parseFact() } else { - // and + 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; + } } - - /*if (getCurrentToken() == ')') // TODO: break if not string and not "and" - { - // TODO: save the verb - break; - }*/ } + + return nullptr; } -void handleAction() +void obelisk::Parser::handleAction() { } -void handleRule() +void obelisk::Parser::handleRule() { } -void handleFact() +void obelisk::Parser::handleFact() { + parseFact(); } + +void obelisk::Parser::insertFact() +{ +} \ No newline at end of file -- 2.30.2