-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathArrayTypes.cs
299 lines (268 loc) · 7.81 KB
/
ArrayTypes.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.Collections.Generic;
namespace DtxCS.DataTypes
{
/// <summary>
/// Represents an array of DataNodes.
/// </summary>
public class DataArray : DataNode
{
public virtual char ClosingChar => ')';
/// <summary>
/// Returns DataType.ARRAY
/// </summary>
public override DataType Type => DataType.ARRAY;
/// <summary>
/// The children of this array.
/// </summary>
public List<DataNode> Children { get; }
/// <summary>
/// Default constructor for a Data Array.
/// </summary>
public DataArray()
{
this.Children = new List<DataNode>();
}
/// <summary>
/// Add a node to this Array.
/// </summary>
/// <param name="node">Node to add</param>
/// <returns>The added node.</returns>
public T AddNode<T>(T node) where T : DataNode
{
Children.Add(node);
node.Parent = this;
return node;
}
/// <summary>
/// Get or set the child of this array at the given index.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public DataNode this[int index]
{
get { return Children[index]; }
set { Children[index] = value; }
}
/// <summary>
/// Find the array in this array's children at the given index.
/// </summary>
/// <param name="idx"></param>
/// <returns></returns>
public DataArray Array(int idx)
{
if (Children[idx].Type == DataType.ARRAY)
{
return (DataArray)Children[idx];
}
throw new Exception("Element at index " + idx + " is not an Array. It is "+Children[idx].GetType().Name);
}
/// <summary>
/// Find the integer in this array's children at the given index.
/// </summary>
/// <param name="idx"></param>
/// <returns></returns>
public int Int(int idx)
{
if (Children[idx].Type == DataType.INT)
{
return ((DataAtom)Children[idx]).Int;
}
throw new Exception("Element at index " + idx + " is not an integer.");
}
/// <summary>
/// Find the float in this array's children at the given index.
/// </summary>
/// <param name="idx"></param>
/// <returns></returns>
public float Float(int idx)
{
if (Children[idx].Type == DataType.FLOAT)
{
return ((DataAtom)Children[idx]).Float;
}
throw new Exception("Element at index " + idx + " is not a float.");
}
public float Number(int idx)
{
if (Children[idx].Type == DataType.FLOAT)
{
return ((DataAtom)Children[idx]).Float;
}
else if (Children[idx].Type == DataType.INT)
{
return ((DataAtom)Children[idx]).Int;
}
throw new Exception("Element at index " + idx + " is not a number.");
}
public DataNode Node(int idx)
{
return Children[idx];
}
/// <summary>
/// Find the string in this array's children at the given index.
/// </summary>
/// <param name="idx"></param>
/// <returns></returns>
public string String(int idx)
{
if (Children[idx].Type == DataType.STRING)
{
return ((DataAtom)Children[idx]).String;
}
throw new Exception("Element at index " + idx + " is not a string.");
}
/// <summary>
/// Find the symbol in this array's children at the given index.
/// </summary>
/// <param name="idx"></param>
/// <returns></returns>
public DataSymbol Symbol(int idx)
{
if(Children[idx].Type == DataType.SYMBOL)
{
return (DataSymbol)Children[idx];
}
throw new Exception("Element at index " + idx + " is not a symbol.");
}
/// <summary>
/// Find the variable in this array's children at the given index.
/// </summary>
/// <param name="idx"></param>
/// <returns></returns>
public DataVariable Var(int idx)
{
if (Children[idx].Type == DataType.VARIABLE)
{
return (DataVariable)Children[idx];
}
throw new Exception("Element at index " + idx + " is not a variable.");
}
/// <summary>
/// Finds any node at the given index and returns it as a string.
/// </summary>
/// <param name="idx"></param>
/// <returns></returns>
public string Any(int idx)
{
return Children[idx].Name;
}
/// <summary>
/// Find the first array in this array's children whose name matches.
/// If none is found, returns null.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public DataArray Array(string name)
{
for (int i = 0; i < Children.Count; i++)
{
if (!(Children[i] is DataArray)) continue;
if (Children[i].Name == name)
{
return ((DataArray)Children[i]);
}
}
return null;
}
public override DataNode Evaluate()
{
var returnArray = new DataArray();
foreach(var node in Children)
{
returnArray.AddNode(node.Evaluate());
}
return returnArray;
}
/// <summary>
/// The string representation of the first element of the array, unless
/// that element is another array, which would result in an empty string.
/// </summary>
public override string Name => Children[0].Type == DataType.ARRAY ?
"" : Children[0].Name;
/// <summary>
/// The number of elements in this array.
/// </summary>
public int Count => Children.Count;
/// <summary>
/// The string representation of this Array, suitable for putting right
/// back into a .dta file.
/// </summary>
/// <returns></returns>
public override string ToString() => ToString(0);
public override string ToString(int depth)
{
string ret = new string(' ', depth*3) + "(";
for (int i = 0; i < Children.Count; i++)
{
var n = Children[i];
ret += n is DataArray ? Environment.NewLine + n.ToString(depth + 1) : n.ToString(depth + 1);
if (i + 1 != Children.Count) ret += " ";
}
ret += ")";
return ret;
}
}
public class DataCommand : DataArray
{
public override DataType Type => DataType.COMMAND;
public override char ClosingChar => '}';
public DataCommand() : base()
{
}
public override string ToString() => ToString(0);
public override string ToString(int depth)
{
string ret = new string(' ', depth*3)+"{";
foreach (DataNode n in Children)
{
ret += (n is DataArray ? Environment.NewLine + n.ToString(depth + 1) : n.ToString(depth + 1) + " ");
}
ret += "}";
return ret;
}
/// <summary>
/// Returns a copy of this datacommand with all children evaulated.
/// </summary>
public DataCommand EvalAll()
{
var ret = new DataCommand();
for(var i = 0; i < Children.Count; i++)
{
ret.AddNode(Children[i].Evaluate());
}
return ret;
}
public override DataNode Evaluate()
{
Func<DataCommand, DataNode> f;
if (Builtins.Funcs.TryGetValue(Symbol(0), out f))
{
return f.Invoke(this);
}
throw new Exception($"Func '{Any(0)}' is not defined.");
}
}
// e.g., [RED 1 0 0] in a file, then (color RED) -> (color 1 0 0)
// or, [TRUE 1] -> (happy TRUE) -> (happy 1)
public class DataMacroDefinition : DataArray
{
public override DataType Type => DataType.MACRO;
public override char ClosingChar => ']';
public DataMacroDefinition() : base()
{
}
public override string ToString() => ToString(0);
public override DataNode Evaluate() => this;
public override string ToString(int depth)
{
string ret = new string(' ', depth*3) + "[";
foreach (DataNode n in Children)
{
ret += (n is DataArray ? Environment.NewLine + n.ToString(depth + 1) : n.ToString(depth + 1) + " ");
}
ret += "]";
return ret;
}
}
}