Implemented value assignment
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "Parser/Statement/StatementFunction.h"
|
||||
#include "Parser/Statement/StatementVariable.h"
|
||||
#include "Parser/Statement/StatementAssignment.h"
|
||||
#include "Parser/Statement/StatementReturn.h"
|
||||
#include "Parser/Statement/StatementExpression.h"
|
||||
#include "Parser/Statement/StatementMetaExternFunction.h"
|
||||
@@ -51,6 +52,10 @@ shared_ptr<Statement> Parser::nextStatement() {
|
||||
if (statement != nullptr)
|
||||
return statement;
|
||||
|
||||
statement = matchStatementAssignment();
|
||||
if (statement != nullptr)
|
||||
return statement;
|
||||
|
||||
statement = matchStatementReturn();
|
||||
if (statement != nullptr)
|
||||
return statement;
|
||||
@@ -163,6 +168,25 @@ shared_ptr<Statement> Parser::matchStatementVariable() {
|
||||
return make_shared<StatementVariable>(identifierToken->getLexme(), valueType, expression);
|
||||
}
|
||||
|
||||
shared_ptr<Statement> Parser::matchStatementAssignment() {
|
||||
if (!tryMatchingTokenKinds({TokenKind::IDENTIFIER, TokenKind::LEFT_ARROW}, true, false))
|
||||
return nullptr;
|
||||
|
||||
shared_ptr<Token> identifierToken = tokens.at(currentIndex);
|
||||
currentIndex++; // identifier
|
||||
currentIndex++; // arrow
|
||||
|
||||
shared_ptr<Expression> expression = nextExpression();
|
||||
if (expression == nullptr || !expression->isValid())
|
||||
return matchStatementInvalid("Expected expression");
|
||||
|
||||
// Expect new line
|
||||
if (!tryMatchingTokenKinds({TokenKind::NEW_LINE}, false, true))
|
||||
return matchStatementInvalid("Expected a new line after variable declaration");
|
||||
|
||||
return make_shared<StatementAssignment>(identifierToken->getLexme(), expression);
|
||||
}
|
||||
|
||||
shared_ptr<Statement> Parser::matchStatementReturn() {
|
||||
if (!tryMatchingTokenKinds({TokenKind::RETURN}, true, true))
|
||||
return nullptr;
|
||||
@@ -396,14 +420,14 @@ shared_ptr<Expression> Parser::matchPrimary() {
|
||||
if (expression != nullptr)
|
||||
return expression;
|
||||
|
||||
expression = matchExpressionVariable();
|
||||
if (expression != nullptr)
|
||||
return expression;
|
||||
|
||||
expression = matchExpressionCall();
|
||||
if (expression != nullptr)
|
||||
return expression;
|
||||
|
||||
expression = matchExpressionVariable();
|
||||
if (expression != nullptr)
|
||||
return expression;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user