diff options
Diffstat (limited to 'forth/foreforthfourth/forth.js')
-rw-r--r-- | forth/foreforthfourth/forth.js | 177 |
1 files changed, 153 insertions, 24 deletions
diff --git a/forth/foreforthfourth/forth.js b/forth/foreforthfourth/forth.js index 3a654d1..af133ea 100644 --- a/forth/foreforthfourth/forth.js +++ b/forth/foreforthfourth/forth.js @@ -933,11 +933,11 @@ const builtinWords = { }, // Multi-stack operations - 'push.red': { + 'move.red': { fn: (state) => { if (state.stacks[state.focusedStack].length === 0) { return updateState(state, { - output: [...state.output, `Error: Stack underflow on push.red - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] + output: [...state.output, `Error: Stack underflow on move.red - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] }); } const { stacks, value } = popFromStack(state.stacks, state.focusedStack); @@ -945,15 +945,15 @@ const builtinWords = { stacks: pushToStack(stacks, 0, value) }); }, - doc: 'Move top item from focused stack to red stack', + doc: 'Move top item from focused stack to red stack (removes from focused stack)', stack: '( x -- )' }, - 'push.1': { + 'move.1': { fn: (state) => { if (state.stacks[state.focusedStack].length === 0) { return updateState(state, { - output: [...state.output, `Error: Stack underflow on push.1 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] + output: [...state.output, `Error: Stack underflow on move.1 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] }); } const { stacks, value } = popFromStack(state.stacks, state.focusedStack); @@ -961,15 +961,15 @@ const builtinWords = { stacks: pushToStack(stacks, 0, value) }); }, - doc: 'Move top item from focused stack to red stack (Stack 1)', + doc: 'Move top item from focused stack to red stack (Stack 1) (removes from focused stack)', stack: '( x -- )' }, - 'push.teal': { + 'move.teal': { fn: (state) => { if (state.stacks[state.focusedStack].length === 0) { return updateState(state, { - output: [...state.output, `Error: Stack underflow on push.teal - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] + output: [...state.output, `Error: Stack underflow on move.teal - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] }); } const { stacks, value } = popFromStack(state.stacks, state.focusedStack); @@ -977,15 +977,15 @@ const builtinWords = { stacks: pushToStack(stacks, 1, value) }); }, - doc: 'Move top item from focused stack to teal stack', + doc: 'Move top item from focused stack to teal stack (removes from focused stack)', stack: '( x -- )' }, - 'push.2': { + 'move.2': { fn: (state) => { if (state.stacks[state.focusedStack].length === 0) { return updateState(state, { - output: [...state.output, `Error: Stack underflow on push.2 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] + output: [...state.output, `Error: Stack underflow on move.2 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] }); } const { stacks, value } = popFromStack(state.stacks, state.focusedStack); @@ -993,15 +993,15 @@ const builtinWords = { stacks: pushToStack(stacks, 1, value) }); }, - doc: 'Move top item from focused stack to teal stack (Stack 2)', + doc: 'Move top item from focused stack to teal stack (Stack 2) (removes from focused stack)', stack: '( x -- )' }, - 'push.blue': { + 'move.blue': { fn: (state) => { if (state.stacks[state.focusedStack].length === 0) { return updateState(state, { - output: [...state.output, `Error: Stack underflow on push.blue - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] + output: [...state.output, `Error: Stack underflow on move.blue - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] }); } const { stacks, value } = popFromStack(state.stacks, state.focusedStack); @@ -1009,15 +1009,15 @@ const builtinWords = { stacks: pushToStack(stacks, 2, value) }); }, - doc: 'Move top item from focused stack to blue stack', + doc: 'Move top item from focused stack to blue stack (removes from focused stack)', stack: '( x -- )' }, - 'push.3': { + 'move.3': { fn: (state) => { if (state.stacks[state.focusedStack].length === 0) { return updateState(state, { - output: [...state.output, `Error: Stack underflow on push.3 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] + output: [...state.output, `Error: Stack underflow on move.3 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] }); } const { stacks, value } = popFromStack(state.stacks, state.focusedStack); @@ -1025,15 +1025,15 @@ const builtinWords = { stacks: pushToStack(stacks, 2, value) }); }, - doc: 'Move top item from focused stack to blue stack (Stack 3)', + doc: 'Move top item from focused stack to blue stack (Stack 3) (removes from focused stack)', stack: '( x -- )' }, - 'push.yellow': { + 'move.yellow': { fn: (state) => { if (state.stacks[state.focusedStack].length === 0) { return updateState(state, { - output: [...state.output, `Error: Stack underflow on push.yellow - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] + output: [...state.output, `Error: Stack underflow on move.yellow - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] }); } const { stacks, value } = popFromStack(state.stacks, state.focusedStack); @@ -1041,15 +1041,15 @@ const builtinWords = { stacks: pushToStack(stacks, 3, value) }); }, - doc: 'Move top item from focused stack to yellow stack', + doc: 'Move top item from focused stack to yellow stack (removes from focused stack)', stack: '( x -- )' }, - 'push.4': { + 'move.4': { fn: (state) => { if (state.stacks[state.focusedStack].length === 0) { return updateState(state, { - output: [...state.output, `Error: Stack underflow on push.4 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] + output: [...state.output, `Error: Stack underflow on move.4 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to move.`] }); } const { stacks, value } = popFromStack(state.stacks, state.focusedStack); @@ -1057,10 +1057,139 @@ const builtinWords = { stacks: pushToStack(stacks, 3, value) }); }, - doc: 'Move top item from focused stack to yellow stack (Stack 4)', + doc: 'Move top item from focused stack to yellow stack (Stack 4) (removes from focused stack)', stack: '( x -- )' }, + // Copy operations (keep item in source stack) + 'copy.red': { + fn: (state) => { + if (state.stacks[state.focusedStack].length === 0) { + return updateState(state, { + output: [...state.output, `Error: Stack underflow on copy.red - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to copy.`] + }); + } + const top = state.stacks[state.focusedStack][state.stacks[state.focusedStack].length - 1]; + return updateState(state, { + stacks: pushToStack(state.stacks, 0, top) + }); + }, + doc: 'Copy top item from focused stack to red stack (keeps item on focused stack)', + stack: '( x -- x )' + }, + + 'copy.1': { + fn: (state) => { + if (state.stacks[state.focusedStack].length === 0) { + return updateState(state, { + output: [...state.output, `Error: Stack underflow on copy.1 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to copy.`] + }); + } + const top = state.stacks[state.focusedStack][state.stacks[state.focusedStack].length - 1]; + return updateState(state, { + stacks: pushToStack(state.stacks, 0, top) + }); + }, + doc: 'Copy top item from focused stack to red stack (Stack 1) (keeps item on focused stack)', + stack: '( x -- x )' + }, + + 'copy.teal': { + fn: (state) => { + if (state.stacks[state.focusedStack].length === 0) { + return updateState(state, { + output: [...state.output, `Error: Stack underflow on copy.teal - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to copy.`] + }); + } + const top = state.stacks[state.focusedStack][state.stacks[state.focusedStack].length - 1]; + return updateState(state, { + stacks: pushToStack(state.stacks, 1, top) + }); + }, + doc: 'Copy top item from focused stack to teal stack (keeps item on focused stack)', + stack: '( x -- x )' + }, + + 'copy.2': { + fn: (state) => { + if (state.stacks[state.focusedStack].length === 0) { + return updateState(state, { + output: [...state.output, `Error: Stack underflow on copy.2 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to copy.`] + }); + } + const top = state.stacks[state.focusedStack][state.stacks[state.focusedStack].length - 1]; + return updateState(state, { + stacks: pushToStack(state.stacks, 1, top) + }); + }, + doc: 'Copy top item from focused stack to teal stack (Stack 2) (keeps item on focused stack)', + stack: '( x -- x )' + }, + + 'copy.blue': { + fn: (state) => { + if (state.stacks[state.focusedStack].length === 0) { + return updateState(state, { + output: [...state.output, `Error: Stack underflow on copy.blue - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to copy.`] + }); + } + const top = state.stacks[state.focusedStack][state.stacks[state.focusedStack].length - 1]; + return updateState(state, { + stacks: pushToStack(state.stacks, 2, top) + }); + }, + doc: 'Copy top item from focused stack to blue stack (keeps item on focused stack)', + stack: '( x -- x )' + }, + + 'copy.3': { + fn: (state) => { + if (state.stacks[state.focusedStack].length === 0) { + return updateState(state, { + output: [...state.output, `Error: Stack underflow on copy.3 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to copy.`] + }); + } + const top = state.stacks[state.focusedStack][state.stacks[state.focusedStack].length - 1]; + return updateState(state, { + stacks: pushToStack(state.stacks, 2, top) + }); + }, + doc: 'Copy top item from focused stack to blue stack (Stack 3) (keeps item on focused stack)', + stack: '( x -- x )' + }, + + 'copy.yellow': { + fn: (state) => { + if (state.stacks[state.focusedStack].length === 0) { + return updateState(state, { + output: [...state.output, `Error: Stack underflow on copy.yellow - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to copy.`] + }); + } + const top = state.stacks[state.focusedStack][state.stacks[state.focusedStack].length - 1]; + return updateState(state, { + stacks: pushToStack(state.stacks, 3, top) + }); + }, + doc: 'Copy top item from focused stack to yellow stack (keeps item on focused stack)', + stack: '( x -- x )' + }, + + 'copy.4': { + fn: (state) => { + if (state.stacks[state.focusedStack].length === 0) { + return updateState(state, { + output: [...state.output, `Error: Stack underflow on copy.4 - ${getFocusedStackName(state.focusedStack)} is empty. Nothing to copy.`] + }); + } + const top = state.stacks[state.focusedStack][state.stacks[state.focusedStack].length - 1]; + return updateState(state, { + stacks: pushToStack(state.stacks, 3, top) + }); + }, + doc: 'Copy top item from focused stack to yellow stack (Stack 4) (keeps item on focused stack)', + stack: '( x -- x )' + }, + 'pop.red': { fn: (state) => popAndPrint(state, 0), doc: 'Pop and print top item from red stack', |