about summary refs log tree commit diff stats
path: root/forth/foreforthfourth/test-forth.js
diff options
context:
space:
mode:
Diffstat (limited to 'forth/foreforthfourth/test-forth.js')
-rw-r--r--forth/foreforthfourth/test-forth.js77
1 files changed, 77 insertions, 0 deletions
diff --git a/forth/foreforthfourth/test-forth.js b/forth/foreforthfourth/test-forth.js
new file mode 100644
index 0000000..54f9963
--- /dev/null
+++ b/forth/foreforthfourth/test-forth.js
@@ -0,0 +1,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);