]> gitweb.factorcode.org Git - factor.git/commitdiff
fjsc: add ability to define new words
authorchris.double <chris.double@double.co.nz>
Mon, 11 Dec 2006 13:55:00 +0000 (13:55 +0000)
committerchris.double <chris.double@double.co.nz>
Mon, 11 Dec 2006 13:55:00 +0000 (13:55 +0000)
apps/furnace-fjsc/resources/bootstrap.js
libs/fjsc/fjsc.factor
libs/fjsc/tests.factor

index 68a470be37e341069486cd97f4775f58dd28a3ed..90f7ce5483769ded7b81044dd4d448ee25bf83ed 100644 (file)
@@ -2,9 +2,10 @@ 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() }
+    dup: function() { self.fjsc_dup(); },
+    drop: function() { self.fjsc_drop(); },
+    nip: function() { self.fjsc_nip(); },
+    alert: function() { self.fjsc_alert(); }
   };  
 }
 
@@ -24,6 +25,18 @@ Factor.prototype.fjsc_eval = function(form) {
    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();
@@ -35,20 +48,16 @@ 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_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
index 0b13719e4d18696b593c26171c6190cb46500bc9..b67271c82018fbd957350c84ca1241d8c3cc3bb8 100644 (file)
@@ -7,6 +7,7 @@ USING: kernel lazy-lists parser-combinators strings math sequences namespaces io
 TUPLE: ast-number value ;
 TUPLE: ast-identifier value ;
 TUPLE: ast-string value ;
+TUPLE: ast-define name expression ;
 TUPLE: ast-expression values ;
 
 LAZY: 'digit' ( -- parser )
@@ -22,22 +23,47 @@ LAZY: 'string' ( -- 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 -- )
 
@@ -54,6 +80,13 @@ M: ast-string (compile)
 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) "; " ,
index 98b37ed7904c7dafebe9b2a8b3b68861cadd0c09..01d25f6af23bc3b01979ccf45f521ce02cd22e90 100644 (file)
@@ -4,8 +4,8 @@
 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\"]()" } [
@@ -19,4 +19,12 @@ IN: temporary
 { "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
+