fjsc: move to CPS compiling javascript

chris.double 2006-12-16 13:46:04 +00:00
parent 6c6d131b0b
commit f25f82cc97
3 changed files with 265 additions and 270 deletions

View File

@ -1,44 +1,213 @@
function Factor() { function Word(name, source, func) {
var self = this; this.name = name;
this.form = false; this.source = source;
this.data_stack = [ ]; this.func = func;
this.words = {
dup: function() { self.fjsc_dup(); },
drop: function() { self.fjsc_drop(); },
nip: function() { self.fjsc_nip(); },
over: function() { self.fjsc_over(); },
swap: function() { self.fjsc_swap(); },
"+": function() { self.fjsc_plus(); },
"-": function() { self.fjsc_minus(); },
"*": function() { self.fjsc_times(); },
"/": function() { self.fjsc_divide(); },
".": function() { self.fjsc_dot(); },
"call": function() { self.fjsc_call(); },
"execute": function() { self.fjsc_call(); },
"map": function() { self.fjsc_map(); },
"reduce": function() { self.fjsc_reduce(); },
"clear": function() { self.fjsc_clear(); },
"if": function() { self.fjsc_if(); },
"=": function() { self.fjsc_equals(); },
"f": function() { self.fjsc_false(); },
"t": function() { self.fjsc_true(); },
"empty?": function() { self.fjsc_is_empty(); },
"window": function() { self.fjsc_window(); },
"run-file": function() { self.fjsc_run_file(); },
"http-get": function() { self.fjsc_http_get(); },
"bootstrap": function() { self.fjsc_bootstrap(); }
};
} }
Factor.prototype.server_eval = function(text) { Word.prototype.execute = function(world, next) {
this.func(world,next);
}
function Stack() {
this.stack = [];
}
Stack.prototype.push = function(v,world,next) {
this.stack.push(v);
next(world);
}
Stack.prototype.pop = function(world,next) {
this.stack.pop();
next(world);
}
function Factor() {
this.words = { };
this.data_stack = new Stack();
this.form = false ;
this.next = false;
}
var factor = new Factor();
factor.words["dup"] = new Word("dup", "primitive", function(world, next) {
var stack = world.data_stack.stack;
stack[stack.length] = stack[stack.length-1];
next(world);
});
factor.words["drop"] = new Word("drop", "primitive", function(world, next) {
world.data_stack.stack.pop();
next(world);
});
factor.words["nip"] = new Word("nip", "primitive", function(world, next) {
var stack = world.data_stack.stack;
stack[stack.length-2] = stack[stack.length-1];
stack.pop();
next(world);
});
factor.words["over"] = new Word("over", "primitive", function(world, next) {
var stack = world.data_stack.stack;
stack[stack.length] = stack[stack.length-2];
next(world);
});
factor.words["swap"] = new Word("swap", "primitive", function(world, next) {
var stack = world.data_stack.stack;
var temp = stack[stack.length-2];
stack[stack.length-2] = stack[stack.length-1];
stack[stack.length-1] = temp;
next(world);
});
factor.words["*"] = new Word("*", "primitive", function(world, next) {
var stack = world.data_stack.stack;
stack.push(stack.pop() * stack.pop());
next(world);
});
factor.words["+"] = new Word("+", "primitive", function(world, next) {
var stack = world.data_stack.stack;
stack.push(stack.pop() + stack.pop());
next(world);
});
factor.words["-"] = new Word("-", "primitive", function(world, next) {
var stack = world.data_stack.stack;
var v1 = stack.pop();
var v2 = stack.pop();
stack.push(v2 - v1);
next(world);
});
factor.words["/"] = new Word("/", "primitive", function(world, next) {
var stack = world.data_stack.stack;
var v1 = stack.pop();
var v2 = stack.pop();
stack.push(v2 / v1);
next(world);
});
factor.words["."] = new Word(".", "primitive", function(world, next) {
alert(world.data_stack.stack.pop());
next(world);
});
factor.words["call"] = new Word("call", "primitive", function(world, next) {
var quot = world.data_stack.stack.pop();
quot.execute(world, next);
});
factor.words["execute"] = new Word("execute", "primitive", function(world, next) {
var quot = world.data_stack.stack.pop();
quot.execute(world, next);
});
factor.words["clear"] = new Word("clear", "primitive", function(world, next) {
world.data_stack.stack = [];
next(world);
});
factor.words["square"] = new Word("square", "primitive", function(world, next) {
var stack = world.data_stack.stack;
stack[stack.length-1] = stack[stack.length-1] * stack[stack.length-1];
next(world);
});
factor.words["if"] = new Word("if", "primitive", function(world, next) {
var stack = world.data_stack.stack;
var else_quot = stack.pop();
var then_quot = stack.pop();
var condition = stack.pop();
if(condition) {
then_quot.execute(world, next);
} else {
else_quot.execute(world, next);
}
});
factor.words["f"] = new Word("f", "primitive", function(world, next) {
world.data_stack.stack.push(false);
next(world);
});
factor.words["t"] = new Word("t", "primitive", function(world, next) {
world.data_stack.stack.push(true);
next(world);
});
factor.words["window"] = new Word("window", "primitive", function(world, next) {
world.data_stack.stack.push(window);
next(world);
});
factor.words["bootstrap"] = new Word("bootstrap", "primitive", function(world, next) {
world.data_stack.stack.push("/responder/fjsc-resources/bootstrap.factor");
world.words["run-file"].execute(world, next);
});
factor.words["run-file"] = new Word("run-file", "primitive", function(world, next) {
var stack = world.data_stack.stack;
var url = stack.pop();
var callback = {
success: function(o) {
var result = o.responseText;
world.server_eval(result, world, next);
},
failure: function(o) {
alert('run-file failed');
next(world);
}
};
YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
});
Factor.prototype.define_word = function(name, source, func, world, next) {
factor.words[name] = new Word(name, source, function(world, next) {
var old = world.next;
world.next = function(world) {
world.next = old;
next(world);
}
func(world);
});
next(world);
}
Factor.prototype.make_quotation = function(source, func) {
return new Word("quotation", source, function(world, next) {
var old = world.next;
world.next = function(world) {
world.next = old;
next(world);
}
func(world);
});
}
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) {
var self = this; var self = this;
var callback = { var callback = {
success: function(o) { success: function(o) {
var v = o.responseText; var v = o.responseText;
eval(v)
self.display_datastack();
document.getElementById('compiled').innerHTML="<pre>" + v + "</pre>"; document.getElementById('compiled').innerHTML="<pre>" + v + "</pre>";
document.getElementById('code').value=""; document.getElementById('code').value="";
var func = eval(v);
factor.next = function() { self.display_datastack(); }
func(factor);
if(world && next)
next(world);
} }
}; };
this.form.code.value=text; this.form.code.value=text;
@ -54,184 +223,12 @@ 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.length; ++i) { for(var i = 0; i < this.data_stack.stack.length; ++i) {
html.push("<tr><td>") html.push("<tr><td>")
html.push(this.data_stack[i]) html.push(this.data_stack.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("");
} }
Factor.prototype.fjsc_dup = function() {
var stack = this.data_stack;
var v = stack.pop();
stack.push(v);
stack.push(v);
}
Factor.prototype.fjsc_drop = function() {
this.data_stack.pop();
}
Factor.prototype.fjsc_nip = function() {
var stack = this.data_stack;
var v = stack.pop();
stack.pop();
stack.push(v);
}
Factor.prototype.fjsc_plus = function() {
var stack = this.data_stack;
var v1 = stack.pop();
var v2 = stack.pop();
stack.push(v1+v2);
}
Factor.prototype.fjsc_minus = function() {
var stack = this.data_stack;
var v1 = stack.pop();
var v2 = stack.pop();
stack.push(v2-v1);
}
Factor.prototype.fjsc_times = function() {
var stack = this.data_stack;
var v1 = stack.pop();
var v2 = stack.pop();
stack.push(v1*v2);
}
Factor.prototype.fjsc_divide = function() {
var stack = this.data_stack;
var v1 = stack.pop();
var v2 = stack.pop();
stack.push(v2/v1);
}
Factor.prototype.fjsc_dot = function() {
alert(this.data_stack.pop());
}
Factor.prototype.fjsc_call = function() {
(this.data_stack.pop())();
}
Factor.prototype.fjsc_map = function() {
var quot = this.data_stack.pop();
var seq = this.data_stack.pop();
var result = [ ];
for(var i=0;i<seq.length;++i) {
this.data_stack.push(seq[i]);
(quot)();
result[i]=this.data_stack.pop();
}
this.data_stack.push(result);
}
Factor.prototype.fjsc_reduce = function() {
var quot = this.data_stack.pop();
var prev = this.data_stack.pop();
var seq = this.data_stack.pop();
for(var i=0;i<seq.length;++i) {
this.data_stack.push(prev);
this.data_stack.push(seq[i]);
(quot)();
prev=this.data_stack.pop();
}
this.data_stack.push(prev);
}
Factor.prototype.fjsc_if = function() {
var else_quot = this.data_stack.pop();
var then_quot = this.data_stack.pop();
var condition = this.data_stack.pop();
if(condition) {
(then_quot)();
} else {
(else_quot)();
}
}
Factor.prototype.fjsc_equals = function() {
var v1 = this.data_stack.pop();
var v2 = this.data_stack.pop();
this.data_stack.push(v1==v2);
}
Factor.prototype.fjsc_clear = function() {
factor.data_stack = [ ]
}
Factor.prototype.fjsc_false = function() {
factor.data_stack.push(false);
}
Factor.prototype.fjsc_true = function() {
factor.data_stack.push(true);
}
Factor.prototype.fjsc_is_empty = function() {
factor.data_stack.push(factor.data_stack.pop().length==0);
}
Factor.prototype.fjsc_over = function() {
var stack = this.data_stack;
stack.push(stack[stack.length-2]);
}
Factor.prototype.fjsc_swap = function() {
var stack = this.data_stack;
var len = stack.length;
var temp = stack[len-2];
stack[len-2] = stack[len-1];
stack[len-1] = temp;
}
Factor.prototype.fjsc_window = function() {
var stack = this.data_stack;
stack.push(window);
}
Factor.prototype.fjsc_run_file = function() {
var self = this;
var stack = this.data_stack;
var url = stack.pop();
var callback = {
success: function(o) {
var result = o.responseText;
self.server_eval(result);
},
failure: function(o) {
alert('run-file failed');
}
};
YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
}
Factor.prototype.fjsc_http_get = function() {
var self = this;
var stack = this.data_stack;
var url = stack.pop();
var callback = {
success: function(o) {
var result = o.responseText;
self.data_stack.push(result);
self.display_datastack();
},
failure: function(o) {
alert('http-get failed');
}
};
YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
}
Factor.prototype.fjsc_bootstrap = function() {
this.data_stack.push("/responder/fjsc-resources/bootstrap.factor");
this.fjsc_run_file();
}
var factor = new Factor();

View File

@ -99,9 +99,9 @@ LAZY: 'atom' ( -- parser )
'identifier' 'number' <|> 'string' <|> ; 'identifier' 'number' <|> 'string' <|> ;
LAZY: 'alien' ( -- parser ) LAZY: 'alien' ( -- parser )
'array' [ ast-array-elements ast-expression-values [ ast-string-value ] map ] <@ 'array' [ ast-array-elements ast-expression-values ] <@
'string' [ ast-string-value ] <@ <&> 'string' [ ast-string-value ] <@ <&>
'array' [ ast-array-elements ast-expression-values [ ast-string-value ] map ] <@ <:&> 'array' [ ast-array-elements ast-expression-values ] <@ <:&>
"alien-invoke" token sp <& [ first3 <ast-alien> ] <@ ; "alien-invoke" token sp <& [ first3 <ast-alien> ] <@ ;
LAZY: 'comment' ( -- parser ) LAZY: 'comment' ( -- parser )
@ -112,12 +112,12 @@ LAZY: 'comment' ( -- parser )
LAZY: 'expression' ( -- parser ) LAZY: 'expression' ( -- parser )
'comment' 'comment'
'define' sp <|>
'word' sp <|>
'alien' sp <|> 'alien' sp <|>
'atom' sp <|>
'quotation' sp <|> 'quotation' sp <|>
'array' sp <|> 'array' sp <|>
'define' sp <|>
'word' sp <|>
'atom' sp <|>
<*> [ <ast-expression> ] <@ ; <*> [ <ast-expression> ] <@ ;
LAZY: 'statement' ( -- parser ) LAZY: 'statement' ( -- parser )
@ -130,9 +130,9 @@ M: ast-number (literal)
ast-number-value number>string , ; ast-number-value number>string , ;
M: ast-number (compile) M: ast-number (compile)
"factor.data_stack.push(" , "world.data_stack.push(" ,
(literal) (literal)
")" , ; ",world," , ;
M: ast-string (literal) M: ast-string (literal)
"'" , "'" ,
@ -142,30 +142,30 @@ M: ast-string (literal)
M: ast-string (compile) M: ast-string (compile)
"factor.data_stack.push(" , "factor.data_stack.push(" ,
(literal) (literal)
")" , ; ",world," , ;
M: ast-identifier (literal) M: ast-identifier (literal)
"factor.words[\"" , ast-identifier-value , "\"]" , ; "world.words[\"" , ast-identifier-value , "\"]" , ;
M: ast-identifier (compile) M: ast-identifier (compile)
(literal) "();" , ; (literal) ".execute(world, " , ;
M: ast-define (compile) M: ast-define (compile)
"factor.words[\"" , "world.define_word(\"" ,
dup ast-define-name ast-identifier-value , dup ast-define-name ast-identifier-value ,
"\"]=function() { " , "\",\"source\"," ,
ast-define-expression (compile) ast-define-expression (compile)
"}" , ; ",world," , ;
M: ast-quotation (literal) M: ast-quotation (literal)
"function() { " , "world.make_quotation(\"source\"," ,
ast-quotation-expression (compile) ast-quotation-expression (compile)
"}" , ; ")" , ;
M: ast-quotation (compile) M: ast-quotation (compile)
"factor.data_stack.push(" , "world.data_stack.push(world.make_quotation(\"source\"," ,
(literal) ast-quotation-expression (compile)
")" , ; "),world," , ;
M: ast-array (literal) M: ast-array (literal)
"[" , "[" ,
@ -173,30 +173,43 @@ M: ast-array (literal)
"]" , ; "]" , ;
M: ast-array (compile) M: ast-array (compile)
"factor.data_stack.push(" , "world.data_stack.push(" , (literal) ",world," , ;
(literal)
")" , ;
M: ast-expression (literal) M: ast-expression (literal)
ast-expression-values [ ast-expression-values [
(literal) (literal)
] each ; ] each ;
: do-expressions ( seq -- )
dup empty? not [
unclip
dup ast-comment? not [
"function(world) {" ,
(compile)
do-expressions
")}" ,
] [
drop do-expressions
] if
] [
drop "world.next" ,
] if ;
M: ast-expression (compile) M: ast-expression (compile)
ast-expression-values [ (compile) ] [ ";" , ] interleave ; ast-expression-values do-expressions ;
M: ast-alien (compile) M: ast-alien (compile)
"world.call_alien(" ,
dup ast-alien-return empty? not [ dup ast-alien-return empty? not [
"factor.data_stack.push(" , "true," ,
] when ] [
dup ast-alien-method , "false," ,
".apply(" , ] if
"factor.data_stack.pop(), [" , dup ast-alien-method "\"" , , "\"," ,
dup ast-alien-args [ drop "factor.data_stack.pop()" , ] [ "," , ] interleave "factor.data_stack.stack.pop(), [" ,
"])" , ast-alien-args [ drop "factor.data_stack.stack.pop()" , ] [ "," , ] interleave
ast-alien-return empty? not [ "],world," , ;
")" ,
] when ;
M: ast-word (literal) M: ast-word (literal)
"factor.words[\"" , "factor.words[\"" ,
@ -206,19 +219,32 @@ M: ast-word (literal)
M: ast-word (compile) M: ast-word (compile)
"factor.data_stack.push(" , "factor.data_stack.push(" ,
(literal) (literal)
")" , ; ",world," , ;
M: ast-comment (compile) M: ast-comment (compile)
drop "/* */" , ; drop ;
M: ast-stack-effect (compile) M: ast-stack-effect (compile)
drop ; drop ;
: fjsc-compile ( ast -- string ) : fjsc-compile ( ast -- string )
[ [
[ (compile) ] { } make [ write ] each [
"(" ,
(compile)
")" ,
] { } make [ write ] each
] string-out ; ] string-out ;
: fjsc-compile* ( string -- string )
'statement' parse car parse-result-parsed fjsc-compile ;
: fc* ( string -- string )
[
'statement' parse car parse-result-parsed ast-expression-values do-expressions
] { } make [ write ] each ;
: fjsc-literal ( ast -- string ) : fjsc-literal ( ast -- string )
[ [
[ (literal) ] { } make [ write ] each [ (literal) ] { } make [ write ] each

View File

@ -16,34 +16,6 @@ IN: temporary
"{ 55 2abc1 100 }" 'array' parse car parse-result-parsed "{ 55 2abc1 100 }" 'array' parse car parse-result-parsed
] unit-test ] unit-test
{ "factor.words[\"alert\"]();" } [
"alert" 'identifier' parse car parse-result-parsed fjsc-compile
] unit-test
{ "factor.data_stack.push(123);factor.words[\"alert\"]();" } [
"123 alert" 'expression' parse car parse-result-parsed fjsc-compile
] unit-test
{ "factor.data_stack.push(123);factor.data_stack.push('hello');factor.words[\"alert\"]();" } [
"123 \"hello\" alert" 'expression' parse car parse-result-parsed fjsc-compile
] unit-test
{ "factor.words[\"foo\"]=function() { factor.data_stack.push(123);factor.data_stack.push('hello')}" } [
": foo 123 \"hello\" ;" 'define' parse car parse-result-parsed fjsc-compile
] unit-test
{ "factor.words[\"foo\"]=function() { factor.data_stack.push(123);factor.data_stack.push('hello')}" } [
": foo 123 \"hello\" ;" 'expression' parse car parse-result-parsed fjsc-compile
] unit-test
{ "alert.apply(factor.data_stack.pop(), [factor.data_stack.pop()])" } [
"{ } \"alert\" { \"string\" } alien-invoke" 'expression' parse car parse-result-parsed fjsc-compile
] unit-test
{ "factor.data_stack.push(alert.apply(factor.data_stack.pop(), [factor.data_stack.pop()]))" } [
"{ \"string\" } \"alert\" { \"string\" } alien-invoke" 'expression' parse car parse-result-parsed fjsc-compile
] unit-test
{ T{ ast-stack-effect f { } { "d" "e" "f" } } } [ { T{ ast-stack-effect f { } { "d" "e" "f" } } } [
"( -- d e f )" 'stack-effect' parse car parse-result-parsed "( -- d e f )" 'stack-effect' parse car parse-result-parsed
] unit-test ] unit-test