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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
function forth(print = console.log) {
const s = []; // the stack
const popNum = () => Number(s.pop());
const fs = {
'/' : () => {
const a2 = popNum();
const a1 = popNum();
s.push(a1 / a2);
},
'-' : () => {
const a2 = popNum();
const a1 = popNum();
s.push(a1 - a2);
},
'+' : () => { s.push(popNum() + popNum()) },
'*' : () => { s.push(popNum() * popNum()) },
'=' : () => { s.push(popNum() === popNum()) },
'swap' : () => {
const a1 = s.pop();
const a2 = s.pop();
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()) {
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' : () => {
const a = s.pop();
s.push(a);
s.push(a);
},
':' : (initialIdx, tokens) => {
let localIdx = initialIdx + 1;
const fname = tokens[localIdx++];
const fnContent = [];
for (;tokens[localIdx] !== ';'; localIdx++) {
fnContent.push(tokens[localIdx]);
}
fs[fname] = () => {
exec(fnContent);
}
return localIdx - initialIdx; // numItemsSkipped
},
}
function exec(tokens) {
for (let tokenIdx = 0; tokenIdx < tokens.length; tokenIdx++) {
const token = tokens[tokenIdx];
if (fs[token]) {
const numItemsSkipped = fs[token](tokenIdx, tokens);
if (Number.isInteger(numItemsSkipped)) {
tokenIdx += numItemsSkipped;
}
}
else {
s.push(token);
}
}
}
return t =>
new Promise(
resolve => {
exec(t.split(/[ \n\t]+/).filter(x => x !== ''));
resolve();
}
);
}
/*
root tag to add to page:
<div id="forthroot"></div>
* */
if (typeof window !== 'undefined') { // browser UI
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">> </label>
<input id="forthline" type="text" value="" autocomplete="off">
</form>
</div>`
forthouter.onclick = () => forthline.focus();
const print = x => {
forthresults.innerHTML = forthresults.innerHTML + x + '\n' ;
forthouter.scrollTo(0, forthouter.scrollHeight);
};
const m = forth(print);
forthform.onsubmit = () => {
const input = forthline.value;
if (input !== '') {
print(`> ${input}`);
m(input);
forthline.value = '';
}
return false;
}
} else { // UI if NodeJS
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
const m = forth();
(async () => {
while (true) {
const input = await prompt("> ");
m(input)
}
})();
}
|