aboutsummaryrefslogtreecommitdiffstats
path: root/forth.js
blob: c7ff27d019bd2d6ab6662e9b03bee267e16d6828 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function exec(x) {
  return Module.ccall(
    "extern_eval", // name of C function
    "string", // return type
    ["string"], // argument types
    [x] // arguments
  );
}

/*
root tag to add to page:
<div id="forthroot"></div>

 * */

const style = document.createElement('style')
style.innerHTML = `
      #forthroot * {
        background-color: #42456f;
        color: white;
        font-family: monospace;
        font-size: 1.2rem;
        box-sizing: border-box;
      }
      #forthouter {
        cursor: text;
        border: solid thin black;
        height: 90vh;
        overflow: scroll;
      }
      #forthline {
        width: 97%;
        border: none;
      }
      #forthline:focus {
        outline: none;
      }
      #forthresultsbox {
      }
      #forthresults {
        margin-top: 0px;
        overflow: clip;
        margin: 0px;
      }
      #forthlinelabel {
        padding-top:1px;
        max-width: 3%;
        float: left;
      }
      #forthform {
        margin-bottom: 0px;
      }
  `;
document.head.appendChild(style);
forthroot.innerHTML = `
    <div id="forthouter">
      <div id='forthresultsbox'>
        <pre id="forthresults"></pre>
      </div>
      <form id="forthform">
        <label id="forthlinelabel" for="forthline" position="left">>&nbsp;</label>
        <input id="forthline" type="text" value="" autocomplete="off">
      </form>
    </div>`
forthouter.onclick = () => forthline.focus();
const print = x => {
  forthresults.innerHTML = forthresults.innerHTML + x ;
  forthouter.scrollTo(0, forthouter.scrollHeight);
};
forthform.onsubmit = () => {
  const input = forthline.value;
  if (input !== '') {
    print(`> ${input}\n`);
    print(exec(input));
    forthline.value = '';
  }
  return false;
}