fjsc: refactoring of javascript
parent
713d328415
commit
3fcc63f1bd
|
@ -1,4 +1,4 @@
|
||||||
<form id="toeval" onsubmit="fjsc_eval(document.getElementById('toeval'));return false;" method="post">
|
<form id="toeval" onsubmit="factor.fjsc_eval(document.getElementById('toeval'));return false;" method="post">
|
||||||
<textarea name="code" id="code">
|
<textarea name="code" id="code">
|
||||||
</textarea>
|
</textarea>
|
||||||
<input type="submit"/>
|
<input type="submit"/>
|
||||||
|
|
|
@ -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 = {
|
var callback = {
|
||||||
success: function(o) {
|
success: function(o) {
|
||||||
var v = o.responseText;
|
var v = o.responseText;
|
||||||
eval(v)
|
eval(v)
|
||||||
display_datastack();
|
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="";
|
||||||
|
|
||||||
|
@ -13,30 +24,31 @@ function fjsc_eval(form) {
|
||||||
YAHOO.util.Connect.asyncRequest('POST', "/responder/fjsc/compile", callback);
|
YAHOO.util.Connect.asyncRequest('POST', "/responder/fjsc/compile", callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
var data_stack = [ ]
|
Factor.prototype.fjsc_dup = function() {
|
||||||
|
var stack = this.data_stack;
|
||||||
function fjsc_dup() {
|
var v = stack.pop();
|
||||||
var v = data_stack.pop();
|
stack.push(v);
|
||||||
data_stack.push(v);
|
stack.push(v);
|
||||||
data_stack.push(v);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function fjsc_drop() {
|
Factor.prototype.fjsc_drop = function() {
|
||||||
data_stack.pop();
|
this.data_stack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
function fjsc_alert() {
|
Factor.prototype.fjsc_alert = function() {
|
||||||
alert(data_stack.pop())
|
alert(this.data_stack.pop());
|
||||||
}
|
}
|
||||||
|
|
||||||
function display_datastack() {
|
Factor.prototype.display_datastack = function() {
|
||||||
var html=[];
|
var html=[];
|
||||||
html.push("<table border='1'>")
|
html.push("<table border='1'>")
|
||||||
for(var i = 0; i < data_stack.length; ++i) {
|
for(var i = 0; i < this.data_stack.length; ++i) {
|
||||||
html.push("<tr><td>")
|
html.push("<tr><td>")
|
||||||
html.push(data_stack[i])
|
html.push(this.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("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var factor = new Factor();
|
|
@ -42,17 +42,17 @@ LAZY: 'expression' ( -- parser )
|
||||||
GENERIC: (compile) ( ast -- )
|
GENERIC: (compile) ( ast -- )
|
||||||
|
|
||||||
M: ast-number (compile)
|
M: ast-number (compile)
|
||||||
"data_stack.push(" ,
|
"factor.data_stack.push(" ,
|
||||||
ast-number-value number>string ,
|
ast-number-value number>string ,
|
||||||
")" , ;
|
")" , ;
|
||||||
|
|
||||||
M: ast-string (compile)
|
M: ast-string (compile)
|
||||||
"data_stack.push('" ,
|
"factor.data_stack.push('" ,
|
||||||
ast-string-value ,
|
ast-string-value ,
|
||||||
"')" , ;
|
"')" , ;
|
||||||
|
|
||||||
M: ast-identifier (compile)
|
M: ast-identifier (compile)
|
||||||
"fjsc_" , ast-identifier-value , "()" , ;
|
"factor.words[\"" , ast-identifier-value , "\"]()" , ;
|
||||||
|
|
||||||
M: ast-expression (compile)
|
M: ast-expression (compile)
|
||||||
ast-expression-values [
|
ast-expression-values [
|
||||||
|
|
|
@ -4,19 +4,19 @@
|
||||||
USING: kernel test parser-combinators lazy-lists fjsc ;
|
USING: kernel test parser-combinators lazy-lists fjsc ;
|
||||||
IN: temporary
|
IN: temporary
|
||||||
|
|
||||||
{ "data_stack.push(123)" } [
|
{ "factor.data_stack.push(123)" } [
|
||||||
"123" 'number' parse car parse-result-parsed compile
|
"123" 'number' parse car parse-result-parsed fjsc-compile
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ "fjsc_alert()" } [
|
{ "factor.words[\"alert\"]()" } [
|
||||||
"alert" 'identifier' parse car parse-result-parsed compile
|
"alert" 'identifier' parse car parse-result-parsed fjsc-compile
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ "data_stack.push(123); fjsc_alert(); " } [
|
{ "factor.data_stack.push(123); factor.words[\"alert\"](); " } [
|
||||||
"123 alert" 'expression' parse car parse-result-parsed compile
|
"123 alert" 'expression' parse car parse-result-parsed fjsc-compile
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ "data_stack.push(123); data_stack.push('hello'); fjsc_alert(); " } [
|
{ "factor.data_stack.push(123); factor.data_stack.push('hello'); factor.words[\"alert\"](); " } [
|
||||||
"123 \"hello\" alert" 'expression' parse car parse-result-parsed compile
|
"123 \"hello\" alert" 'expression' parse car parse-result-parsed fjsc-compile
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
Loading…
Reference in New Issue