Statement 27 February 2019 OSU CSE 1
BL Compiler Structure Code Tokenizer Parser Generator string of string of abstract string of characters tokens program integers (source code) (“words”) (object code) A BL program consists of some statements … 27 February 2019 OSU CSE 2
Statement • The Statement component family allows you to manipulate values that are ASTs for BL statements • The mathematical model of a Statement value is a tree of STATEMENT_LABEL with some constraints 27 February 2019 OSU CSE 3
BLOCK s = IF condition CALL IF_ELSE instruction WHILE condition condition 27 February 2019 OSU CSE 4
BLOCK s = IF condition A Statement variable’s value is a CALL tree of STATEMENT_LABEL IF_ELSE instruction WHILE condition condition with some constraints , so we use rather than to illustrate its recursive structure. 27 February 2019 OSU CSE 5
The kind of statement BLOCK (based on the root) determines how many and which kinds of s = children it may have. IF condition CALL IF_ELSE instruction WHILE condition condition 27 February 2019 OSU CSE 6
The children of a BLOCK BLOCK statement may not be BLOCK statements. s = IF condition CALL IF_ELSE instruction WHILE condition condition 27 February 2019 OSU CSE 7
The child of an IF or BLOCK WHILE statement must be a BLOCK statement. s = IF condition CALL IF_ELSE instruction WHILE condition condition 27 February 2019 OSU CSE 8
The two children of an BLOCK IF_ELSE statement must be BLOCK statements. s = IF condition CALL IF_ELSE instruction WHILE condition condition 27 February 2019 OSU CSE 9
BLOCK A CALL statement has no children. s = IF condition CALL IF_ELSE instruction WHILE condition condition 27 February 2019 OSU CSE 10
Interfaces and Classes Standard extends Statement- Kernel extends Statement implements Statement1 27 February 2019 OSU CSE 11
Interfaces and Classes Standard extends Statement- Kernel StatementKernel has contracts for 12 extends methods that involve “assembling” and Statement “disassembling” the various BL statements implements (similar to Tree methods). Statement1 27 February 2019 OSU CSE 12
Interfaces and Classes Statement has these additional Standard methods (not discussed here): extends parse Statement- parseBlock Kernel prettyPrint extends Statement implements Statement1 27 February 2019 OSU CSE 13
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
Example: The Kind Enum • The interface StatementKernel contains this code: /** * The kinds of statements. */ enum Kind { BLOCK, IF, IF_ELSE, WHILE, CALL } 27 February 2019 OSU CSE 15
Example: The Kind Enum This is quite different from: final int BLOCK = 1; final int IF = 2; • The interface StatementKernel ... contains this code: You may not do arithmetic with the enum constants, where you could /** with the int constants above. * The kinds of statements. */ enum Kind { BLOCK, IF, IF_ELSE, WHILE, CALL } 27 February 2019 OSU CSE 16
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
Mathematical Model (1) IDENTIFIER is string of character exemplar 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
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
Mathematical Model (2) Notice the use of the enum s in this model, as well as the STATEMENT_LABEL is ( previously defined kind: Kind, IDENTIFIER . 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
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
Mathematical Model (4) type StatementKernel is modeled by STATEMENT_MODEL 27 February 2019 OSU CSE 22
No-argument Constructor • Ensures: this = compose ((BLOCK, ?, ?), <>) 27 February 2019 OSU CSE 23
No-argument Constructor • Ensures: this = compose ((BLOCK, ?, ?), <>) The use of ? here means we do not know—and, frankly, do not care about—the values of the 2 nd and 3 rd tuple components ( test and call ); the model says they are irrelevant if the 1 st tuple component ( kind ) is BLOCK . 27 February 2019 OSU CSE 24
Example Code State Statement s = new Statement1(); 27 February 2019 OSU CSE 25
Example Code State Statement s = new Statement1(); BLOCK s = 27 February 2019 OSU CSE 26
kind Statement.Kind kind() • Reports the kind of statement this is. • Ensures: kind = [the statement kind of this ] 27 February 2019 OSU CSE 27
Example Code State BLOCK s = Kind k = s.kind(); 27 February 2019 OSU CSE 28
Example Code State BLOCK s = Kind k = s.kind(); BLOCK s = k = BLOCK 27 February 2019 OSU CSE 29
addToBlock void 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] and 0 <= pos <= [length of this BLOCK] • Ensures: this = [# this with child #s inserted at position pos] 27 February 2019 OSU CSE 30
Example Code State BLOCK s = ns = s.addToBlock(1, ns); 27 February 2019 OSU CSE 31
Example Code State BLOCK s = ns = s.addToBlock(1, ns); BLOCK BLOCK s = ns = 27 February 2019 OSU CSE 32
removeFromBlock Statement removeFromBlock( int pos) • Removes and returns the statement at position pos of this BLOCK statement. • Updates: this • Requires: [ this is a BLOCK statement] and 0 <= pos < [length of this BLOCK] • Ensures: this = [# this with child at position pos removed and returned as result] 27 February 2019 OSU CSE 33
Example Code State BLOCK s = Statement ns = s.removeFromBlock(1); 27 February 2019 OSU CSE 34
Example Code State BLOCK s = Statement ns = s.removeFromBlock(1); BLOCK s = ns = 27 February 2019 OSU CSE 35
lengthOfBlock int lengthOfBlock() • Reports the number of statements in this BLOCK statement. • Requires: [ this is a BLOCK statement] • Ensures: lengthOfBlock = [the number of children of this ] 27 February 2019 OSU CSE 36
assembleIf void 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
Example Code State s = ? ns = s.assembleIf( RANDOM, ns); 27 February 2019 OSU CSE 38
Example Code State s = ? ns = s.assembleIf( RANDOM, ns); IF BLOCK RANDOM s = ns = 27 February 2019 OSU CSE 39
disassembleIf Statement.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 dynamic type 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
Example Code State IF TRUE s = ns = ? Condition c = s.disassembleIf(ns); 27 February 2019 OSU CSE 41
Example Code State IF TRUE s = ns = ? Condition c = s.disassembleIf(ns); ns = BLOCK s = c = TRUE 27 February 2019 OSU CSE 42
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
Recommend
More recommend