aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordan <[email protected]>2023-05-26 13:45:59 -0400
committerdan <[email protected]>2023-05-26 13:45:59 -0400
commit1ac1b0c94195be3231a4aaa7335dc2f3a169820c (patch)
tree8fb99fb7321beeaabc9534691fc56b264ca3d2a4
parent742bcacc541e76ea2a982a8f14aafe48aae33dec (diff)
downloadforth-1ac1b0c94195be3231a4aaa7335dc2f3a169820c.tar.gz
forth-1ac1b0c94195be3231a4aaa7335dc2f3a169820c.tar.bz2
forth-1ac1b0c94195be3231a4aaa7335dc2f3a169820c.zip
fix: js forth has same built-ins as c forth
-rw-r--r--forth.js37
1 files changed, 23 insertions, 14 deletions
diff --git a/forth.js b/forth.js
index e589387..9cad5ac 100644
--- a/forth.js
+++ b/forth.js
@@ -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) {