update to new AM37 syntax
[fleet.git] / src / edu / berkeley / fleet / assembler / Parser.java
1 package edu.berkeley.fleet.assembler;
2 import edu.berkeley.fleet.api.*;
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.util.*;
8 import static edu.berkeley.fleet.util.BitManipulations.*;
9 import edu.berkeley.fleet.two.*;
10 import edu.berkeley.fleet.api.Instruction.Set;
11 import static edu.berkeley.fleet.api.Instruction.*;
12 import static edu.berkeley.fleet.api.Instruction.Set.*;
13 import static edu.berkeley.fleet.api.Predicate.*;
14 import edu.berkeley.fleet.two.*;
15 import static edu.berkeley.fleet.two.FleetTwoFleet.SHIFT;
16 import edu.berkeley.fleet.fpga.*;
17 import edu.berkeley.fleet.interpreter.*;
18 import java.util.*;
19 import java.io.*;
20
21
22 /**
23  *  @author Adam Megacz <megacz@cs.berkeley.edu>
24  */
25 public class Parser {
26
27     private static final BitVector SIGNAL_ZERO = new BitVector(1);
28     private static final BitVector SIGNAL_ONE  = new BitVector(1);
29     static {
30         SIGNAL_ONE.set(0,true);
31     }
32     
33     Parser(Fleet fleet) {
34         expect = new ArrayList<Long>();
35         this.fleet = fleet;
36     }
37
38     //////////////////////////////////////////////////////////////////////////////
39
40     private Fleet fleet;
41     private ArrayList<String> imports = new ArrayList<String>();
42
43     private HashMap<String,Ship> shipMap = new HashMap<String,Ship>();
44
45     // codebags in numerical order
46     private ArrayList<CodeBag> codeBags = new ArrayList<CodeBag>();
47     private HashMap<String,CodeBag> codeBagsByName = new HashMap<String,CodeBag>();
48     
49     private CodeBag getCodeBag(String name) {
50         CodeBag cb = codeBagsByName.get(name);
51         if (cb!=null) return cb;
52         return new CodeBag(name);
53     }
54
55     private static Union grammar;
56     private static synchronized Union getGrammar() throws Exception {
57         if (grammar != null) return grammar;
58         InputStream grammarStream =
59             Parser.class.getClassLoader().getResourceAsStream("edu/berkeley/fleet/assembler/fleet.g");
60         CharParser metaGrammarParser   = new CharParser(GrammarAST.getMetaGrammar());
61         Tree<String> parsedGrammar     = metaGrammarParser.parse(new CharInput(grammarStream)).expand1();
62         grammar                        = GrammarAST.buildFromAST(parsedGrammar, "s", new GrammarAST.ImportResolver() {
63                 public InputStream getImportStream(String importname) { return null; }
64             });
65         return grammar;
66     }
67
68     // FIXME: this ought to be cached via serialization, I think
69     private static CharParser cp = null;
70     Tree<String> parseIt(Reader r) throws Exception {
71         if (cp==null)
72             cp = new CharParser(getGrammar());
73         CharInput ci = new CharInput(r);
74         Forest f = cp.parse(ci);
75         return f.expand1();
76     }
77
78     public Instruction[] parse(Reader r) throws Exception {
79         // this needs to be "code bag zero"
80         CodeBag baseCodeBag = new CodeBag();
81         CodeBag rootCodeBag = new CodeBag();
82         skip = false;
83         Tree<String> parsed = (Tree<String>)parseIt(r);
84         walk(parsed, rootCodeBag);
85
86         // map from arbitrary identifiers to actual addresses
87         int[] codeBagMap = new int[codeBags.size()];
88         int count = 0;
89         for(int i=0; i<codeBags.size(); i++) {
90             CodeBag c = codeBags.get(i);
91             codeBagMap[i] = count;
92             for(Instruction inst : c) {
93                 count++;
94             }
95         }
96
97         // now write for real
98         count = 0;
99         ArrayList<Instruction> ret = new ArrayList<Instruction>();
100         for(int i=0; i<codeBags.size(); i++) {
101             CodeBag c = codeBags.get(i);
102             for(int j=0; j<c.size(); j++) {
103                 Instruction inst = c.get(j);
104                 if (c.isCBD.contains(j)) {
105                     Set old = (Set)inst;
106                     long lit = 0;
107                     lit = FleetTwoFleet.CBD_SIZE.setval(lit, codeBags.get((int)old.immediate).size());
108                     lit = FleetTwoFleet.CBD_OFFSET.setval(lit, codeBagMap[(int)old.immediate]);
109                     inst = new Set(old.dock, false, IgnoreOLC, SetDest.DataLatch, lit);
110                 }
111                 ret.add(inst);
112                 count++;
113             }
114         }
115         long startcbd = 0;
116         for(int i=0; i<codeBags.size(); i++) {
117             if (codeBags.get(i)==rootCodeBag) {
118                 long lit = 0;
119                 lit = FleetTwoFleet.CBD_SIZE.setval(lit, codeBags.get(i).size());
120                 lit = FleetTwoFleet.CBD_OFFSET.setval(lit, codeBagMap[i]);
121                 startcbd = lit;
122             }
123         }
124         if (codeBags.size()<=2)
125             return (Instruction[])ret.toArray(new Instruction[0]);
126         return fixup((Instruction[])ret.toArray(new Instruction[0]), startcbd);
127     }
128
129     private Instruction[] fixup(Instruction[] instructions, long startcbd) {
130         ArrayList<Instruction> ret = new ArrayList<Instruction>();
131         Fleet fpga = fleet;
132
133         Dock inAddrWrite = null;
134         Dock inDataWrite = null;
135         Dock inCBD       = null;
136         Dock out         = null;
137         Dock debugIn     = null;
138         Dock ihorn       = null;
139
140         for(Ship ship : fpga) {
141             if ("Memory".equals(ship.getType()) && ship.getOrdinal()==0) {
142                 inAddrWrite = ship.getDock("inAddrWrite");
143                 inDataWrite = ship.getDock("inDataWrite");
144                 inCBD = ship.getDock("inCBD");
145                 out = ship.getDock("out");
146                 ihorn = out;
147                 //ihorn = ship.getDock("outIhorn");
148             }
149             if ("Debug".equals(ship.getType()) && ship.getOrdinal()==0) {
150                 debugIn = ship.getDock("in");
151             }
152         }
153
154         for(int i=0; i<instructions.length; i++) {
155             long lit = ((FleetTwoFleet)fpga).writeInstruction(instructions[i], out);
156             ret.add(discard(out));
157             ret.add(new Instruction.Shift(inDataWrite, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(36, 19, lit))));
158             ret.add(new Instruction.Shift(inDataWrite, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(18,  0, lit))));
159             ret.add(deliver(inDataWrite));
160             ret.add(new Instruction.Shift(inAddrWrite, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(36, 19, i))));
161             ret.add(new Instruction.Shift(inAddrWrite, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(18,  0, i))));
162             ret.add(deliver(inAddrWrite));
163         }
164         ret.add(new Instruction.Shift(inCBD, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(36, 19, startcbd))));
165         ret.add(new Instruction.Shift(inCBD, false, IgnoreOLC, new BitVector(fpga.getWordWidth()).set(getField(18,  0, startcbd))));
166         ret.add(wait(inCBD));
167         ret.add(deliver(inCBD));
168         ret.add(sendto(out, out.getPath(inCBD.getDataDestination(),null)));
169
170         int count = (int)FleetTwoFleet.CBD_SIZE.getval(startcbd);
171         // FIXME FIXME FIXME!
172         int MAX_ILC = 31;
173         int num_instrs = 0;
174         while(count > 0) {
175             int num = Math.min(count, MAX_ILC);
176             num_instrs+=2;
177             count -= num;
178             ret.add(new Instruction.Set(ihorn, false, IgnoreOLC, SetDest.InnerLoopCounter, num));
179             ret.add(new Instruction.Move(ihorn, false, IgnoreOLC, false, null,false,true,true,true,true,false));
180         }
181         if (num_instrs > ihorn.getInstructionFifoSize()) throw new RuntimeException();
182
183         return (Instruction[])ret.toArray(new Instruction[0]);
184     }
185
186     /** in the first pass, codebags are assigned "addresses" in arbitrary order */
187     void walk(Tree<String> t, CodeBag cb) {
188
189         String head = t.head();
190         if (head==null) {
191         } else if (head.equals("Program")) {
192             for(Tree<String> tc : t.child(0))
193                 walk(tc, cb);
194             if (t.size()>1)
195                 for(Tree<String> statement : t.child(1))
196                     fillCodeBag(statement, cb);
197    
198         } else if (head.equals("#ship")) {
199             String name = name(t.child(0));
200             String type = string(t.child(1));
201             Ship ship = null;
202
203             if (fleet instanceof FleetWithDynamicShips) {
204                 FleetWithDynamicShips dyn = ((FleetWithDynamicShips)fleet);
205                 ship = dyn.createShip(type, name);
206                 if (ship==null)
207                     throw new RuntimeException("couldn't find a ship called \""+type+"\"");
208             } else {
209                 ship = allocateShip(type);
210             }
211             shipMap.put(name, ship);
212
213         } else if (head.equals("#expect")) {
214             expect.add(number(t.child(0)));
215         } else if (head.equals("#skip")) {
216             skip = true;
217
218         }
219     }
220
221     String string(Tree<String> t) {
222         String ret = "";
223         if (t.head() != null) ret += t.head();
224         for(Tree<String> c : t)
225             ret += string(c);
226         return ret;
227     }
228
229     String stringBody(Tree<String> t) {
230         String ret = "";
231         for(Tree<String> c : t)
232             ret += string(c);
233         return ret;
234     }
235
236     String name(Tree<String> t) {
237         return string(t.child(0))+string(t.child(1));
238     }
239
240     Path path(Dock dock, Tree<String> t) {
241         if (!"Dock".equals(t.head()) && !"Destination".equals(t.head()) && !"ShipSpecificLiteral".equals(t.head())) return null;
242         String shipName = name(t.child(0));
243         String portName = name(t.child(1));
244         Ship ship = shipMap.get(shipName);
245         if (ship==null) throw new RuntimeException("no such ship \""+shipName+"\"");
246         Destination ret = null;
247         Dock bb = null;
248         for(Dock b : ship)
249             if (b.getName().equals(portName)) {
250                 bb = b;
251             }
252         if (bb==null)
253             throw new RuntimeException("no such pump \""+portName+"\"");
254         if (t.size() >= 3) {
255             if (":i".equals(t.child(2).head())) return dock.getPath(bb.getInstructionDestination(),SIGNAL_ZERO);
256             if (":1".equals(t.child(2).head())) return dock.getPath(bb.getDataDestination(),SIGNAL_ONE);
257             if (":0".equals(t.child(2).head())) return dock.getPath(bb.getDataDestination(),SIGNAL_ZERO);
258         }
259         return dock.getPath(bb.getDataDestination(),SIGNAL_ZERO);
260     }
261
262     Dock dock(Tree<String> t) {
263         if (!"Dock".equals(t.head()) && !"Destination".equals(t.head()) && !"ShipSpecificLiteral".equals(t.head()))
264             throw new RuntimeException(t+"");
265         String shipName = name(t.child(0));
266         String portName = name(t.child(1));
267         Ship ship = shipMap.get(shipName);
268         if (ship==null) throw new RuntimeException("no such ship \""+shipName+"\"");
269         Dock bb = null;
270         for(Dock b : ship)
271             if (b.getName().equals(portName))
272                 return b;
273         throw new RuntimeException("no such pump \""+portName+"\"");
274     }
275
276     private HashMap<String,Integer> numAllocated = new HashMap<String,Integer>();
277
278     Ship allocateShip(String shipType) {
279         int allocated = 0;
280         if (numAllocated.get(shipType) != null)
281             allocated = numAllocated.get(shipType);
282         numAllocated.put(shipType, allocated+1);
283         for(Iterator<Ship> it = fleet.iterator();
284             it.hasNext();
285             ) {
286             Ship s = it.next();
287             if (s.getType().equals(shipType)) {
288                 if (allocated == 0) return s;
289                 allocated--;
290             }
291         }
292         throw new RuntimeException("no more ships of type \""+shipType+"\"");
293     }
294
295     private long parseSSL(Tree t) {
296         String shipType = name(t.child(0));
297         String portName = name(t.child(1));
298         Ship chosenship = null;
299         for(Ship ship : fleet) {
300             if (ship.getType().equals(shipType)) {
301                 chosenship = ship;
302                 break;
303             }
304         }
305         if (chosenship==null) throw new RuntimeException("no ships of type " + shipType);
306         Dock chosenport = chosenship.getDock(portName);
307         Tree specs = t.child(2);
308         long literal = 0;
309         for(int i=0; i<specs.size(); i++) {
310             Tree tt = specs.child(i);
311             literal |= resolveLiteral(chosenport, stringBody(tt));
312         }
313         return literal;
314     }
315
316     private static long resolveLiteral(Dock dd, String s) {
317         long val = 0;
318         long ret = 0;
319         boolean hasval = false;
320         if (s.indexOf('=') != -1) {
321             val = Long.parseLong(s.substring(s.indexOf('=')+1));
322             s = s.substring(0, s.indexOf('='));
323             hasval = true;
324         }
325         ShipDescription.Constant c = ((FleetTwoDock)dd).getConstant(s);
326         if (c==null) throw new RuntimeException("no constant " + s + " on dock " + dd);
327         ret |= c.setbits;
328         ret &= ~c.clearbits;
329         if (hasval)
330             ret |= ((~(0xffffffffffffffffL << c.numberWidth)) & val) << c.numberOffset;
331         return ret;
332     }
333
334     private static FlagFunction parseFlags(Tree<String> t) {
335         FlagFunction ret = FlagFunction.ZERO;
336         for(int i=0; i<t.size(); i++) {
337             String s = t.child(i).head();
338             if (s.equals("0"))  ret = ret.add(FlagFunction.ZERO);
339             if (s.equals("1"))  ret = ret.add(FlagFunction.ONE);
340             if (s.equals("a"))  ret = ret.add(FlagA);
341             if (s.equals("!a")) ret = ret.add(NotFlagA);
342             if (s.equals("b"))  ret = ret.add(FlagB);
343             if (s.equals("!b")) ret = ret.add(NotFlagB);
344             if (s.equals("c"))  ret = ret.add(FlagC);
345             if (s.equals("!c")) ret = ret.add(NotFlagC);
346         }
347         return ret;
348     }
349
350     private int anoncount = 1;
351     void fillCodeBag(Tree<String> t, CodeBag cb) {
352         if (t.head()==null) return;
353         else if (t.head().equals("CodeBagDef")) {
354             CodeBag cb2 = getCodeBag(name(t.child(0)));
355             for(Tree<String> statement : t.child(1))
356                 fillCodeBag(statement, cb2);
357
358         } else if (t.head().equals("Fiber")) {
359             Dock dock = (Dock)dock(t.child(0));
360             
361             OUTER: for(Tree tt : t.child(1)) {
362
363                 if ("tail".equals(tt.head()))    {
364                     cb.add(new Tail(dock));
365                     continue;
366                 }
367
368                 int count = 1;
369                 Predicate predicate = Default;
370                 boolean looping = false;
371                 boolean interruptible = false;
372                 for(int i=0; i<tt.child(0).size(); i++) {
373                     Tree ttt = tt.child(0).child(i);
374                     if ("[a]".equals(ttt.head()))  predicate = FlagA;
375                     if ("[b]".equals(ttt.head()))  predicate = FlagB;
376                     if ("[c]".equals(ttt.head()))  predicate = FlagC;
377                     if ("[!a]".equals(ttt.head())) predicate = NotFlagA;
378                     if ("[!b]".equals(ttt.head())) predicate = NotFlagB;
379                     if ("[!c]".equals(ttt.head())) predicate = NotFlagC;
380                     // FIXME: test case for this
381                     if ("[*]".equals(ttt.head()))  predicate = IgnoreOLC;
382                     // FIXME: test case for this
383                     if ("[olc=0]".equals(ttt.head()))  predicate = OLCZero;
384                     if ("[Rq]".equals(ttt.head()))  looping = true;
385                 }
386                 tt = tt.child(1);
387                 if ("tail".equals(tt.head()))    {
388                     cb.add(new Tail(dock));
389                     continue;
390                 } else if ("flags".equals(tt.head()))    {
391                     cb.add(new Set(dock, looping, predicate, parseFlags(tt.child(0)), parseFlags(tt.child(1))));
392                     continue;
393                 } else if ("olc=word".equals(tt.head()))    {
394                     cb.add(new Set(dock, looping, predicate, SetDest.OuterLoopCounter, SetSource.DataLatch));
395                     continue;
396                 } else if ("ilc=word".equals(tt.head()))    {
397                     cb.add(new Set(dock, looping, predicate, SetDest.InnerLoopCounter, SetSource.DataLatch));
398                     continue;
399                 } else if ("*".equals(tt.head()))    {
400                     cb.add(new Set(dock, looping, predicate, SetDest.InnerLoopCounter, SetSource.Infinity));
401                     continue;
402                 } else if ("olc=int".equals(tt.head()))    {
403                     cb.add(new Set(dock, looping, predicate, SetDest.OuterLoopCounter, (number(tt.child(0)))));
404                     continue;
405                 } else if ("ilc=int".equals(tt.head()))    {
406                     cb.add(new Set(dock, looping, predicate, SetDest.InnerLoopCounter, (number(tt.child(0)))));
407                     continue;
408                 } else if ("--".equals(tt.head())) {
409                     cb.add(new Set(dock, looping, predicate, SetDest.OuterLoopCounter, SetSource.Decrement));
410                     continue;
411                 } else if ("nop".equals(tt.head())) {
412                     // FIXME: test case for "torpedoable nop"
413                     if (tt.child(0).size() > 0 && "[T]".equals(tt.child(0).head()))  interruptible = true;
414                     cb.add(new Move(dock, looping, predicate, interruptible, null, false, false, false, false, false, false));
415                 } else if ("shift".equals(tt.head())) {
416                     cb.add(new Shift(dock, looping, predicate,
417                                      new BitVector(dock.getShip().getFleet().getWordWidth()).set(number(tt.child(0)))));
418                 } else if ("word".equals(tt.head())) {
419                     long literal = 0;
420                     if (tt.child(0).head().equals("CodeBagBody")) {
421                         Tree<String> tq = tt;
422                         CodeBag cb2 = getCodeBag("anon"+(anoncount++));
423                         for(Tree<String> statement : tq.child(0))
424                             fillCodeBag(statement, cb2);
425                         cb.add(new Set(dock, looping, predicate, SetDest.DataLatch, (cb2.getFakeAddress())), true);
426                         continue OUTER;
427                     } else if (tt.child(0).head().equals("Name")) {
428                         String refname = name(tt.child(0));
429                         CodeBag cb2 = getCodeBag(refname);
430                         cb.add(new Set(dock, looping, predicate, SetDest.DataLatch, (cb2.getFakeAddress())), true);
431                         continue OUTER;
432                     } else if (tt.child(0).head().equals("[")) {
433                         literal = parseSSL(tt.child(0));
434                     } else {
435                         literal = number(tt.child(0));
436                     }
437                     count = 1;
438                     /*
439                     if ("int".equals(tt.child(1).head())) {
440                         count = (int)number(tt.child(1));
441                     } else if ("forever".equals(tt.child(1).head())) {
442                         count = 0;
443                     }
444                     */
445
446
447                     if (FleetTwoFleet.isSmallEnoughToFit(literal)) {
448                         cb.add(new Set(dock, looping, predicate, SetDest.DataLatch, (literal)));
449                     } else {
450                         int counter = 0;
451                         while(counter < dock.getShip().getFleet().getWordWidth()) counter += SHIFT.valmaskwidth;
452                         while(counter > 0) {
453                             cb.add(new Shift(dock, looping, predicate,
454                                              new BitVector(dock.getShip().getFleet().getWordWidth())
455                                              .set(getField(counter-1, counter-SHIFT.valmaskwidth, literal))));
456                             counter -= SHIFT.valmaskwidth;
457                         }
458                     }
459                         
460                     continue;
461                 }
462                 Tree ttx = null;
463                 boolean requeue = false;
464                 if ("[T]".equals(tt.child(0).head()))  interruptible = true;
465                 ttx = tt.child(tt.size()-1);
466                 boolean tokenIn = false;
467                 boolean dataIn = false;
468                 boolean latch = false;
469                 boolean dataOut = false;
470                 boolean tokenOut = false;
471                 boolean dispatch = false;
472                 boolean localLiteral = false;
473                 boolean ignoreUntilLast = false;
474                 long literal = 0;
475                 Path path = null;
476                 for(int i=0; i<ttx.size(); i++) {
477                     Tree ttt = ttx.child(i);
478                     if      ("recv token".equals(ttt.head()))    { tokenIn = true; }
479                     else if ("discard".equals(ttt.head())) { dataIn = true; latch = false; }
480                     else if ("take".equals(ttt.head()))    { dataIn = true; latch = true; }
481                     else if ("collect".equals(ttt.head()))    {
482                         if (dock.isInputDock())
483                             throw new RuntimeException("you can't use \"collect\" at input docks; try \"recv\" instead");
484                         dataIn = true;
485                         latch = true;
486                     }
487                     else if ("collect path".equals(ttt.head()))    {
488                         if (dock.isInputDock())
489                             throw new RuntimeException("you can't use \"collect\" at input docks; try \"recv\" instead");
490                         dataIn = true;
491                         latch = false;
492                         dispatch = true;
493                     }
494                     else if ("collect packet".equals(ttt.head()))    {
495                         if (dock.isInputDock())
496                             throw new RuntimeException("you can't use \"collect\" at input docks; try \"recv\" instead");
497                         dataIn = true;
498                         latch = true;
499                         dispatch = true;
500                     }
501                     else if ("collect nothing".equals(ttt.head()))    {
502                         if (dock.isInputDock())
503                             throw new RuntimeException("you can't use \"collect\" at input docks; try \"recv\" instead");
504                         dataIn = true;
505                     }
506                     else if ("recv".equals(ttt.head()))    {
507                         if (dock.isOutputDock())
508                             throw new RuntimeException("you can't use \"recv\" at input docks; try \"collect\" instead");
509                         dataIn = true;
510                         latch = true;
511                     }
512                     else if ("recv path".equals(ttt.head()))    {
513                         if (dock.isOutputDock())
514                             throw new RuntimeException("you can't use \"recv\" at input docks; try \"collect\" instead");
515                         dataIn = true;
516                         latch = false;
517                         dispatch = true;
518                     }
519                     else if ("recv packet".equals(ttt.head()))    {
520                         if (dock.isOutputDock())
521                             throw new RuntimeException("you can't use \"recv\" at input docks; try \"collect\" instead");
522                         dataIn = true;
523                         latch = true;
524                         dispatch = true;
525                     }
526                     else if ("recv nothing".equals(ttt.head()))    {
527                         if (dock.isOutputDock())
528                             throw new RuntimeException("you can't use \"recv\" at input docks; try \"collect\" instead");
529                         dataIn = true;
530                     }
531                     else if ("send".equals(ttt.head()))  {
532                         dataOut = true;
533                     }
534                     else if ("send to".equals(ttt.head()))  { dataOut = true; path = path(dock, ttt.child(0)); }
535                     else if ("deliver".equals(ttt.head())) { dataOut = true;  }
536                     else if ("send token".equals(ttt.head()))     { tokenOut = true; }
537                     else if ("send token to".equals(ttt.head()))     { tokenOut = true; path = path(dock, ttt.child(0)); }
538                 }
539                 cb.add(new Move(dock, looping, predicate,
540                                             interruptible, path, tokenIn, dataIn,
541                                             latch, dispatch, dataOut, tokenOut));
542             }
543         }
544     }
545
546     private class CodeBag extends ArrayList<Instruction> {
547         public long address = -1;
548         public final String name;
549         private HashSet<Integer> isCBD = new HashSet<Integer>();
550         public CodeBag() { codeBags.add(this); this.name = "root"; }
551         public CodeBag(String name) { codeBags.add(this); codeBagsByName.put(name, this); this.name = name; }
552         public long getFakeAddress() { return codeBags.indexOf(this); }
553         public boolean equals(Object o) { return this==o; }
554         public void add(Instruction i, boolean isCBD) {
555             if (isCBD) this.isCBD.add(size());
556             add(i);
557         }
558     }
559
560     // hideous hack
561     public static ArrayList<Long> expect;
562     public static boolean         skip;
563
564     private static long number(Tree t) {
565         String ret = "";
566         for(Object c : t.child(2)) ret += ((Tree)c).head();
567         boolean negative = "-".equals(t.child(0).head());
568         ret = ret.trim();
569         long val = 0;
570         if      ("0x".equals(t.child(1).head())) val = Long.parseLong(ret, 16);
571         else if ("0b".equals(t.child(1).head())) val = Long.parseLong(ret, 2);
572         else                                     val = Long.parseLong(ret);
573         if (negative) val = -1L * val;
574         return val;
575     }
576     /**
577      *  This interface marks Fleets which can create ships on the fly, like the fleeterpreter;
578      *  if available, the parser will use this interface to create ships out of #ship definitions.
579      */
580     public static interface FleetWithDynamicShips {
581         public Ship createShip(String shiptype, String shipname);
582     }
583
584     private static Move discard(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, true,  false, false, false, false); }
585     private static Move deliver(Dock dock)           { return new Move(dock, false, IgnoreOLC, false, null, false, false, false, false, true,  false); }
586     private static Move wait(Dock dock)              { return new Move(dock, false, IgnoreOLC, false, null, true,  false, false, false, false, false); }
587     private static Move sendto(Dock dock, Path path) { return new Move(dock, false, IgnoreOLC, false, path, false, false, false, false, true,  false); }
588 }