2019-05-20 15:45:03 +00:00
|
|
|
|
|
|
|
#include "Parser.hpp"
|
|
|
|
#include "UnaryOperatorKind.hpp"
|
|
|
|
#include "BinaryOperatorKind.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
ParsedScriptStatement* Parser::Parse() {
|
|
|
|
vector<ParsedStatement*> statements;
|
|
|
|
while (true){
|
2019-05-21 12:00:14 +00:00
|
|
|
auto next = this -> Next();
|
2019-05-20 15:45:03 +00:00
|
|
|
if (next->GetKind() == TokenKind::EndOfFile){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (next->GetKind() == TokenKind::WhiteSpace){
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-21 12:00:14 +00:00
|
|
|
statements.push_back(this -> ParseStatement(next));
|
2019-05-20 15:45:03 +00:00
|
|
|
}
|
|
|
|
return new ParsedScriptStatement(statements);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParsedStatement* Parser::ParseStatement(IToken* current){
|
2019-05-21 12:00:14 +00:00
|
|
|
return new ParsedExpressionStatement(this -> ParseExpression(current));
|
2019-05-20 15:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ParsedExpression* Parser::ParseExpression(IToken* current){
|
2019-05-21 12:00:14 +00:00
|
|
|
return this -> ParseBinaryExpression(current, OperatorPrecedence::No);
|
2019-05-20 15:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OperatorPrecedence GetUnaryPrecedence(TokenKind kind){
|
|
|
|
switch (kind){
|
|
|
|
case TokenKind::PlusToken:
|
|
|
|
case TokenKind::MinusToken:
|
|
|
|
case TokenKind::NotKeyword:
|
|
|
|
return OperatorPrecedence::Unary;
|
|
|
|
default:
|
|
|
|
return OperatorPrecedence::No;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnaryOperatorKind GetUnaryOperatorKind(TokenKind kind){
|
|
|
|
switch (kind){
|
|
|
|
case TokenKind::PlusToken: return UnaryOperatorKind::Identity;
|
|
|
|
case TokenKind::MinusToken: return UnaryOperatorKind::Negation;
|
|
|
|
case TokenKind::NotKeyword: return UnaryOperatorKind::LogicalNegation;
|
2019-05-21 12:00:14 +00:00
|
|
|
default: // This should never trigger, so throw.
|
2019-05-20 15:45:03 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryOperatorKind GetBinaryOperatorKind(TokenKind kind){
|
|
|
|
switch (kind){
|
|
|
|
case TokenKind::PlusToken: return BinaryOperatorKind ::Addition;
|
|
|
|
case TokenKind::MinusToken: return BinaryOperatorKind ::Subtraction;
|
|
|
|
case TokenKind::StarToken: return BinaryOperatorKind ::Multiplication;
|
|
|
|
case TokenKind::SlashToken: return BinaryOperatorKind ::Division;
|
|
|
|
case TokenKind::EqualityToken: return BinaryOperatorKind ::Equality;
|
|
|
|
case TokenKind::AndKeyword: return BinaryOperatorKind ::LogicalAnd;
|
|
|
|
case TokenKind::OrKeyword: return BinaryOperatorKind ::LogicalOr;
|
2019-05-21 12:00:14 +00:00
|
|
|
default: // This should never trigger, so throw.
|
2019-05-20 15:45:03 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OperatorPrecedence GetBinaryPrecedence(TokenKind kind){
|
|
|
|
switch (kind){
|
|
|
|
case TokenKind::PlusToken: return OperatorPrecedence ::Additive;
|
|
|
|
case TokenKind::MinusToken: return OperatorPrecedence ::Additive;
|
|
|
|
case TokenKind::StarToken: return OperatorPrecedence ::Multiplication;
|
|
|
|
case TokenKind::SlashToken: return OperatorPrecedence ::Multiplication;
|
|
|
|
case TokenKind::EqualityToken: return OperatorPrecedence ::Equality;
|
|
|
|
case TokenKind::AndKeyword: return OperatorPrecedence ::LogicalAnd;
|
|
|
|
case TokenKind::OrKeyword: return OperatorPrecedence ::LogicalOr;
|
|
|
|
default:
|
|
|
|
return OperatorPrecedence::No;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ParsedExpression* Parser::ParseBinaryExpression(IToken* current, OperatorPrecedence parentPrecedence){
|
|
|
|
OperatorPrecedence unaryPrecedence = GetUnaryPrecedence(current -> GetKind());
|
|
|
|
ParsedExpression* left;
|
|
|
|
if (unaryPrecedence != OperatorPrecedence::No && unaryPrecedence >= parentPrecedence){
|
|
|
|
UnaryOperatorKind operatorKind = GetUnaryOperatorKind(current -> GetKind());
|
2019-05-21 12:00:14 +00:00
|
|
|
auto next = this -> Next();
|
|
|
|
auto operand = this -> ParseBinaryExpression(next, unaryPrecedence);
|
2019-05-20 15:45:03 +00:00
|
|
|
auto startPos = current -> GetStartPosition();
|
|
|
|
left = new UnaryExpression(operatorKind, operand, startPos, operand -> GetEndPosition() - startPos);
|
|
|
|
} else{
|
2019-05-21 12:00:14 +00:00
|
|
|
left = this -> ParsePrimaryExpression(current);
|
2019-05-20 15:45:03 +00:00
|
|
|
}
|
|
|
|
while (true){
|
2019-05-21 12:00:14 +00:00
|
|
|
auto next = this -> Peek();
|
2019-05-20 15:45:03 +00:00
|
|
|
OperatorPrecedence binaryPrecedence = GetBinaryPrecedence(next -> GetKind());
|
|
|
|
if (binaryPrecedence == OperatorPrecedence::No || binaryPrecedence <= parentPrecedence){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto operatorKind = GetBinaryOperatorKind(next -> GetKind());
|
2019-05-21 12:00:14 +00:00
|
|
|
this -> Next();
|
2019-05-21 15:16:53 +00:00
|
|
|
auto right = this -> ParseBinaryExpression(this -> Next(), binaryPrecedence);
|
2019-05-20 15:45:03 +00:00
|
|
|
auto startPos = left -> GetStartPosition();
|
|
|
|
left = new BinaryExpression(operatorKind, left, right, startPos, right -> GetEndPosition() - startPos);
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParsedExpression *Parser::ParsePrimaryExpression(IToken *current) {
|
|
|
|
switch (current -> GetKind()){
|
|
|
|
case TokenKind ::Integer: return new LiteralIntegerExpression((IntegerToken*)current);
|
|
|
|
case TokenKind ::Float: return new LiteralFloatExpression((FloatToken*)current);
|
|
|
|
case TokenKind ::TrueKeyword: return new LiteralBoolExpression(current);
|
|
|
|
case TokenKind ::FalseKeyword: return new LiteralBoolExpression(current);
|
2019-05-21 15:16:53 +00:00
|
|
|
case TokenKind ::OpenParenthesis: return this -> ParseParenthesizedExpression(current);
|
2019-05-21 13:11:00 +00:00
|
|
|
// 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());
|
2019-05-20 15:45:03 +00:00
|
|
|
default:
|
2019-05-21 12:15:39 +00:00
|
|
|
this -> ScriptData -> Diagnostics -> LogError(DiagnosticCode::UnexpectedToken, current->GetStartPosition(), current->GetLength());
|
2019-05-21 12:00:14 +00:00
|
|
|
return new BadExpression(current->GetStartPosition(), current->GetLength());
|
2019-05-20 15:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 15:16:53 +00:00
|
|
|
ParsedExpression *Parser::ParseParenthesizedExpression(IToken *current) {
|
|
|
|
auto next = this -> Next();
|
|
|
|
auto expression = this -> ParseExpression(next);
|
|
|
|
auto closeToken = this -> Next();
|
|
|
|
if (closeToken -> GetKind() != TokenKind::CloseParenthesis){
|
|
|
|
this -> ScriptData -> Diagnostics -> LogError(DiagnosticCode::UnexpectedToken, closeToken->GetStartPosition(), closeToken->GetLength());
|
|
|
|
return new BadExpression(closeToken->GetStartPosition(), closeToken->GetLength());
|
|
|
|
}
|
|
|
|
auto start = current -> GetStartPosition();
|
|
|
|
return new ParenthesizedExpression(expression, start, closeToken->GetEndPosition() - start);
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:45:03 +00:00
|
|
|
|
|
|
|
IToken *Parser::Peek() {
|
2019-05-21 12:00:14 +00:00
|
|
|
return this -> _tokens[_position];
|
2019-05-20 15:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IToken *Parser::Next() {
|
2019-05-21 12:00:14 +00:00
|
|
|
this -> _position++;
|
|
|
|
return this -> _tokens[_position - 1];
|
2019-05-20 15:45:03 +00:00
|
|
|
}
|
|
|
|
|