-<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>
<input type="submit"/>
-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="<pre>" + v + "</pre>";
document.getElementById('code').value="";
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("<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(data_stack[i])
+ html.push(this.data_stack[i])
html.push("</td></tr>")
}
html.push("</table>")
document.getElementById('stack').innerHTML=html.join("");
}
+
+var factor = new Factor();
\ No newline at end of file
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 [
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