SUB: subtract the two arguments; treat link as carry
MAX:
MIN:
-SORT: output min(in1,in2) followed by max(in1,in2) (FIXME: redundant?)
== TeX ==============================================================
This ship is a two-input arithmetic unit. It features several
//MOD:
== Fleeterpreter ====================================================
+public long resolveLiteral(String literal) {
+ if (literal.equals("ADD")) return 0;
+ if (literal.equals("SUB")) return 1;
+ if (literal.equals("MAX")) return 2;
+ if (literal.equals("MIN")) return 3;
+ return super.resolveLiteral(literal);
+}
public void service() {
if (box_in1.dataReadyForShip() &&
box_in2.dataReadyForShip() &&
break;
case 1: box_out.addDataFromShip(a-b); // SUB
break;
- case 2: box_out.addDataFromShip(Math.max(a,b)); // ADD
+ case 2: box_out.addDataFromShip(Math.max(a,b)); // MAX
break;
- case 3: box_out.addDataFromShip(Math.min(a,b)); // SUB
+ case 3: box_out.addDataFromShip(Math.min(a,b)); // MIN
break;
default: box_out.addDataFromShip(0);
break;
}
public String toString() { return getType() + "[" + getOrdinal() + "]"; }
-
+ public long resolveLiteral(String literal) {
+ throw new RuntimeException("unknown literal \""+literal+"\" on ship "+this);
+ }
}
}
BenkoBox portReference(Tree<String> t) {
- if (!"Port".equals(t.head())) return null;
+ if (!"Port".equals(t.head()) && !"ShipSpecificLiteral".equals(t.head())) return null;
String shipName = name(t.child(0));
String portName = name(t.child(1));
Ship ship = shipMap.get(shipName);
for(Tree<String> statement : t.child(1))
fillCodeBag(statement, cb2);
+ } else if (t.head().equals("ShipSpecificLiteral")) {
+ String shipType = name(t.child(0).child(0));
+ String portName = name(t.child(0).child(1));
+ Ship chosenship = null;
+ for(Ship ship : fleet) {
+ if (ship.getType().equals(shipType)) {
+ chosenship = ship;
+ break;
+ }
+ }
+ long literal = chosenship.resolveLiteral(portName);
+ BenkoBox benkobox = portReference(t.child(1));
+ cb.add(new Instruction.Literal.Absolute(benkobox, literal));
+
} else if (t.head().equals("Literal")) {
int literal = Integer.parseInt(string(t.child(0)));
BenkoBox benkobox = portReference(t.child(1));
Statement = Fiber:: Source ":" (Instruction +/ ws) /ws
| Literal:: int ":" "sendto" Port ";" /ws
+ | ShipSpecificLiteral:: SSL ":" "sendto" Port ";" /ws
| CodeBagDescriptor:: CodeBag ":" "sendto" Port ";" /ws
| NamedCodeBag:: name ":" "{" CodeBagBody "}" /ws
Source = Port
| ShipSpecific
+SSL = ShipSpecificLiteral:: shiptype "." ShipSpecificLiteral
Port = Port:: shipname "." portname
| ^"()"
CodeBag = CodeBagRef:: CodeBagName
| AnonymousCodeBag:: "{" CodeBagBody "}" /ws
-CodeBagName = name
-shipname = name
-portname = name
-name = Name:: [A-Za-z] [A-Za-z0-9\[\]_]**
-index = "[" [0-9]+ "]" | [0-9]+
-int = [\-0-9]++
-ShipSpecific = ShipSpecific:: "\"" ~[\"]++ "\""
+CodeBagName = name
+shipname = name
+shiptype = Name:: [A-Z] [A-Za-z0-9\[\]_]**
+ShipSpecificLiteral = Name:: [A-Z] [A-Z0-9\[\]_]**
+portname = Name:: [a-z] [A-Za-z0-9\[\]_]**
+name = Name:: [A-Za-z] [A-Za-z0-9\[\]_]**
+index = "[" [0-9]+ "]" | [0-9]+
+int = [\-0-9]++
+ShipSpecific = ShipSpecific:: "\"" ~[\"]++ "\""
// the following are not part of the official FLEET syntax and are
// specific to Adam's interpreter.
--- /dev/null
+// expected output
+#ship debug : Debug
+#ship alu : Alu2
+
+#expect 17
+#expect 1
+#expect 8
+#expect 9
+
+debug.in: [*] take, deliver;
+9: sendto alu.in1;
+9: sendto alu.in1;
+9: sendto alu.in1;
+9: sendto alu.in1;
+8: sendto alu.in2;
+8: sendto alu.in2;
+8: sendto alu.in2;
+8: sendto alu.in2;
+
+Alu2.ADD: sendto alu.inOp;
+Alu2.SUB: sendto alu.inOp;
+Alu2.MIN: sendto alu.inOp;
+Alu2.MAX: sendto alu.inOp;
+
+alu.in1: [*] take, deliver;
+alu.in2: [*] take, deliver;
+alu.inOp: [*] take, deliver;
+alu.out:
+ [*] take, sendto debug.in;