forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTupleExpression.cs
More file actions
102 lines (83 loc) · 2.82 KB
/
TupleExpression.cs
File metadata and controls
102 lines (83 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime;
using IronPython.Runtime.Binding;
using IronPython.Runtime.Operations;
using MSAst = System.Linq.Expressions;
namespace IronPython.Compiler.Ast {
using Ast = MSAst.Expression;
public class TupleExpression : SequenceExpression {
public TupleExpression(bool expandable, params Expression[] items)
: base(items) {
IsExpandable = expandable;
}
internal override string CheckAssign() {
if (Items.Count == 0) {
// TODO: remove this when we get to 3.6
return "can't assign to ()";
}
return base.CheckAssign();
}
internal override string CheckDelete() {
if (Items.Count == 0)
return "can't delete ()"; // TODO: remove this when we get to 3.6
return base.CheckDelete();
}
public override MSAst.Expression Reduce() {
if (IsExpandable) {
return Ast.NewArrayInit(
typeof(object),
ToObjectArray(Items)
);
}
if (Items.Count == 0) {
return Ast.Field(
null,
typeof(PythonOps).GetField(nameof(PythonOps.EmptyTuple))
);
}
return Ast.Call(
AstMethods.MakeTuple,
Ast.NewArrayInit(
typeof(object),
ToObjectArray(Items)
)
);
}
public override void Walk(PythonWalker walker) {
if (walker.Walk(this)) {
if (Items != null) {
foreach (Expression e in Items) {
e.Walk(walker);
}
}
}
walker.PostWalk(this);
}
public bool IsExpandable { get; }
internal override bool IsConstant {
get {
foreach (var item in Items) {
if (!item.IsConstant) {
return false;
}
}
return true;
}
}
internal override object GetConstantValue() {
if (Items.Count == 0) {
return PythonTuple.EMPTY;
}
object[] items = new object[Items.Count];
for (int i = 0; i < items.Length; i++) {
items[i] = Items[i].GetConstantValue();
}
return PythonOps.MakeTuple(items);
}
}
}