Implements inequality token
This commit is contained in:
parent
d6a6e116fe
commit
9131fbfee7
|
@ -133,6 +133,9 @@ BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){
|
||||||
case BinaryOperatorKind ::Equality:
|
case BinaryOperatorKind ::Equality:
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, new ScriptType(TypeClass::Bool),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality, new ScriptType(TypeClass::Bool),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
|
case BinaryOperatorKind ::Inequality:
|
||||||
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality, new ScriptType(TypeClass::Bool),
|
||||||
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
case BinaryOperatorKind ::LogicalAnd:
|
case BinaryOperatorKind ::LogicalAnd:
|
||||||
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd, new ScriptType(TypeClass::Bool),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd, new ScriptType(TypeClass::Bool),
|
||||||
|
@ -143,8 +146,6 @@ BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){
|
||||||
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr, new ScriptType(TypeClass::Bool),
|
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr, new ScriptType(TypeClass::Bool),
|
||||||
expression->GetStartPosition(), expression->GetLength());
|
expression->GetStartPosition(), expression->GetLength());
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
this -> ScriptData -> Diagnostics->LogError(DiagnosticCode::NoBinaryOperationFound, expression->GetStartPosition(), expression->GetLength());
|
this -> ScriptData -> Diagnostics->LogError(DiagnosticCode::NoBinaryOperationFound, expression->GetStartPosition(), expression->GetLength());
|
||||||
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
|
||||||
|
|
|
@ -8,6 +8,7 @@ enum class BoundBinaryOperation{
|
||||||
Multiplication,
|
Multiplication,
|
||||||
Division,
|
Division,
|
||||||
Equality,
|
Equality,
|
||||||
|
Inequality,
|
||||||
LogicalAnd,
|
LogicalAnd,
|
||||||
LogicalOr,
|
LogicalOr,
|
||||||
Concatenation
|
Concatenation
|
||||||
|
|
|
@ -43,6 +43,15 @@ BooleanEvalValue* Evaluator::EvaluateBooleanBinary(BoundBinaryExpression* expres
|
||||||
delete rightValue;
|
delete rightValue;
|
||||||
return new BooleanEvalValue(equals);
|
return new BooleanEvalValue(equals);
|
||||||
}
|
}
|
||||||
|
case BoundBinaryOperation::Inequality:
|
||||||
|
{
|
||||||
|
EvalValue* leftValue = this -> EvaluateExpression(expression->GetLeft());
|
||||||
|
EvalValue* rightValue = this -> EvaluateExpression(expression->GetRight());
|
||||||
|
bool equals = leftValue->operator!=(rightValue);
|
||||||
|
delete leftValue;
|
||||||
|
delete rightValue;
|
||||||
|
return new BooleanEvalValue(equals);
|
||||||
|
}
|
||||||
case BoundBinaryOperation::LogicalAnd:
|
case BoundBinaryOperation::LogicalAnd:
|
||||||
{
|
{
|
||||||
BooleanEvalValue* leftValue = this -> EvaluateBoolExpression(expression->GetLeft());
|
BooleanEvalValue* leftValue = this -> EvaluateBoolExpression(expression->GetLeft());
|
||||||
|
|
|
@ -7,6 +7,7 @@ enum class BinaryOperatorKind{
|
||||||
Multiplication,
|
Multiplication,
|
||||||
Division,
|
Division,
|
||||||
Equality,
|
Equality,
|
||||||
|
Inequality,
|
||||||
LogicalAnd,
|
LogicalAnd,
|
||||||
LogicalOr,
|
LogicalOr,
|
||||||
};
|
};
|
||||||
|
|
|
@ -62,6 +62,13 @@ IToken* Lexer::LexNext(char c){
|
||||||
return new SimpleToken(TokenKind::EqualityToken, this -> Position - 2, 2);
|
return new SimpleToken(TokenKind::EqualityToken, this -> Position - 2, 2);
|
||||||
}
|
}
|
||||||
return new SimpleToken(TokenKind::AssignmentToken, this -> Position - 1, 1);
|
return new SimpleToken(TokenKind::AssignmentToken, this -> Position - 1, 1);
|
||||||
|
case '~':
|
||||||
|
if (Lexer::Peek() == '='){
|
||||||
|
Lexer::Next();
|
||||||
|
return new SimpleToken(TokenKind::InequalityToken, this -> Position - 2, 2);
|
||||||
|
}
|
||||||
|
this -> ScriptData -> Diagnostics -> LogError(DiagnosticCode::UnexpectedCharacter, this -> Position - 1, 1);
|
||||||
|
return new SimpleToken(TokenKind::BadToken, this -> Position - 1, 1);
|
||||||
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
|
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
|
||||||
return LexNumber(c);
|
return LexNumber(c);
|
||||||
case '"':
|
case '"':
|
||||||
|
|
|
@ -56,6 +56,7 @@ BinaryOperatorKind GetBinaryOperatorKind(TokenKind kind){
|
||||||
case TokenKind::StarToken: return BinaryOperatorKind ::Multiplication;
|
case TokenKind::StarToken: return BinaryOperatorKind ::Multiplication;
|
||||||
case TokenKind::SlashToken: return BinaryOperatorKind ::Division;
|
case TokenKind::SlashToken: return BinaryOperatorKind ::Division;
|
||||||
case TokenKind::EqualityToken: return BinaryOperatorKind ::Equality;
|
case TokenKind::EqualityToken: return BinaryOperatorKind ::Equality;
|
||||||
|
case TokenKind::InequalityToken: return BinaryOperatorKind ::Inequality;
|
||||||
case TokenKind::AndKeyword: return BinaryOperatorKind ::LogicalAnd;
|
case TokenKind::AndKeyword: return BinaryOperatorKind ::LogicalAnd;
|
||||||
case TokenKind::OrKeyword: return BinaryOperatorKind ::LogicalOr;
|
case TokenKind::OrKeyword: return BinaryOperatorKind ::LogicalOr;
|
||||||
default: // This should never trigger, so throw.
|
default: // This should never trigger, so throw.
|
||||||
|
@ -70,6 +71,7 @@ OperatorPrecedence GetBinaryPrecedence(TokenKind kind){
|
||||||
case TokenKind::StarToken: return OperatorPrecedence ::Multiplication;
|
case TokenKind::StarToken: return OperatorPrecedence ::Multiplication;
|
||||||
case TokenKind::SlashToken: return OperatorPrecedence ::Multiplication;
|
case TokenKind::SlashToken: return OperatorPrecedence ::Multiplication;
|
||||||
case TokenKind::EqualityToken: return OperatorPrecedence ::Equality;
|
case TokenKind::EqualityToken: return OperatorPrecedence ::Equality;
|
||||||
|
case TokenKind::InequalityToken: return OperatorPrecedence ::Equality;
|
||||||
case TokenKind::AndKeyword: return OperatorPrecedence ::LogicalAnd;
|
case TokenKind::AndKeyword: return OperatorPrecedence ::LogicalAnd;
|
||||||
case TokenKind::OrKeyword: return OperatorPrecedence ::LogicalOr;
|
case TokenKind::OrKeyword: return OperatorPrecedence ::LogicalOr;
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -12,6 +12,7 @@ enum class TokenKind{
|
||||||
StarToken,
|
StarToken,
|
||||||
AssignmentToken,
|
AssignmentToken,
|
||||||
EqualityToken,
|
EqualityToken,
|
||||||
|
InequalityToken,
|
||||||
OpenParenthesis,
|
OpenParenthesis,
|
||||||
CloseParenthesis,
|
CloseParenthesis,
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,21 @@ TEST_CASE( "True Not Equals True", "[integration]" ) {
|
||||||
auto lastValue = script.GetLastValue();
|
auto lastValue = script.GetLastValue();
|
||||||
REQUIRE(!lastValue->EvaluateBool());
|
REQUIRE(!lastValue->EvaluateBool());
|
||||||
}
|
}
|
||||||
|
TEST_CASE( "True Nequals False", "[integration]" ) {
|
||||||
|
Script script = Script::Create("true ~= false");
|
||||||
|
REQUIRE(!script.Diagnostics -> HasErrors());
|
||||||
|
script.Evaluate();
|
||||||
|
auto lastValue = script.GetLastValue();
|
||||||
|
REQUIRE(lastValue->EvaluateBool());
|
||||||
|
}
|
||||||
|
TEST_CASE( "True Not Nequals True", "[integration]" ) {
|
||||||
|
Script script = Script::Create("true ~= true");
|
||||||
|
REQUIRE(!script.Diagnostics -> HasErrors());
|
||||||
|
script.Evaluate();
|
||||||
|
auto lastValue = script.GetLastValue();
|
||||||
|
REQUIRE(!lastValue->EvaluateBool());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE( "False Equals False", "[integration]" ) {
|
TEST_CASE( "False Equals False", "[integration]" ) {
|
||||||
Script script = Script::Create("false == false");
|
Script script = Script::Create("false == false");
|
||||||
|
|
Loading…
Reference in New Issue