From cf1daf78051f6a862cee287bb2bc4d46a3ae2302 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 1 Sep 2019 16:16:36 +0200 Subject: [PATCH] Handle nil keyword --- src/Binder/Binder.cpp | 2 ++ src/Binder/BoundExpressions/BoundExpression.hpp | 13 +++++++++++++ src/Evaluator/Evaluator.cpp | 3 +++ src/Parser/ParsedExpressions/ParsedExpression.hpp | 12 ++++++++++++ src/Parser/Parser.cpp | 2 ++ 5 files changed, 32 insertions(+) diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index 6398acd..6084002 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -366,6 +366,8 @@ namespace Porygon::Binder { case ParsedExpressionKind::LiteralBool: return new BoundLiteralBoolExpression(((LiteralBoolExpression *) expression)->GetValue(), expression->GetStartPosition(), expression->GetLength()); + case ParsedExpressionKind ::Nil: + return new BoundNilExpression(expression->GetStartPosition(), expression->GetLength()); case ParsedExpressionKind::Variable: return this->BindVariableExpression((VariableExpression *) expression); diff --git a/src/Binder/BoundExpressions/BoundExpression.hpp b/src/Binder/BoundExpressions/BoundExpression.hpp index 60f2391..a6ab0e0 100644 --- a/src/Binder/BoundExpressions/BoundExpression.hpp +++ b/src/Binder/BoundExpressions/BoundExpression.hpp @@ -18,6 +18,7 @@ namespace Porygon::Binder { LiteralFloat, LiteralString, LiteralBool, + Nil, Variable, Unary, @@ -152,6 +153,18 @@ namespace Porygon::Binder { } }; + class BoundNilExpression : public BoundExpression { + public: + BoundNilExpression(unsigned int start, unsigned int length) + : BoundExpression(start, length, ScriptType::NilType) + { } + + [[nodiscard]] + inline BoundExpressionKind GetKind() const final { + return BoundExpressionKind::Nil; + } + }; + class BoundVariableExpression : public BoundExpression { const BoundVariableKey *_key; public: diff --git a/src/Evaluator/Evaluator.cpp b/src/Evaluator/Evaluator.cpp index 316722f..e7c6ea5 100644 --- a/src/Evaluator/Evaluator.cpp +++ b/src/Evaluator/Evaluator.cpp @@ -5,6 +5,7 @@ #include "EvaluationScope/EvaluationScope.hpp" #include "EvalValues/ScriptFunctionEvalValue.hpp" #include "EvalValues/TableEvalValue.hpp" +#include "EvalValues/NilEvalValue.hpp" #include "../Binder/BoundExpressions/BoundTableExpression.hpp" #include "../Binder/BoundExpressions/BoundFunctionCallExpression.hpp" #include "../Binder/BoundExpressions/BoundRequireExpression.hpp" @@ -239,6 +240,8 @@ namespace Porygon::Evaluation { return new StringEvalValue(*((BoundLiteralStringExpression *) expression)->GetValue()); case BoundExpressionKind::LiteralBool: return new BooleanEvalValue(((BoundLiteralBoolExpression *) expression)->GetValue()); + case BoundExpressionKind::Nil: + return new NilEvalValue(); case BoundExpressionKind::Variable: return this -> GetVariable((BoundVariableExpression*)expression); case BoundExpressionKind::Unary: diff --git a/src/Parser/ParsedExpressions/ParsedExpression.hpp b/src/Parser/ParsedExpressions/ParsedExpression.hpp index a5801b1..9d26b55 100644 --- a/src/Parser/ParsedExpressions/ParsedExpression.hpp +++ b/src/Parser/ParsedExpressions/ParsedExpression.hpp @@ -18,6 +18,7 @@ namespace Porygon::Parser { LiteralFloat, LiteralString, LiteralBool, + Nil, Variable, Unary, @@ -133,6 +134,17 @@ namespace Porygon::Parser { } }; + class NilExpression : public ParsedExpression { + public: + [[nodiscard]] inline const ParsedExpressionKind GetKind() const final { + return ParsedExpressionKind::Nil; + } + + explicit NilExpression(const Token *token) + : ParsedExpression(token->GetStartPosition(), token->GetLength()) { + } + }; + class VariableExpression : public ParsedExpression { const HashedString _value; public: diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index f405f12..5127286 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -496,6 +496,8 @@ namespace Porygon::Parser { return new LiteralBoolExpression(current); case TokenKind::FalseKeyword: return new LiteralBoolExpression(current); + case TokenKind::NilKeyword: + return new NilExpression(current); case TokenKind::Identifier: return new VariableExpression(dynamic_cast(current)); case TokenKind::OpenParenthesis: