From 3fcc63f1bd4032ade99431173ff55ac8c1c05e54 Mon Sep 17 00:00:00 2001 From: "chris.double" Date: Mon, 11 Dec 2006 13:11:10 +0000 Subject: [PATCH] fjsc: refactoring of javascript --- apps/furnace-fjsc/repl.fhtml | 2 +- apps/furnace-fjsc/resources/bootstrap.js | 42 +++++++++++++++--------- libs/fjsc/fjsc.factor | 6 ++-- libs/fjsc/tests.factor | 16 ++++----- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/apps/furnace-fjsc/repl.fhtml b/apps/furnace-fjsc/repl.fhtml index 1c28ef8e90..e50870d923 100644 --- a/apps/furnace-fjsc/repl.fhtml +++ b/apps/furnace-fjsc/repl.fhtml @@ -1,4 +1,4 @@ -
+ diff --git a/apps/furnace-fjsc/resources/bootstrap.js b/apps/furnace-fjsc/resources/bootstrap.js index 25a784c836..68a470be37 100644 --- a/apps/furnace-fjsc/resources/bootstrap.js +++ b/apps/furnace-fjsc/resources/bootstrap.js @@ -1,9 +1,20 @@ -function fjsc_eval(form) { +function Factor() { + var self = this; + this.data_stack = [ ]; + this.words = { + dup: function() { self.fjsc_dup() }, + drop: function() { self.fjsc_drop() }, + alert: function() { self.fjsc_alert() } + }; +} + +Factor.prototype.fjsc_eval = function(form) { + var self = this; var callback = { success: function(o) { var v = o.responseText; eval(v) - display_datastack(); + self.display_datastack(); document.getElementById('compiled').innerHTML="
" + v + "
"; document.getElementById('code').value=""; @@ -13,30 +24,31 @@ function fjsc_eval(form) { YAHOO.util.Connect.asyncRequest('POST', "/responder/fjsc/compile", callback); } -var data_stack = [ ] - -function fjsc_dup() { - var v = data_stack.pop(); - data_stack.push(v); - data_stack.push(v); +Factor.prototype.fjsc_dup = function() { + var stack = this.data_stack; + var v = stack.pop(); + stack.push(v); + stack.push(v); } -function fjsc_drop() { - data_stack.pop(); +Factor.prototype.fjsc_drop = function() { + this.data_stack.pop(); } -function fjsc_alert() { - alert(data_stack.pop()) +Factor.prototype.fjsc_alert = function() { + alert(this.data_stack.pop()); } -function display_datastack() { +Factor.prototype.display_datastack = function() { var html=[]; html.push("") - for(var i = 0; i < data_stack.length; ++i) { + for(var i = 0; i < this.data_stack.length; ++i) { html.push("") } html.push("
") - html.push(data_stack[i]) + html.push(this.data_stack[i]) html.push("
") document.getElementById('stack').innerHTML=html.join(""); } + +var factor = new Factor(); \ No newline at end of file diff --git a/libs/fjsc/fjsc.factor b/libs/fjsc/fjsc.factor index 9868be1f55..0b13719e4d 100644 --- a/libs/fjsc/fjsc.factor +++ b/libs/fjsc/fjsc.factor @@ -42,17 +42,17 @@ LAZY: 'expression' ( -- parser ) GENERIC: (compile) ( ast -- ) M: ast-number (compile) - "data_stack.push(" , + "factor.data_stack.push(" , ast-number-value number>string , ")" , ; M: ast-string (compile) - "data_stack.push('" , + "factor.data_stack.push('" , ast-string-value , "')" , ; M: ast-identifier (compile) - "fjsc_" , ast-identifier-value , "()" , ; + "factor.words[\"" , ast-identifier-value , "\"]()" , ; M: ast-expression (compile) ast-expression-values [ diff --git a/libs/fjsc/tests.factor b/libs/fjsc/tests.factor index e9f7fb983c..98b37ed790 100644 --- a/libs/fjsc/tests.factor +++ b/libs/fjsc/tests.factor @@ -4,19 +4,19 @@ USING: kernel test parser-combinators lazy-lists fjsc ; IN: temporary -{ "data_stack.push(123)" } [ - "123" 'number' parse car parse-result-parsed compile +{ "factor.data_stack.push(123)" } [ + "123" 'number' parse car parse-result-parsed fjsc-compile ] unit-test -{ "fjsc_alert()" } [ - "alert" 'identifier' parse car parse-result-parsed compile +{ "factor.words[\"alert\"]()" } [ + "alert" 'identifier' parse car parse-result-parsed fjsc-compile ] unit-test -{ "data_stack.push(123); fjsc_alert(); " } [ - "123 alert" 'expression' parse car parse-result-parsed compile +{ "factor.data_stack.push(123); factor.words[\"alert\"](); " } [ + "123 alert" 'expression' parse car parse-result-parsed fjsc-compile ] unit-test -{ "data_stack.push(123); data_stack.push('hello'); fjsc_alert(); " } [ - "123 \"hello\" alert" 'expression' parse car parse-result-parsed compile +{ "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 \ No newline at end of file