diff options
author | dan <[email protected]> | 2023-05-20 10:54:57 -0400 |
---|---|---|
committer | dan <[email protected]> | 2023-05-20 10:54:57 -0400 |
commit | 153cf4a4bab033bbb38341a01060ebbc60275132 (patch) | |
tree | fa195abcfad5fa4c6fd6a3daea62d1b881369175 | |
parent | 84b064f65e25d6e6e3a8c8cfe8a10062a2da66a0 (diff) | |
download | forth-153cf4a4bab033bbb38341a01060ebbc60275132.tar.gz forth-153cf4a4bab033bbb38341a01060ebbc60275132.tar.bz2 forth-153cf4a4bab033bbb38341a01060ebbc60275132.zip |
feat: `if` command; fix: input should be printed above output
-rw-r--r-- | forth.js | 35 | ||||
-rw-r--r-- | index.html | 12 |
2 files changed, 39 insertions, 8 deletions
@@ -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> |