diff --git a/core/Parser/ASTNode.cpp b/core/Parser/ASTNode.cpp index e5fa092..58637d9 100644 --- a/core/Parser/ASTNode.cpp +++ b/core/Parser/ASTNode.cpp @@ -45,7 +45,7 @@ class BinaryExpressionNode : public ASTNode { std::cout << padRight(" ",indent) - << "BinaryExpressionNode" + << "BinaryExpressionNode " << OPERATOR_MAP_STRREV.find(this->Operator)->second << "( "<< OPERATOR_MAP_REV.find(this->Operator)->second << " )" << "\n"; @@ -72,4 +72,38 @@ class LiteralNode : public ASTNode ParserToken parserToken; }; +class IdentifierNode : public ASTNode +{ + protected: + ASTKind kind = ASTKind::Literal; + void log(int indent) + { + std::cout << padRight(" ",indent) << "IdentifierNode {" << this->lexerToken.token << "}\n"; + } + public: + Token lexerToken; + ParserToken parserToken; +}; +class PostfixNode : public ASTNode +{ + protected: + ASTKind kind = ASTKind::Literal; + void log(int indent) + { + std::cout + << padRight(" ",indent) + << "Postfix " + << OPERATOR_MAP_STRREV.find(this->Operator)->second + << "( "<< OPERATOR_MAP_REV.find(this->Operator)->second << " )" + << "\n"; + this->operand->log(indent + 4); + } + public: + ASTNode * operand; + TokenType Operator; +}; + + + + #endif \ No newline at end of file diff --git a/core/Parser/Parser.cpp b/core/Parser/Parser.cpp index 3b5373b..15d859d 100644 --- a/core/Parser/Parser.cpp +++ b/core/Parser/Parser.cpp @@ -60,7 +60,7 @@ ParserToken Parser::parseToken(Token token){ } else if(token.gettype() == "identifier") { - pToken.type = KEYWORD_MAP.find(token.token)->second; + pToken.type = TokenType::IDENTIFIER; } return pToken; @@ -118,6 +118,10 @@ void Parser::primaryExpression() } } +// Expresssionu tamamen okuyup bitiren kısım burası +// Bir expressionda kendi içerisindeki operatörleri işler +// Terimler ile ilgilenmez yanlızca ifadelerin önceliğini belirler +// Sayılar, fonksionlar, tanımlamalar gibi şeyler burda işlenmez ASTNode * Parser::volumeExpression(uint16_t precedence) { if (this->currentToken().type == TokenType::SVR_VOID) @@ -127,9 +131,17 @@ ASTNode * Parser::volumeExpression(uint16_t precedence) ASTNode* left = this->volumeNullDominatorExpression(); + if (!left) { + return nullptr; + } + while(1) { auto nextToken = this->lookehead(+1); + if (nextToken.type == TokenType::RPAREN) { + break; + } + nextToken = this->lookehead(+1); if(precedence < nextToken.getPowerOperator()) { this->nextToken(); @@ -141,8 +153,9 @@ ASTNode * Parser::volumeExpression(uint16_t precedence) return left; } - - +// Terimler burada işlenir tanımlamalar sayılar burada işlenir +// işlem önceliği işlenmez +// Tek yönlü operatörler !a b++ ++c burada işlenir ASTNode * Parser::volumeNullDominatorExpression() { auto currentToken = this->currentToken(); @@ -151,7 +164,19 @@ ASTNode * Parser::volumeNullDominatorExpression() // Hata: "Beklenmedik dosya sonu, bir değer bekleniyordu!" return nullptr; } + + if (currentToken.type == TokenType::RPAREN) { + return nullptr; + } + if (currentToken.type == TokenType::LPAREN) { + this->nextToken(); + ASTNode* expr = this->volumeExpression(0); + + this->nextToken(); + + return expr; + } if(currentToken.is({ TokenType::PLUS_PLUS, @@ -170,23 +195,40 @@ ASTNode * Parser::volumeNullDominatorExpression() return binNode; }; - if(currentToken.is({ - TokenType::NUMBER - })) { + if(currentToken.is(TokenType::NUMBER)) { LiteralNode * lNode = new LiteralNode(); lNode->lexerToken = currentToken.token; lNode->parserToken = currentToken; return lNode; } + if(currentToken.is(TokenType::IDENTIFIER)) { + IdentifierNode * iNode = new IdentifierNode(); + iNode->lexerToken = currentToken.token; + iNode->parserToken = currentToken; + return iNode; + } + return nullptr; } - - +// Çift yönlü matematiksel işlemler burada işlenir +// [volume]+[volume] veya [volume] * [volume] gibi terimleri birbirine bağlayarak geri döner ASTNode * Parser::volumeLeftDominatorExpression(ASTNode * left) { auto currentToken = this->currentToken(); + + if (currentToken.is({ + TokenType::PLUS_PLUS, + TokenType::MINUS_MINUS + })) { + this->nextToken(); + PostfixNode* node = new PostfixNode(); + node->operand = left; + node->Operator = currentToken.type; + return node; + } + uint16_t precedence = currentToken.getPowerOperator(); this->nextToken(); auto right = this->volumeExpression(precedence); diff --git a/saqut b/saqut index fe08c30..38f891c 100755 Binary files a/saqut and b/saqut differ diff --git a/source.sqt b/source.sqt index 56b8e3c..d7cac1d 100644 --- a/source.sqt +++ b/source.sqt @@ -1 +1 @@ -1 - 2 + 3 * 4 / 5 \ No newline at end of file +++1 - b++ \ No newline at end of file