-
Notifications
You must be signed in to change notification settings - Fork 0
/
forth.html
181 lines (150 loc) · 3.76 KB
/
forth.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
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
180
181
<html>
<head><title>Forth interpreter</title>
<style>
#output {
font-family: monospace;
white-space: pre;
}
input {
margin-top: 20px;
}
</style>
</head>
<body>
<div id=output>
<div>Welcome to FORTH</div>
</div>
<input id="input" size=80>
<button>Run</button>
<script>
let inputEl = document.querySelector('input');
let outputEl = document.querySelector('#output');
let runEl = document.querySelector('button');
function output(s){
let divEl = document.createElement('div');
divEl.innerText = s;
outputEl.appendChild(divEl);
}
let stack = [];
const variables = []; // a list of [name, value]
const builtIns = {
swap: function () {
let a = stack.pop();
let b = stack.pop();
stack.push(a);
stack.push(b);
},
over: function () {
let a = stack.pop();
let b = stack.pop();
stack.push(b);
stack.push(a);
stack.push(b);
},
drop: function() {stack.pop()},
rot: function () {
let a = stack.pop();
let b = stack.pop();
let c = stack.pop();
stack.push(a);
stack.push(c);
stack.push(b);
},
dup: function() {
let a = stack.pop();
stack.push(a);
stack.push(a);
},
cr: function() {output('')},
emit: function() {output(String.fromCharCode(stack.pop()))},
invert: function () {
stack.push(stack.pop() ? 0 : -1)
},
and: function () {
stack.push(stack.pop() && stack.pop() ? -1 : 0)
},
or: function () {
stack.push(stack.pop() || stack.pop() ? -1 : 0)
},
variable: function () {
let name = unprocessedWords.shift();
if (findVariable(name) != null) {
output('Variable ' + name + ' is already defined');
return;
}
variables.push([name, 0]);
}
}
builtIns['.'] = function() {output(stack.pop());};
builtIns['+'] = function() {stack.push(stack.pop() + stack.pop());};
builtIns['-'] = function() {stack.push(stack.pop() - stack.pop());};
builtIns['*'] = function() {stack.push(stack.pop() * stack.pop());};
builtIns['/'] = function() {stack.push(Math.floor(stack.pop() / stack.pop()));};
builtIns['='] = function() {stack.push(stack.pop() == stack.pop() ? -1 : 0);};
builtIns['>'] = function() {stack.push(stack.pop() <= stack.pop() ? -1 : 0);};
builtIns['<'] = function() {stack.push(stack.pop() >= stack.pop() ? -1 : 0);};
builtIns[':'] = function() {
output('user-defined functions are not supported yet');
while (unprocessedWords.length && ';' != unprocessedWords.shift());
};
builtIns['alloc'] = function() {
output('alloc is not supported yet');
};
builtIns['cells'] = function() {
// do nothing because it would multiply by 1.
};
builtIns['@'] = function() {
stack.push(variables[stack.pop()][1]);
}
builtIns['!'] = function() {
variables[stack.pop()][1] = stack.pop();
}
function findVariable(word) {
for (let i = 0; i<variables.length; i++)
if (word == variables[i][0]) {
return i;
}
return null;
}
function execute(word) {
if (!isNaN(word)) {
stack.push(Number(word));
return;
}
let f = builtIns[word];
var addr = findVariable(word);
if (f) {
f.call();
} else if (addr != null) {
stack.push(addr);
} else {
// TODO: user-defined functions
output('unknown word: ' + word);
}
}
let unprocessedWords = [];
function repl(s) {
if (s.length == 0) return;
output('>> ' + s);
unprocessedWords = s.split(' ');
while (w = unprocessedWords.shift()) {
execute(w);
}
output('Stack is: ' + stack.join(' '));
output('ok');
output('');
inputEl.value = '';
}
runEl.addEventListener('click', (e) => {
repl(inputEl.value);
inputEl.value = '';
});
inputEl.addEventListener('keyup', (e) => {
if (e.key === 'Enter' || e.keyCode === 13) {
repl(inputEl.value);
inputEl.value = '';
}
});
</script>
</body>
</html>