-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
133 lines (102 loc) · 3.38 KB
/
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
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
var REPL = (function() {
var ENTER = 13,
ARROW_UP = 38,
ARROW_DN = 40
function prepend(element, html) {
var frag = document.createDocumentFragment(),
tmp = document.createElement('body'),
child
tmp.innerHTML = html
while (child = tmp.firstChild)
frag.appendChild(child)
element.insertBefore(frag, element.lastChild)
frag = tmp = null;
}
function pad(number, length) {
var str = '' + number
while (str.length < length)
str = '0' + str
return str
}
/************************************************
* History Iterator Thingy *
************************************************/
function History() {
this.list = []
this.curr = 0
}
History.prototype.length = function() {
return this.list.length
}
History.prototype.push = function(cmd) {
this.list.push(cmd)
this.curr = this.length() - 1
}
History.prototype.up = function(cmd) {
this.curr = Math.max(this.curr - 1, 0)
return this.list[this.curr]
}
History.prototype.down = function(cmd) {
this.curr = Math.min(this.curr + 1, this.length() - 1)
return this.list[this.curr]
}
/************************************************
* REPL *
************************************************/
function REPL(container, options) {
this.options = options || ({
exec : function() {}
})
this.container = container
this.history = new History()
this.build_prompt()
this.next_prompt()
var that = this
container.querySelector('.repl_input').onkeypress = function(e) { that.keypress(e) }
}
REPL.prototype.build_prompt = function() {
prepend(this.container, '<p class="repl_output"><span class="repl_prompt"></span><input type="text" size="70" class="repl_input" /></p>')
}
REPL.prototype.prompt_string = function() {
return "lisp.js " + pad(this.history.length() + 1, 3) + " >"
}
REPL.prototype.next_prompt = function() {
this.container.querySelector('.repl_prompt').innerHTML = this.prompt_string()
this.container.querySelector('.repl_input').focus()
this.container.querySelector('.repl_input').value = ""
}
REPL.prototype.update_prompt = function(cmd) {
this.container.querySelector('.repl_input').value = cmd
this.container.querySelector('.repl_input').focus()
}
REPL.prototype.run_cmd = function() {
var command = this.prompt_string() + " " + this.container.querySelector('.repl_input').value
prepend(this.container, '<p class="repl_output">' + command + '</p>')
this.history.push(this.container.querySelector('.repl_input').value)
prepend(this.container, '<p class="repl_output">' + this.options.exec(this.container.querySelector('.repl_input').value) + '</p>')
this.next_prompt()
this.container.scrollTop = 500000;
}
REPL.prototype.keypress = function(e) {
switch(e.keyCode) {
case ENTER:
this.run_cmd()
break;
case ARROW_UP:
this.update_prompt(this.history.up())
break;
case ARROW_DN:
this.update_prompt(this.history.down())
break;
}
}
return REPL
})()
window.onload = function() {
var env = new Env({}, global_env)
var lisp_repl = new REPL(document.getElementById('repl'), {
exec : function(cmd) {
return to_string(evaluate(parse(cmd), env))
}
})
}