-
Notifications
You must be signed in to change notification settings - Fork 2
/
TDSymbolNode.j
51 lines (42 loc) · 1.17 KB
/
TDSymbolNode.j
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
@import <Foundation/CPObject.j>
@implementation TDSymbolNode : CPObject
{
CPString ancestry;
TDSymbolNode parent;
CPDictionary children;
int character;
CPString string;
}
- (id)initWithParent:(TDSymbolNode)p character:(int)c
{
self = [super init];
if (self) {
parent = p;
character = c;
children = [CPDictionary dictionary];
// this private property is an optimization.
// cache the NSString for the char to prevent it being constantly recreated in -determinAncestry
string = [CPString stringWithFormat:@"%C", character];
[self determineAncestry];
}
return self;
}
- (void)determineAncestry
{
if (-1 == parent.character) { // optimization for sinlge-char symbol (parent is symbol root node)
ancestry = string;
} else {
var result = "",
n = self;
while (-1 != n.character) {
result = n.string + result;
n = n.parent;
}
self.ancestry = result; // assign an immutable copy
}
}
- (CPString)description
{
return [CPString stringWithFormat:@"<TDSymbolNode %@>", ancestry];
}
@end