Skip to content

Commit 94ad01f

Browse files
Make feature experimental
1 parent 137a37b commit 94ad01f

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

src/System.Management.Automation/engine/ExperimentalFeature/ExperimentalFeature.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ static ExperimentalFeature()
114114
description: "New parameter set for ForEach-Object to run script blocks in parallel"),
115115
new ExperimentalFeature(
116116
name: "PSTernaryOperator",
117-
description: "Support the ternary operator in PowerShell language")
117+
description: "Support the ternary operator in PowerShell language"),
118+
new ExperimentalFeature(
119+
name: "PSNullCoalescingOperators",
120+
description: "Support the null coalescing operator and null coalescing assignment operator in PowerShell language")
118121
};
119122
EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);
120123

src/System.Management.Automation/engine/parser/Compiler.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -806,17 +806,20 @@ internal Expression ReduceAssignment(ISupportsAssignment left, TokenKind tokenKi
806806

807807
private Expression GetCoalesceExpression(Expression rhs, IAssignableValue leftAssignableValue, TokenKind tokenKind)
808808
{
809-
var exprs = new List<Expression>();
810-
var temps = new List<ParameterExpression>();
811-
var leftExpr = leftAssignableValue.GetValue(this, exprs, temps);
812-
813-
if (tokenKind == TokenKind.QuestionQuestionEquals)
809+
if (ExperimentalFeature.IsEnabled("PSNullCoalescingOperators"))
814810
{
815-
exprs.Add(leftAssignableValue.SetValue(this, Expression.MakeBinary(ExpressionType.Coalesce,leftExpr,rhs)));
816-
return Expression.Block(temps, exprs);
817-
}
811+
var exprs = new List<Expression>();
812+
var temps = new List<ParameterExpression>();
813+
var leftExpr = leftAssignableValue.GetValue(this, exprs, temps);
818814

819-
return null;
815+
if (tokenKind == TokenKind.QuestionQuestionEquals)
816+
{
817+
exprs.Add(leftAssignableValue.SetValue(this, Expression.MakeBinary(ExpressionType.Coalesce, leftExpr, rhs)));
818+
return Expression.Block(temps, exprs);
819+
}
820+
}
821+
822+
return null;
820823
}
821824

822825
internal Expression GetLocal(int tupleIndex)
@@ -5247,7 +5250,7 @@ public object VisitBinaryExpression(BinaryExpressionAst binaryExpressionAst)
52475250
CachedReflectionInfo.ParserOps_SplitOperator,
52485251
_executionContextParameter, Expression.Constant(binaryExpressionAst.ErrorPosition), lhs.Cast(typeof(object)), rhs.Cast(typeof(object)),
52495252
ExpressionCache.Constant(false));
5250-
case TokenKind.QuestionQuestion:
5253+
case TokenKind.QuestionQuestion when ExperimentalFeature.IsEnabled("PSNullCoalescingOperators") :
52515254
if (lhs is ConstantExpression lhsConstExpr && lhsConstExpr.Value != null)
52525255
{
52535256
return lhs;

src/System.Management.Automation/engine/parser/token.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public enum TokenKind
416416
/// <summary>The ternary operator '?'.</summary>
417417
QuestionMark = 100,
418418

419-
/// <summary>The null conditional assignment operator '?='.</summary>
419+
/// <summary>The null conditional assignment operator '??='.</summary>
420420
QuestionQuestionEquals = 101,
421421

422422
/// <summary>The null coalesce operator '??'.</summary>
@@ -675,7 +675,7 @@ public enum TokenFlags
675675
SpecialOperator = 0x00001000,
676676

677677
/// <summary>
678-
/// The token is one of the assignment operators: '=', '+=', '-=', '*=', '/=', '%=' or '?='
678+
/// The token is one of the assignment operators: '=', '+=', '-=', '*=', '/=', '%=' or '??='
679679
/// </summary>
680680
AssignmentOperator = 0x00002000,
681681

src/System.Management.Automation/engine/parser/tokenizer.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4996,24 +4996,23 @@ internal Token NextToken()
49964996
case '?' when InExpressionMode():
49974997
c1 = PeekChar();
49984998

4999-
if (c1 == '?')
4999+
if (ExperimentalFeature.IsEnabled("PSNullCoalescingOperators"))
50005000
{
5001-
SkipChar();
5002-
5003-
c1 = PeekChar();
5004-
5005-
if (c1 == '=')
5001+
if (c1 == '?')
50065002
{
50075003
SkipChar();
5008-
return CheckOperatorInCommandMode(c, c1, TokenKind.QuestionQuestionEquals);
5009-
}
5010-
5011-
return CheckOperatorInCommandMode(c, c1, TokenKind.QuestionQuestion);
5012-
}
5013-
50145004

5005+
c1 = PeekChar();
50155006

5007+
if (c1 == '=')
5008+
{
5009+
SkipChar();
5010+
return CheckOperatorInCommandMode(c, c1, TokenKind.QuestionQuestionEquals);
5011+
}
50165012

5013+
return CheckOperatorInCommandMode(c, c1, TokenKind.QuestionQuestion);
5014+
}
5015+
}
50175016

50185017
return this.NewToken(TokenKind.QuestionMark);
50195018

0 commit comments

Comments
 (0)