changes to parser

This commit is contained in:
Martin Araneda 2022-11-08 22:27:26 -03:00
parent 0c891dbf23
commit 2d8f697f73
2 changed files with 133 additions and 0 deletions

View File

@ -3,6 +3,9 @@
#include "ast/variable_expression_ast.h" #include "ast/variable_expression_ast.h"
#include "parser.h" #include "parser.h"
#include <stack>
#include <vector>
obelisk::Parser::Parser() obelisk::Parser::Parser()
{ {
lexer_ = new obelisk::Lexer(); lexer_ = new obelisk::Lexer();
@ -223,3 +226,127 @@ std::unique_ptr<obelisk::PrototypeAST> obelisk::Parser::parseExtern()
getNextToken(); getNextToken();
return parsePrototype(); 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::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()
{
}

View File

@ -32,6 +32,9 @@ namespace obelisk
std::unique_ptr<obelisk::FunctionAST> parseDefinition(); std::unique_ptr<obelisk::FunctionAST> parseDefinition();
std::unique_ptr<obelisk::FunctionAST> parseTopLevelExpression(); std::unique_ptr<obelisk::FunctionAST> parseTopLevelExpression();
std::unique_ptr<obelisk::PrototypeAST> parseExtern(); 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: public:
Parser(); Parser();
@ -47,6 +50,9 @@ namespace obelisk
void handleDefinition(); void handleDefinition();
void handleExtern(); void handleExtern();
void handleTopLevelExpression(); void handleTopLevelExpression();
void handleAction();
void handleRule();
void handleFact();
}; };
} // namespace obelisk } // namespace obelisk