fjsc: javascript refactorings

chris.double 2006-12-16 23:25:12 +00:00
parent b8163eadfb
commit 81820c1bca
2 changed files with 68 additions and 65 deletions

View File

@ -8,29 +8,21 @@ Word.prototype.execute = function(world, next) {
this.func(world,next); this.func(world,next);
} }
function Stack() { function Continuation() {
this.stack = []; this.data_stack = [ ];
this.retain_stack = [ ];
} }
Stack.prototype.push = function(v,world,next) { Continuation.prototype.clone = function() {
this.stack.push(v); var c = new Continuation();
next(world); c.data_stack = this.data_stack.slice(0);
} c.retain_stack = this.retain_stack.slice(0);
return c;
Stack.prototype.pop = function(world,next) {
this.stack.pop();
next(world);
}
Stack.prototype.clone = function() {
var stack = new Stack();
stack.stack = this.stack.slice(0);
return stack;
} }
function Factor() { function Factor() {
this.words = { }; this.words = { };
this.data_stack = new Stack(); this.cont = new Continuation();
this.form = false ; this.form = false ;
this.next = false; this.next = false;
} }
@ -38,51 +30,65 @@ function Factor() {
var factor = new Factor(); var factor = new Factor();
factor.words["dup"] = new Word("dup", "primitive", function(world, next) { factor.words["dup"] = new Word("dup", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
stack[stack.length] = stack[stack.length-1]; stack[stack.length] = stack[stack.length-1];
next(world); next(world);
}); });
factor.words["drop"] = new Word("drop", "primitive", function(world, next) { factor.words["drop"] = new Word("drop", "primitive", function(world, next) {
world.data_stack.stack.pop(); world.cont.data_stack.pop();
next(world); next(world);
}); });
factor.words["nip"] = new Word("nip", "primitive", function(world, next) { factor.words["nip"] = new Word("nip", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
stack[stack.length-2] = stack[stack.length-1]; stack[stack.length-2] = stack[stack.length-1];
stack.pop(); stack.pop();
next(world); next(world);
}); });
factor.words["over"] = new Word("over", "primitive", function(world, next) { factor.words["over"] = new Word("over", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
stack[stack.length] = stack[stack.length-2]; stack[stack.length] = stack[stack.length-2];
next(world); next(world);
}); });
factor.words["swap"] = new Word("swap", "primitive", function(world, next) { factor.words["swap"] = new Word("swap", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
var temp = stack[stack.length-2]; var temp = stack[stack.length-2];
stack[stack.length-2] = stack[stack.length-1]; stack[stack.length-2] = stack[stack.length-1];
stack[stack.length-1] = temp; stack[stack.length-1] = temp;
next(world); next(world);
}); });
factor.words[">r"] = new Word(">r", "primitive", function(world, next) {
var data_stack = world.cont.data_stack;
var retain_stack = world.cont.retain_stack;
retain_stack.push(data_stack.pop());
next(world);
});
factor.words["r>"] = new Word("r>", "primitive", function(world, next) {
var data_stack = world.cont.data_stack;
var retain_stack = world.cont.retain_stack;
data_stack.push(retain_stack.pop());
next(world);
});
factor.words["*"] = new Word("*", "primitive", function(world, next) { factor.words["*"] = new Word("*", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
stack.push(stack.pop() * stack.pop()); stack.push(stack.pop() * stack.pop());
next(world); next(world);
}); });
factor.words["+"] = new Word("+", "primitive", function(world, next) { factor.words["+"] = new Word("+", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
stack.push(stack.pop() + stack.pop()); stack.push(stack.pop() + stack.pop());
next(world); next(world);
}); });
factor.words["-"] = new Word("-", "primitive", function(world, next) { factor.words["-"] = new Word("-", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
var v1 = stack.pop(); var v1 = stack.pop();
var v2 = stack.pop(); var v2 = stack.pop();
stack.push(v2 - v1); stack.push(v2 - v1);
@ -90,7 +96,7 @@ factor.words["-"] = new Word("-", "primitive", function(world, next) {
}); });
factor.words["/"] = new Word("/", "primitive", function(world, next) { factor.words["/"] = new Word("/", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
var v1 = stack.pop(); var v1 = stack.pop();
var v2 = stack.pop(); var v2 = stack.pop();
stack.push(v2 / v1); stack.push(v2 / v1);
@ -98,33 +104,34 @@ factor.words["/"] = new Word("/", "primitive", function(world, next) {
}); });
factor.words["."] = new Word(".", "primitive", function(world, next) { factor.words["."] = new Word(".", "primitive", function(world, next) {
alert(world.data_stack.stack.pop()); alert(world.cont.data_stack.pop());
next(world); next(world);
}); });
factor.words["call"] = new Word("call", "primitive", function(world, next) { factor.words["call"] = new Word("call", "primitive", function(world, next) {
var quot = world.data_stack.stack.pop(); var quot = world.cont.data_stack.pop();
quot.execute(world, next); quot.execute(world, next);
}); });
factor.words["execute"] = new Word("execute", "primitive", function(world, next) { factor.words["execute"] = new Word("execute", "primitive", function(world, next) {
var quot = world.data_stack.stack.pop(); var quot = world.cont.data_stack.pop();
quot.execute(world, next); quot.execute(world, next);
}); });
factor.words["clear"] = new Word("clear", "primitive", function(world, next) { factor.words["clear"] = new Word("clear", "primitive", function(world, next) {
world.data_stack.stack = []; world.cont.data_stack = [];
world.cont.retain_stack = [];
next(world); next(world);
}); });
factor.words["square"] = new Word("square", "primitive", function(world, next) { factor.words["square"] = new Word("square", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
stack[stack.length-1] = stack[stack.length-1] * stack[stack.length-1]; stack[stack.length-1] = stack[stack.length-1] * stack[stack.length-1];
next(world); next(world);
}); });
factor.words["if"] = new Word("if", "primitive", function(world, next) { factor.words["if"] = new Word("if", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
var else_quot = stack.pop(); var else_quot = stack.pop();
var then_quot = stack.pop(); var then_quot = stack.pop();
var condition = stack.pop(); var condition = stack.pop();
@ -136,33 +143,33 @@ factor.words["if"] = new Word("if", "primitive", function(world, next) {
}); });
factor.words["f"] = new Word("f", "primitive", function(world, next) { factor.words["f"] = new Word("f", "primitive", function(world, next) {
world.data_stack.stack.push(false); world.cont.data_stack.push(false);
next(world); next(world);
}); });
factor.words["t"] = new Word("t", "primitive", function(world, next) { factor.words["t"] = new Word("t", "primitive", function(world, next) {
world.data_stack.stack.push(true); world.cont.data_stack.push(true);
next(world); next(world);
}); });
factor.words["="] = new Word("=", "primitive", function(world, next) { factor.words["="] = new Word("=", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
stack.push(stack.pop()==stack.pop()); stack.push(stack.pop()==stack.pop());
next(world); next(world);
}); });
factor.words["window"] = new Word("window", "primitive", function(world, next) { factor.words["window"] = new Word("window", "primitive", function(world, next) {
world.data_stack.stack.push(window); world.cont.data_stack.push(window);
next(world); next(world);
}); });
factor.words["bootstrap"] = new Word("bootstrap", "primitive", function(world, next) { factor.words["bootstrap"] = new Word("bootstrap", "primitive", function(world, next) {
world.data_stack.stack.push("/responder/fjsc-resources/bootstrap.factor"); world.cont.data_stack.push("/responder/fjsc-resources/bootstrap.factor");
world.words["run-file"].execute(world, next); world.words["run-file"].execute(world, next);
}); });
factor.words["run-file"] = new Word("run-file", "primitive", function(world, next) { factor.words["run-file"] = new Word("run-file", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
var url = stack.pop(); var url = stack.pop();
var callback = { var callback = {
success: function(o) { success: function(o) {
@ -178,9 +185,9 @@ factor.words["run-file"] = new Word("run-file", "primitive", function(world, nex
}); });
factor.words["callcc0"] = new Word("callcc0", "primitive", function(world, next) { factor.words["callcc0"] = new Word("callcc0", "primitive", function(world, next) {
var stack = world.data_stack; var data_stack = world.cont.data_stack;
var quot = stack.stack.pop(); var quot = data_stack.pop();
var new_stack = stack.clone(); var new_cont = world.cont.clone();
var old_next = world.next; var old_next = world.next;
var cont = { var cont = {
world: world, world: world,
@ -188,22 +195,22 @@ factor.words["callcc0"] = new Word("callcc0", "primitive", function(world, next)
world.next = old_next; world.next = old_next;
next(world); next(world);
}, },
stack: stack cont: world.cont
}; };
new_stack.stack.push(cont); new_cont.data_stack.push(cont);
world.data_stack = new_stack; world.cont = new_cont;;
quot.execute(world, next); quot.execute(world, next);
}); });
factor.words["continue"] = new Word("continue", "primitive", function(world, next) { factor.words["continue"] = new Word("continue", "primitive", function(world, next) {
var stack = world.data_stack; var data_stack = world.cont.data_stack;
var cont = stack.stack.pop(); var cont = data_stack.pop();
world.data_stack = cont.stack.clone(); world.cont = cont.cont.clone();
(cont.next)(world); (cont.next)(world);
}); });
factor.words["alien-invoke"] = new Word("alien-invoke", "primitive", function(world, next) { factor.words["alien-invoke"] = new Word("alien-invoke", "primitive", function(world, next) {
var stack = world.data_stack.stack; var stack = world.cont.data_stack;
var arg_types = stack.pop(); var arg_types = stack.pop();
var method_name = stack.pop(); var method_name = stack.pop();
var library_name = stack.pop(); var library_name = stack.pop();
@ -219,6 +226,11 @@ factor.words["alien-invoke"] = new Word("alien-invoke", "primitive", function(wo
next(world); next(world);
}); });
Factor.prototype.push_data = function(v, world, next) {
world.cont.data_stack.push(v);
next(world);
}
Factor.prototype.define_word = function(name, source, func, world, next) { Factor.prototype.define_word = function(name, source, func, world, next) {
factor.words[name] = new Word(name, source, function(world, next) { factor.words[name] = new Word(name, source, function(world, next) {
var old = world.next; var old = world.next;
@ -242,14 +254,6 @@ Factor.prototype.make_quotation = function(source, func) {
}); });
} }
Factor.prototype.call_alien = function(has_return,method_name, object, args, world, next) {
var v = object[method_name].apply(object, args);
if(has_return)
world.data_stack.stack.push(v);
next(world);
}
Factor.prototype.server_eval = function(text, world, next) { Factor.prototype.server_eval = function(text, world, next) {
var self = this; var self = this;
var callback = { var callback = {
@ -277,12 +281,11 @@ Factor.prototype.fjsc_eval = function(form) {
Factor.prototype.display_datastack = function() { Factor.prototype.display_datastack = function() {
var html=[]; var html=[];
html.push("<table border='1'>") html.push("<table border='1'>")
for(var i = 0; i < this.data_stack.stack.length; ++i) { for(var i = 0; i < this.cont.data_stack.length; ++i) {
html.push("<tr><td>") html.push("<tr><td>")
html.push(this.data_stack.stack[i]) html.push(this.cont.data_stack[i])
html.push("</td></tr>") html.push("</td></tr>")
} }
html.push("</table>") html.push("</table>")
document.getElementById('stack').innerHTML=html.join(""); document.getElementById('stack').innerHTML=html.join("");
} }

View File

@ -122,7 +122,7 @@ M: ast-number (literal)
ast-number-value number>string , ; ast-number-value number>string , ;
M: ast-number (compile) M: ast-number (compile)
"world.data_stack.push(" , "world.push_data(" ,
(literal) (literal)
",world," , ; ",world," , ;
@ -132,7 +132,7 @@ M: ast-string (literal)
"'" , ; "'" , ;
M: ast-string (compile) M: ast-string (compile)
"factor.data_stack.push(" , "factor.push_data(" ,
(literal) (literal)
",world," , ; ",world," , ;
@ -170,7 +170,7 @@ M: ast-quotation (literal)
")" , ; ")" , ;
M: ast-quotation (compile) M: ast-quotation (compile)
"world.data_stack.push(world.make_quotation(\"source\"," , "world.push_data(world.make_quotation(\"source\"," ,
ast-quotation-values do-expressions ast-quotation-values do-expressions
"),world," , ; "),world," , ;
@ -180,7 +180,7 @@ M: ast-array (literal)
"]" , ; "]" , ;
M: ast-array (compile) M: ast-array (compile)
"world.data_stack.push(" , (literal) ",world," , ; "world.push_data(" , (literal) ",world," , ;
M: ast-expression (literal) M: ast-expression (literal)
@ -197,7 +197,7 @@ M: ast-word (literal)
"\"]" , ; "\"]" , ;
M: ast-word (compile) M: ast-word (compile)
"factor.data_stack.push(" , "factor.push_data(" ,
(literal) (literal)
",world," , ; ",world," , ;