diff --git a/apps/furnace-fjsc/resources/bootstrap.js b/apps/furnace-fjsc/resources/bootstrap.js index 5e3f63f50e..dc822e654a 100644 --- a/apps/furnace-fjsc/resources/bootstrap.js +++ b/apps/furnace-fjsc/resources/bootstrap.js @@ -1,44 +1,213 @@ -function Factor() { - var self = this; - this.form = false; - this.data_stack = [ ]; - 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(); } - }; +function Word(name, source, func) { + this.name = name; + this.source = source; + this.func = func; } -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 callback = { success: function(o) { var v = o.responseText; - eval(v) - self.display_datastack(); document.getElementById('compiled').innerHTML="
" + v + "
"; 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; @@ -54,184 +223,12 @@ Factor.prototype.fjsc_eval = function(form) { Factor.prototype.display_datastack = function() { var html=[]; html.push("") - for(var i = 0; i < this.data_stack.length; ++i) { + for(var i = 0; i < this.data_stack.stack.length; ++i) { html.push("") } html.push("
") - html.push(this.data_stack[i]) + html.push(this.data_stack.stack[i]) html.push("
") 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 'string' <|> ; 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 ] <@ <&> - 'array' [ ast-array-elements ast-expression-values [ ast-string-value ] map ] <@ <:&> + 'array' [ ast-array-elements ast-expression-values ] <@ <:&> "alien-invoke" token sp <& [ first3 ] <@ ; LAZY: 'comment' ( -- parser ) @@ -112,12 +112,12 @@ LAZY: 'comment' ( -- parser ) LAZY: 'expression' ( -- parser ) 'comment' - 'define' sp <|> - 'word' sp <|> 'alien' sp <|> - 'atom' sp <|> 'quotation' sp <|> 'array' sp <|> + 'define' sp <|> + 'word' sp <|> + 'atom' sp <|> <*> [ ] <@ ; LAZY: 'statement' ( -- parser ) @@ -130,9 +130,9 @@ M: ast-number (literal) ast-number-value number>string , ; M: ast-number (compile) - "factor.data_stack.push(" , + "world.data_stack.push(" , (literal) - ")" , ; + ",world," , ; M: ast-string (literal) "'" , @@ -142,30 +142,30 @@ M: ast-string (literal) M: ast-string (compile) "factor.data_stack.push(" , (literal) - ")" , ; + ",world," , ; M: ast-identifier (literal) - "factor.words[\"" , ast-identifier-value , "\"]" , ; + "world.words[\"" , ast-identifier-value , "\"]" , ; M: ast-identifier (compile) - (literal) "();" , ; + (literal) ".execute(world, " , ; M: ast-define (compile) - "factor.words[\"" , + "world.define_word(\"" , dup ast-define-name ast-identifier-value , - "\"]=function() { " , + "\",\"source\"," , ast-define-expression (compile) - "}" , ; + ",world," , ; M: ast-quotation (literal) - "function() { " , + "world.make_quotation(\"source\"," , ast-quotation-expression (compile) - "}" , ; + ")" , ; M: ast-quotation (compile) - "factor.data_stack.push(" , - (literal) - ")" , ; + "world.data_stack.push(world.make_quotation(\"source\"," , + ast-quotation-expression (compile) + "),world," , ; M: ast-array (literal) "[" , @@ -173,30 +173,43 @@ M: ast-array (literal) "]" , ; M: ast-array (compile) - "factor.data_stack.push(" , - (literal) - ")" , ; + "world.data_stack.push(" , (literal) ",world," , ; + M: ast-expression (literal) ast-expression-values [ (literal) ] 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) - ast-expression-values [ (compile) ] [ ";" , ] interleave ; + ast-expression-values do-expressions ; M: ast-alien (compile) + "world.call_alien(" , dup ast-alien-return empty? not [ - "factor.data_stack.push(" , - ] when - dup ast-alien-method , - ".apply(" , - "factor.data_stack.pop(), [" , - dup ast-alien-args [ drop "factor.data_stack.pop()" , ] [ "," , ] interleave - "])" , - ast-alien-return empty? not [ - ")" , - ] when ; + "true," , + ] [ + "false," , + ] if + dup ast-alien-method "\"" , , "\"," , + "factor.data_stack.stack.pop(), [" , + ast-alien-args [ drop "factor.data_stack.stack.pop()" , ] [ "," , ] interleave + "],world," , ; M: ast-word (literal) "factor.words[\"" , @@ -206,19 +219,32 @@ M: ast-word (literal) M: ast-word (compile) "factor.data_stack.push(" , (literal) - ")" , ; + ",world," , ; M: ast-comment (compile) - drop "/* */" , ; + drop ; M: ast-stack-effect (compile) drop ; : fjsc-compile ( ast -- string ) [ - [ (compile) ] { } make [ write ] each + [ + "(" , + (compile) + ")" , + ] { } make [ write ] each ] 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 ) [ [ (literal) ] { } make [ write ] each diff --git a/libs/fjsc/tests.factor b/libs/fjsc/tests.factor index ba467021f8..0c3b1dc20c 100644 --- a/libs/fjsc/tests.factor +++ b/libs/fjsc/tests.factor @@ -16,34 +16,6 @@ IN: temporary "{ 55 2abc1 100 }" 'array' parse car parse-result-parsed ] 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" } } } [ "( -- d e f )" 'stack-effect' parse car parse-result-parsed ] unit-test