Skip to content

Commit 78442d0

Browse files
gpetrouslozier
authored andcommitted
Use pattern matching (#596)
1 parent d6c4bec commit 78442d0

37 files changed

Lines changed: 120 additions & 244 deletions

Src/IronPython/Compiler/Ast/AssignmentStatement.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ private MSAst.Expression AssignComplex(MSAst.Expression right) {
9292
private MSAst.Expression AssignOne() {
9393
Debug.Assert(_left.Length == 1);
9494

95-
SequenceExpression seLeft = _left[0] as SequenceExpression;
96-
SequenceExpression seRight = _right as SequenceExpression;
97-
98-
if (seLeft != null && seRight != null && seLeft.Items.Count == seRight.Items.Count) {
95+
if (_left[0] is SequenceExpression seLeft &&
96+
_right is SequenceExpression seRight &&
97+
seLeft.Items.Count == seRight.Items.Count) {
9998
int cnt = seLeft.Items.Count;
10099

101100
// a, b = 1, 2, or [a,b] = 1,2 - not something like a, b = range(2)

Src/IronPython/Compiler/Ast/BinaryExpression.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ private bool NeedComparisonTransformation() {
7070
return IsComparison() && IsComparison(_right);
7171
}
7272

73-
public static bool IsComparison(Expression expression) {
74-
BinaryExpression be = expression as BinaryExpression;
75-
return be != null && be.IsComparison();
76-
}
73+
public static bool IsComparison(Expression expression) => expression is BinaryExpression be && be.IsComparison();
7774

7875
// This is a compound comparison operator like: a < b < c.
7976
// That's represented as binary operators, but it's not the same as (a<b) < c, so we do special transformations.

Src/IronPython/Compiler/Ast/ClassDefinition.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ private string FindSelfNames() {
299299
if (stmts == null) return "";
300300

301301
foreach (Statement stmt in stmts.Statements) {
302-
FunctionDefinition def = stmt as FunctionDefinition;
303-
if (def != null && def.Name == "__init__") {
302+
if (stmt is FunctionDefinition def && def.Name == "__init__") {
304303
return string.Join(",", SelfNameFinder.FindNames(def));
305304
}
306305
}
@@ -353,8 +352,7 @@ public override bool Walk(FunctionDefinition node) {
353352

354353
public override bool Walk(AssignmentStatement node) {
355354
foreach (Expression lhs in node.Left) {
356-
MemberExpression me = lhs as MemberExpression;
357-
if (me != null) {
355+
if (lhs is MemberExpression me) {
358356
if (IsSelfReference(me.Target)) {
359357
_names[me.Name] = true;
360358
}

Src/IronPython/Compiler/Ast/ExpressionStatement.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public override void Walk(PythonWalker walker) {
4747

4848
public override string Documentation {
4949
get {
50-
ConstantExpression ce = _expression as ConstantExpression;
51-
if (ce != null) {
50+
if (_expression is ConstantExpression ce) {
5251
return ce.Value as string;
5352
}
5453
return null;

Src/IronPython/Compiler/Ast/ForStatement.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ internal override bool CanThrow {
169169
}
170170

171171
// most constants (int, float, long, etc...) will throw here
172-
ConstantExpression ce = _list as ConstantExpression;
173-
if (ce != null) {
172+
if (_list is ConstantExpression ce) {
174173
if (ce.Value is string) {
175174
return false;
176175
}

Src/IronPython/Compiler/Ast/FromImportStatement.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ public override MSAst.Expression Reduce() {
123123
}
124124

125125
private object GetLevel() {
126-
RelativeModuleName rmn = _root as RelativeModuleName;
127-
if (rmn != null) {
126+
if (_root is RelativeModuleName rmn) {
128127
return rmn.DotCount;
129128
}
130129

Src/IronPython/Compiler/Ast/FunctionDefinition.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -451,14 +451,12 @@ private static void CompileAssignment(LightCompiler compiler, MSAst.Expression v
451451
return;
452452
}
453453

454-
MSAst.ParameterExpression functionValueParam = variable as MSAst.ParameterExpression;
455-
if (functionValueParam != null) {
454+
if (variable is MSAst.ParameterExpression functionValueParam) {
456455
instructions.EmitStoreLocal(compiler.Locals.GetLocalIndex(functionValueParam));
457456
return;
458457
}
459458

460-
var globalVar = variable as PythonGlobalVariableExpression;
461-
if (globalVar != null) {
459+
if (variable is PythonGlobalVariableExpression globalVar) {
462460
instructions.Emit(new PythonSetGlobalInstruction(globalVar.Global));
463461
instructions.EmitPop();
464462
return;
@@ -575,8 +573,7 @@ private LightLambdaExpression CreateFunctionLambda() {
575573
List<MSAst.Expression> init = new List<MSAst.Expression>();
576574

577575
foreach (var param in _parameters) {
578-
IPythonVariableExpression pyVar = GetVariableExpression(param.PythonVariable) as IPythonVariableExpression;
579-
if (pyVar != null) {
576+
if (GetVariableExpression(param.PythonVariable) is IPythonVariableExpression pyVar) {
580577
var varInit = pyVar.Create();
581578
if (varInit != null) {
582579
init.Add(varInit);
@@ -848,8 +845,7 @@ internal class ArbitraryGlobalsVisitor : MSAst.ExpressionVisitor {
848845
protected override MSAst.Expression VisitExtension(MSAst.Expression node) {
849846

850847
// update the global get/set/raw gets variables
851-
var global = node as PythonGlobalVariableExpression;
852-
if (global != null) {
848+
if (node is PythonGlobalVariableExpression global) {
853849
return new LookupGlobalVariable(
854850
PythonAst._globalContext,
855851
global.Variable.Name,
@@ -858,8 +854,7 @@ protected override MSAst.Expression VisitExtension(MSAst.Expression node) {
858854
}
859855

860856
// set covers sets and deletes
861-
var setGlobal = node as PythonSetGlobalVariableExpression;
862-
if (setGlobal != null) {
857+
if (node is PythonSetGlobalVariableExpression setGlobal) {
863858
if (setGlobal.Value == PythonGlobalVariableExpression.Uninitialized) {
864859
return new LookupGlobalVariable(
865860
PythonAst._globalContext,

Src/IronPython/Compiler/Ast/IndexExpression.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ public override MSAst.Expression Reduce() {
4141
}
4242

4343
private MSAst.Expression[] GetActionArgumentsForGetOrDelete() {
44-
TupleExpression te = _index as TupleExpression;
45-
if (te != null && te.IsExpandable) {
44+
if (_index is TupleExpression te && te.IsExpandable) {
4645
return ArrayUtils.Insert(_target, te.Items);
4746
}
4847

49-
SliceExpression se = _index as SliceExpression;
50-
if (se != null) {
48+
if (_index is SliceExpression se) {
5149
if (se.StepProvided) {
5250
return new[] {
5351
_target,

Src/IronPython/Compiler/Ast/Node.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,9 @@ internal static MSAst.Expression TransformMaybeSingleLineSuite(Statement body, S
312312
}
313313

314314
internal static MSAst.Expression RemoveDebugInfo(int prevStart, MSAst.Expression res) {
315-
MSAst.BlockExpression block = res as MSAst.BlockExpression;
316-
if (block != null && block.Expressions.Count > 0) {
317-
MSAst.DebugInfoExpression dbgInfo = block.Expressions[0] as MSAst.DebugInfoExpression;
315+
if (res is BlockExpression block && block.Expressions.Count > 0) {
318316
// body on the same line as an if, don't generate a 2nd sequence point
319-
if (dbgInfo != null && dbgInfo.StartLine == prevStart) {
317+
if (block.Expressions[0] is DebugInfoExpression dbgInfo && dbgInfo.StartLine == prevStart) {
320318
// we remove the debug info based upon how it's generated in DebugStatement.AddDebugInfo which is
321319
// the helper method which adds the debug info.
322320
if (block.Type == typeof(void)) {
@@ -351,8 +349,7 @@ internal static MSAst.Expression RemoveFrame(MSAst.Expression expression) {
351349

352350
class FramedCodeVisitor : ExpressionVisitor {
353351
public override MSAst.Expression Visit(MSAst.Expression node) {
354-
FramedCodeExpression framedCode = node as FramedCodeExpression;
355-
if (framedCode != null) {
352+
if (node is FramedCodeExpression framedCode) {
356353
return framedCode.Body;
357354
}
358355
return base.Visit(node);
@@ -443,17 +440,15 @@ internal MSAst.Expression MakeAssignment(MSAst.ParameterExpression variable, MSA
443440
Debug.Assert(expression != null);
444441
Debug.Assert(value != null);
445442

446-
IPythonVariableExpression pyGlobal = expression as IPythonVariableExpression;
447-
if (pyGlobal != null) {
443+
if (expression is IPythonVariableExpression pyGlobal) {
448444
return pyGlobal.Assign(value);
449445
}
450446

451447
return Ast.Assign(expression, value);
452448
}
453449

454450
internal static MSAst.Expression/*!*/ Delete(MSAst.Expression/*!*/ expression) {
455-
IPythonVariableExpression pyGlobal = expression as IPythonVariableExpression;
456-
if (pyGlobal != null) {
451+
if (expression is IPythonVariableExpression pyGlobal) {
457452
return pyGlobal.Delete();
458453
}
459454

0 commit comments

Comments
 (0)