-
Notifications
You must be signed in to change notification settings - Fork 2
/
TDAssembly.j
162 lines (127 loc) · 3.15 KB
/
TDAssembly.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
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
@import <Foundation/Foundation.j>
TDAssemblyDefaultDelimiter = @"/";
@implementation TDAssembly : CPObject
{
CPArray stack;
id target;
unsigned index;
CPString string;
CPString defaultDelimiter;
}
+ (id)assemblyWithString:(CPString)s
{
return [[self alloc] initWithString:s];
}
- (id)init
{
return [self initWithString:nil];
}
- (id)initWithString:(CPString)s
{
if (self = [super init]) {
stack = [];
string = s;
}
return self;
}
// this private intializer exists simply to improve the performance of the -copyWithZone: method.
// note flow *does not* go thru the designated initializer above. however, that ugliness is worth it cuz
// the perf of -copyWithZone: in this class is *vital* to the framework's performance
- (id)_init
{
return [super init];
}
// this method diverges from coding standards cuz it is vital to the entire framework's performance
- (id)copy
{
var a = [[[self class] alloc] _init];
a.stack = [stack copy];
a.string = string;
a.index = index;
if (defaultDelimiter)
a.defaultDelimiter = defaultDelimiter;
if (target)
a.target = [target copy]
return a;
}
- (id)next {
//NSAssert1(0, @"-[TDAssembly %s] must be overriden", _cmd);
return nil;
}
- (BOOL)hasMore {
//NSAssert1(0, @"-[TDAssembly %s] must be overriden", _cmd);
return NO;
}
- (CPString)consumedObjectsJoinedByString:(CPString)delimiter {
//NSAssert1(0, @"-[TDAssembly %s] must be overriden", _cmd);
return nil;
}
- (CPString)remainingObjectsJoinedByString:(CPString)delimiter {
//NSAssert1(0, @"-[TDAssembly %s] must be overriden", _cmd);
return nil;
}
- (unsigned)length {
//NSAssert1(0, @"-[TDAssembly %s] must be overriden", _cmd);
return 0;
}
- (unsigned)objectsConsumed {
//NSAssert1(0, @"-[TDAssembly %s] must be overriden", _cmd);
return 0;
}
- (unsigned)objectsRemaining {
//NSAssert1(0, @"-[TDAssembly %s] must be overriden", _cmd);
return 0;
}
- (id)peek {
//NSAssert1(0, @"-[TDAssembly %s] must be overriden", _cmd);
return nil;
}
- (id)pop
{
var result = nil;
if (stack.length) {
result = [stack lastObject];
[stack removeLastObject];
}
return result;
}
- (void)push:(id)object
{
if (object) {
[stack addObject:object];
}
}
- (BOOL)isStackEmpty {
return 0 === stack.length;
}
- (CPArray)objectsAbove:(id)fence
{
var result = [];
while (stack.count) {
var obj = [self pop];
if ([obj isEqual:fence]) {
[self push:obj];
break;
} else {
[result addObject:obj];
}
}
return result;
}
- (CPString)description
{
var s = "[",
i = 0,
length = stack.count;
for (;i<length;i++) {
s = s + [obj description];
if (len - 1 !== i++) {
s = s + ", ";
}
}
s = s + "]";
var d = defaultDelimiter ? defaultDelimiter : TDAssemblyDefaultDelimiter;
s = s + [self consumedObjectsJoinedByString:d] + "^" + [self remainingObjectsJoinedByString:d];
return s;
}
@end