added support for ship-specific constants
authoradam <adam@megacz.com>
Thu, 23 Nov 2006 12:09:41 +0000 (13:09 +0100)
committeradam <adam@megacz.com>
Thu, 23 Nov 2006 12:09:41 +0000 (13:09 +0100)
fleet.g
flow-control-example.fleet
src/edu/berkeley/fleet/FleetParser.java
src/edu/berkeley/fleet/Literal.java
src/edu/berkeley/fleet/Ship.java
src/edu/berkeley/fleet/ships/ArithmeticShip.java

diff --git a/fleet.g b/fleet.g
index 4ef8ab6..232179b 100644 (file)
--- a/fleet.g
+++ b/fleet.g
@@ -33,6 +33,7 @@ Arrow           = ^"->"
 Source         = Port
                | CodeBag
                | int
+               | ShipSpecific
 
 Port           = Port::     shipname "." portname
                |           ^"()"
@@ -47,6 +48,7 @@ portname       = name &~ Opcode
 name           = Name:: [A-Za-z] [A-Za-z0-9\[\]_]**
 index          = "[" [0-9]+ "]" | [0-9]+
 int            = [\-0-9]++
+ShipSpecific   = ShipSpecific:: "\"" ~[\"]++ "\""
 
 // the following are not part of the official FLEET syntax and are
 // specific to Adam's interpreter.
index 7999e07..be44680 100644 (file)
@@ -1,5 +1,6 @@
 #import edu.berkeley.fleet.ships
 
+#ship math        : ArithmeticShip
 #ship helper      : FifoShip
 #ship source      : FifoShip
 #ship dest        : FifoShip
@@ -10,6 +11,7 @@
 // NOTE: "accept" is a synonym for "move" it is less confusing in the case
 //       of inboxes, but is otherwise identical
 
+"foo" -> math.din
 
 //////////////////////////////////////////////////////////////////////////////
 // The following three instructions simply produce one hundred "3"s
index 85fc297..5132605 100644 (file)
@@ -154,6 +154,9 @@ public class FleetParser {
             } else if ("CodeBagRef".equals(t.child(1).head())) {
                 cb.add(new Literal.CodeBagRef(name(t.child(1).child(0)), cb, d));
 
+            } else if ("ShipSpecific".equals(t.child(1).head())) {
+                cb.add(new Literal.ShipSpecific(string(t.child(1).child(0)), d, count));
+
             } else {
                 PortReference s = portReference(t.child(1));
                 Instruction inst = null;
index a4c0b77..9bd798d 100644 (file)
@@ -56,4 +56,25 @@ public class Literal {
         }
     }
 
+    public static class ShipSpecific extends Dispatchable {
+        private String data;
+        private PortReference destination;
+        private int count;
+        public ShipSpecific(String data, PortReference destination, int count) {
+            this.data = data;
+            this.destination = destination;
+            this.count = count;
+        }
+        public void dispatch(Fleet fleet) {
+            for(int i=0; i<count; i++) {
+                Port port = destination.resolve(fleet);
+                Log.data("\""+data+"\"", null, port);
+                int dat = port.getShip().resolveShipSpecificConstant(data);
+                port.addDataFromFabric(dat);
+            }
+        }
+        public String toString() {
+            return ("\""+data+"\""+" -"+(count==1?"":("["+count+"]-"))+"> "+destination);
+        }
+    }
 }
index c85d7d0..93a899d 100644 (file)
@@ -53,4 +53,8 @@ public abstract class Ship {
         for(Port p : ports.values())
             p.shutdown();
     }
+
+    public int resolveShipSpecificConstant(String shipSpecificData) {
+        throw new RuntimeException("don't know how to resolve \""+shipSpecificData+"\"");
+    }
 }
index c2492d0..c866aa0 100644 (file)
@@ -30,19 +30,30 @@ public class ArithmeticShip extends Ship {
         // here's some sample code snippets that should cover all the
         //    cases you need
 
+        if (!din.dataReadyForShip())
+            return;
+        /*
         if (!tin.tokenReadyForShip())
             return;
-        tin.removeTokenForShip();
+        */
 
-        if (!din.dataReadyForShip())
-            return;
         int mydat = din.removeDataForShip();
+        Log.println("ArithmeticShip: got a " + mydat);
+        /*
+        tin.removeTokenForShip();
+        */
 
+        /*
         if (tout.readyForTokenFromShip())
             tout.addTokenFromShip();
         if (dout.readyForDataFromShip())
             dout.addDataFromShip(123);
+        */
+    }
 
+    public int resolveShipSpecificConstant(String shipSpecificData) {
+        // normally you would want to parse shipSpecificData somehow
+        return shipSpecificData.length();
     }
 
 }