relocate PercolatedPort to edu.berkeley.fleet.two
[fleet.git] / src / edu / berkeley / fleet / two / ShipDescription.java
1 package edu.berkeley.fleet.two;
2 import edu.berkeley.fleet.api.*;
3 import java.io.*;
4 import java.util.*;
5
6 /** NOT YET FINALIZED: A description (specification) of a ship */
7 public class ShipDescription implements Iterable<DockDescription> {
8
9     private String name;
10     private LinkedHashMap<String,DockDescription> docks         = new LinkedHashMap<String,DockDescription>();
11     private LinkedHashMap<String,DockDescription> ports = new LinkedHashMap<String,DockDescription>();
12     private HashMap<String,String>                sections      = new HashMap<String,String>();
13     private HashMap<String,Constant>              constants     = new HashMap<String,Constant>();
14
15     public String getName() { return name; }
16     public String getSection(String sectionName) { return sections.get(sectionName); }
17     public DockDescription getDockDescription(String name) { return docks.get(name); }
18     public Iterator<DockDescription> iterator() { return docks.values().iterator(); }
19     public Iterable<DockDescription> ports() {
20         return ports.values();
21     }
22
23     public final LinkedList<PercolatedPort> percolatedPorts = new LinkedList<PercolatedPort>();
24
25     public ShipDescription(String name, BufferedReader r) throws IOException {
26         if (name.endsWith(".ship")) name = name.substring(0, name.length()-".ship".length());
27         this.name = name;
28         String sectionName = null;
29         StringBuffer sb = new StringBuffer();
30         while(true) {
31             String s = r.readLine();
32             if (s==null || s.startsWith("==")) {
33                 if (sectionName != null) sections.put(sectionName, sb.toString());
34                 if (s==null) break;
35                 sb = new StringBuffer();
36                 sectionName = s.trim();
37                 while(sectionName.startsWith("="))
38                     sectionName = sectionName.substring(1);
39                 while(sectionName.endsWith("="))
40                     sectionName = sectionName.substring(0, sectionName.length()-1);
41                 sectionName = sectionName.trim().toLowerCase();
42                 continue;
43             }
44             sb.append(s+"\n");
45         }
46         for(String s : sections.keySet())
47             processSection(s);
48     }
49
50     public Constant getConstant(String name) {
51         return constants.get(name);
52     }
53
54     private void processSection(String section) throws IOException {
55         if (section.equals("")) {
56             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
57             for(String s = br.readLine(); s != null; s = br.readLine()) {
58                 if (s.trim().length()==0) continue;
59                 String key = s.substring(0, s.indexOf(':')).trim();
60                 String val = s.substring(s.indexOf(':')+1).trim();
61                 if (key.toLowerCase().equals("ship"))
62                     name = val.trim();
63             }
64         } else if (section.equals("constants")) {
65             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
66             for(String s = br.readLine(); s != null; s = br.readLine()) {
67                 if (s.indexOf(':')==-1) continue;
68                 String key = s.substring(0, s.indexOf(':')).trim();
69                 if (key.startsWith("constant")) {
70                     String constname = key.substring("constant".length()+1).trim();
71                     String val       = s.substring(s.indexOf(':')+1).trim();
72                     constants.put(constname, new Constant(val));
73                 }
74             }
75         } else if (section.equals("ports")) {
76             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
77             boolean rightSide = false;
78             DockDescription p = null;
79             for(String s = br.readLine(); s != null; s = br.readLine()) {
80                 if (s.trim().length()==0) { rightSide = true; continue; }
81
82                 String key = s.substring(0, s.indexOf(':')).trim();
83                 boolean inbox = false;
84                 boolean dockless = false;
85                 key = key.replaceAll("  +", " ");
86                 if      (key.equals("data in"))   { inbox = true;  }
87                 else if (key.equals("data out"))  { inbox = false; }
88                 else if (key.equals("in"))        { inbox = true;  }
89                 else if (key.equals("dockless out")) { inbox = false; dockless = true; }
90                 else if (key.equals("out"))       { inbox = false; }
91                 else if (key.startsWith("percolate")) { 
92                     key = s;
93                     key = key.substring("percolate".length()+1).trim();
94                     PercolatedPort.PortType type = null;
95                     if (key.startsWith("up")) type = PercolatedPort.PortType.UP;
96                     if (key.startsWith("down")) type = PercolatedPort.PortType.DOWN;
97                     if (key.startsWith("inout")) type = PercolatedPort.PortType.INOUT;
98                     key = key.substring(key.indexOf(':')+1).trim();
99                     String name = key.substring(0, key.indexOf(' '));
100                     int width = Integer.parseInt(key.substring(key.indexOf(' ')).trim());
101                     percolatedPorts.add(new PercolatedPort(name, width, type));
102                     continue;
103                 }
104                 else if (key.startsWith("constant")) {
105                     String constname = key.substring("constant".length()+1).trim();
106                     String val       = s.substring(s.indexOf(':')+1).trim();
107                     p.addConstant(constname, new Constant(val));
108                     continue;
109                 } else if (key.startsWith("shortcut to")) {
110                     continue;
111                 }
112                 else throw new RuntimeException("unknown port type: \""+key+"\"");
113
114                 p = null;
115                 String val = s.substring(s.indexOf(':')+1).trim();
116                 String boxname = val.indexOf('.') != -1 ? val.substring(0, val.indexOf('.')) : val;
117                 String dest    = val.indexOf('.') != -1 ? val.substring(val.indexOf('.')+1)  : "";
118                 p = docks.get(boxname);
119                 if (p==null) {
120                     p = new DockDescription(this, boxname, !rightSide, inbox, dockless);
121                     ports.put(boxname, p);
122                     if (!dockless) docks.put(boxname, p);
123                 }
124             }
125         }
126     }
127
128     public void printTeX(PrintWriter pw) throws Exception {
129         ShipDescription sd = this;
130         pw.println("\\pagebreak");
131         pw.println("\\section*{The {\\tt "+sd.getName()+"} Ship}");
132         pw.println("\\addcontentsline{toc}{subsection}{"+sd.getName()+"}");
133         String tex = sd.getSection("tex");
134         /*
135         for(DockDescription bbd : sd) {
136             pw.println("{\\bf "+(bbd.isInputDock() ? "Input: " : "Output: ")+"{\\tt "+bbd.getName()+"}}\n\n");
137         }
138         */
139         int boxGap = 5;
140         int boxHeight = 25;
141         int boxWidth = 75;
142
143         int leftSize = 0;
144         int rightSize = 0;
145         for(DockDescription bbd : sd)
146             if (bbd.isLeft()) leftSize += (boxHeight+boxGap);
147             else              rightSize += (boxHeight+boxGap);
148
149         int totalHeight = Math.max(leftSize, rightSize);
150         int shipWidth = (int)(boxWidth * 1.5);
151         int totalWidth = boxGap*2 + boxWidth*2 + shipWidth;
152
153         pw.println("");
154         pw.println("\\begin{center}");
155         pw.println("\\begin{empfile}["+sd.getName()+"]");
156         pw.println("\\begin{emp}["+sd.getName()+"]("+(totalWidth+10)+","+(totalHeight+10)+")");
157         pw.println("  beginfig(1)");
158         pw.println("      pickup pencircle scaled 1pt;");
159         pw.println("      draw "+
160                    "("+((totalWidth-shipWidth)/2)+","+0+")--"+
161                    "("+((totalWidth-shipWidth)/2)+","+totalHeight+")--"+
162                    "("+(totalWidth-((totalWidth-shipWidth)/2))+","+totalHeight+")--"+
163                    "("+(totalWidth-((totalWidth-shipWidth)/2))+","+0+")--"+
164                    "("+((totalWidth-shipWidth)/2)+","+0+");");
165         int left = 0;
166         int right = 0;
167         for(DockDescription bbd : sd) {
168             int ypos = (totalHeight - (boxGap/2) - (bbd.isLeft() ? left : right));
169             int half = (totalWidth-shipWidth)/2;
170             int p1 = bbd.isLeft() ? (half-5) : ((totalWidth-half)+5);
171             int p3 = bbd.isLeft() ? (p1 - boxWidth) : (p1 + boxWidth);
172             if (bbd.isInputDock()) {
173                 int p1x = p1;
174                 p1 = p3;
175                 p3 = p1x;
176             }
177             boolean goo = ((bbd.isLeft() && bbd.isInputDock()) || (!bbd.isLeft() && bbd.isOutputDock()));
178             int p2 = goo ? (p3 - (boxHeight/2)) : (p3 + (boxHeight/2));
179             if (bbd.isLeft()) left += (boxHeight+boxGap);
180             else              right += (boxHeight+boxGap);
181             if (goo) {
182                 pw.println("      label.rt(btex \\tt "+bbd.getName()+" etex, ("+(p1+3)+","+(ypos-boxHeight/2)+"));");
183             } else {
184                 pw.println("      label.lft(btex \\tt "+bbd.getName()+" etex, ("+(p1-3)+","+(ypos-boxHeight/2)+"));");
185             }
186             pw.println("      draw "+
187                        "  ("+p1+","+ypos+")--"+
188                        "  ("+p2+","+ypos+")--"+
189                        "  ("+p3+","+(ypos-(boxHeight/2))+")--"+
190                        "  ("+p2+","+(ypos-boxHeight)+")--"+
191                        "  ("+p1+","+(ypos-boxHeight)+")--"+
192                        "  ("+p1+","+ypos+");");
193             if (bbd.isLeft()) leftSize += boxHeight;
194             else              rightSize += boxHeight;
195         }
196         pw.println("  endfig;");
197         pw.println("\\end{emp}");
198         pw.println("\\end{empfile}");
199         pw.println("\\end{center}");
200         pw.println("");
201
202         if (tex!=null)
203             pw.println(tex);
204     }
205
206     // FIXME: merge with BitMask
207     public class Constant {
208         public long    setbits   = 0;
209         public long    clearbits = 0;
210         public boolean signExtend = false;
211         public int     numberOffset = 0;
212         public int     numberWidth = 0;
213         public Constant(String s) {
214             if (s.startsWith("0x")) {
215                 setbits = Long.parseLong(s.substring(2), 16);
216                 clearbits = ~setbits;
217             } else if (s.length() == 37) {
218                 for(int i=36; i>=0; i--) {
219                     char c = s.charAt(36-i);
220                     switch(c) {
221                         case '0': clearbits |= (1<<i); break;
222                         case '1': setbits   |= (1<<i); break;
223                         case '.': break;
224                         case 's': signExtend = true;  numberOffset = i; numberWidth++; break;
225                         case 'u': signExtend = false; numberOffset = i; numberWidth++; break;
226                     }
227                 }
228             } else {
229                 setbits = Long.parseLong(s);
230                 clearbits = ~setbits;
231             }
232         }
233     }
234
235 }