diff options
author | dan <[email protected]> | 2023-05-26 13:45:59 -0400 |
---|---|---|
committer | dan <[email protected]> | 2023-05-26 13:45:59 -0400 |
commit | 1ac1b0c94195be3231a4aaa7335dc2f3a169820c (patch) | |
tree | 8fb99fb7321beeaabc9534691fc56b264ca3d2a4 | |
parent | 742bcacc541e76ea2a982a8f14aafe48aae33dec (diff) | |
download | forth-1ac1b0c94195be3231a4aaa7335dc2f3a169820c.tar.gz forth-1ac1b0c94195be3231a4aaa7335dc2f3a169820c.tar.bz2 forth-1ac1b0c94195be3231a4aaa7335dc2f3a169820c.zip |
fix: js forth has same built-ins as c forth
-rw-r--r-- | forth.js | 37 |
1 files changed, 23 insertions, 14 deletions
@@ -22,6 +22,24 @@ function forth(print = console.log) { s.push(a1); s.push(a2); }, + 'drop' : () => { + s.pop(); + }, + 'over' : () => { + const a1 = s.pop(); + const a2 = s.pop(); + s.push(a2); + s.push(a1); + s.push(a2); + }, + 'rot' : () => { + const a1 = s.pop(); + const a2 = s.pop(); + const a3 = s.pop(); + s.push(a2); + s.push(a1); + s.push(a3); + }, 'then' : () => {/*Doing nothing skips the token*/}, 'if' : (initialIdx, tokens) => { if (!s.pop()) { @@ -33,7 +51,11 @@ function forth(print = console.log) { 'not' : () => { s.push(!s.pop()) }, '.' : () => { print(s.pop()) }, 'peek' : () => { print(s[s.length - 1]) }, - 'dup' : () => { s.push(s[s.length - 1]) }, + 'dup' : () => { + const a = s.pop(); + s.push(a); + s.push(a); + }, ':' : (initialIdx, tokens) => { let localIdx = initialIdx + 1; const fname = tokens[localIdx++]; @@ -46,19 +68,6 @@ function forth(print = console.log) { } return localIdx - initialIdx; // numItemsSkipped }, - 's' : () => print(JSON.stringify(s)), - 'sum' : () => { - let total = 0; - while (s.length > 0) { - total += popNum(); - } - s.push(total); - }, - '(' : (initialIdx, tokens) => { - let localIdx = initialIdx + 1; - while (tokens[localIdx] !== ')') { localIdx++ } - return localIdx - initialIdx; - } } function exec(tokens) { |