Implements basic numerical tables
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -69,6 +69,10 @@ IToken* Lexer::LexNext(char c){
|
||||
return new SimpleToken(TokenKind::OpenSquareBracket, this -> _position - 1, 1);
|
||||
case ']':
|
||||
return new SimpleToken(TokenKind::CloseSquareBracket, this -> _position - 1, 1);
|
||||
case '{':
|
||||
return new SimpleToken(TokenKind::OpenCurlyBracket, this -> _position - 1, 1);
|
||||
case '}':
|
||||
return new SimpleToken(TokenKind::CloseCurlyBracket, this -> _position - 1, 1);
|
||||
case ',':
|
||||
return new SimpleToken(TokenKind::CommaToken, this -> _position - 1, 1);
|
||||
case '.':
|
||||
|
||||
@@ -23,6 +23,7 @@ enum class ParsedExpressionKind{
|
||||
Parenthesized,
|
||||
FunctionCall,
|
||||
Indexer,
|
||||
NumericalTable,
|
||||
};
|
||||
|
||||
class ParsedExpression {
|
||||
@@ -270,5 +271,28 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class ParsedNumericalTableExpression : public ParsedExpression{
|
||||
vector<ParsedExpression*> _expressions;
|
||||
public:
|
||||
ParsedNumericalTableExpression(vector<ParsedExpression*> expressions, unsigned int start, unsigned int length)
|
||||
: ParsedExpression(start, length){
|
||||
_expressions = std::move(expressions);
|
||||
}
|
||||
|
||||
~ParsedNumericalTableExpression() final{
|
||||
for (auto s: _expressions){
|
||||
delete s;
|
||||
}
|
||||
}
|
||||
|
||||
vector<ParsedExpression*> GetExpressions(){
|
||||
return _expressions;
|
||||
}
|
||||
|
||||
ParsedExpressionKind GetKind() final{
|
||||
return ParsedExpressionKind::NumericalTable;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //PORYGONLANG_PARSEDEXPRESSION_HPP
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <utility>
|
||||
|
||||
#ifndef PORYGONLANG_PARSEDSTATEMENT_HPP
|
||||
#define PORYGONLANG_PARSEDSTATEMENT_HPP
|
||||
|
||||
|
||||
@@ -307,6 +307,7 @@ ParsedExpression *Parser::ParsePrimaryExpression(IToken *current) {
|
||||
case TokenKind ::FalseKeyword: return new LiteralBoolExpression(current);
|
||||
case TokenKind ::Identifier: return new VariableExpression((IdentifierToken*)current);
|
||||
case TokenKind ::OpenParenthesis: return this -> ParseParenthesizedExpression(current);
|
||||
case TokenKind ::OpenCurlyBracket: return this -> ParseTableExpression(current);
|
||||
// If we find a bad token here, we should have already logged it in the lexer, so don't log another error.
|
||||
case TokenKind ::BadToken: return new BadExpression(current->GetStartPosition(), current->GetLength());
|
||||
default:
|
||||
@@ -365,5 +366,41 @@ ParsedExpression* Parser::ParseIndexExpression(ParsedExpression* indexingExpress
|
||||
return new IndexExpression(indexingExpression, indexExpression, start, closeBracket->GetEndPosition() - start);
|
||||
}
|
||||
|
||||
|
||||
ParsedExpression* Parser::ParseTableExpression(IToken* current){
|
||||
if (this -> Peek() -> GetKind() == TokenKind::CloseCurlyBracket){
|
||||
this -> Next();
|
||||
auto start = current->GetStartPosition();
|
||||
return new ParsedNumericalTableExpression({}, start, this -> Peek()->GetEndPosition() - start);
|
||||
}
|
||||
auto firstItem = this->ParseStatement(this -> Next());
|
||||
// If the first item is an expression, and is followed by a comma, we're dealing with a simple {1, 2, 3} kind of array
|
||||
if (firstItem->GetKind() == ParsedStatementKind::Expression &&
|
||||
(this->Peek()->GetKind() == TokenKind::CommaToken )){
|
||||
auto expr = ((ParsedExpressionStatement*)firstItem)->GetExpression();
|
||||
auto expressions = vector<ParsedExpression*>{expr};
|
||||
auto n = this -> Next(); // consume the comma
|
||||
bool hasErrors = false;
|
||||
while (n->GetKind() != TokenKind::CloseCurlyBracket){
|
||||
auto expression = this->ParseExpression(this->Next());
|
||||
expressions.push_back(expression);
|
||||
n = this->Next();
|
||||
if (n->GetKind() != TokenKind::CommaToken && n->GetKind() != TokenKind ::CloseCurlyBracket && !hasErrors){
|
||||
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, n->GetStartPosition(), n->GetLength());
|
||||
hasErrors = true;
|
||||
}
|
||||
}
|
||||
auto start = current->GetStartPosition();
|
||||
if (hasErrors){
|
||||
return new BadExpression(start, n->GetEndPosition() - start);
|
||||
}
|
||||
return new ParsedNumericalTableExpression(expressions, start, n->GetEndPosition() - start);
|
||||
}
|
||||
// Otherwise we have a more complex table, which can be defined by a block
|
||||
else {
|
||||
auto block = (ParsedBlockStatement*)this -> ParseBlock({TokenKind ::CloseCurlyBracket});
|
||||
auto statements = block->GetStatements();
|
||||
statements.insert(statements.begin(), firstItem);
|
||||
throw "not implemented TODO";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,8 @@ public:
|
||||
|
||||
|
||||
IToken *PeekAt(int offset);
|
||||
|
||||
ParsedExpression *ParseTableExpression(IToken *current);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ enum class TokenKind{
|
||||
CloseParenthesis,
|
||||
OpenSquareBracket,
|
||||
CloseSquareBracket,
|
||||
OpenCurlyBracket,
|
||||
CloseCurlyBracket,
|
||||
|
||||
PeriodToken,
|
||||
CommaToken,
|
||||
|
||||
Reference in New Issue
Block a user