1 package edu.berkeley.fleet;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.meta.*;
7 import edu.berkeley.sbp.bind.*;
8 import edu.berkeley.sbp.util.*;
11 import static edu.berkeley.fleet.Instruction.IgnoreCopyTake.*;
14 * @author Adam Megacz <megacz@cs.berkeley.edu>
15 * @author Thomas Kho <tkho@eecs.berkeley.edu>
17 public class FleetParser {
19 public static void main(String[] s) throws Exception {
20 for(int i=0; i<s.length; i++) {
21 if (s[i].startsWith("--color=")) {
22 String val = s[i].substring(s[i].indexOf('=')+1);
23 if (val.equals("on")) {
24 Log.ansi_color = true;
26 } else if (val.equals("off")) {
27 Log.ansi_color = false;
30 } else if (s[i].startsWith("--inboxes=")) {
31 String val = s[i].substring(s[i].indexOf('=')+1);
32 if (val.equals("configured")) {
33 DataInbox.defaultInstruction =
34 new Instruction(null, null, Integer.MAX_VALUE, TAKE, false, false, true);
36 } else if (val.equals("unconfigured")) {
37 DataInbox.defaultInstruction = null;
40 } else if (s[i].startsWith("--memory=")) {
41 String val = s[i].substring(s[i].indexOf('=')+1);
42 if (val.equals("hide")) {
45 } else if (val.equals("show")) {
50 System.out.println("Fleeterpreter usage:");
51 System.out.println("");
52 System.out.println(" --color={on|off}");
53 System.out.println(" --inboxes={configured|unconfigured}");
54 System.out.println(" --memory={hide|show}");
57 go(new InputStreamReader(System.in));
60 public static void go(Reader r) throws Exception {
61 InputStream grammarStream =
62 FleetParser.class.getClassLoader().getResourceAsStream("fleet.g");
64 Parser metaGrammarParser = new CharParser(MetaGrammar.newInstance());
65 Tree<String> parsedGrammar = metaGrammarParser.parse(new CharInput(grammarStream)).expand1();
66 Union grammar = Grammar.create(parsedGrammar, "s",
67 new Grammar.Bindings() { });
68 Parser parser = new CharParser(grammar);
69 Tree tree = parser.parse(new CharInput(r)).expand1();
70 //System.out.println(tree);
71 Fleet fleet = new Fleet();
72 FleetParser fp = new FleetParser(fleet);
73 fp.walk((Tree<String>)tree);
78 private ArrayList<String> imports = new ArrayList<String>();
79 private CodeBag rootCodeBag;
80 private static boolean debugMemory = true;
82 public FleetParser(Fleet fleet) {
87 if (rootCodeBag != null) {
88 if (debugMemory) { fleet.dumpMem(); }
89 System.out.println(rootCodeBag);
90 rootCodeBag.dispatch(fleet);
92 if (debugMemory) { fleet.dumpMem(); }
96 public void walk(Tree<String> t) {
97 String head = t.head();
99 } else if (head.equals("Program")) {
100 for(Tree<String> tc : t.child(0))
102 CodeBag cb = new CodeBag(null, null);
103 for(Tree<String> statement : t.child(1))
104 fillCodeBag(statement, cb);
107 } else if (head.equals("Import")) {
108 imports.add(string(t.child(0)));
110 } else if (head.equals("Ship")) {
111 String name = name(t.child(0));
112 String classname = string(t.child(1));
113 boolean good = false;
114 for(String s : imports)
115 if (fleet.tryCreate(s+"."+classname, name)) {
120 throw new RuntimeException("couldn't find a ship called \""+classname+"\"");
122 } else if (head.equals("Memory")) {
123 if (fleet.mem.length != 0)
124 throw new RuntimeException("multiple memory directives found");
125 Tree<String> m = t.child(0);
126 int[] mem = new int[m.size()];
127 for(int i=0; i<mem.length; i++)
128 mem[i] = Integer.parseInt(string(m.child(i)));
133 public String string(Tree<String> t) {
135 if (t.head() != null) ret += t.head();
136 for(Tree<String> c : t)
141 public String name(Tree<String> t) {
142 return string(t.child(0))+string(t.child(1));
145 public PortReference portReference(Tree<String> t) {
146 if (!"Port".equals(t.head())) return null;
147 return new PortReference(name(t.child(0)), name(t.child(1)));
150 public void fillCodeBag(Tree<String> t, CodeBag cb) {
151 if (t.head()==null) return;
152 else if (t.head().equals("NamedCodeBag")) {
153 CodeBag cb2 = new CodeBag(cb, name(t.child(0)));
154 for(Tree<String> statement : t.child(1))
155 fillCodeBag(statement, cb2);
157 } else if (t.head().equals("Instruction")) {
158 Tree<String> opcode = t.child(0);
159 boolean trigger = opcode != null && opcode.size()>0 && "Triggered".equals(opcode.child(0).head());
162 Tree arrow = t.child(2);
163 if (arrow.head().equals("->")) count = 1;
164 else if (arrow.head().equals("-[*]->")) count = Integer.MAX_VALUE;
165 else if (arrow.head().equals("-[")) count = Integer.parseInt(string(arrow.child(0)));
167 Tree opcodebody = opcode.size()==0 ? null : opcode.child(0).child(opcode.size()-1);
169 PortReference d = portReference(t.child(3));
170 if (t.child(1).head() == null) {
171 int literal = Integer.parseInt(string(t.child(1)));
172 cb.add(new Literal.LiteralDatum(literal, d, false, count));
174 } else if ("AnonymousCodeBag".equals(t.child(1).head())) {
175 CodeBag cb3 = new CodeBag(cb, null);
176 for(Tree<String> tc : t.child(1).child(0))
177 fillCodeBag(tc, cb3);
178 cb.add(new Literal.LiteralDatum(cb3.getDescriptor(), d, true));
180 } else if ("CodeBagRef".equals(t.child(1).head())) {
181 cb.add(new Literal.CodeBagRef(name(t.child(1).child(0)), cb, d));
183 } else if ("ShipSpecific".equals(t.child(1).head())) {
184 cb.add(new Literal.ShipSpecific(string(t.child(1).child(0)), d, count));
187 PortReference s = portReference(t.child(1));
188 Instruction inst = null;
189 if (opcodebody==null) inst = new Instruction(s, d, count, TAKE, false, trigger, true);
190 else if (opcodebody.head().equals("nop")) inst = new Instruction(s, d, count, IGNORE, false, trigger, false);
191 else if (opcodebody.head().equals("synthesize")) inst = new Instruction(s, d, count, IGNORE, false, trigger, true);
192 else if (opcodebody.head().equals("synthesize+ack")) inst = new Instruction(s, d, count, IGNORE, true, trigger, true);
193 else if (opcodebody.head().equals("wait")) inst = new Instruction(s, d, count, COPY, false, trigger, false);
194 else if (opcodebody.head().equals("discard")) inst = new Instruction(s, d, count, TAKE, false, trigger, false);
195 else if (opcodebody.head().equals("nop+ack")) inst = new Instruction(s, d, count, IGNORE, true, trigger, false);
196 else if (opcodebody.head().equals("wait+ack")) inst = new Instruction(s, d, count, COPY, true, trigger, false);
197 else if (opcodebody.head().equals("discard+ack")) inst = new Instruction(s, d, count, TAKE, true, trigger, false);
198 else if (opcodebody.head().equals("copy")) inst = new Instruction(s, d, count, COPY, false, trigger, true);
199 else if (opcodebody.head().equals("copy+ack")) inst = new Instruction(s, d, count, COPY, true, trigger, true);
200 else if (opcodebody.head().equals("move")) inst = new Instruction(s, d, count, TAKE, false, trigger, true);
201 else if (opcodebody.head().equals("move+ack")) inst = new Instruction(s, d, count, TAKE, true, trigger, true);
202 else if (opcodebody.head().equals("accept")) inst = new Instruction(s, d, count, TAKE, false, trigger, true);
203 else if (opcodebody.head().equals("accept+ack")) inst = new Instruction(s, d, count, TAKE, true, trigger, true);