-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisp-repl.js
47 lines (45 loc) · 1.5 KB
/
lisp-repl.js
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
// REPL - using the jQuery Terminal script
// Install a LISP terminal in a given jQuery element
lisp.replTerminal = function(elt) {
elt.terminal(
function(str, terminal) {
try {
var term = lisp.parse(str);
if (term != null) {
var result = lisp.evalCode(term, lisp.env);
terminal.echo(result.print());
}
} catch (err) {
terminal.error(err);
}
},
{
greetings: "LISP Interpreter. Type 'clear' to clear",
prompt: '> '
});
lisp.terminal = elt.terminal();
};
// Clicking 'button' will load 'source' to the interpreter
lisp.replLoader = function(source, button) {
button.click(
function() {
lisp.terminal.echo('Loading source...');
var parser = new lisp.Parser(source.val());
try {
while (!parser.empty()) {
var term = parser.readTerm();
if (term != null) {
var result = lisp.evalCode(term, lisp.env);
lisp.terminal.echo(result.print());
} else { // term == null
if (!parser.empty())
parser.parseError();
}
}
lisp.terminal.focus();
} catch (err) {
console.log(err);
lisp.terminal.error(err);
}
});
};