From 5be0089d8d863c35fb5e46adbad8be19383d5f86 Mon Sep 17 00:00:00 2001 From: SL Date: Tue, 5 Jul 2016 13:44:43 +0700 Subject: [PATCH] Type comparison operators implemented --- .../Pash/Implementation/ExecutionVisitor.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Source/System.Management/Pash/Implementation/ExecutionVisitor.cs b/Source/System.Management/Pash/Implementation/ExecutionVisitor.cs index e64c11ba..9c28f519 100644 --- a/Source/System.Management/Pash/Implementation/ExecutionVisitor.cs +++ b/Source/System.Management/Pash/Implementation/ExecutionVisitor.cs @@ -174,7 +174,10 @@ object EvaluateBinaryExpression(BinaryExpressionAst binaryExpressionAst) var right = LanguagePrimitives.ConvertTo(rightOperand); return string.Format(left, right); } - + case TokenKind.Is: + return IsInstanceOfType(leftOperand, rightOperand); + case TokenKind.IsNot: + return !IsInstanceOfType(leftOperand, rightOperand); case TokenKind.Equals: case TokenKind.PlusEquals: case TokenKind.MinusEquals: @@ -200,8 +203,6 @@ object EvaluateBinaryExpression(BinaryExpressionAst binaryExpressionAst) case TokenKind.Cin: case TokenKind.Cnotin: case TokenKind.Csplit: - case TokenKind.Is: - case TokenKind.IsNot: case TokenKind.As: case TokenKind.Shl: case TokenKind.Shr: @@ -212,6 +213,17 @@ object EvaluateBinaryExpression(BinaryExpressionAst binaryExpressionAst) } } + private static bool IsInstanceOfType(object leftOperand, object rightOperand) + { + var type = rightOperand as Type; + if (type == null) + { + throw new InvalidOperationException(@"The right operand of '-is' must be a type."); + } + + return type.IsInstanceOfType(leftOperand); + } + private bool Match(object leftOperand, object rightOperand, RegexOptions regexOptions) { if (!(leftOperand is string) || !(rightOperand is string))