78934e326af4216951f8fc87ea944e53f3a797f3
[fleet.git] / src / edu / berkeley / fleet / FleetParser.java
1 package edu.berkeley.fleet;
2
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.*;
9 import java.util.*;
10 import java.io.*;
11 import static edu.berkeley.fleet.Instruction.IgnoreCopyTake.*;
12
13 /**
14  *  @author Adam Megacz <megacz@cs.berkeley.edu>
15  *  @author Thomas Kho <tkho@eecs.berkeley.edu>
16  */
17 public class FleetParser {
18
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;
25                     continue;
26                 } else if (val.equals("off")) {
27                     Log.ansi_color = false;
28                     continue;
29                 }
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);
35                     continue;
36                 } else if (val.equals("unconfigured")) {
37                     DataInbox.defaultInstruction = null;
38                     continue;
39                 }
40             } else if (s[i].startsWith("--memory=")) {
41                 String val = s[i].substring(s[i].indexOf('=')+1);
42                 if (val.equals("hide")) {
43                     debugMemory = false;
44                     continue;
45                 } else if (val.equals("show")) {
46                     debugMemory = true;
47                     continue;
48                 }
49             }
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}");
55             System.exit(-1);
56         }
57         go(new InputStreamReader(System.in));
58     }
59
60     public static void go(Reader r) throws Exception {
61         InputStream grammarStream =
62             FleetParser.class.getClassLoader().getResourceAsStream("fleet.g");
63
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);
74         fp.done();
75     }
76
77     private Fleet fleet;
78     private ArrayList<String> imports = new ArrayList<String>();
79     private CodeBag rootCodeBag;
80     private static boolean debugMemory = true;
81
82     public FleetParser(Fleet fleet) {
83         this.fleet = fleet;
84     }
85
86     public void done() {
87         if (rootCodeBag != null) {
88             if (debugMemory) { fleet.dumpMem(); }
89             System.out.println(rootCodeBag);
90             rootCodeBag.dispatch(fleet);
91             fleet.go();
92             if (debugMemory) { fleet.dumpMem(); }
93         }
94     }
95
96     public void walk(Tree<String> t) {
97         String head = t.head();
98         if (head==null) {
99         } else if (head.equals("Program")) {
100             for(Tree<String> tc : t.child(0))
101                 walk(tc);
102             CodeBag cb = new CodeBag(null, null);
103             for(Tree<String> statement : t.child(1))
104                 fillCodeBag(statement, cb);
105             rootCodeBag = cb;
106    
107         } else if (head.equals("Import")) {
108             imports.add(string(t.child(0)));
109
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)) {
116                     good = true;
117                     break;
118                 }
119             if (!good)
120                 throw new RuntimeException("couldn't find a ship called \""+classname+"\"");
121
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)));
129             fleet.mem = mem;
130         }
131     }
132
133     public String string(Tree<String> t) {
134         String ret = "";
135         if (t.head() != null) ret += t.head();
136         for(Tree<String> c : t)
137             ret += string(c);
138         return ret;
139     }
140
141     public String name(Tree<String> t) {
142         return string(t.child(0))+string(t.child(1));
143     }
144
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)));
148     }
149
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);
156
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());
160
161             int count = 0;
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)));
166
167             Tree opcodebody = opcode.size()==0 ? null : opcode.child(0).child(opcode.size()-1);
168             
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));
173
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));
179
180             } else if ("CodeBagRef".equals(t.child(1).head())) {
181                 cb.add(new Literal.CodeBagRef(name(t.child(1).child(0)), cb, d));
182
183             } else if ("ShipSpecific".equals(t.child(1).head())) {
184                 cb.add(new Literal.ShipSpecific(string(t.child(1).child(0)), d, count));
185
186             } else {
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);
204                 cb.add(inst);
205             }
206         }
207     }
208
209 }