From: adam Date: Sat, 17 Feb 2007 08:27:41 +0000 (+0100) Subject: add ship specific literals and a test case X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=d865b7cf30cd580de8cfffd1ed4b7c3b0fe457ac;p=fleet.git add ship specific literals and a test case --- diff --git a/ships/Alu2.ship b/ships/Alu2.ship index 94e853b..14de79f 100644 --- a/ships/Alu2.ship +++ b/ships/Alu2.ship @@ -12,7 +12,6 @@ ADD: add the two arguments; treat link as carry 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 @@ -33,6 +32,13 @@ move elsewhere: //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() && @@ -46,9 +52,9 @@ public void service() { 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; diff --git a/src/edu/berkeley/fleet/api/Ship.java b/src/edu/berkeley/fleet/api/Ship.java index 787b1c2..c96d584 100644 --- a/src/edu/berkeley/fleet/api/Ship.java +++ b/src/edu/berkeley/fleet/api/Ship.java @@ -30,5 +30,7 @@ public abstract class Ship { } public String toString() { return getType() + "[" + getOrdinal() + "]"; } - + public long resolveLiteral(String literal) { + throw new RuntimeException("unknown literal \""+literal+"\" on ship "+this); + } } diff --git a/src/edu/berkeley/fleet/assembler/Parser.java b/src/edu/berkeley/fleet/assembler/Parser.java index 1e3e12e..2782870 100644 --- a/src/edu/berkeley/fleet/assembler/Parser.java +++ b/src/edu/berkeley/fleet/assembler/Parser.java @@ -166,7 +166,7 @@ public class Parser { } BenkoBox portReference(Tree 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); @@ -203,6 +203,20 @@ public class Parser { for(Tree 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)); diff --git a/src/edu/berkeley/fleet/assembler/fleet.g b/src/edu/berkeley/fleet/assembler/fleet.g index 736d4ae..d406c3d 100644 --- a/src/edu/berkeley/fleet/assembler/fleet.g +++ b/src/edu/berkeley/fleet/assembler/fleet.g @@ -15,6 +15,7 @@ Program = Program:: Directive+/ws 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 @@ -38,6 +39,7 @@ Command = Nop:: "nop" Source = Port | ShipSpecific +SSL = ShipSpecificLiteral:: shiptype "." ShipSpecificLiteral Port = Port:: shipname "." portname | ^"()" @@ -45,13 +47,15 @@ CodeBagBody = Statement +/ ws 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. diff --git a/tests/alu2/simple-alu2-test.fleet b/tests/alu2/simple-alu2-test.fleet new file mode 100644 index 0000000..965d80a --- /dev/null +++ b/tests/alu2/simple-alu2-test.fleet @@ -0,0 +1,29 @@ +// 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;