added am10 inbox/outbox support
[fleet.git] / src / edu / berkeley / fleet / FleetParser.java
index 7cd3dee..761e3c3 100644 (file)
@@ -8,52 +8,144 @@ import edu.berkeley.sbp.bind.*;
 import edu.berkeley.sbp.util.*;
 import java.util.*;
 import java.io.*;
+import static edu.berkeley.fleet.Instruction.IgnoreCopyTake.*;
 
 public class FleetParser {
 
     public static void main(String[] s) throws Exception {
+        go(new InputStreamReader(System.in));
+    }
 
+    public static void go(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();
-        Grammar.Bindings gbr       = new AnnotationGrammarBindings(Program.class);
-        Union   mathGrammar        = Grammar.create(parsedGrammar, "s", gbr);
-        Parser  mathParser         = new CharParser(mathGrammar);
+        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();
+    }
 
-        System.out.println("about to parse: tests/test.fleet");
-        Tree tree = mathParser.parse(new CharInput(System.in)).expand1();
+    private Fleet fleet;
+    private ArrayList<String> imports = new ArrayList<String>();
+    private CodeBag rootCodeBag;
 
-        // below is ugly voodoo which will go away very soon.  ignore it.
-        TreeFunctor tf = (TreeFunctor)tree.head();
-        Program program = (Program)tf.invoke(tree);
-        // above is ugly voodoo which will go away very soon.  ignore it.
+    public FleetParser(Fleet fleet) {
+        this.fleet = fleet;
+    }
 
-        System.out.println();
-        System.out.println("dispatching root codebag:");
-        System.out.println(program.root);
+    public void done() {
+        if (rootCodeBag != null) {
+            rootCodeBag.dispatch(fleet);
+            fleet.go();
+        }
+    }
 
-        Fleet fleet = new Fleet();
-        program.configure(fleet);
-
-        System.out.println("memory before execution:");
-        System.out.print("  ");
-        for(int i=0; i<fleet.mem.length; i++)
-            System.out.print(fleet.mem[i] + " ");
-        System.out.println();
-
-        System.out.println();
-        System.out.println("enabling execution...");
-        fleet.go();
-        System.out.println("execution halted.");
-
-        System.out.println();
-        System.out.println("memory after execution:");
-        System.out.print("  ");
-        for(int i=0; i<fleet.mem.length; i++)
-            System.out.print(fleet.mem[i] + " ");
-        System.out.println();
+    public void walk(Tree<String> t) {
+        String head = t.head();
+        if (head==null) {
+        } else if (head.equals("Program")) {
+            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);
+            rootCodeBag = cb;
+   
+        } else if (head.equals("Import")) {
+            imports.add(string(t.child(0)));
+
+        } else if (head.equals("Ship")) {
+            String name = name(t.child(0));
+            String classname = string(t.child(1));
+            boolean good = false;
+            for(String s : imports)
+                if (fleet.tryCreate(s+"."+classname, name)) {
+                    good = true;
+                    break;
+                }
+            if (!good)
+                throw new RuntimeException("couldn't find a ship called \""+classname+"\"");
+        }
+    }
+
+    public String string(Tree<String> t) {
+        String ret = "";
+        if (t.head() != null) ret += t.head();
+        for(Tree<String> c : t)
+            ret += string(c);
+        return ret;
+    }
+
+    public String name(Tree<String> t) {
+        return string(t.child(0))+string(t.child(1));
+    }
+
+    public PortReference portReference(Tree<String> t) {
+        if (!"Port".equals(t.head())) return null;
+        return new PortReference(name(t.child(0)), name(t.child(1)));
+    }
+
+    public void fillCodeBag(Tree<String> t, CodeBag cb) {
+        if (t.head()==null) return;
+        else if (t.head().equals("NamedCodeBag")) {
+            CodeBag cb2 = new CodeBag(cb, name(t.child(0)));
+            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)));
+
+            Tree opcodebody = opcode.size()==0 ? null : opcode.child(0).child(opcode.size()-1);
+            
+            PortReference d = portReference(t.child(3));
+            if (t.child(1).head() == null) {
+                int literal = Integer.parseInt(string(t.child(1)));
+                cb.add(new Literal.LiteralDatum(literal, d, false));
+
+            } else if ("AnonymousCodeBag".equals(t.child(1).head())) {
+                CodeBag cb3 = new CodeBag(cb, null);
+                for(Tree<String> tc : t.child(1).child(0))
+                    fillCodeBag(tc, cb3);
+                cb.add(new Literal.LiteralDatum(cb3.getDescriptor(), d, true));
+
+            } 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);
+            }
+        }
     }
 
 }