cse 2231 - statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.statement.pdfthe...
TRANSCRIPT
![Page 1: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/1.jpg)
Statement
27 February 2019 OSU CSE 1
![Page 2: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/2.jpg)
BL Compiler Structure
27 February 2019 OSU CSE 2
CodeGeneratorParserTokenizer
string ofcharacters
(source code)
string oftokens
(“words”)
abstractprogram
string ofintegers
(object code)
A BL program consists of some statements …
![Page 3: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/3.jpg)
Statement
• The Statement component family allows you to manipulate values that are ASTs for BL statements
• The mathematical model of a Statementvalue is a tree of STATEMENT_LABELwith some constraints
27 February 2019 OSU CSE 3
![Page 4: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/4.jpg)
27 February 2019 OSU CSE 4
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
![Page 5: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/5.jpg)
27 February 2019 OSU CSE 5
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
A Statement variable’s value is atree of STATEMENT_LABEL
with some constraints, so we userather than
to illustrate its recursive structure.
![Page 6: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/6.jpg)
27 February 2019 OSU CSE 6
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
The kind of statement (based on the root)
determines how many and which kinds of
children it may have.
![Page 7: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/7.jpg)
27 February 2019 OSU CSE 7
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
The children of a BLOCK statement may
not be BLOCKstatements.
![Page 8: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/8.jpg)
27 February 2019 OSU CSE 8
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
The child of an IF or WHILE statement must
be a BLOCKstatement.
![Page 9: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/9.jpg)
27 February 2019 OSU CSE 9
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
The two children of an IF_ELSE statement
must be BLOCKstatements.
![Page 10: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/10.jpg)
27 February 2019 OSU CSE 10
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
A CALL statement has no children.
![Page 11: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/11.jpg)
Interfaces and Classes
Statement-Kernel
extends
Standard
extends
27 February 2019 OSU CSE 11
Statement
implements
Statement1
![Page 12: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/12.jpg)
Interfaces and Classes
Statement-Kernel
extends
Standard
extends
27 February 2019 OSU CSE 12
Statement
implements
Statement1
StatementKernelhas contracts for 12 methods that involve
“assembling” and “disassembling” the
various BL statements (similar to Tree
methods).
![Page 13: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/13.jpg)
Interfaces and Classes
Statement-Kernel
extends
Standard
extends
27 February 2019 OSU CSE 13
Statement
implements
Statement1
Statementhas these additional
methods (not discussed here):
parseparseBlockprettyPrint
![Page 14: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/14.jpg)
Enumerations
• Java has a special construct, enum (short for “enumeration”), that easily allows you to use meaningful symbolic names where you might otherwise be inclined to declare int variables and give them arbitrary values to participate just in equality tests
27 February 2019 OSU CSE 14
![Page 15: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/15.jpg)
Example: The Kind Enum
• The interface StatementKernelcontains this code:/**
* The kinds of statements.
*/
enum Kind {
BLOCK, IF, IF_ELSE, WHILE, CALL
}
27 February 2019 OSU CSE 15
![Page 16: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/16.jpg)
Example: The Kind Enum
• The interface StatementKernelcontains this code:/**
* The kinds of statements.
*/
enum Kind {
BLOCK, IF, IF_ELSE, WHILE, CALL
}
27 February 2019 OSU CSE 16
This is quite different from:final int BLOCK = 1;final int IF = 2;...
You may not do arithmetic with the enum constants, where you could
with the int constants above.
![Page 17: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/17.jpg)
Example: The Condition Enum/**
* The possible conditions for IF, IF_ELSE,
* and WHILE statements.
*/
enum Condition {
NEXT_IS_EMPTY, NEXT_IS_NOT_EMPTY,
NEXT_IS_WALL, NEXT_IS_NOT_WALL,
NEXT_IS_FRIEND, NEXT_IS_NOT_FRIEND,
NEXT_IS_ENEMY, NEXT_IS_NOT_ENEMY,
RANDOM, TRUE
}
27 February 2019 OSU CSE 17
![Page 18: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/18.jpg)
Mathematical Model (1)IDENTIFIER is string of characterexemplar id
constraint[id starts with a letter 'a'-'z',
'A'-'Z'] and[id contains only letters, digits
'0'-'9', and '-'] and[id is not one of the keywords or
conditions in the BL language]
27 February 2019 OSU CSE 18
![Page 19: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/19.jpg)
Mathematical Model (2)STATEMENT_LABEL is (
kind: Kind,
test: Condition,
call: IDENTIFIER)
exemplar sl
constraint[if sl.kind = BLOCK then sl.test and sl.call are
irrelevant] and[if sl.kind = IF or sl.kind = IF_ELSE or
sl.kind = WHILE then sl.call is irrelevant] and[if sl.kind = CALL then sl.test is irrelevant]
27 February 2019 OSU CSE 19
![Page 20: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/20.jpg)
Mathematical Model (2)STATEMENT_LABEL is (
kind: Kind,
test: Condition,
call: IDENTIFIER)
exemplar sl
constraint[if sl.kind = BLOCK then sl.test and sl.call are
irrelevant] and[if sl.kind = IF or sl.kind = IF_ELSE or
sl.kind = WHILE then sl.call is irrelevant] and[if sl.kind = CALL then sl.test is irrelevant]
27 February 2019 OSU CSE 20
Notice the use of the enumsin this model, as well as the
previously defined IDENTIFIER.
![Page 21: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/21.jpg)
Mathematical Model (3)STATEMENT_MODEL is tree of STATEMENT_LABEL
exemplar s
constraint|s| > 0 and[BLOCK can have 0 or more children, but
not another BLOCK as a child] and[IF must have exactly one BLOCK child] and[IF_ELSE must have exactly two BLOCK
children] and[WHILE must have exactly one BLOCK child] and[CALL must have no children (must be a leaf)]
27 February 2019 OSU CSE 21
![Page 22: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/22.jpg)
Mathematical Model (4)type StatementKernel is modeled by
STATEMENT_MODEL
27 February 2019 OSU CSE 22
![Page 23: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/23.jpg)
No-argument Constructor
• Ensures:this = compose((BLOCK, ?, ?), <>)
27 February 2019 OSU CSE 23
![Page 24: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/24.jpg)
No-argument Constructor
• Ensures:this = compose((BLOCK, ?, ?), <>)
27 February 2019 OSU CSE 24
The use of ? here means we do not know—and, frankly, do not care about—the values of the 2nd
and 3rd tuple components (test and call); the model says they are irrelevant if the 1st tuple
component (kind) is BLOCK.
![Page 25: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/25.jpg)
Example
27 February 2019 OSU CSE 25
Code State
Statement s =new Statement1();
![Page 26: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/26.jpg)
Example
27 February 2019 OSU CSE 26
Code State
Statement s =new Statement1();
s = BLOCK
![Page 27: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/27.jpg)
kind
Statement.Kind kind()
• Reports the kind of statement this is.• Ensures:kind = [the statement kind of this]
27 February 2019 OSU CSE 27
![Page 28: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/28.jpg)
Example
27 February 2019 OSU CSE 28
Code State
s =
Kind k = s.kind();
BLOCK
![Page 29: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/29.jpg)
Example
27 February 2019 OSU CSE 29
Code State
s =
Kind k = s.kind();
s = k = BLOCK
BLOCK
BLOCK
![Page 30: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/30.jpg)
addToBlockvoid addToBlock(int pos, Statement s)• Adds the statement s at position pos in this BLOCK
statement.• Updates: this• Clears: s• Requires:
[this is a BLOCK statement] and[s is not a BLOCK statement] and0 <= pos <= [length of this BLOCK]
• Ensures:this = [#this with child #s inserted at
position pos]
27 February 2019 OSU CSE 30
![Page 31: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/31.jpg)
Example
27 February 2019 OSU CSE 31
Code State
s = ns =
s.addToBlock(1,ns);
BLOCK
![Page 32: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/32.jpg)
Example
27 February 2019 OSU CSE 32
Code State
s = ns =
s.addToBlock(1,ns);
s = ns =
BLOCK
BLOCK BLOCK
![Page 33: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/33.jpg)
removeFromBlockStatement removeFromBlock(int pos)
• Removes and returns the statement at position pos of this BLOCK statement.
• Updates: this• Requires:
[this is a BLOCK statement] and0 <= pos < [length of this BLOCK]
• Ensures:this = [#this with child at position pos
removed and returned as result]
27 February 2019 OSU CSE 33
![Page 34: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/34.jpg)
Example
27 February 2019 OSU CSE 34
Code State
s =
Statement ns =s.removeFromBlock(1);
BLOCK
![Page 35: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/35.jpg)
Example
27 February 2019 OSU CSE 35
Code State
s =
Statement ns =s.removeFromBlock(1);
s = ns =
BLOCK
BLOCK
![Page 36: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/36.jpg)
lengthOfBlockint lengthOfBlock()
• Reports the number of statements in thisBLOCK statement.
• Requires:[this is a BLOCK statement]
• Ensures:lengthOfBlock =
[the number of children of this]
27 February 2019 OSU CSE 36
![Page 37: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/37.jpg)
assembleIfvoid assembleIf(Statement.Condition c,
Statement s)• Assembles in this a statement with root label (IF, c , ?)
and only subtree the BLOCK s; the declaration notwithstanding, the dynamic type of s must be the same as the dynamic type of this.
• Replaces: this• Clears: s• Requires:
[s is a BLOCK statement]
• Ensures:this = compose((IF, c, ?), <#s>)
27 February 2019 OSU CSE 37
![Page 38: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/38.jpg)
Example
27 February 2019 OSU CSE 38
Code State
s = ? ns =
s.assembleIf(RANDOM, ns);
![Page 39: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/39.jpg)
Example
27 February 2019 OSU CSE 39
Code State
s = ? ns =
s.assembleIf(RANDOM, ns);
s = ns =IF
RANDOM BLOCK
![Page 40: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/40.jpg)
disassembleIfStatement.Condition disassembleIf(
Statement s)• Disassembles IF statement this into its test Condition, which is
returned as the value of the function, and its only subtree, the BLOCK statement s; the declaration notwithstanding, the dynamictype of s must be the same as the dynamic type of this.
• Replaces: s• Clears: this• Requires:
[this is an IF statement]
• Ensures:#this = compose((IF, disassembleIf, ?), <s>)
27 February 2019 OSU CSE 40
![Page 41: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/41.jpg)
Example
27 February 2019 OSU CSE 41
Code State
s = ns = ?
Condition c = s.disassembleIf(ns);
IFTRUE
![Page 42: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/42.jpg)
Example
27 February 2019 OSU CSE 42
Code State
s = ns = ?
Condition c = s.disassembleIf(ns);
ns =s =
c = TRUE
IFTRUE
BLOCK
![Page 43: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/43.jpg)
Other Methods
• See the Javadoc for Statement for details of the other methods:– assembleIfElse– disassembleIfElse– assembleWhile– disassembleWhile– assembleCall– disassembleCall
27 February 2019 OSU CSE 43
![Page 44: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/44.jpg)
27 February 2019 OSU CSE 44
BLOCK
IFcondition
WHILEcondition
CALLinstruction
s =
IF_ELSEcondition
![Page 45: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/45.jpg)
“Processing” a Statementif (s.kind() == BLOCK) { ... }
else if (s.kind() == IF) { ... }else if (s.kind() == IF_ELSE) { ... }else if (s.kind() == WHILE) { ... }else if (s.kind() == CALL) { ... }
27 February 2019 OSU CSE 45
![Page 46: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/46.jpg)
“Processing” a Statementif (s.kind() == BLOCK) { ... }
else if (s.kind() == IF) { ... }else if (s.kind() == IF_ELSE) { ... }else if (s.kind() == WHILE) { ... }else if (s.kind() == CALL) { ... }
27 February 2019 OSU CSE 46
Technically, there is no reason you need to test this last condition; because what else could it be?
![Page 47: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/47.jpg)
Java’s switch Constructswitch (s.kind()) {
case BLOCK: { ... ; break;}case IF: { ... ; break;}case IF_ELSE: { ... ; break;}case WHILE: { ... ; break;}case CALL: { ... ; break;}default: { ... ; break;}
}
27 February 2019 OSU CSE 47
![Page 48: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/48.jpg)
Java’s switch Constructswitch (s.kind()) {
case BLOCK: { ... ; break;}case IF: { ... ; break;}case IF_ELSE: { ... ; break;}case WHILE: { ... ; break;}case CALL: { ... ; break;}default: { ... ;}
}
27 February 2019 OSU CSE 48
The switch is recommended over a long string of
if-else-if-else-if-...
![Page 49: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/49.jpg)
Java’s switch Constructswitch (s.kind()) {
case BLOCK: { ... ; break;}case IF: { ... ; break;}case IF_ELSE: { ... ; break;}case WHILE: { ... ; break;}case CALL: { ... ; break;}default: { ... ;}
}
27 February 2019 OSU CSE 49
The default case is recommended even when technically it is not needed
(as here).
![Page 50: CSE 2231 - Statementweb.cse.ohio-state.edu/software/2231/web-sw2/extras/slides/22.Statement.pdfThe children of a BLOCK statement may not be BLOCK statements. 27 February 2019 OSU CSE](https://reader031.vdocument.in/reader031/viewer/2022040401/5e78c1e786075b48390a9c13/html5/thumbnails/50.jpg)
Resources
• OSU CSE Components API: Statement– http://cse.osu.edu/software/common/doc/
• Big Java, Section 5.3– http://osu.worldcat.org/title/big-java/oclc/754642794
27 February 2019 OSU CSE 50