From: adam Date: Mon, 4 Dec 2006 12:57:38 +0000 (+0100) Subject: added #import, broke demo into two files X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=a6c32cb14c1258a800da200f5041bcc29013b95d;p=fleet.git added #import, broke demo into two files --- diff --git a/demo.fleet b/demo.fleet new file mode 100644 index 0000000..85a2380 --- /dev/null +++ b/demo.fleet @@ -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 index 0000000..cf84978 --- /dev/null +++ b/demo.ships @@ -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 --- 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 diff --git a/flow-control-example.fleet b/flow-control-example.fleet index 53442d0..d1c23be 100644 --- a/flow-control-example.fleet +++ b/flow-control-example.fleet @@ -1,4 +1,4 @@ - #import edu.berkeley.fleet.ships +#import edu.berkeley.fleet.ships //#ship math : ArithmeticShip #ship helper : FifoShip diff --git a/src/edu/berkeley/fleet/FleetParser.java b/src/edu/berkeley/fleet/FleetParser.java index 78934e3..1e32059 100644 --- a/src/edu/berkeley/fleet/FleetParser.java +++ b/src/edu/berkeley/fleet/FleetParser.java @@ -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)parse(r)); + fp.done(); + } + + public static Tree parse(Reader r) throws Exception { InputStream grammarStream = FleetParser.class.getClassLoader().getResourceAsStream("fleet.g"); - Parser metaGrammarParser = new CharParser(MetaGrammar.newInstance()); Tree 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)tree); - fp.done(); + return tree; } private Fleet fleet; @@ -100,8 +101,9 @@ public class FleetParser { for(Tree tc : t.child(0)) walk(tc); CodeBag cb = new CodeBag(null, null); - for(Tree statement : t.child(1)) - fillCodeBag(statement, cb); + if (t.size()>1) + for(Tree 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");