1 function Word(vocab, name, source, func) {
8 Word.prototype.execute = function(next) {
12 Word.prototype.toString = function() {
14 html.push("<a href='/responder/browser/browse?vocab=")
15 html.push(this.vocab);
16 html.push("&word=")
24 function Hashtable() {
28 Hashtable.prototype.fromAlist = function(alist) {
30 for(var i=0;i<alist.length;++i) {
32 obj[item[0]] = item[1];
38 Hashtable.prototype.toAlist = function() {
39 var table = this.table;
48 Hashtable.prototype.get = function(key) {
49 var table = this.table;
50 if(table.hasOwnProperty(key)) {
56 Hashtable.prototype.set = function(key, value) {
57 this.table[key] = value;
60 Hashtable.prototype.toString = function() {
61 var table = this.table;
73 return result.join("");
76 function Continuation() {
77 this.data_stack = [ ];
78 this.retain_stack = [ ];
79 this.name_stack = [ ];
84 Continuation.prototype.clone = function() {
85 var c = new Continuation();
86 c.data_stack = this.data_stack.slice(0);
87 c.retain_stack = this.retain_stack.slice(0);
88 c.name_stack = this.name_stack.slice(0);
89 c.nesting = this.nesting;
95 this.vocabs = { scratchpad: { }, io: { } };
96 this.in_vocab = "scratchpad";
97 this.using_vocabs = [ "scratchpad", "kernel","math","sequences","parser","alien","browser-dom", "words", "io", "hashtables" ];
98 this.cont = new Continuation();
101 var factor = new Factor();
103 Factor.prototype.call_next = function(next) {
105 if(this.cont.nesting++ > 150) {
106 this.cont.nesting = 0;
115 Factor.prototype.push_data = function(v, next) {
116 factor.cont.data_stack.push(v);
117 factor.call_next(next);
120 Factor.prototype.use = function(v, next) {
121 factor.cont.data_stack.push(v);
122 factor.get_word("kernel", "use+").execute(next);
125 Factor.prototype.using = function(v, next) {
126 factor.cont.data_stack.push(v);
127 factor.get_word("kernel", "using").execute(next);
130 Factor.prototype.set_in = function(v, next) {
131 factor.cont.data_stack.push(v);
132 factor.get_word("kernel", "set-in").execute(next);
135 Factor.prototype.get_word = function(vocab,name) {
136 return factor.vocabs[vocab][name];
139 Factor.prototype.find_word = function(name) {
140 for(var v = 0; v<factor.using_vocabs.length; ++v) {
141 var w = factor.vocabs[factor.using_vocabs[v]][name];
148 Factor.prototype.add_word = function(vocab,name, source, func) {
149 var v = factor.vocabs[vocab];
152 factor.vocabs[vocab] = v;
154 v[name] = new Word(vocab,name,source,func);
157 Factor.prototype.remove_word = function(vocab,name) {
158 var v = factor.vocabs[vocab];
161 factor.vocabs[vocab] = v;
166 Factor.prototype.define_word = function(name, source, func, next) {
167 factor.vocabs[factor.in_vocab][name] = new Word(factor.in_vocab, name, source, function(next) {
168 var old = factor.cont.next;
169 factor.cont.next = function() {
170 factor.cont.next = old;
171 factor.call_next(next);
175 factor.call_next(next);
178 Factor.prototype.make_quotation = function(source, func) {
179 return new Word("quotations", "quotation", source, function(next) {
180 var old = factor.cont.next;
181 factor.cont.next = function() {
182 factor.cont.next = old;
183 factor.call_next(next);
189 Factor.prototype.server_eval = function(text, handler, next) {
191 $.post("/responder/fjsc/compile", { code: text }, function(result) {
192 factor.run_eval = function(func) {
193 factor.cont.next = function() { handler(text,result); }
197 alert("Error Parsing Javascript: " + ex);
198 handler(text, result);
201 factor.call_next(next);
204 eval("factor.run_eval" + result);
206 factor.run_eval(function() { alert("Error Parsing Javascript: " + e); });
211 /* kernel-internals vocabulary */
212 factor.add_word("kernel-internals", ">n", "primitive", function(next) {
213 var data_stack = factor.cont.data_stack;
214 var name_stack = factor.cont.name_stack;
215 name_stack.push(data_stack.pop());
216 factor.call_next(next);
219 factor.add_word("kernel-internals", "n>", "primitive", function(next) {
220 var data_stack = factor.cont.data_stack;
221 var name_stack = factor.cont.name_stack;
222 data_stack.push(name_stack.pop());
223 factor.call_next(next);
226 /* Kernel Vocabulary */
227 factor.add_word("kernel","dup", "primitive", function(next) {
228 var stack = factor.cont.data_stack;
229 stack[stack.length] = stack[stack.length-1];
230 factor.call_next(next);
233 factor.add_word("kernel", "drop", "primitive", function(next) {
234 factor.cont.data_stack.pop();
235 factor.call_next(next);
238 factor.add_word("kernel", "nip", "primitive", function(next) {
239 var stack = factor.cont.data_stack;
240 stack[stack.length-2] = stack[stack.length-1];
242 factor.call_next(next);
245 factor.add_word("kernel", "over", "primitive", function(next) {
246 var stack = factor.cont.data_stack;
247 stack[stack.length] = stack[stack.length-2];
248 factor.call_next(next);
251 factor.add_word("kernel", "pick", "primitive", function(next) {
252 var stack = factor.cont.data_stack;
253 stack[stack.length] = stack[stack.length-3];
254 factor.call_next(next);
257 factor.add_word("kernel", "swap", "primitive", function(next) {
258 var stack = factor.cont.data_stack;
259 var temp = stack[stack.length-2];
260 stack[stack.length-2] = stack[stack.length-1];
261 stack[stack.length-1] = temp;
262 factor.call_next(next);
265 factor.add_word("kernel", ">r", "primitive", function(next) {
266 var data_stack = factor.cont.data_stack;
267 var retain_stack = factor.cont.retain_stack;
268 retain_stack.push(data_stack.pop());
269 factor.call_next(next);
272 factor.add_word("kernel", "r>", "primitive", function(next) {
273 var data_stack = factor.cont.data_stack;
274 var retain_stack = factor.cont.retain_stack;
275 data_stack.push(retain_stack.pop());
276 factor.call_next(next);
279 factor.add_word("kernel", "call", "primitive", function(next) {
280 var quot = factor.cont.data_stack.pop();
284 factor.add_word("kernel", "execute", "primitive", function(next) {
285 var quot = factor.cont.data_stack.pop();
289 factor.add_word("kernel", "clear", "primitive", function(next) {
290 factor.cont.data_stack = [];
291 factor.cont.retain_stack = [];
292 factor.call_next(next);
295 factor.add_word("kernel", "if", "primitive", function(next) {
296 var stack = factor.cont.data_stack;
297 var else_quot = stack.pop();
298 var then_quot = stack.pop();
299 var condition = stack.pop();
301 then_quot.execute(next);
303 else_quot.execute(next);
307 factor.add_word("kernel", "f", "primitive", function(next) {
308 factor.cont.data_stack.push(false);
309 factor.call_next(next);
312 factor.add_word("kernel", "t", "primitive", function(next) {
313 factor.cont.data_stack.push(true);
314 factor.call_next(next);
317 factor.add_word("kernel", "=", "primitive", function(next) {
318 var stack = factor.cont.data_stack;
319 stack.push(stack.pop()==stack.pop());
320 factor.call_next(next);
323 factor.add_word("kernel", "bootstrap", "primitive", function(next) {
324 factor.cont.data_stack.push("/responder/fjsc-resources/bootstrap.factor");
325 factor.get_word("parser", "run-file").execute(next);
328 factor.add_word("kernel", "callcc0", "primitive", function(next) {
329 var data_stack = factor.cont.data_stack;
330 var quot = data_stack.pop();
331 var new_cont = factor.cont.clone();
332 var old_next = factor.cont.next;
333 factor.cont.next = function() {
334 factor.cont.next = old_next;
335 factor.call_next(next);
337 new_cont.data_stack.push(factor.cont);
338 factor.cont = new_cont;
342 factor.add_word("kernel", "callcc1", "primitive", function(next) {
343 factor.get_word("kernel", "callcc0").execute(next);
346 factor.add_word("kernel", "continue", "primitive", function(next) {
347 var data_stack = factor.cont.data_stack;
348 var cont = data_stack.pop();
349 factor.cont = cont.clone();
350 factor.call_next(cont.next);
353 factor.add_word("kernel", "continue-with", "primitive", function(next) {
354 var data_stack = factor.cont.data_stack;
355 var cont = data_stack.pop();
356 var data = data_stack.pop();
357 factor.cont = cont.clone();
358 factor.cont.data_stack.push(data);
359 factor.call_next(cont.next);
362 factor.add_word("kernel", "set-in", "primitive", function(next) {
363 var stack = factor.cont.data_stack;
364 var vocab = stack.pop();
365 var v = factor.vocabs[vocab];
368 factor.vocabs[vocab] = v;
370 factor.in_vocab = vocab;
371 factor.call_next(next);
374 factor.add_word("kernel", "current-vocab", "primitive", function(next) {
375 var stack = factor.cont.data_stack;
376 stack.push(factor.in_vocab);
377 factor.call_next(next);
380 factor.add_word("kernel", "use+", "primitive", function(next) {
381 var stack = factor.cont.data_stack;
382 var vocab = stack.pop();
383 var v = factor.vocabs[vocab];
386 factor.vocabs[vocab] = v;
388 factor.using_vocabs.push(vocab);
389 factor.call_next(next);
392 factor.add_word("kernel", "using", "primitive", function(next) {
393 var stack = factor.cont.data_stack;
394 var vocabs = stack.pop();
396 for(var i=0;i<vocabs.length;++i) {
397 var v = factor.vocabs[vocabs[i]];
400 factor.vocabs[vocabs[i]] = v;
402 factor.using_vocabs.push(vocabs[i]);
404 factor.call_next(next);
407 factor.add_word("kernel", "current-using", "primitive", function(next) {
408 var stack = factor.cont.data_stack;
409 stack.push(factor.using_vocabs);
410 factor.call_next(next);
413 factor.add_word("kernel", "forget", "primitive", function(next) {
414 var stack = factor.cont.data_stack;
415 var word = stack.pop();
416 factor.remove_word(word.vocab, word.name);
417 factor.call_next(next);
420 factor.add_word("kernel", ">function", "primitive", function(next) {
421 var stack = factor.cont.data_stack;
422 var word = stack.pop();
423 stack.push(function() { word.func(function() { }) });
424 factor.call_next(next);
427 factor.add_word("kernel", "curry", "primitive", function(next) {
428 var stack = factor.cont.data_stack;
429 var quot = stack.pop();
430 var value = stack.pop();
432 stack.push(factor.make_quotation("quotation", function(next) {
433 factor.cont.data_stack.push(value);
434 quot.execute(factor.cont.next);
436 factor.call_next(next);
439 /* Math vocabulary */
440 factor.add_word("math", "*", "primitive", function(next) {
441 var stack = factor.cont.data_stack;
442 stack.push(stack.pop() * stack.pop());
443 factor.call_next(next);
446 factor.add_word("math", "+", "primitive", function(next) {
447 var stack = factor.cont.data_stack;
448 stack.push(stack.pop() + stack.pop());
449 factor.call_next(next);
452 factor.add_word("math", "-", "primitive", function(next) {
453 var stack = factor.cont.data_stack;
454 var v1 = stack.pop();
455 var v2 = stack.pop();
457 factor.call_next(next);
460 factor.add_word("math", "/", "primitive", function(next) {
461 var stack = factor.cont.data_stack;
462 var v1 = stack.pop();
463 var v2 = stack.pop();
465 factor.call_next(next);
468 factor.add_word("parser", "run-file", "primitive", function(next) {
469 var stack = factor.cont.data_stack;
470 var url = stack.pop();
471 $.get(url, function(result) {
472 factor.server_eval(result, function() { }, next);
477 factor.add_word("alien", "alien-invoke", "primitive", function(next) {
478 var stack = factor.cont.data_stack;
479 var arg_types = stack.pop();
480 var method_name = stack.pop();
481 var library_name = stack.pop();
482 var return_values = stack.pop();
483 var obj = stack.pop();
485 for(var i = 0; i < arg_types.length; ++i) {
486 args[i] = stack.pop();
488 var func = obj[method_name];
489 if(method_name=="alert") {
490 func = function(a) { alert(a); }
492 var v = func.apply(obj, args.reverse());
493 if(return_values.length > 0)
495 factor.call_next(next);
498 factor.add_word("alien", "alien-property", "primitive", function(next) {
499 var stack = factor.cont.data_stack;
500 var obj = stack.pop();
501 var property_name = stack.pop();
502 var v = obj[property_name];
504 factor.call_next(next);
507 factor.add_word("alien", "set-alien-property", "primitive", function(next) {
508 var stack = factor.cont.data_stack;
509 var obj = stack.pop();
510 var property_name = stack.pop();
511 var data = stack.pop();
512 obj[property_name] = v;
513 factor.call_next(next);
516 factor.add_word("alien", "uneval", "primitive", function(next) {
517 var stack = factor.cont.data_stack;
518 stack.push(uneval(stack.pop()));
519 factor.call_next(next);
522 factor.add_word("words", "vocabs", "primitive", function(next) {
523 var stack = factor.cont.data_stack;
525 for(v in factor.vocabs) {
529 factor.call_next(next);
532 factor.add_word("words", "words", "primitive", function(next) {
533 var stack = factor.cont.data_stack;
534 var vocab = factor.vocabs[stack.pop()];
537 result.push(vocab[w]);
540 factor.call_next(next);
543 factor.add_word("words", "all-words", "primitive", function(next) {
544 var stack = factor.cont.data_stack;
546 for(v in factor.vocabs) {
547 for(w in factor.vocabs[v]) {
548 result.push(factor.vocabs[v][w]);
552 factor.call_next(next);
555 /* Sequences vocabulary */
556 factor.add_word("sequences", "nth", "primitive", function(next) {
557 var stack = factor.cont.data_stack;
558 var seq = stack.pop();
559 var index = stack.pop();
560 stack.push(seq[index]);
561 factor.call_next(next);
564 factor.add_word("sequences", "first", "primitive", function(next) {
565 var stack = factor.cont.data_stack;
566 var seq = stack.pop();
568 factor.call_next(next);
571 factor.add_word("sequences", "second", "primitive", function(next) {
572 var stack = factor.cont.data_stack;
573 var seq = stack.pop();
575 factor.call_next(next);
578 factor.add_word("sequences", "third", "primitive", function(next) {
579 var stack = factor.cont.data_stack;
580 var seq = stack.pop();
582 factor.call_next(next);
585 factor.add_word("sequences", "fourth", "primitive", function(next) {
586 var stack = factor.cont.data_stack;
587 var seq = stack.pop();
589 factor.call_next(next);
592 factor.add_word("sequences", "first2", "primitive", function(next) {
593 var stack = factor.cont.data_stack;
594 var seq = stack.pop();
595 stack.push(seq[0],seq[1]);
596 factor.call_next(next);
599 factor.add_word("sequences", "first3", "primitive", function(next) {
600 var stack = factor.cont.data_stack;
601 var seq = stack.pop();
602 stack.push(seq[0],seq[1],seq[2]);
603 factor.call_next(next);
606 factor.add_word("sequences", "first4", "primitive", function(next) {
607 var stack = factor.cont.data_stack;
608 var seq = stack.pop();
609 stack.push(seq[0],seq[1],seq[2],seq[3]);
610 factor.call_next(next);
613 factor.add_word("sequences", "each", "primitive", function(next) {
614 var stack = factor.cont.data_stack;
615 var quot = stack.pop();
616 var seq = stack.pop();
617 for(var i=0;i<seq.length;++i) {
621 factor.call_next(next);
624 factor.add_word("sequences", "map", "primitive", function(next) {
625 var stack = factor.cont.data_stack;
626 var quot = stack.pop();
627 var seq = stack.pop();
629 for(var i=0;i<seq.length;++i) {
632 result[i]=stack.pop();
635 factor.call_next(next);
638 factor.add_word("sequences", "reduce", "primitive", function(next) {
639 var stack = factor.cont.data_stack;
640 var quot = stack.pop();
641 var prev = stack.pop();
642 var seq = stack.pop();
643 for(var i=0;i<seq.length;++i) {
650 factor.call_next(next);
653 /* hashtables vocabulary */
654 factor.add_word("hashtables", "alist>hash", "primitive", function(next) {
655 var stack = factor.cont.data_stack;
656 var alist = stack.pop();
657 stack.push(new Hashtable().fromAlist(alist));
658 factor.call_next(next);
661 factor.add_word("hashtables", "hash>alist", "primitive", function(next) {
662 var stack = factor.cont.data_stack;
663 var hashtable = stack.pop();
664 stack.push(hashtable.toAlist());
665 factor.call_next(next);
668 factor.add_word("hashtables", "hash", "primitive", function(next) {
669 var stack = factor.cont.data_stack;
670 var hashtable = stack.pop();
671 var key = stack.pop();
672 stack.push(hashtable.get(key));
673 factor.call_next(next);
676 factor.add_word("hashtables", "set-hash", "primitive", function(next) {
677 var stack = factor.cont.data_stack;
678 var hashtable = stack.pop();
679 var key = stack.pop();
680 var value = stack.pop();
681 hashtable.set(key, value);
682 factor.call_next(next);
685 factor.add_word("sequences", "hash-each", "primitive", function(next) {
686 var stack = factor.cont.data_stack;
687 var quot = stack.pop();
688 var hashtable = stack.pop().table;
689 for(k in hashtable) {
690 var v = hashtable[k];
694 factor.call_next(next);
697 factor.add_word("sequences", "hash-map", "primitive", function(next) {
698 var stack = factor.cont.data_stack;
699 var quot = stack.pop();
700 var hashtable = stack.pop().table;
702 for(k in hashtable) {
703 var v = hashtable[k];
706 result.push(stack.pop());
709 factor.call_next(next);
712 /* browser-dom vocab */
713 factor.add_word("browser-dom", "window", "primitive", function(next) {
714 factor.cont.data_stack.push(window);
715 factor.call_next(next);
718 factor.add_word("browser-dom", "document", "primitive", function(next) {
719 factor.cont.data_stack.push(document);
720 factor.call_next(next);
723 factor.add_word("browser-dom", "load-script", "primitive", function(next) {
724 var stack = factor.cont.data_stack;
725 $("head/script#jsonrequest").remove();
726 var script = document.createElement("script");
727 script.id = "jsonrequest";
728 script.type = "text/javascript";
729 script.src = stack.pop();
730 $("head").append(script);
731 factor.call_next(next);
734 var handle_json = false;
735 factor.add_word("browser-dom", "json-request", "primitive", function(next) {
736 var stack = factor.cont.data_stack;
737 handle_json = function(data) {
738 factor.cont.data_stack.push(data);
739 factor.call_next(next);
741 factor.get_word("browser-dom", "load-script").execute();