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
|
const ForthInterpreter = require('./forth.js');
console.log('๐งช Comprehensive Cross-Stack Operations Test Suite\n');
let state = ForthInterpreter.createInitialState();
let testCount = 0;
let passCount = 0;
// Test helper function
const test = (name, testFn) => {
testCount++;
try {
testFn();
console.log(`โ
${name}`);
passCount++;
} catch (error) {
console.log(`โ ${name}: ${error.message}`);
}
};
// Test 1: Basic Cross-Stack Operations
test('dup.stacks - Basic Functionality', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack
state = ForthInterpreter.parseAndExecute(state, 'focus.red');
state = ForthInterpreter.parseAndExecute(state, '42');
// Execute dup.stacks
state = ForthInterpreter.parseAndExecute(state, 'dup.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
if (state.crossStackOperation !== 'dup') throw new Error('Should set operation to dup');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '2'); // Target Teal stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[0].length !== 1) throw new Error('Source stack should still have 1 item');
if (state.stacks[1].length !== 1) throw new Error('Target stack should have 1 item');
if (state.stacks[0][state.stacks[0].length - 1] !== 42) throw new Error('Source stack should still have 42');
if (state.stacks[1][state.stacks[1].length - 1] !== 42) throw new Error('Target stack should have 42');
});
test('over.stacks - Copy Second Item', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack with multiple items
state = ForthInterpreter.parseAndExecute(state, 'focus.blue');
state = ForthInterpreter.parseAndExecute(state, '10');
state = ForthInterpreter.parseAndExecute(state, '20');
state = ForthInterpreter.parseAndExecute(state, '30');
// Execute over.stacks
state = ForthInterpreter.parseAndExecute(state, 'over.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '1'); // Target Red stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[2].length !== 3) throw new Error('Source stack should still have 3 items');
if (state.stacks[0].length !== 1) throw new Error('Target stack should have 1 item');
if (state.stacks[0][state.stacks[0].length - 1] !== 20) throw new Error('Target stack should have second item (20)');
});
test('swap.stacks - Swap Top Items', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack
state = ForthInterpreter.parseAndExecute(state, 'focus.yellow');
state = ForthInterpreter.parseAndExecute(state, '100');
state = ForthInterpreter.parseAndExecute(state, '200');
// Execute swap.stacks
state = ForthInterpreter.parseAndExecute(state, 'swap.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '3'); // Target Blue stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[3].length !== 2) throw new Error('Source stack should still have 2 items');
if (state.stacks[2].length !== 2) throw new Error('Target stack should have 2 items');
if (state.stacks[3][state.stacks[3].length - 1] !== 200) throw new Error('Source stack top should be 200');
if (state.stacks[3][state.stacks[3].length - 2] !== 100) throw new Error('Source stack second should be 100');
if (state.stacks[2][state.stacks[2].length - 1] !== 100) throw new Error('Target stack top should be 100');
if (state.stacks[2][state.stacks[2].length - 2] !== 200) throw new Error('Target stack second should be 200');
});
test('nip.stacks - Move Second Item', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack
state = ForthInterpreter.parseAndExecute(state, 'focus.teal');
state = ForthInterpreter.parseAndExecute(state, '50');
state = ForthInterpreter.parseAndExecute(state, '60');
// Execute nip.stacks
state = ForthInterpreter.parseAndExecute(state, 'nip.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '4'); // Target Yellow stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[1].length !== 1) throw new Error('Source stack should have 1 item after nip');
if (state.stacks[3].length !== 1) throw new Error('Target stack should have 1 item');
if (state.stacks[1][state.stacks[1].length - 1] !== 60) throw new Error('Source stack should keep top item (60)');
if (state.stacks[3][state.stacks[3].length - 1] !== 50) throw new Error('Target stack should have second item (50)');
});
test('tuck.stacks - Tuck Operation', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack
state = ForthInterpreter.parseAndExecute(state, 'focus.red');
state = ForthInterpreter.parseAndExecute(state, '7');
state = ForthInterpreter.parseAndExecute(state, '8');
// Execute tuck.stacks
state = ForthInterpreter.parseAndExecute(state, 'tuck.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '2'); // Target Teal stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[0].length !== 1) throw new Error('Source stack should have 1 item after tuck');
if (state.stacks[1].length !== 3) throw new Error('Target stack should have 3 items');
if (state.stacks[0][state.stacks[0].length - 1] !== 7) throw new Error('Source stack should keep top item (7)');
if (state.stacks[1][state.stacks[1].length - 1] !== 8) throw new Error('Target stack should have 8 at top');
if (state.stacks[1][state.stacks[1].length - 2] !== 7) throw new Error('Target stack should have 7 in middle');
if (state.stacks[1][state.stacks[1].length - 3] !== 8) throw new Error('Target stack should have 8 at bottom');
});
test('rot.stacks - Rotate Top 3 Items', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack
state = ForthInterpreter.parseAndExecute(state, 'focus.blue');
state = ForthInterpreter.parseAndExecute(state, '1');
state = ForthInterpreter.parseAndExecute(state, '2');
state = ForthInterpreter.parseAndExecute(state, '3');
// Execute rot.stacks
state = ForthInterpreter.parseAndExecute(state, 'rot.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '1'); // Target Red stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[2].length !== 3) throw new Error('Source stack should still have 3 items');
if (state.stacks[0].length !== 3) throw new Error('Target stack should have 3 items');
// Source stack should be rotated: [1, 3, 2] (top=2, second=3, third=1)
if (state.stacks[2][state.stacks[2].length - 1] !== 2) throw new Error('Source stack top should be 2');
if (state.stacks[2][state.stacks[2].length - 2] !== 3) throw new Error('Source stack second should be 3');
if (state.stacks[2][state.stacks[2].length - 3] !== 1) throw new Error('Source stack third should be 1');
// Target stack should have rotated items: [1, 3, 2] (top=2, second=3, third=1)
if (state.stacks[0][state.stacks[0].length - 1] !== 2) throw new Error('Target stack top should be 2');
if (state.stacks[0][state.stacks[0].length - 2] !== 3) throw new Error('Target stack second should be 3');
if (state.stacks[0][state.stacks[0].length - 3] !== 1) throw new Error('Target stack third should be 1');
});
test('2dup.stacks - Duplicate Top 2 Items', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack
state = ForthInterpreter.parseAndExecute(state, 'focus.yellow');
state = ForthInterpreter.parseAndExecute(state, '25');
state = ForthInterpreter.parseAndExecute(state, '35');
// Execute 2dup.stacks
state = ForthInterpreter.parseAndExecute(state, '2dup.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '3'); // Target Blue stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[3].length !== 2) throw new Error('Source stack should still have 2 items');
if (state.stacks[2].length !== 2) throw new Error('Target stack should have 2 items');
if (state.stacks[3][state.stacks[3].length - 1] !== 35) throw new Error('Source stack top should be 35');
if (state.stacks[3][state.stacks[3].length - 2] !== 25) throw new Error('Source stack second should be 25');
if (state.stacks[2][state.stacks[2].length - 1] !== 35) throw new Error('Target stack top should be 35');
if (state.stacks[2][state.stacks[2].length - 2] !== 25) throw new Error('Target stack second should be 25');
});
test('2over.stacks - Copy Second Pair', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack with 4 items
state = ForthInterpreter.parseAndExecute(state, 'focus.red');
state = ForthInterpreter.parseAndExecute(state, '10');
state = ForthInterpreter.parseAndExecute(state, '20');
state = ForthInterpreter.parseAndExecute(state, '30');
state = ForthInterpreter.parseAndExecute(state, '40');
// Execute 2over.stacks
state = ForthInterpreter.parseAndExecute(state, '2over.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '2'); // Target Teal stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[0].length !== 4) throw new Error('Source stack should still have 4 items');
if (state.stacks[1].length !== 2) throw new Error('Target stack should have 2 items');
if (state.stacks[1][state.stacks[1].length - 1] !== 20) throw new Error('Target stack top should be 20');
if (state.stacks[1][state.stacks[1].length - 2] !== 30) throw new Error('Target stack second should be 30');
});
test('2swap.stacks - Swap Top 2 Pairs', () => {
state = ForthInterpreter.createInitialState();
// Set up source stack with 4 items
state = ForthInterpreter.parseAndExecute(state, 'focus.teal');
state = ForthInterpreter.parseAndExecute(state, '1');
state = ForthInterpreter.parseAndExecute(state, '2');
state = ForthInterpreter.parseAndExecute(state, '3');
state = ForthInterpreter.parseAndExecute(state, '4');
// Execute 2swap.stacks
state = ForthInterpreter.parseAndExecute(state, '2swap.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Specify target stack
state = ForthInterpreter.parseAndExecute(state, '1'); // Target Red stack
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
// Verify results
if (state.stacks[1].length !== 4) throw new Error('Source stack should still have 4 items');
if (state.stacks[0].length !== 4) throw new Error('Target stack should have 4 items');
// Source stack should be: [1, 2, 3, 4] (top=4, second=3, third=2, fourth=1)
if (state.stacks[1][state.stacks[1].length - 1] !== 4) throw new Error('Source stack top should be 4');
if (state.stacks[1][state.stacks[1].length - 2] !== 3) throw new Error('Source stack second should be 3');
if (state.stacks[1][state.stacks[1].length - 3] !== 2) throw new Error('Source stack third should be 2');
if (state.stacks[1][state.stacks[1].length - 4] !== 1) throw new Error('Source stack fourth should be 1');
// Target stack should be: [1, 2, 3, 4] (top=4, second=3, third=2, fourth=1)
if (state.stacks[0][state.stacks[0].length - 1] !== 4) throw new Error('Target stack top should be 4');
if (state.stacks[0][state.stacks[0].length - 2] !== 3) throw new Error('Target stack second should be 3');
if (state.stacks[0][state.stacks[0].length - 3] !== 2) throw new Error('Target stack third should be 2');
if (state.stacks[0][state.stacks[0].length - 4] !== 1) throw new Error('Target stack fourth should be 1');
});
// Test 2: Error Handling
test('Error Handling - Stack Underflow', () => {
state = ForthInterpreter.createInitialState();
// Try dup.stacks on empty stack
state = ForthInterpreter.parseAndExecute(state, 'focus.red');
state = ForthInterpreter.parseAndExecute(state, 'dup.stacks');
const lastOutput = state.output[state.output.length - 1];
if (!lastOutput.includes('Error: Stack underflow')) {
throw new Error('Should show stack underflow error');
}
});
test('Error Handling - Invalid Target Stack', () => {
state = ForthInterpreter.createInitialState();
// Set up valid operation
state = ForthInterpreter.parseAndExecute(state, 'focus.red');
state = ForthInterpreter.parseAndExecute(state, '42');
state = ForthInterpreter.parseAndExecute(state, 'dup.stacks');
// Try invalid target stack
state = ForthInterpreter.parseAndExecute(state, '5'); // Invalid stack number
const lastOutput = state.output[state.output.length - 1];
if (!lastOutput.includes('Error: Invalid target stack')) {
throw new Error('Should show invalid target stack error');
}
if (state.crossStackInProgress) {
throw new Error('Should clear cross-stack mode on error');
}
});
// Test 3: Edge Cases
test('Edge Cases - Single Item Operations', () => {
state = ForthInterpreter.createInitialState();
// Test over.stacks with only 1 item
state = ForthInterpreter.parseAndExecute(state, 'focus.blue');
state = ForthInterpreter.parseAndExecute(state, '100');
state = ForthInterpreter.parseAndExecute(state, 'over.stacks');
const lastOutput = state.output[state.output.length - 1];
if (!lastOutput.includes('Error: Stack underflow')) {
throw new Error('Should show stack underflow error for over.stacks with 1 item');
}
});
test('Edge Cases - Focus Persistence', () => {
state = ForthInterpreter.createInitialState();
// Set focus and perform operation
state = ForthInterpreter.parseAndExecute(state, 'focus.yellow');
const originalFocus = state.focusedStack;
state = ForthInterpreter.parseAndExecute(state, '50');
state = ForthInterpreter.parseAndExecute(state, 'dup.stacks');
state = ForthInterpreter.parseAndExecute(state, '1');
if (state.focusedStack !== originalFocus) {
throw new Error('Focus should persist through cross-stack operations');
}
});
// Test 4: Complex Workflows
test('Complex Workflows - Multi-Operation Chain', () => {
state = ForthInterpreter.createInitialState();
// Set up multiple stacks
state = ForthInterpreter.parseAndExecute(state, 'focus.red');
state = ForthInterpreter.parseAndExecute(state, '100');
state = ForthInterpreter.parseAndExecute(state, '200');
state = ForthInterpreter.parseAndExecute(state, 'focus.teal');
state = ForthInterpreter.parseAndExecute(state, '300');
// Chain operations
state = ForthInterpreter.parseAndExecute(state, 'dup.stacks');
state = ForthInterpreter.parseAndExecute(state, '1'); // Target Red
state = ForthInterpreter.parseAndExecute(state, 'focus.red');
state = ForthInterpreter.parseAndExecute(state, 'over.stacks');
state = ForthInterpreter.parseAndExecute(state, '3'); // Target Blue
// Verify final state
if (state.stacks[0].length !== 3) throw new Error('Red stack should have 3 items');
if (state.stacks[1].length !== 1) throw new Error('Teal stack should have 1 item');
if (state.stacks[2].length !== 1) throw new Error('Blue stack should have 1 item');
});
// Test 5: State Management
test('State Management - Cross-Stack Mode Reset', () => {
state = ForthInterpreter.createInitialState();
// Start operation
state = ForthInterpreter.parseAndExecute(state, 'focus.red');
state = ForthInterpreter.parseAndExecute(state, '42');
state = ForthInterpreter.parseAndExecute(state, 'dup.stacks');
if (!state.crossStackInProgress) throw new Error('Should set cross-stack mode');
// Complete operation
state = ForthInterpreter.parseAndExecute(state, '2');
if (state.crossStackInProgress) throw new Error('Should clear cross-stack mode');
if (state.crossStackOperation !== null) throw new Error('Should clear operation');
if (state.crossStackData !== null) throw new Error('Should clear data');
});
console.log(`\n๐ Test Results: ${passCount}/${testCount} tests passed`);
console.log(`๐ฏ Success Rate: ${((passCount / testCount) * 100).toFixed(1)}%`);
if (passCount === testCount) {
console.log('\n๐ All cross-stack operation tests passed!');
} else {
console.log('\nโ ๏ธ Some tests failed. Please review the implementation.');
}
console.log('\n๐ Cross-stack operations test suite complete!');
|