代写代考 CSE 10

Using Statement and Program
23 October 2020 OSU CSE 1

Statement and Program

Copyright By PowCoder代写 加微信 powcoder

• The Statement and Program
component families for the BL language are similar to what a Java compiler uses to represent a Java program
• Consider refactoring of Java or BL programs, e.g.:
– Simplifying if-then-else constructs – Renaming methods/instructions
23 October 2020 OSU CSE 2

Statement and Program Refactoring means
restructuring code (presumably to improve its
• The Statement and Program readability, etc.) without
choamngpinognitesnbtehfavmioirl.ies for the BL language are similar to what a Java compiler uses to represent a Java program
• Consider refactoring of Java or BL programs, e.g.:
– Simplifying if-then-else constructs – Renaming methods/instructions
23 October 2020 OSU CSE 3

simplifyIfElse for Statement void simplifyIfElse(Statement s)
• RefactorsssothateveryIF_ELSEstatement with a negated condition (NEXT_IS_NOT_-
EMPTY, NEXT_IS_NOT_ENEMY, NEXT_IS_- NOT_FRIEND, NEXT_IS_NOT_WALL) is replaced by an equivalent IF_ELSE with the opposite condition and the “then” and “else” BLOCKs switched. Every other statement is left unmodified.
23 October 2020 OSU CSE 4

simplifyIfElse for Statement
void simplifyIfElse(Statement s)
• Updates:s
• Ensures:
s = [#s refactored so that IF_ELSE
statements with “not” conditions are
simplified so the “not” is removed]
23 October 2020 OSU CSE 5

The Idea • After:
IF_ELSE NEXT_IS_NOT- _EMPTY
NEXT_IS_- EMPTY
23 October 2020

happens for every IF_ELSE statement in the entire
IF_ELSE NEXT_IS_NOT- _EMPTY
NEXT_IS_- EMPTY
The Idea The same transformation
• After: statement s.
23 October 2020

simplifyIfElse for Program void simplifyIfElse(Program p)
• RefactorspsothateveryIF_ELSEstatementin the program body or the body of any user- defined instruction with a negated condition (NEXT_IS_NOT_EMPTY, NEXT_IS_NOT_- ENEMY, NEXT_IS_NOT_FRIEND, NEXT_IS_- NOT_WALL) is replaced by an equivalent IF_ELSE with the opposite condition and the “then” and “else” BLOCKs switched. Everything else is left unmodified.
23 October 2020 OSU CSE 8

simplifyIfElse for Program
void simplifyIfElse(Program p)
• Updates:p
• Ensures:
p = [#p refactored so that IF_ELSE
statements with “not” conditions are
simplified so the “not” is removed]
23 October 2020 OSU CSE 9

• Assume you are given an implementation
of simplifyIfElse(Statement) • Write an implementation of
simplifyIfElse(Program)
23 October 2020 OSU CSE 10

Structure of the Code
* For each user-defined instruction body,
* simplify its IF_ELSE statements
* For the program body, simplify its
* IF_ELSE statements
23 October 2020 OSU CSE 11

Structure of the Code
Map ctxt = p.newContext(); Map c = p.newContext(); p.swapContext(ctxt);
while (ctxt.size() > 0) {
Map.Pair instr = ctxt.removeAny();
// simplify IF_ELSE for body of instr (a Statement)
c.add(name, body);
p.swapContext(c);
Statement pBody = p.newBody(); p.swapBody(pBody);
// simplify IF_ELSE for pBody (a Statement) p.swapBody(pBody);
23 October 2020 OSU CSE 12

Structure of the Code
Map ctxt = p.newContext(); Map c = p.newContext(); p.swapContext(ctxt);
while (ctxt.size() > 0) {
Map.Pair instr = ctxt.removeAny();
// simplify IF_ELSE for body of instr (a Statement)
c.add(name, body);
p.swapContext(c);
Statement pBody = p.newBody();
We need to use the
removeAny style for this loop
Map in the loop body.
23 October 2020 OSU CSE 13
(not the iterator style) // simplify IF_ELSE for pBody (a Statement)
p.swapBody(pBody);
p.swapBody(pBody);because we need to modify the

Another Refactoring Idea
• Eclipse allows you to change the name of a Java method, including all calls to it, with a single refactoring step
– See: Refactor >> Rename …
• You can write code to do the same thing
for a BL program; how?
23 October 2020 OSU CSE 14

renameInstruction for Statement
void renameInstruction(Statement s, String oldName, String newName)
• Refactors s by renaming every occurrence of instruction oldName to newName. Every other statement is left unmodified.
• Updates: s
• Requires:
[newName is a valid IDENTIFIER]
• Ensures:
s = [#s refactored so that every occurrence of instruction oldName is replaced by newName]
23 October 2020 OSU CSE 15

The Idea • After:
CALL oldName
23 October 2020

renameInstruction for Program
void renameInstruction(Program p, String oldName, String newName)
• Refactors p by renaming instruction oldName, and every call to it, to newName. Everything else is left unmodified.
• Updates: p
• Requires:
oldName is in DOMAIN(p.context) and
[newName is a valid IDENTIFIER but not the name
of a primitive instruction in the BL language] and
newName is not in DOMAIN(p.context)
• Ensures:
p = [#p refactored so that instruction
oldName and every call to it are
replaced by newName]
23 October 2020 OSU CSE 17

Example: Rename bar to car
PROGRAM Example IS INSTRUCTION foo IS
IF random THEN bar
ELSE turnleft
END foo INSTRUCTION bar IS
turnright END bar
23 October 2020
WHILE true DO
IF next-is-empty THEN move
IF next-is-enemy THEN
bar ELSE foo
END IF END IF
END WHILE END Example
OSU CSE 18

Example: Rename bar to car
PROGRAM Example IS INSTRUCTION foo IS
IF random THEN
ELSE turnleft
END foo INSTRUCTION bar IS
turnright END bar
23 October 2020
WHILE true DO
IF next-is-empty THEN move
IF next-is-enemy THEN
END IF END IF
END WHILE END Example
OSU CSE 19

Example: Rename bar to car
PROGRAM Example IS INSTRUCTION foo IS
IF random THEN
ELSE turnleft
END foo INSTRUCTION car IS
turnright END car
23 October 2020
WHILE true DO
IF next-is-empty THEN move
IF next-is-enemy THEN
END IF END IF
END WHILE END Example
OSU CSE 20

• Assume you are given an implementation of renameInstruction(Statement, String, String)
• Write an implementation of
renameInstruction(Program,
String, String)
23 October 2020 OSU CSE 21

Draw AST from Code
• What is the value of instr at the end?
Statement instr = new Statement1(); Statement eblk = instr.newInstance(); instr.assembleCall(“move”); eblk.addToBlock(0, instr);
Statement tblk = instr.newInstance(); instr.assembleCall(“infect”);
tblk.addToBlock(0, instr);
instr.assembleCall(“turnleft”);
tblk.addToBlock(1, instr); instr.assembleIfElse(Condition.NEXT_IS_ENEMY, tblk, eblk);
23 October 2020 OSU CSE 22

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com