var self = this;
this.data_stack = [ ];
this.words = {
- dup: function() { self.fjsc_dup() },
- drop: function() { self.fjsc_drop() },
- alert: function() { self.fjsc_alert() }
+ dup: function() { self.fjsc_dup(); },
+ drop: function() { self.fjsc_drop(); },
+ nip: function() { self.fjsc_nip(); },
+ alert: function() { self.fjsc_alert(); }
};
}
YAHOO.util.Connect.asyncRequest('POST', "/responder/fjsc/compile", callback);
}
+Factor.prototype.display_datastack = function() {
+ var html=[];
+ html.push("<table border='1'>")
+ for(var i = 0; i < this.data_stack.length; ++i) {
+ html.push("<tr><td>")
+ html.push(this.data_stack[i])
+ html.push("</td></tr>")
+ }
+ html.push("</table>")
+ document.getElementById('stack').innerHTML=html.join("");
+}
+
Factor.prototype.fjsc_dup = function() {
var stack = this.data_stack;
var v = stack.pop();
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_alert = function() {
alert(this.data_stack.pop());
}
-Factor.prototype.display_datastack = function() {
- var html=[];
- html.push("<table border='1'>")
- for(var i = 0; i < this.data_stack.length; ++i) {
- html.push("<tr><td>")
- 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
TUPLE: ast-number value ;
TUPLE: ast-identifier value ;
TUPLE: ast-string value ;
+TUPLE: ast-define name expression ;
TUPLE: ast-expression values ;
LAZY: 'digit' ( -- parser )
'quote' sp [
CHAR: " = not
] satisfy <+> [ >string <ast-string> ] <@ &> 'quote' <& ;
-
-LAZY: 'identifier' ( -- parser )
+
+LAZY: 'identifier-ends' ( -- parser )
[
[ blank? not ] keep
- [ digit? not ] keep
[ CHAR: : = not ] keep
[ CHAR: " = not ] keep
CHAR: ; = not
+ and and and
+ ] satisfy <*> ;
+
+LAZY: 'identifier-middle' ( -- parser )
+ [
+ [ blank? not ] keep
+ [ CHAR: : = not ] keep
+ [ CHAR: " = not ] keep
+ [ CHAR: ; = not ] keep
+ digit? not
and and and and
- ] satisfy <+> [ >string <ast-identifier> ] <@ ;
+ ] satisfy <+> ;
+
+USE: prettyprint
+LAZY: 'identifier' ( -- parser )
+ 'identifier-ends'
+ 'identifier-middle' <&> [ first2 append ] <@
+ 'identifier-ends' <&> [ first2 append ] <@
+ [ >string <ast-identifier> ] <@ ;
+
+LAZY: 'define' ( -- parser )
+ ":" token sp
+ 'identifier' sp &>
+ 'expression' <&>
+ ";" token sp <& [ first2 <ast-define> ] <@ ;
LAZY: 'atom' ( -- parser )
- 'number' 'identifier' <|> 'string' <|> ;
+ 'identifier' 'number' <|> 'string' <|> ;
LAZY: 'expression' ( -- parser )
- 'atom' sp <*> [ <ast-expression> ] <@ ;
+ 'define' sp 'atom' sp <|> <*> [ <ast-expression> ] <@ ;
+
+LAZY: 'statement' ( -- parser )
+ 'define' 'expression' <|> ;
GENERIC: (compile) ( ast -- )
M: ast-identifier (compile)
"factor.words[\"" , ast-identifier-value , "\"]()" , ;
+M: ast-define (compile)
+ "factor.words[\"" ,
+ dup ast-define-name ast-identifier-value ,
+ "\"]=function() { " ,
+ ast-define-expression (compile)
+ "}" , ;
+
M: ast-expression (compile)
ast-expression-values [
(compile) "; " ,
USING: kernel test parser-combinators lazy-lists fjsc ;
IN: temporary
-{ "factor.data_stack.push(123)" } [
- "123" 'number' parse car parse-result-parsed fjsc-compile
+{ T{ ast-expression f { T{ ast-number f 55 } T{ ast-identifier f "2abc1" } T{ ast-number f 100 } } } } [
+ "55 2abc1 100" 'expression' parse car parse-result-parsed
] unit-test
{ "factor.words[\"alert\"]()" } [
{ "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
+
+{ "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
+