-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhaskell.hiji.js
More file actions
306 lines (269 loc) · 10.3 KB
/
haskell.hiji.js
File metadata and controls
306 lines (269 loc) · 10.3 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// TODO omm man bläddrar upp i historyn så ska det man skrivit in sparas
var ENTER = '13';
var UP = '38';
var DOWN = '40';
var is_module_loaded = false;
var modules = new Array();
var commands = new Array();
commands[":l"] = "LOAD";
commands[":load"] = "LOAD";
commands[":h"] = "HELP";
commands[":help"] = "HELP";
commands[":t"] = "TYPE";
commands[":type"] = "TYPE";
(function($){
var evaluateHaskell = function(line, env)
{
var line_ = '{' + line + '}';
ast = haskell.parser.parse(line_).ast;
if (ast == undefined){
return "Syntax Error";
}
if (ast.type == "DoExpr") {
ast = new haskell.ast.DoExpr(new haskell.ast.Application(new haskell.ast.VariableLookup("hijiOutputLine#"), ast.expr));
}
var doexpr = new haskell.ast.Do([ast,
new haskell.ast.DoExpr(new haskell.ast.Primitive(
function(env) {
return new haskell.interpreter.Data("IO", [new haskell.interpreter.HeapPtr(env)]);
}
))]);
console.log("%o", doexpr);
var res = haskell.interpreter.eval(doexpr, env);
return res.ptrs[0].dereference();
};
var makeModules = function(modules){
return "<ul class='modules'><li>" + modules.join("</li><li>") + "</li></ul>";
};
var makeEntered = function(modules, entered){
return $("<li class='entered'>" + makeModules(modules) + "<span class='line'>" + entered + "</span></li>");
};
var makeResponse = function(response){
};
var makeInput = function(modules){
return "<li class='input' id='inputTest'>" + makeModules(modules) + "<input type='text' name='inputBox' id='inbox'></li>";
};
var makeOutput = function(output) {
console.log("%o", output);
return $("<li class='output'></li>").text(output.toString());
};
$.fn.startHiji = function() {
//var modules = new Array();
var hist = new Array();
// history
var hiss = new historry;
try{
// load history from cookie
hiss_cookie = $.cookie("hiss");
if(hiss_cookie != null){
hiss.history_array = JSON.parse(hiss_cookie);
}
}
catch(err){
console.log("Error: History not loaded from cookie");
}
var env = new haskell.interpreter.RootEnv();
haskell.primitives.init(env);
haskell.primitives.initHiji(env);
load_module('hs/Prelude.hs');
modules[0] = "Prelude";
this.html("<ol>" + makeInput(modules) + "</ol>");
$("input:text:visible:first").focus();
this.keydown(function(e){
var input = $('input', this);
var line = input.attr("value");
if(e.which==UP){
input.attr("value", hiss.older(line));
}
if(e.which==DOWN){
input.attr("value", hiss.newer(line));
}
if (e.which==ENTER){
// history
hiss.addHistory(line);
try{
$.cookie("hiss", JSON.stringify(hiss.history_array), {expires: 3 });
}
catch(err){
console.log("Error: History not saved to cookie");
}
input.attr("value","");
$('.input', this).replaceWith(makeEntered(modules, line));
if(isCommand(line)){
runCommand(line, input, line);
}else
{
try {
var newLine = makeEntered(modules, line);
// Global variable:
printArea = $("ol", this);
env = evaluateHaskell(line, env);
console.log("%o", env);
}
catch(e) {
console.log("%o", e);
};
}
$("ol",this).append(makeInput(modules));
//set focus
$("input:text:visible:first").focus();
}
});
// load a module
function load_module(module){
is_module_loaded = false;
jQuery.ajax({
async : false,
url : module,
success: function(prelude_data){
console.log(prelude_data);
try {
var ast = haskell.parser.parse(prelude_data);
console.log("%o", ast);
if (ast.ast == undefined) {
console.log("Syntax Error");
}
else {
haskell.interpreter.prepare(ast.ast, env);
is_module_loaded = true;
}
} catch(e) {
console.log("%o", e);
}
}
});
}
function isCommand(l){
var line = trim(l);
if(line.charAt(0) == ':')
return true
else
return false
}
function runCommand(i, input2, line){
var input = trim(i);
var command = input.indexOf(" ") != -1 ? input.substr(0, input.indexOf(" ")) : input;
// load module
if(commands[command] == "LOAD"){
var arg = trim(input.substr(command.length));
var module_name = arg.substr(0, arg.lastIndexOf('.'));
load_module(arg);
if(is_module_loaded){
var module_already_in_modules = false;
for(x in modules){
if(modules[x] == module_name)
module_already_in_modules = true;
}
if(module_already_in_modules == false){
var newLine = makeEntered(modules, line);
var output = makeOutput("Module " + module_name +" loaded");
$('.input').after(output).replaceWith(newLine);
modules.push(module_name);
$("ol").append(makeInput(modules));
}else{
var newLine = makeEntered(modules, line);
var output = makeOutput("Module " + module_name + " already loaded");
$('.input').after(output).replaceWith(newLine);
$("ol").append(makeInput(modules));
}
}else{
var newLine = makeEntered(modules, line);
var output = makeOutput("Module " + module_name + " not found");
$('.input').after(output).replaceWith(newLine);
$("ol").append(makeInput(modules));
}
}else if(commands[command] == "HELP"){
var newLine = makeEntered(modules, line);
var output_row = new Array();
output_row.push(makeOutput("Help"));
output_row.push(makeOutput(" "));
output_row.push(makeOutput("Commands:"));
output_row.push(makeOutput(":l [Module] ... load a module"));
var str = "$('.input')";
for (var i = output_row.length-1; i>=0; i--){
str += ".after(" + output_row[i] + ")";
// $('.input').after(output_row[i]).after(output).replaceWith(newLine);
}
str += ".replaceWith(newLine);";
alert(str);
eval(str);
// var output = makeOutput("HELP HELP HELP" + "<br>" + "asdas");
// $('.input').after(output1).after(output).replaceWith(newLine);
// $('.input').after(output).replaceWith(newLine);
// $('.input').after(output).replaceWith(newLine);
$("ol").append(makeInput(modules));
} else if (commands[command] == "TYPE") {
var arg = trim(input.substr(command.length));
var ast = haskell.parser.parse('{' + arg + '}').ast.expr;
var tc = haskell.typechecker;
var env = new tc.Environment(new tc.Assumps(), new tc.Subst(), new tc.NameGen());
var newLine = "";
try {
var infered = ast.infer(env);
var type = infered.type.apply(env.getSubst());
var predsSubst = infered.preds.map(function(p) { return p.apply(env.getSubst())});
var preds = tc.unique(predsSubst.filter(
function (p) {
return tc.any(
p.tv(),
function(t) {
return tc.elem(type.tv(), t);
}
);
}
));
var predsString = preds.map(function(p) { return p.toString(); }).join(", ");
if (predsString.length > 0) {
predsString = "(" + predsString + ") => ";
}
newLine = ast.stringify() + " :: " + predsString + type.toString();
} catch (x) {
newLine=x;
}
$("ol").append(makeOutput(newLine));
}
}
};
})(jQuery);
function trim(str){
return str.replace(/^\s+|\s+$/g,"");
}
// historry-class with nice name
// !!!WARNING!!! NICE NAME. conflict with javascript
historry = function(input){
this.pointer = -1;
this.history_array = new Array();
this.active_value = "";
this.addHistory = function(input){
this.history_array.unshift(input);
this.pointer = -1;
};
this.updateHistory = function(input){
this.history_array[this.pointer] = input;
}
this.older = function(input){
if(this.pointer == -1){
this.active_value = input;
}else{
this.updateHistory(input);
}
this.pointer++;
if(this.pointer >= this.history_array.length){
this.pointer = this.history_array.length-1
}
return this.history_array[this.pointer];
};
this.newer = function(input){
if(this.pointer == -1){
this.active_value = input;
}else{
this.updateHistory(input);
}
this.pointer--;
if(this.pointer < 0){
this.pointer = -1
return this.active_value;
}
return this.history_array[this.pointer];
};
};