-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatKinds.cs
569 lines (488 loc) · 15 KB
/
CatKinds.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/// Dedicated to the public domain by Christopher Diggins
/// http://creativecommons.org/licenses/publicdomain/
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Cat
{
/// <summary>
/// All CatKinds should be immutable. This avoids a lot of problems and confusion.
/// </summary>
abstract public class CatKind
{
public static int gnId = 0;
public static CatKind Create(AstType node)
{
if (node is AstSimpleType)
{
string s = node.ToString();
Trace.Assert(s.Length > 0);
if (s.Length > 1 && s[0] == '_')
return CatCustomKind.GetCustomKind(s);
else
return new CatSimpleTypeKind(s);
}
else if (node is AstTypeVar)
{
return new CatTypeVar(node.ToString());
}
else if (node is AstStackVar)
{
return new CatStackVar(node.ToString());
}
else if (node is AstFxnType)
{
return new CatFxnType(node as AstFxnType);
}
else
{
throw new Exception("unrecognized kind " + node.ToString());
}
}
public CatKind()
{
}
public override string ToString()
{
throw new Exception("ToString must be overridden");
}
public virtual string ToIdString()
{
return ToString();
}
public abstract bool Equals(CatKind k);
public virtual bool IsSubtypeOf(CatKind k)
{
if (k.ToString().Equals("any"))
return true;
return this.Equals(k);
}
public bool IsKindVar()
{
return (this is CatTypeVar) || (this is CatStackVar);
}
public static string TypeNameFromObject(Object o)
{
if (o is HashList) return "hash_list";
if (o is CatList) return "list";
if (o is Boolean) return "bool";
if (o is int) return "int";
if (o is Double) return "double";
if (o is string) return "string";
if (o is Byte) return "byte";
if (o is Primitives.Bit) return "bit";
if (o is Function) return (o as Function).GetFxnTypeString();
if (o is Char) return "char";
return "any";
}
public static string TypeToString(Type t)
{
// TODO: fix this up. I don't like where it is.
switch (t.Name)
{
case ("HashList"): return "hash_list";
case ("Int32"): return "int";
case ("Double"): return "double";
case ("FList"): return "list";
case ("Object"): return "any";
case ("Function"): return "fun";
case ("Boolean"): return "bool";
case ("String"): return "string";
case ("Char"): return "char";
default: return t.Name;
}
}
public static CatKind GetKindFromObject(object o)
{
if (o is CatObject) return (o as CatObject).GetClass();
if (o is Function) return (o as Function).GetFxnType();
return new CatSimpleTypeKind(TypeNameFromObject(o));
}
public virtual bool IsAny()
{
return false;
}
public virtual bool IsDynFxn()
{
return false;
}
public abstract IEnumerable<CatKind> GetChildKinds();
public abstract IEnumerable<CatKind> GetDescendantKinds();
}
/// <summary>
/// Base class for the different Cat types
/// </summary>
public abstract class CatTypeKind : CatKind
{
public CatTypeKind()
{ }
}
public class CatSimpleTypeKind : CatTypeKind
{
string msName;
public CatSimpleTypeKind(string s)
{
msName = s;
}
public override string ToString()
{
return msName;
}
public override bool Equals(CatKind k)
{
return (k is CatSimpleTypeKind) && (msName == k.ToString());
}
public override bool IsSubtypeOf(CatKind k)
{
if (Equals(k))
return true;
// meta_int is a subtype of int
// and meta_bool is a subtype of bool
// and so on.
if (msName.IndexOf("meta_") == 0)
{
string s = k.ToString();
if (s.Length == 0)
throw new Exception("missing type name");
if (msName.IndexOf(s) == 5)
return true;
}
return false;
}
public override bool IsAny()
{
return msName.Equals("any");
}
public override bool IsDynFxn()
{
return msName.Equals("fun");
}
public override IEnumerable<CatKind> GetChildKinds()
{
yield return this;
}
public override IEnumerable<CatKind> GetDescendantKinds()
{
yield return this;
}
}
public class CatTypeVar : CatTypeKind
{
string msName;
public CatTypeVar(string s)
{
msName = s;
}
public override string ToString()
{
return "'" + msName;
}
public static CatTypeVar CreateUnique()
{
return new CatTypeVar("t$" + (gnId++).ToString());
}
public override bool Equals(CatKind k)
{
if (!(k is CatTypeVar))
return false;
return ToString().CompareTo(k.ToString()) == 0;
}
public override IEnumerable<CatKind> GetChildKinds()
{
yield return this;
}
public override IEnumerable<CatKind> GetDescendantKinds()
{
yield return this;
}
}
public abstract class CatStackKind : CatKind
{
}
public class CatTypeVector : CatStackKind
{
List<CatKind> mList;
public CatTypeVector(AstStack node)
{
mList = new List<CatKind>();
foreach (AstType tn in node.mTypes)
mList.Add(Create(tn));
}
public CatTypeVector()
{
mList = new List<CatKind>();
}
public CatTypeVector(CatTypeVector k)
{
mList = new List<CatKind>(k.mList);
}
public CatTypeVector(List<CatKind> list)
{
mList = new List<CatKind>(list);
}
/// <summary>
/// This is a reversed stack, position [0] is the bottom.
/// </summary>
public List<CatKind> GetKinds()
{
return mList;
}
public IEnumerable<CatKind> GetRevKinds()
{
for (int i = mList.Count - 1; i >= 0; --i)
yield return mList[i];
}
public void Add(CatKind k)
{
Trace.Assert(k != null);
if (k is CatTypeVector)
mList.AddRange((k as CatTypeVector).GetKinds()); else
mList.Add(k);
}
public void PushKindBottom(CatKind k)
{
Trace.Assert(k != null);
if (k is CatTypeVector)
mList.InsertRange(0, (k as CatTypeVector).GetKinds()); else
mList.Insert(0, k);
}
public bool IsEmpty()
{
return mList.Count == 0;
}
public CatKind GetBottom()
{
if (mList.Count > 0)
return mList[0]; else
return null;
}
public CatKind GetTop()
{
if (mList.Count > 0)
return mList[mList.Count - 1]; else
return null;
}
public CatTypeVector GetRest()
{
return new CatTypeVector(mList.GetRange(0, mList.Count - 1));
}
public override string ToString()
{
string ret = "";
foreach (CatKind k in mList)
ret += " " + k.ToString();
if (mList.Count > 0)
return ret.Substring(1); else
return "";
}
public override string ToIdString()
{
string ret = "";
foreach (CatKind k in mList)
ret += " " + k.ToIdString();
if (mList.Count > 0)
return ret.Substring(1); else
return "";
}
public override bool Equals(CatKind k)
{
if (!(k is CatTypeVector))
return false;
CatTypeVector v1 = this;
CatTypeVector v2 = k as CatTypeVector;
while (!v1.IsEmpty() && !v2.IsEmpty())
{
CatKind t1 = v1.GetTop();
CatKind t2 = v2.GetTop();
if (!t1.Equals(t2))
return false;
v1 = v1.GetRest();
v2 = v2.GetRest();
}
if (!v1.IsEmpty())
return false;
if (!v2.IsEmpty())
return false;
return true;
}
public override bool IsSubtypeOf(CatKind k)
{
if (k.IsAny())
return true;
if (k is CatStackVar)
return true;
if (!(k is CatTypeVector))
return false;
CatTypeVector v1 = this;
CatTypeVector v2 = k as CatTypeVector;
while (!v1.IsEmpty() && !v2.IsEmpty())
{
CatKind t1 = v1.GetTop();
CatKind t2 = v2.GetTop();
if (!t1.IsSubtypeOf(t2))
return false;
v1 = v1.GetRest();
v2 = v2.GetRest();
}
// v1 has to be at least as long to be a subtype
if (v1.IsEmpty() && !v2.IsEmpty())
return false;
return true;
}
public CatTypeVector Clone()
{
CatTypeVector ret = new CatTypeVector();
foreach (CatKind k in GetKinds())
ret.Add(k);
return ret;
}
public void RemoveBottom()
{
GetKinds().RemoveAt(0);
}
public override IEnumerable<CatKind> GetChildKinds()
{
foreach (CatKind k in GetKinds())
yield return k;
}
public override IEnumerable<CatKind> GetDescendantKinds()
{
foreach (CatKind k in GetKinds())
foreach (CatKind j in k.GetDescendantKinds())
yield return j;
}
public bool IsValid()
{
foreach (CatKind k in GetKinds())
if (k is CatTypeVector)
return false;
return true;
}
}
public class CatStackVar : CatStackKind
{
string msName;
public CatStackVar(string s)
{
msName = s;
}
public override string ToString()
{
return "'" + msName;
}
public static CatStackVar CreateUnique()
{
return new CatStackVar("R$" + (gnId++).ToString());
}
public override bool Equals(CatKind k)
{
if (!(k is CatStackVar)) return false;
return k.ToString() == this.ToString();
}
public override IEnumerable<CatKind> GetChildKinds()
{
yield return this;
}
public override IEnumerable<CatKind> GetDescendantKinds()
{
yield return this;
}
}
public class CatCustomKind : CatKind
{
int mnId;
static List<CatCustomKind> gpPool = new List<CatCustomKind>();
public CatCustomKind()
{
mnId = gpPool.Count;
gpPool.Add(this);
}
public int GetId()
{
return mnId;
}
public override string ToString()
{
return "_" + mnId.ToString();
}
public static CatCustomKind GetCustomKind(int n)
{
return gpPool[n];
}
public static CatCustomKind GetCustomKind(string s)
{
Trace.Assert(s.Length > 1);
Trace.Assert(s[0] == '_');
int n = Int32.Parse(s.Substring(1));
return GetCustomKind(n);
}
public override bool Equals(CatKind k)
{
if (!(k is CatCustomKind))
return false;
if ((k as CatCustomKind).GetId() == GetId())
{
Trace.Assert(k == this);
return true;
}
else
{
return false;
}
}
public override IEnumerable<CatKind> GetChildKinds()
{
yield return this;
}
public override IEnumerable<CatKind> GetDescendantKinds()
{
yield return this;
}
}
public class CatMetaValue<T> : CatCustomKind
{
T mData;
CatSimpleTypeKind mpSuperType = new CatSimpleTypeKind(TypeToString(typeof(T)));
public CatMetaValue(T x)
{
mData = x;
}
public T GetData()
{
return mData;
}
public override bool Equals(CatKind k)
{
if (k == this)
return true;
if (!(k is CatMetaValue<T>))
return false;
CatMetaValue<T> tmp = k as CatMetaValue<T>;
return tmp.GetData().Equals(mData);
}
public CatKind GetSuperType()
{
return mpSuperType;
}
public override bool IsSubtypeOf(CatKind k)
{
if (k is CatSimpleTypeKind)
return GetSuperType().IsSubtypeOf(k);
return this.Equals(k);
}
public override IEnumerable<CatKind> GetChildKinds()
{
yield return this;
}
public override IEnumerable<CatKind> GetDescendantKinds()
{
yield return this;
}
public override string ToString()
{
//return "meta_" + TypeToString(typeof(T));
return mData.ToString();
}
}
}