Implements indexing, currently can only be used with strings
This commit is contained in:
@@ -22,6 +22,7 @@ enum class ParsedExpressionKind{
|
||||
Binary,
|
||||
Parenthesized,
|
||||
FunctionCall,
|
||||
Indexer,
|
||||
};
|
||||
|
||||
class ParsedExpression {
|
||||
@@ -246,5 +247,28 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class IndexExpression : public ParsedExpression{
|
||||
std::unique_ptr<ParsedExpression> _indexerExpression;
|
||||
std::unique_ptr<ParsedExpression> _indexExpression;
|
||||
public:
|
||||
IndexExpression(ParsedExpression* indexer, ParsedExpression* index, unsigned int start, unsigned int length)
|
||||
:ParsedExpression(start, length){
|
||||
_indexerExpression = std::unique_ptr<ParsedExpression>(indexer);
|
||||
_indexExpression = std::unique_ptr<ParsedExpression>(index);
|
||||
}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
return ParsedExpressionKind::Indexer;
|
||||
}
|
||||
|
||||
ParsedExpression* GetIndexer(){
|
||||
return _indexerExpression.get();
|
||||
}
|
||||
|
||||
ParsedExpression* GetIndex(){
|
||||
return _indexExpression.get();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||
|
||||
@@ -146,10 +146,11 @@ ParsedExpression* Parser::ParseExpression(IToken* current){
|
||||
if (peekKind == TokenKind::OpenParenthesis){
|
||||
expression = this->ParseFunctionCallExpression(expression);
|
||||
} else if (peekKind == TokenKind::OpenSquareBracket){
|
||||
//TODO: index expression
|
||||
expression = this->ParseIndexExpression(expression);
|
||||
} else {
|
||||
//TODO: index period expression
|
||||
}
|
||||
peekKind = this->Peek()->GetKind();
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
@@ -283,7 +284,17 @@ ParsedExpression *Parser::ParseFunctionCallExpression(ParsedExpression* function
|
||||
return new FunctionCallExpression(functionExpression, parameters, start, peeked->GetEndPosition() - start);
|
||||
}
|
||||
|
||||
|
||||
ParsedExpression* Parser::ParseIndexExpression(ParsedExpression* indexingExpression){
|
||||
this->Next(); // consume '[' token
|
||||
auto indexExpression = this -> ParseExpression(this -> Next());
|
||||
auto closeBracket = this->Next();
|
||||
if (closeBracket->GetKind() != TokenKind::CloseSquareBracket){
|
||||
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, closeBracket->GetStartPosition(), closeBracket->GetLength());
|
||||
return new BadExpression(closeBracket->GetStartPosition(), closeBracket->GetLength());
|
||||
}
|
||||
auto start = indexingExpression->GetStartPosition();
|
||||
return new IndexExpression(indexingExpression, indexExpression, start, closeBracket->GetEndPosition() - start);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -34,7 +34,9 @@ class Parser {
|
||||
ParsedExpression* ParseBinaryExpression(IToken* current, OperatorPrecedence parentPrecedence);
|
||||
ParsedExpression* ParsePrimaryExpression(IToken* current);
|
||||
ParsedExpression* ParseParenthesizedExpression(IToken *current);
|
||||
|
||||
ParsedExpression* ParseFunctionCallExpression(ParsedExpression* functionExpression);
|
||||
ParsedExpression *ParseIndexExpression(ParsedExpression *indexingExpression);
|
||||
public:
|
||||
ParsedScriptStatement* Parse();
|
||||
explicit Parser(vector<IToken*> tokens, Script* scriptData){
|
||||
|
||||
Reference in New Issue
Block a user