Removed expression invalid

This commit is contained in:
Rafał Grodziński
2025-07-03 22:52:05 +09:00
parent d809f317d8
commit 20a8ccf966
6 changed files with 19 additions and 51 deletions

View File

@@ -11,7 +11,3 @@ ExpressionKind Expression::getKind() {
ValueType Expression::getValueType() {
return valueType;
}
bool Expression::isValid() {
return kind != ExpressionKind::INVALID;
}

View File

@@ -15,8 +15,7 @@ enum class ExpressionKind {
IF_ELSE,
VAR,
CALL,
BLOCK,
INVALID
BLOCK
};
class Expression {
@@ -31,7 +30,6 @@ public:
virtual ~Expression() { }
ExpressionKind getKind();
ValueType getValueType();
bool isValid();
};
#endif

View File

@@ -1,9 +0,0 @@
#include "ExpressionInvalid.h"
ExpressionInvalid::ExpressionInvalid(shared_ptr<Token> token):
Expression(ExpressionKind::INVALID, ValueType::NONE), token(token) {
}
shared_ptr<Token> ExpressionInvalid::getToken() {
return token;
}

View File

@@ -1,10 +0,0 @@
#include "Parser/Expression/Expression.h"
class ExpressionInvalid: public Expression {
private:
shared_ptr<Token> token;
public:
ExpressionInvalid(shared_ptr<Token> token);
shared_ptr<Token> getToken();
};

View File

@@ -10,7 +10,6 @@
#include "Parser/Expression/ExpressionIfElse.h"
#include "Parser/Expression/ExpressionBinary.h"
#include "Parser/Expression/ExpressionBlock.h"
#include "Parser/Expression/ExpressionInvalid.h"
#include "Parser/Statement/StatementFunction.h"
#include "Parser/Statement/StatementVariable.h"
@@ -176,9 +175,8 @@ shared_ptr<Statement> Parser::matchStatementVariable() {
}
shared_ptr<Expression> expression = nextExpression();
if (expression == nullptr || !expression->isValid()) {
if (expression == nullptr)
return nullptr;
}
return make_shared<StatementVariable>(identifierToken->getLexme(), valueType, expression);
}
@@ -255,15 +253,14 @@ shared_ptr<Statement> Parser::matchStatementBlock(vector<TokenKind> terminalToke
while (!tryMatchingTokenKinds(terminalTokenKinds, false, false)) {
shared_ptr<Statement> statement = nextInBlockStatement();
if (statement == nullptr)
return nullptr;
statements.push_back(statement);
if (statement != nullptr)
statements.push_back(statement);
if (tryMatchingTokenKinds(terminalTokenKinds, false, false))
break;
// except new line
if (!tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, true)) {
if (statement != nullptr && !tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, true)) {
markError(TokenKind::NEW_LINE, {});
return nullptr;
}
@@ -395,10 +392,10 @@ shared_ptr<Expression> Parser::nextExpression() {
shared_ptr<Expression> Parser::matchEquality() {
shared_ptr<Expression> expression = matchComparison();
if (expression == nullptr || !expression->isValid())
return expression;
if (expression == nullptr)
return nullptr;
while (tryMatchingTokenKinds({Token::tokensEquality}, false, false))
if (tryMatchingTokenKinds({Token::tokensEquality}, false, false))
expression = matchExpressionBinary(expression);
return expression;
@@ -406,10 +403,10 @@ shared_ptr<Expression> Parser::matchEquality() {
shared_ptr<Expression> Parser::matchComparison() {
shared_ptr<Expression> expression = matchTerm();
if (expression == nullptr || !expression->isValid())
return expression;
if (expression == nullptr)
return nullptr;
while (tryMatchingTokenKinds({Token::tokensComparison}, false, false))
if (tryMatchingTokenKinds({Token::tokensComparison}, false, false))
expression = matchExpressionBinary(expression);
return expression;
@@ -417,10 +414,10 @@ shared_ptr<Expression> Parser::matchComparison() {
shared_ptr<Expression> Parser::matchTerm() {
shared_ptr<Expression> expression = matchFactor();
if (expression == nullptr || !expression->isValid())
return expression;
if (expression == nullptr)
return nullptr;
while (tryMatchingTokenKinds({Token::tokensTerm}, false, false))
if (tryMatchingTokenKinds({Token::tokensTerm}, false, false))
expression = matchExpressionBinary(expression);
return expression;
@@ -428,10 +425,10 @@ shared_ptr<Expression> Parser::matchTerm() {
shared_ptr<Expression> Parser::matchFactor() {
shared_ptr<Expression> expression = matchPrimary();
if (expression == nullptr || !expression->isValid())
return expression;
if (expression == nullptr)
return nullptr;
while (tokens.at(currentIndex)->isOfKind(Token::tokensFactor))
if (tokens.at(currentIndex)->isOfKind(Token::tokensFactor))
expression = matchExpressionBinary(expression);
return expression;
@@ -507,8 +504,8 @@ shared_ptr<Expression> Parser::matchExpressionCall() {
tryMatchingTokenKinds({TokenKind::NEW_LINE}, true, true); // optional new line
shared_ptr<Expression> argumentExpression = nextExpression();
if (argumentExpression == nullptr || !argumentExpression->isValid())
return argumentExpression;
if (argumentExpression == nullptr)
return nullptr;
argumentExpressions.push_back(argumentExpression);
} while (tryMatchingTokenKinds({TokenKind::COMMA}, true, true));
@@ -585,8 +582,6 @@ shared_ptr<Expression> Parser::matchExpressionBinary(shared_ptr<Expression> left
if (right == nullptr) {
return nullptr;
} else if (!right->isValid()) {
return right;
} else {
return make_shared<ExpressionBinary>(token, left, right);
}

View File

@@ -9,8 +9,6 @@ enum class TokenKind;
class Error;
class Expression;
class ExpressionInvalid;
class Statement;
using namespace std;