-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
42 lines (42 loc) · 1.88 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<html>
<head><style>h4{ margin: 0 } .bg{ padding:8px; font-size:24px; margin:0 }</style></head>
<body>
<div style='margin-left:8px; font-size:20px'>
<pre id='txt' style='display:block; height:85%; padding-top:0px; overflow-y:scroll'></pre>
<input id='tib' class='bg' style='height:36px; width:100%' value=''
onkeydown='if (13===event.keyCode) { forth_eval(tib.value); tib.value="" }'/>
<pre id='ss' class='bg' style='background-color:#eee'>[ ]</pre>
</div>
<script src='eforth.js'></script>
<script>
const txt = document.getElementById('txt') ///< text console
const ss = document.getElementById('ss') ///< data stack
const to_txt = (t)=>{ ///< output to textarea
txt.innerHTML += t; txt.scrollTop = txt.scrollHeight
}
const banner = ()=>{
txt.innerHTML = '<h4>eForth.js - <em>Forth in Javascript</em></h4>'
}
const vm = new ForthVM(to_txt) ///< create Forth VM object
const forth_eval = (cmd)=>{
to_txt('<b>'+cmd+'</b><br/>')
if (cmd=='clear') banner(); else vm.exec(cmd)
ss.innerHTML = '[ '+vm.ss.map(
v=>vm.base() != 10
? v.toString(vm.base()) /// * i.e. display hex
: v.toLocaleString(undefined,{minimumFractionDigits:0, maximumFractionDigits:6})
).join(' ')+' ]'
}
console.log(vm) /// * show VM on console
banner()
</script>
<script type='application/forth'>
.( \ can also embed Forth in HTML, for example ) cr cr
.( <b>: tk for i . next ." hello!" ;</b> \ create a word tk) cr
: tk for i . next ." hello!" ;
.( <b>5 tk </b> \ and execute tk) cr
5 tk cr cr
.( <b>\ so your Forth 'app' becomes a turn-key system</b>) cr
</script>
</body>
</html>