-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTElement.cs
53 lines (43 loc) · 1.37 KB
/
ASTElement.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
using System.Collections.Generic;
using System.Linq;
namespace MiniC {
public abstract class ASTElement {
private List<ASTElement>[] children = null;
public List<ASTElement> Parents = new List<ASTElement>();
public readonly int Serial;
private static int counter = 0;
public string Name { get; set; }
public abstract string[] ContextNames { get; }
public IEnumerable<ASTElement> GetChildren() {
return children.SelectMany(t => t);
}
public IEnumerable<ASTElement> GetChildren(int context) {
return children[context];
}
protected ASTElement(int context) {
Serial = counter++;
Name = GenerateNodeName();
if (context == 0) return;
children = new List<ASTElement>[context];
for (int i = 0; i < context; i++) {
children[i] = new List<ASTElement>();
}
}
public void AddChild(ASTElement child, int contextIndex) {
child.Parents.Add(this);
children[contextIndex].Add(child);
}
public ASTElement GetChild(int context, int index) {
return children[context][index];
}
public int ContextNumber() {
return children.Length;
}
public int ChildrenNumber(int context) {
return children[context].Count;
}
public virtual string GenerateNodeName() {
return "_" + Serial;
}
}
}