checkpoint
[fleet.git] / src / edu / berkeley / fleet / Literal.java
1 package edu.berkeley.fleet;
2
3 /** represents a literal; currently handled in an extremely hoakey fashion */
4 public class Literal {
5
6     public static class LiteralDatum extends Dispatchable {
7         int data;
8         PortReference destination;
9         private boolean isCodeBag;
10         private int count;
11         public LiteralDatum(int data, PortReference destination, boolean isCodeBag) { this(data, destination, isCodeBag, 1); }
12         public LiteralDatum(int data, PortReference destination, boolean isCodeBag, int count) {
13             this.data = data;
14             this.destination = destination;
15             this.isCodeBag = isCodeBag;
16             this.count = count;
17         }
18         protected int getData() { return data; }
19         public void dispatch(Fleet fleet) {
20             for(int i=0; i<count; i++) {
21                 Port port = destination.resolve(fleet);
22                 Log.data((isCodeBag?(CodeBag.getCodeBagByDescriptor(getData())+""):(getData()+"")), null, port);
23                 port.addDataFromFabric(getData());
24             }
25         }
26         public String toString() {
27             return (isCodeBag?(CodeBag.getCodeBagByDescriptor(getData())+""):(getData()+""))+" -"+(count==1?"":("["+count+"]-"))+"> "+destination;
28         }
29     }
30
31     public static class CodeBagRef extends LiteralDatum {
32         private String name;
33         private CodeBag parent;
34         public CodeBagRef(String name, CodeBag parent, PortReference destination) {
35             super(0, destination, true);
36             this.parent = parent;
37             this.name = name;
38         }
39         protected int getData() {
40             if (parent.getCodeBag(name)==null)
41                 throw new RuntimeException("[invalid codebag, name="+name+"]");
42             return parent.getCodeBag(name).getDescriptor();
43         }
44     }
45
46     public static class LiteralToken extends Dispatchable {
47         private PortReference destination;
48         public LiteralToken(PortReference destination) {
49             this.destination = destination;
50         }
51         public void dispatch(Fleet fleet) {
52             destination.resolve(fleet).addTokenFromFabric();
53         }
54         public String toString() {
55             return "token -> "+destination;
56         }
57     }
58
59     public static class ShipSpecific extends Dispatchable {
60         private String data;
61         private PortReference destination;
62         private int count;
63         public ShipSpecific(String data, PortReference destination, int count) {
64             this.data = data;
65             this.destination = destination;
66             this.count = count;
67         }
68         public void dispatch(Fleet fleet) {
69             for(int i=0; i<count; i++) {
70                 Port port = destination.resolve(fleet);
71                 Log.data("\""+data+"\"", null, port);
72                 int dat = port.getShip().resolveShipSpecificConstant(data);
73                 port.addDataFromFabric(dat);
74             }
75         }
76         public String toString() {
77             return ("\""+data+"\""+" -"+(count==1?"":("["+count+"]-"))+"> "+destination);
78         }
79     }
80 }