added #import, broke demo into two files
authoradam <adam@megacz.com>
Mon, 4 Dec 2006 12:57:38 +0000 (13:57 +0100)
committeradam <adam@megacz.com>
Mon, 4 Dec 2006 12:57:38 +0000 (13:57 +0100)
demo.fleet [new file with mode: 0644]
demo.ships [new file with mode: 0644]
fleet.g
flow-control-example.fleet
src/edu/berkeley/fleet/FleetParser.java

diff --git a/demo.fleet b/demo.fleet
new file mode 100644 (file)
index 0000000..85a2380
--- /dev/null
@@ -0,0 +1,38 @@
+#include "demo.ships"
+
+// NOTE: "accept" is a synonym for "move" it is less confusing in the case
+//       of inboxes, but is otherwise identical
+
+//////////////////////////////////////////////////////////////////////////////
+// The following three instructions simply produce one hundred "3"s
+// and put them into fifo "a".  Ignore the switch-fabric clogging
+// issue here; this is just setup for the rest of the code.
+
+                 3                -> helper.in
+            copy helper.out  -[10]-> source.in
+         discard helper.out       -> ()
+
+
+//////////////////////////////////////////////////////////////////////////////
+// set up a counting move of 10 items from source.out to dest.in, but require
+// flow control tokens (10 items is too many to send at once)
+
+ triggered move  source.out -[10]-> dest.in
+
+
+//////////////////////////////////////////////////////////////////////////////
+// The next instruction tears down the "default" standing move on dest.in
+// and uses the resulting tokens to prime source.out's pump.  Once that
+// has been kicked off, any subsequent incoming items will generate tokens
+// which are sent back to the source outbox
+
+          nop+ack dest.in     -[3]-> source.out
+       accept+ack dest.in     -[7]-> source.out
+       accept     dest.in     -[3]-> source.out
+          nop+ack dest.in         -> fetch.release    // then release the fetch
+           accept dest.in     -[*]-> ()               // and return the fabric to normal
+
+{ tokensource.out -> halt.in }    -> fetch.codebag    // termination codebag
+
+
+
diff --git a/demo.ships b/demo.ships
new file mode 100644 (file)
index 0000000..cf84978
--- /dev/null
@@ -0,0 +1,8 @@
+#import edu.berkeley.fleet.ships
+
+#ship helper      : FifoShip
+#ship source      : FifoShip
+#ship dest        : FifoShip
+#ship halt        : HaltShip
+#ship fetch       : FetchShip
+#ship tokensource : TokenSourceShip
diff --git a/fleet.g b/fleet.g
index 232179b..4032e78 100644 (file)
--- a/fleet.g
+++ b/fleet.g
@@ -10,9 +10,8 @@ Comment        = "//" ~[\n]* "\n"
 ws             = ([\r\n ] | Comment)* -> ~[\r\n ]
 
 s               = ws! Program ws!
-Program         = Program::
-                    (Directive ws!)*
-                    CodeBagBody
+Program         = Program:: Directive+/ws
+                | Program:: (Directive+/ws) ws! CodeBagBody
 
 Statement       = Instruction:: (Opcode ws!)? Source ws! Arrow ws! Port
                 | NamedCodeBag:: name ":" "{" CodeBagBody "}" /ws
@@ -38,7 +37,7 @@ Source         = Port
 Port           = Port::     shipname "." portname
                |           ^"()"
 
-CodeBagBody    = Statement */ (ws (";" ws)?!)
+CodeBagBody    = Statement +/ (ws (";" ws)?!)
 CodeBag        = CodeBagRef:: CodeBagName
                | AnonymousCodeBag:: "{" CodeBagBody "}" /ws
 
@@ -55,5 +54,6 @@ ShipSpecific   = ShipSpecific:: "\"" ~[\"]++ "\""
 
 Directive      = Memory::  "#memory" "{" (int +/ (ws! "," ws!)) "}" /ws
                | Import::  "#import" [A-Za-z_.]++ /ws
-               | Ship::    "#ship" shipname ":" [A-Za-z_\.]++ /ws
+               | Include:: "#include" ("\"" (~[\"])+ "\"") /ws
+               | Ship::    "#ship" shipname ":" [A-Za-z_.]++ /ws
 
index 53442d0..d1c23be 100644 (file)
@@ -1,4 +1,4 @@
- #import edu.berkeley.fleet.ships
+#import edu.berkeley.fleet.ships
 
 //#ship math        : ArithmeticShip
 #ship helper      : FifoShip
index 78934e3..1e32059 100644 (file)
@@ -58,20 +58,21 @@ public class FleetParser {
     }
 
     public static void go(Reader r) throws Exception {
+        Fleet fleet = new Fleet();
+        FleetParser fp = new FleetParser(fleet);
+        fp.walk((Tree<String>)parse(r));
+        fp.done();
+    }
+
+    public static Tree<String> parse(Reader r) throws Exception {
         InputStream grammarStream =
             FleetParser.class.getClassLoader().getResourceAsStream("fleet.g");
-
         Parser metaGrammarParser   = new CharParser(MetaGrammar.newInstance());
         Tree<String> parsedGrammar = metaGrammarParser.parse(new CharInput(grammarStream)).expand1();
-        Union   grammar            = Grammar.create(parsedGrammar, "s",
-                                                    new Grammar.Bindings() { });
+        Union   grammar            = Grammar.create(parsedGrammar, "s", new Grammar.Bindings() { });
         Parser  parser             = new CharParser(grammar);
         Tree tree = parser.parse(new CharInput(r)).expand1();
-        //System.out.println(tree);
-        Fleet fleet = new Fleet();
-        FleetParser fp = new FleetParser(fleet);
-        fp.walk((Tree<String>)tree);
-        fp.done();
+        return tree;
     }
 
     private Fleet fleet;
@@ -100,8 +101,9 @@ public class FleetParser {
             for(Tree<String> tc : t.child(0))
                 walk(tc);
             CodeBag cb = new CodeBag(null, null);
-            for(Tree<String> statement : t.child(1))
-                fillCodeBag(statement, cb);
+            if (t.size()>1)
+                for(Tree<String> statement : t.child(1))
+                    fillCodeBag(statement, cb);
             rootCodeBag = cb;
    
         } else if (head.equals("Import")) {
@@ -119,6 +121,13 @@ public class FleetParser {
             if (!good)
                 throw new RuntimeException("couldn't find a ship called \""+classname+"\"");
 
+        } else if (head.equals("Include")) {
+            try {
+                walk(parse(new InputStreamReader(new FileInputStream(string(t.child(0))))));
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+            
         } else if (head.equals("Memory")) {
             if (fleet.mem.length != 0)
                 throw new RuntimeException("multiple memory directives found");