remove math.blas.syntax and merge parsing words into math.blas.vectors/matrices
[factor/jcg.git] / extra / fjsc / resources / bootstrap.js
blob032829c36318faec0b05d50ceab621bf67a4ccda
1 function Word(vocab, name, source, func) {
2   this.vocab = vocab;
3   this.name = name;
4   this.source = source;
5   this.func = func;
8 Word.prototype.execute = function(next) {
9   this.func(next);
12 Word.prototype.toString = function() {
13   var html = [];
14   html.push("<a href='/responder/browser/browse?vocab=")
15   html.push(this.vocab);
16   html.push("&amp;word=")
17   html.push(this.name);
18   html.push("'>")
19   html.push(this.name);
20   html.push("</a>");
21   return html.join("");
24 function Hashtable() {
25   this.table = { }
28 Hashtable.prototype.fromAlist = function(alist) {
29   var obj = { }
30   for(var i=0;i<alist.length;++i) {  
31     var item = alist[i];
32     obj[item[0]] = item[1];
33   }
34   this.table = obj;
35   return this;
38 Hashtable.prototype.toAlist = function() {
39   var table = this.table;
40   var alist = [ ];
41   for(k in table) {
42     var v = table[k];
43     alist.push([k,v]);
44   }
45   return alist;
48 Hashtable.prototype.get = function(key) {
49   var table = this.table;
50   if(table.hasOwnProperty(key)) {
51     return table[key];
52   }
53   return false;
56 Hashtable.prototype.set = function(key, value) {
57   this.table[key] = value;
60 Hashtable.prototype.toString = function() {
61   var table = this.table;
62   var result = [];
63   result.push("H{")
64   for(k in table) {
65     var v = table[k];
66     result.push(" { ");
67     result.push(k);
68     result.push(" ");
69     result.push(v);
70     result.push(" }");
71   }
72   result.push("}");
73   return result.join("");    
76 function Continuation() {
77   this.data_stack = [ ];
78   this.retain_stack = [ ];
79   this.name_stack = [ ];
80   this.next = false;
81   this.nesting = 0;
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;
90   c.next = this.next;
91   return c;
94 function Factor() {
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) {
104   if(next) {
105     if(this.cont.nesting++ > 150) {
106       this.cont.nesting = 0;
107       setTimeout(next, 0);
108    }
109     else {
110       next();
111     }
112   }
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];
142     if(w)
143       return w;
144   }
145   return false;
148 Factor.prototype.add_word = function(vocab,name, source, func) {
149   var v = factor.vocabs[vocab];
150   if(!v) {
151     v = { };
152     factor.vocabs[vocab] = v;
153   }
154   v[name] = new Word(vocab,name,source,func);
157 Factor.prototype.remove_word = function(vocab,name) {
158   var v = factor.vocabs[vocab];
159   if(!v) {
160     v = { };
161     factor.vocabs[vocab] = v;
162   }
163   delete v[name];
165     
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);
172     }
173     func();
174   });
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);
184     }
185     func();
186   });
189 Factor.prototype.server_eval = function(text, handler, next) {
190    var self = this;
191    $.post("/responder/fjsc/compile", { code: text }, function(result) {
192      factor.run_eval = function(func) {
193        factor.cont.next = function() { handler(text,result); } 
194        try {
195          func(factor);
196        } catch(ex) {
197          alert("Error Parsing Javascript: " + ex); 
198          handler(text, result);
199        }
200        if(next) 
201          factor.call_next(next);
202      }
203      try {
204        eval("factor.run_eval" + result);
205      } catch(e) {
206        factor.run_eval(function() { alert("Error Parsing Javascript: " + e); });
207      }
208    });
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];
241   stack.pop();
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();
281   quot.execute(next);
284 factor.add_word("kernel", "execute", "primitive", function(next) {
285   var quot = factor.cont.data_stack.pop();
286   quot.execute(next);
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();
300   if(condition) {
301     then_quot.execute(next);
302   } else {
303     else_quot.execute(next);
304   }
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);
336   }
337   new_cont.data_stack.push(factor.cont);
338   factor.cont = new_cont;
339   quot.execute(next);  
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];
366   if(!v) {
367     v = { };
368     factor.vocabs[vocab] = v;
369   }
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];
384   if(!v) {
385     v = { };
386     factor.vocabs[vocab] = v;
387   }
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]];
398     if(!v) {
399       v = { };
400       factor.vocabs[vocabs[i]] = v;
401     }
402     factor.using_vocabs.push(vocabs[i]);
403   }
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();
431   
432   stack.push(factor.make_quotation("quotation", function(next) {
433     factor.cont.data_stack.push(value);   
434     quot.execute(factor.cont.next);
435   }));
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();
456   stack.push(v2 - v1);
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();
464   stack.push(v2 / v1);
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);
473   });
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();
484   var args = [ ];
485   for(var i = 0; i < arg_types.length; ++i) {
486     args[i] = stack.pop();
487   }
488   var func = obj[method_name];
489   if(method_name=="alert") {
490     func = function(a) { alert(a); }
491   }
492   var v = func.apply(obj, args.reverse());
493   if(return_values.length > 0)
494     stack.push(v);
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];
503   stack.push(v);
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;
524   var result = [];
525   for(v in factor.vocabs) {
526     result.push(v);
527   }
528   stack.push(result);
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()];
535   var result = [];
536   for(w in vocab) {
537     result.push(vocab[w]);
538   }
539   stack.push(result);
540   factor.call_next(next);
543 factor.add_word("words", "all-words", "primitive", function(next) {   
544   var stack = factor.cont.data_stack;
545   var result = [];
546   for(v in factor.vocabs) {
547     for(w in factor.vocabs[v]) {
548       result.push(factor.vocabs[v][w]);
549     }
550   }
551   stack.push(result);
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();
567   stack.push(seq[0]);
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();
574   stack.push(seq[1]);
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();
581   stack.push(seq[2]);
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();
588   stack.push(seq[0]);
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) {  
618     stack.push(seq[i]);
619     quot.execute();
620   }
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();
628   var result = [ ];
629   for(var i=0;i<seq.length;++i) {  
630     stack.push(seq[i]);
631     quot.execute();
632     result[i]=stack.pop();
633   }
634   stack.push(result);
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) {  
644     stack.push(prev);
645     stack.push(seq[i]);
646     quot.execute();
647     prev=stack.pop();Al
648   }
649   stack.push(prev);
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]; 
691     stack.push(k, v);
692     quot.execute();
693   }
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;
701   var result = [ ];
702   for(k in hashtable) {  
703     var v = hashtable[k];
704     stack.push(k,v);
705     quot.execute();
706     result.push(stack.pop());
707   }
708   stack.push(result);
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);
740   }
741   factor.get_word("browser-dom", "load-script").execute();