--- /dev/null
+#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
+
+
+
--- /dev/null
+#import edu.berkeley.fleet.ships
+
+#ship helper : FifoShip
+#ship source : FifoShip
+#ship dest : FifoShip
+#ship halt : HaltShip
+#ship fetch : FetchShip
+#ship tokensource : TokenSourceShip
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
Port = Port:: shipname "." portname
| ^"()"
-CodeBagBody = Statement */ (ws (";" ws)?!)
+CodeBagBody = Statement +/ (ws (";" ws)?!)
CodeBag = CodeBagRef:: CodeBagName
| AnonymousCodeBag:: "{" CodeBagBody "}" /ws
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
- #import edu.berkeley.fleet.ships
+#import edu.berkeley.fleet.ships
//#ship math : ArithmeticShip
#ship helper : FifoShip
}
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;
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")) {
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");