aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--forth.js35
-rw-r--r--index.html12
2 files changed, 39 insertions, 8 deletions
diff --git a/forth.js b/forth.js
index ab3dba6..54b9d1d 100644
--- a/forth.js
+++ b/forth.js
@@ -3,9 +3,6 @@ function forth(print = console.log) {
const popNum = () => Number(s.pop());
const fs = {
- ' ' : () => {},
- '\n' : () => {},
- '\t' : () => {},
'/' : () => {
const a2 = popNum();
const a1 = popNum();
@@ -18,6 +15,16 @@ function forth(print = console.log) {
},
'+' : () => { s.push(popNum() + popNum()) },
'*' : () => { s.push(popNum() * popNum()) },
+ '=' : () => { s.push(popNum() === popNum()) },
+ 'then' : () => {/*Doing nothing skips the token*/},
+ 'if' : (initialIdx, tokens) => {
+ if (!s.pop()) {
+ let localIdx = initialIdx;
+ while (tokens[localIdx] && tokens[localIdx] !== 'then') {localIdx++}
+ return localIdx - initialIdx;
+ }
+ },
+ 'not' : () => { s.push(!s.pop()) },
'.' : () => { print(s.pop()) },
'peek' : () => { print(s[s.length - 1]) },
'dup' : () => { s.push(s[s.length - 1]) },
@@ -62,7 +69,13 @@ function forth(print = console.log) {
}
}
}
- return t => exec(t.split(/[ \n\t]+/));
+ return t =>
+ new Promise(
+ resolve => {
+ exec(t.split(/[ \n\t]+/).filter(x => x !== ''));
+ resolve();
+ }
+ );
}
/*
@@ -96,19 +109,25 @@ if (typeof window !== 'undefined') { // browser UI
height: 90vh;
}
#forthresults {
+ height: 100%;
margin-top: 0px;
+ overflow: clip;
+ margin: 0px;
}
#forthlinelabel {
padding-top:1px;
max-width: 3%;
}
+ #forthform {
+ margin-bottom: 0px;
+ }
`;
document.head.appendChild(style);
forthroot.innerHTML = `
<div id="forthouter">
<form id="forthform">
<label id="forthlinelabel" for="forthline" position="left">></label>
- <input id="forthline" type="text" value="">
+ <input id="forthline" type="text" value="" autocomplete="off">
</form>
<div id='forthresultsbox'>
<pre id="forthresults"></pre>
@@ -118,9 +137,9 @@ if (typeof window !== 'undefined') { // browser UI
const print = x => { forthresults.innerHTML = x + '\n' + forthresults.innerHTML; };
const m = forth(print);
forthform.onsubmit = () => {
- print(`> ${forthline.value}`);
- if (forthline.value !== '') {
- m(forthline.value);
+ const input = forthline.value;
+ if (input !== '') {
+ m(input).then(() => print(`> ${input}`));
forthline.value = '';
}
return false;
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..2ab7c6e
--- /dev/null
+++ b/index.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="" lang="">
+ <head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
+ <title>Forth Demo</title>
+ </head>
+ <body>
+ <div id="forthroot"></div>
+ <script src="forth.js"></script>
+ </body>
+</html>