Make parsed statements constant during binding

This commit is contained in:
2019-06-13 18:14:59 +02:00
parent 601c4a3f89
commit 5910cbbfa9
8 changed files with 238 additions and 215 deletions

View File

@@ -1,3 +1,5 @@
#include <utility>
#include <algorithm>
#include "Parser.hpp"
@@ -9,7 +11,7 @@
ParsedScriptStatement* Parser::Parse() {
vector<ParsedStatement*> statements;
vector<const ParsedStatement*> statements;
while (this->_position < this->_tokens.size()){
auto next = this -> Next();
if (next->GetKind() == TokenKind::EndOfFile){
@@ -73,8 +75,8 @@ ParsedStatement *Parser::ParseAssignment(IToken *current) {
return new ParsedAssignmentStatement(isLocal, ((IdentifierToken*)identifier) -> Value, expression, start, expression->GetEndPosition() - start);
}
ParsedStatement *Parser::ParseBlock(const vector<TokenKind>& endTokens) {
vector<ParsedStatement*> statements;
ParsedStatement *Parser::ParseBlock(const vector<TokenKind>& endTokens, const vector<const ParsedStatement*>& openStatements) {
auto statements = openStatements;
auto start = this->_position;
while (this->_position < this->_tokens.size()){
auto next = this -> Next();
@@ -331,7 +333,7 @@ ParsedExpression *Parser::ParseParenthesizedExpression(IToken *current) {
ParsedExpression *Parser::ParseFunctionCallExpression(ParsedExpression* functionExpression) {
this -> Next(); // consume the open parenthesis
vector<ParsedExpression*> parameters;
vector<const ParsedExpression*> parameters;
auto peeked = this -> Peek();
auto peekedKind = peeked->GetKind();
if (peekedKind == TokenKind::CloseParenthesis){
@@ -379,7 +381,7 @@ ParsedExpression* Parser::ParseTableExpression(IToken* current){
if (firstItem->GetKind() == ParsedStatementKind::Expression &&
(this->Peek()->GetKind() == TokenKind::CommaToken )){
auto expr = ((ParsedExpressionStatement*)firstItem)->GetExpression();
auto expressions = vector<ParsedExpression*>{expr};
auto expressions = vector<const ParsedExpression*>{expr};
auto n = this -> Next(); // consume the comma
bool hasErrors = false;
while (n->GetKind() != TokenKind::CloseCurlyBracket){
@@ -398,9 +400,7 @@ ParsedExpression* Parser::ParseTableExpression(IToken* current){
}
// 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);
auto block = (ParsedBlockStatement*)this -> ParseBlock({TokenKind ::CloseCurlyBracket}, {firstItem});
auto closeToken = this -> PeekAt(-1);
return new ParsedTableExpression(block, start, closeToken->GetEndPosition() - start);
}