added fleet api classes
[fleet.git] / src / edu / berkeley / fleet / FleetParser.java
index 761e3c3..209889c 100644 (file)
@@ -8,43 +8,99 @@ import edu.berkeley.sbp.bind.*;
 import edu.berkeley.sbp.util.*;
 import java.util.*;
 import java.io.*;
-import static edu.berkeley.fleet.Instruction.IgnoreCopyTake.*;
 
+/**
+ *  @author Adam Megacz <megacz@cs.berkeley.edu>
+ *  @author Thomas Kho <tkho@eecs.berkeley.edu>
+ */
 public class FleetParser {
 
+    public static boolean dump_fabric = false;
+    public static boolean dump_code = false;
+
     public static void main(String[] s) throws Exception {
+        for(int i=0; i<s.length; i++) {
+            if (s[i].startsWith("--color=")) {
+                String val = s[i].substring(s[i].indexOf('=')+1);
+                if (val.equals("on")) {
+                    Log.ansi_color = true;
+                    continue;
+                } else if (val.equals("off")) {
+                    Log.ansi_color = false;
+                    continue;
+                }
+            } else if (s[i].startsWith("--dump-fabric")) {
+                dump_fabric = true;
+                continue;
+            } else if (s[i].startsWith("--dump-code")) {
+                dump_code = true;
+                continue;
+
+            } else if (s[i].startsWith("--memory=")) {
+                String val = s[i].substring(s[i].indexOf('=')+1);
+                if (val.equals("hide")) {
+                    debugMemory = false;
+                    continue;
+                } else if (val.equals("show")) {
+                    debugMemory = true;
+                    continue;
+                }
+            }
+            System.out.println("Fleeterpreter usage:");
+            System.out.println("");
+            System.out.println("  --dump-fabric");
+            System.out.println("  --dump-code");
+            System.out.println("  --color={on|off}");
+            System.out.println("  --inboxes={configured|unconfigured}");
+            System.out.println("  --memory={hide|show}");
+            System.exit(-1);
+        }
         go(new InputStreamReader(System.in));
     }
 
     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;
     private ArrayList<String> imports = new ArrayList<String>();
     private CodeBag rootCodeBag;
+    private static boolean debugMemory = true;
 
     public FleetParser(Fleet fleet) {
         this.fleet = fleet;
     }
 
     public void done() {
-        if (rootCodeBag != null) {
+        if (dump_fabric) {
+            fleet.dumpFabric(false);
+
+        } else if (dump_code) {
+            fleet.dumpFabric(true);
+            try {
+                rootCodeBag.dump(fleet);
+            } catch (Exception e) { throw new RuntimeException(e); }
+
+        } else if (rootCodeBag != null) {
+            if (debugMemory) { fleet.dumpMem(); }
+            System.out.println(rootCodeBag);
             rootCodeBag.dispatch(fleet);
             fleet.go();
+            if (debugMemory) { fleet.dumpMem(); }
         }
     }
 
@@ -55,9 +111,9 @@ public class FleetParser {
             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);
-            System.out.println(cb);
+            if (t.size()>1)
+                for(Tree<String> statement : t.child(1))
+                    fillCodeBag(statement, cb);
             rootCodeBag = cb;
    
         } else if (head.equals("Import")) {
@@ -74,6 +130,22 @@ 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");
+            Tree<String> m = t.child(0);
+            int[] mem = new int[m.size()];
+            for(int i=0; i<mem.length; i++)
+                mem[i] = Integer.parseInt(string(m.child(i)));
+            fleet.mem = mem;
         }
     }
 
@@ -101,22 +173,45 @@ public class FleetParser {
             for(Tree<String> statement : t.child(1))
                 fillCodeBag(statement, cb2);
 
-        } else if (t.head().equals("Instruction")) {
-            Tree<String> opcode = t.child(0);
-            boolean trigger = opcode != null && opcode.size()>0 && "Triggered".equals(opcode.child(0).head());
-
-            int count = 0;
-            Tree arrow = t.child(2);
-            if      (arrow.head().equals("->"))     count = 1;
-            else if (arrow.head().equals("-[*]->")) count = Integer.MAX_VALUE;
-            else if (arrow.head().equals("-["))     count = Integer.parseInt(string(arrow.child(0)));
+        } else if (t.head().equals("Literal")) {
+            int literal = Integer.parseInt(string(t.child(0)));
+            PortReference benkobox = portReference(t.child(1));
+            cb.add(new Literal.LiteralDatum(literal, benkobox, false, 1));
 
-            Tree opcodebody = opcode.size()==0 ? null : opcode.child(0).child(opcode.size()-1);
+        } else if (t.head().equals("Fiber")) {
+            PortReference benkobox = portReference(t.child(0));
             
-            PortReference d = portReference(t.child(3));
+            for(Tree tt : t.child(1)) {
+                int count = 1;
+                Tree ttx = null;
+                if (tt.size() > 1 && tt.child(0).size()>0) {
+                    System.out.println(tt.child(0));
+                    if (tt.child(0).size() > 0 && "Star".equals(tt.child(0).child(0).head())) count=Integer.MAX_VALUE;
+                    else count = Integer.parseInt(string(tt.child(0)));
+                }
+                ttx = tt.child(1);
+                boolean tokenIn = false;
+                boolean dataIn = false;
+                boolean latch = false;
+                boolean dataOut = false;
+                boolean tokenOut = false;
+                PortReference dest = null;
+                for(int i=0; i<ttx.size(); i++) {
+                    Tree ttt = ttx.child(i);
+                    if      ("Wait".equals(ttt.head()))    { tokenIn = true; }
+                    else if ("Discard".equals(ttt.head())) { dataIn = true; latch = false; }
+                    else if ("Take".equals(ttt.head()))    { dataIn = true; latch = true; }
+                    else if ("SendTo".equals(ttt.head()))  { dataOut = true; dest = portReference(ttt.child(0)); }
+                    else if ("Accept".equals(ttt.head()))  { dataOut = true;  }
+                    else if ("Ack".equals(ttt.head()))     { tokenOut = true; dest = portReference(ttt.child(0)); }
+                }
+                cb.add(new Instruction(benkobox, dest, count, dataIn, latch, tokenOut, tokenIn, dataOut));
+            }
+        }
+            /*
             if (t.child(1).head() == null) {
                 int literal = Integer.parseInt(string(t.child(1)));
-                cb.add(new Literal.LiteralDatum(literal, d, false));
+                cb.add(new Literal.LiteralDatum(literal, d, false, count));
 
             } else if ("AnonymousCodeBag".equals(t.child(1).head())) {
                 CodeBag cb3 = new CodeBag(cb, null);
@@ -127,25 +222,8 @@ 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 {
-                PortReference s = portReference(t.child(1));
-                Instruction inst = null;
-                if (opcodebody==null)         inst = new Instruction(s, d, count, TAKE,   false,  trigger, true);
-                else if (opcodebody.head().equals("nop"))          inst = new Instruction(s, d, count, IGNORE, false,  trigger, false);
-                else if (opcodebody.head().equals("wait"))         inst = new Instruction(s, d, count, COPY,   false,  trigger, false);
-                else if (opcodebody.head().equals("discard"))      inst = new Instruction(s, d, count, TAKE,   false,  trigger, false);
-                else if (opcodebody.head().equals("nop+ack"))      inst = new Instruction(s, d, count, IGNORE, true,   trigger, false);
-                else if (opcodebody.head().equals("wait+ack"))     inst = new Instruction(s, d, count, COPY,   true,   trigger, false);
-                else if (opcodebody.head().equals("discard+ack"))  inst = new Instruction(s, d, count, TAKE,   true,   trigger, false);
-                else if (opcodebody.head().equals("copy"))         inst = new Instruction(s, d, count, COPY,   false,  trigger, true);
-                else if (opcodebody.head().equals("copy+ack"))     inst = new Instruction(s, d, count, COPY,   true,   trigger, true);
-                else if (opcodebody.head().equals("move"))         inst = new Instruction(s, d, count, TAKE,   false,  trigger, true);
-                else if (opcodebody.head().equals("move+ack"))     inst = new Instruction(s, d, count, TAKE,   true,   trigger, true);
-                else if (opcodebody.head().equals("accept"))         inst = new Instruction(s, d, count, TAKE,   false,  trigger, true);
-                else if (opcodebody.head().equals("accept+ack"))     inst = new Instruction(s, d, count, TAKE,   true,   trigger, true);
-                cb.add(inst);
-            }
-        }
+            } else if ("ShipSpecific".equals(t.child(1).head())) {
+                cb.add(new Literal.ShipSpecific(string(t.child(1).child(0)), d, count));
+            */
     }
-
 }