Translate the following four BL programs into their executable form in the tables next…
- User
Translate the following four BL programs into their executable form in the tables next to each example using the code generation "patterns" discussed in class (especially slides 30-47). Use only the primitives: MOVE, TURNLEFT, TURNRIGHT, INFECT, SKIP, and HALT; unconditional jump: JUMP; and the conditional jumps: JUMP_IF_NOT_NEXT_IS_EMPTY, JUMP_IF_NOT_NEXT_IS_NOT_EMPTY, etc.) Note that the tables may be bigger than the actual length of the generated code. PROGRAM Example1 IS BEGIN IF next-is-wall THEN turnright turnright infect END IF END Example1 0 1 2 3 4 5 6 7 8 9 10 PROGRAM Example2 IS BEGIN IF next-is-wall THEN turnright turnright infect ELSE infect move END IF END Example2 0 1 2 3 4 5 6 7 8 9 10 PROGRAM Example3 IS BEGIN WHILE next-is-not-empty DO IF next-is-wall THEN turnright turnright infect ELSE infect move END IF END WHILE END Example3 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 PROGRAM Example4 IS
codeINSTRUCTION TurnBackAndInfect IS turnright turnright IF next-is-enemy THEN infect END IF END TurnBackAndInfectBEGIN WHILE true DO TurnBackAndInfect END WHILE END Example4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Complete the body of the following public static method. A discussion of the BugsWorld virtual machine and its instruction set and of Java enumerated types is in the Code Generation slides. Review your code carefully and trace it on some examples. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /**
- Returns the location of the next primitive instruction to execute in
- compiled program {@code cp} given what the bug sees {@code wbs} and
- starting from location {@code pc}.
- @param cp
-
code
the compiled program - @param wbs
-
code
the {@code CellState} indicating what the bug sees - @param pc
-
code
the program counter - @return the location of the next primitive instruction to execute
- @requires
- [cp is a valid compiled BL program] and
- 0 <= pc < cp.length and
- [pc is the location of an instruction byte code in cp, that is, pc
- cannot be the location of an address]
- @ensures
- [return the address of the next primitive instruction that
- should be executed in program cp given what the bug sees wbs and
- Assistant