about summary refs log tree commit diff stats
path: root/forth/foreforthfourth/test-forth.js
blob: 54f9963a16b6988459fccc4289115ad723704058 (plain) (blame)
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
// Simple test file for the Forth interpreter
// Run with: node test-forth.js

const ForthInterpreter = require('./forth.js');

console.log('🧪 Testing 4-Stack Toy Forth Interpreter\n');

// Test 1: Basic number pushing
console.log('Test 1: Basic number pushing');
let state = ForthInterpreter.createInitialState();
state = ForthInterpreter.parseAndExecute(state, '5 3 2');
console.log('Stack 1 after "5 3 2":', state.stacks[0]);
console.log('Expected: [5, 3, 2]\n');

// Test 2: Basic arithmetic
console.log('Test 2: Basic arithmetic');
state = ForthInterpreter.parseAndExecute(state, '+');
console.log('Stack 1 after "+":', state.stacks[0]);
console.log('Expected: [5, 5] (3+2=5)\n');

// Test 3: Stack manipulation
console.log('Test 3: Stack manipulation');
state = ForthInterpreter.parseAndExecute(state, 'dup over');
console.log('Stack 1 after "dup over":', state.stacks[0]);
console.log('Expected: [5, 5, 5, 5] (dup then over)\n');

// Test 4: Stack inspection
console.log('Test 4: Stack inspection');
state = ForthInterpreter.parseAndExecute(state, '.s');
console.log('Output:', state.output[state.output.length - 1]);
console.log('Expected: <4> 5 5 5 5\n');

// Test 5: Comparison operators
console.log('Test 5: Comparison operators');
state = ForthInterpreter.parseAndExecute(state, '5 3 >');
console.log('Stack 1 after "5 3 >":', state.stacks[0]);
console.log('Expected: [5, 5, 5, 5, -1] (5 > 3 = true = -1)\n');

// Test 6: Word definition
console.log('Test 6: Word definition');
state = ForthInterpreter.parseAndExecute(state, ': double dup + ;');
console.log('Output:', state.output[state.output.length - 1]);
console.log('Expected: Word \'double\' defined\n');

// Test 7: Using defined word
console.log('Test 7: Using defined word');
state = ForthInterpreter.parseAndExecute(state, 'double');
console.log('Stack 1 after "double":', state.stacks[0]);
console.log('Expected: [5, 5, 5, 5, -1, 10] (double of 5 = 10)\n');

// Test 8: List all words
console.log('Test 8: List all words');
state = ForthInterpreter.parseAndExecute(state, 'words');
console.log('Output:', state.output.slice(-3));
console.log('Expected: Built-in words, User defined words, Total words count\n');

// Test 9: Stack juggling
console.log('Test 9: Stack juggling');
state = ForthInterpreter.parseAndExecute(state, 'push.teal');
console.log('Stack 1 after "push.teal":', state.stacks[0]);
console.log('Stack 2 after "push.teal":', state.stacks[1]);
console.log('Expected: Stack 1: [5, 5, 5, 5, -1], Stack 2: [10]\n');

// Test 10: Error handling
console.log('Test 10: Error handling');
state = ForthInterpreter.parseAndExecute(state, 'drop drop drop drop drop drop drop drop drop drop drop');
console.log('Output:', state.output[state.output.length - 1]);
console.log('Expected: Error: Stack underflow on drop\n');

console.log('✅ All tests completed!');
console.log('\nFinal state:');
console.log('Stack 1:', state.stacks[0]);
console.log('Stack 2:', state.stacks[1]);
console.log('Stack 3:', state.stacks[2]);
console.log('Stack 4:', state.stacks[3]);
console.log('Dictionary size:', state.dictionary.size);
console.log('Output messages:', state.output.length);