From d49aa7344a721d815da24733e2c35b480d400eec Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 23 Nov 2006 13:09:41 +0100 Subject: [PATCH] added support for ship-specific constants --- fleet.g | 2 ++ flow-control-example.fleet | 2 ++ src/edu/berkeley/fleet/FleetParser.java | 3 +++ src/edu/berkeley/fleet/Literal.java | 21 +++++++++++++++++++++ src/edu/berkeley/fleet/Ship.java | 4 ++++ src/edu/berkeley/fleet/ships/ArithmeticShip.java | 17 ++++++++++++++--- 6 files changed, 46 insertions(+), 3 deletions(-) diff --git a/fleet.g b/fleet.g index 4ef8ab6..232179b 100644 --- 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. diff --git a/flow-control-example.fleet b/flow-control-example.fleet index 7999e07..be44680 100644 --- a/flow-control-example.fleet +++ b/flow-control-example.fleet @@ -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 diff --git a/src/edu/berkeley/fleet/FleetParser.java b/src/edu/berkeley/fleet/FleetParser.java index 85fc297..5132605 100644 --- a/src/edu/berkeley/fleet/FleetParser.java +++ b/src/edu/berkeley/fleet/FleetParser.java @@ -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; diff --git a/src/edu/berkeley/fleet/Literal.java b/src/edu/berkeley/fleet/Literal.java index a4c0b77..9bd798d 100644 --- a/src/edu/berkeley/fleet/Literal.java +++ b/src/edu/berkeley/fleet/Literal.java @@ -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 "+destination); + } + } } diff --git a/src/edu/berkeley/fleet/Ship.java b/src/edu/berkeley/fleet/Ship.java index c85d7d0..93a899d 100644 --- a/src/edu/berkeley/fleet/Ship.java +++ b/src/edu/berkeley/fleet/Ship.java @@ -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+"\""); + } } diff --git a/src/edu/berkeley/fleet/ships/ArithmeticShip.java b/src/edu/berkeley/fleet/ships/ArithmeticShip.java index c2492d0..c866aa0 100644 --- a/src/edu/berkeley/fleet/ships/ArithmeticShip.java +++ b/src/edu/berkeley/fleet/ships/ArithmeticShip.java @@ -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(); } } -- 1.7.10.4