BL Program

28
BL Program PROGRAM identifier IS BEGIN END identifier sequence of statements sequence of new instructions

description

sequence of new instructions. sequence of statements. BL Program. PROGRAM identifier IS BEGIN END identifier. Math Model for Program. What pieces of information do we need to keep track of for a BL program? name (IDENTIFIER) new instructions (CONTEXT) body (BLOCK STATEMENT). - PowerPoint PPT Presentation

Transcript of BL Program

Page 1: BL Program

BL Program

PROGRAM identifier IS

BEGIN

END identifier

sequence of statements

sequence of new instructions

Page 2: BL Program

Math Model for Program

What pieces of information do we need to keep track of for a BL program? name (IDENTIFIER) new instructions (CONTEXT) body (BLOCK STATEMENT)

Page 3: BL Program

The Math Model

math subtype PROGRAM is ( name: IDENTIFIER context: CONTEXT body: STATEMENT ) exemplar p constraint root (p.body).kind = BLOCK

Page 4: BL Program

New Instruction

INSTRUCTION identifier IS

END identifier

sequence of statements

Page 5: BL Program

Math Model Continued…

What pieces of information do we need to keep track of for a new BL instruction? name (IDENTIFIER) body (BLOCK STATEMENT)

Page 6: BL Program

Math Model Continued…

math subtype CONTEXT is finite set of ( name: IDENTIFIER body: STATEMENT ) exemplar c constraint . . .

1. c is a (partial) function2. for each (name, body) pair in c,

name is not one of primitivesnor the empty string, and bodyis a BLOCK

Page 7: BL Program

Program Component

Type Program_Kernel is modeled by

PROGRAM Initial Value

IS_INITIAL_PROGRAM (self) self.name = empty_string and self.context = empty_set and IS_INITIAL_STATEMENT (self.body)

Page 8: BL Program

Program SteerClear

Draw a picture of this BL program’s abstract view:

PROGRAM SteerClear IS INSTRUCTION StepAside IS IF random THEN turnright ELSE turnleft END IF move END StepAside INSTRUCTION TurnAround IS turnright turnright END TurnAround

BEGIN WHILE true DO IF next-is-empty THEN skip ELSE IF next-is-wall THEN TurnAround ELSE StepAside END IF END IF END WHILEEND SteerClear

Page 9: BL Program

Program SteerClear (cont’d)

p = ( , ,

)

p.name

p.context

p.body

Page 10: BL Program

Program SteerClear (cont’d)

p = ( “SteerClear”, ,

)

p.name

p.context

p.body

Page 11: BL Program

Program SteerClear (cont’d)

p = ( “SteerClear”, { },

)

p.name

p.context

p.body

Page 12: BL Program

Program SteerClear (cont’d)

p = ( “SteerClear”, { , },

)

p.name

p.body

p.context

Page 13: BL Program

Program SteerClear (cont’d)

p = ( “SteerClear”, { ( , ), ( , ) },

)

p.name

p.body

p.context

Page 14: BL Program

Program SteerClear (cont’d)

p = ( “SteerClear”, { ( “TurnAround”, ), ( “StepAside”, ) },

)

BLOCK

IF_ELSERANDOM

BLOCK BLOCK

CALL“turnright”

CALL“turnleft”

CALL“move”

BLOCK

CALL“turnright”

CALL“turnright”

p.name

p.body

p.context

Page 15: BL Program

Program SteerClear (cont’d)

p = ( “SteerClear”, { ( “TurnAround”, ), ( “StepAside”, ) },

BLOCK

IF_ELSERANDOM

BLOCK BLOCK

CALL“turnright”

CALL“turnleft”

CALL“move”

BLOCK

CALL“turnright”

CALL“turnright”

)

p.name

p.context

p.body

BLOCK

WHILE TRUE

BLOCK

IF_ELSENEXT_IS_EMPTY

BLOCK BLOCK

CALL “skip”

IF_ELSENEXT_IS_WALL

CALL “TurnAround”

CALL “StepAside”

BLOCKBLOCK

Page 16: BL Program

Program Continued… Operations

p.Swap_Name (name) p.Swap_Body (statement) p.Add_To_Context (name, statement) p.Remove_From_Context (name, name_copy,

statement) p.Remove_Any_From_Context (name,

statement) p.Is_In_Context (name) p.Size_Of_Context ()

Page 17: BL Program

Swap_Name

BLOCK

WHILE TRUE

BLOCK

IF_ELSENEXT_IS_EMPTY

BLOCK BLOCK

CALL “skip”

IF_ELSENEXT_IS_WALL

CALL “TurnAround”

CALL “StepAside”

BLOCKBLOCK

P=( “Timid”, { ( “TurnAround”, ), (“StepAside”, ) },

BLOCK

IFRANDOM

BLOCK BLOCK

CALL“turnright”

CALL“turnleft”

CALL“move”

BLOCK

CALL“turnright”

CALL“turnright”

)

What effect willthe followingstatements have on p,the program object forprogram SteerClear?

object Text name;name = “Timid”;p.Swap_Name (name);

name = “SteerClear”

Page 18: BL Program

Swap_BodyWhat effect willthe following statements have on object p from the previousslide?

object Statement b, i, c;object Text m = “move”;object Integer test = RANDOM;c.Compose_Call (m);b.Add_To_Block (0, c);i.Compose_If (test, b);b.Add_to_Block (0, i);p.Swap_Body (b);

P=( “Timid”, { ( “TurnAround”, ), (“StepAside”, ) },

BLOCK

IF_ELSERANDOM

BLOCK BLOCK

CALL“turnright”

CALL“turnleft”

CALL“move”

BLOCK

CALL“turnright”

CALL“turnright”

)

BLOCK

IFRANDOM

BLOCK

CALL “move”

b =BLOCK

WHILE TRUE

BLOCK

IF_ELSENEXT_IS_EMPTY

BLOCK BLOCK

CALL “skip”

IF_ELSENEXT_IS_WALL

CALL

“TurnAround”

CALL

“StepAside”

BLOCK BLOCK

Page 19: BL Program

Context Operations

p.Size_Of_Context () ?

p.Is_In_Context (“StepAside”) ?

What result is produced by these statements ifp has its original value?

)

P=( “SteerClear”, { ( “TurnAround”, ), (“StepAside”, ) },

BLOCK

IF_ELSERANDOM

BLOCK BLOCK

CALL“turnright”

CALL“turnleft”

CALL“move”

BLOCK

CALL“turnright”

CALL“turnright”

BLOCK

WHILE TRUE

BLOCK

IF_ELSENEXT_IS_EMPTY

BLOCK BLOCK

CALL “skip”

IF_ELSENEXT_IS_WALL

CALL “TurnAround”

CALL “StepAside”

BLOCKBLOCK

Page 20: BL Program

Context Operations (cont’d)

What result is produced by these statements ifp has the value shown on the previous slide?

object Text n;object Statement b;p.Remove_From_Context (“TurnAround”, n, b) ?

BLOCK

CALL“turnright”

CALL“turnright”

n=“TurnAround” b =

)

P=( “SteerClear”, { (“StepAside”, ) },BLOCK

IF_ELSERANDOM

BLOCK BLOCK

CALL“turnright”

CALL“turnleft”

CALL“move”

BLOCK

WHILE TRUE

BLOCK

IF_ELSENEXT_IS_EMPTY

BLOCK BLOCK

CALL “skip”

IF_ELSENEXT_IS_WALL

CALL “TurnAround”

CALL “StepAside”

BLOCKBLOCK

Page 21: BL Program

Context Operations (cont’d)

What result is producedby these statements if p and b have the values shown on the previous slide?

n = “TurnBack”;p.Add_To_Context (n, b) ?

BLOCK

n=“” b =

)

P= ( “SteerClear”, { ( “TurnBack”, ), (“StepAside”, ) },

BLOCK

IFRANDOM

BLOCK BLOCK

CALL“turnright”

CALL“turnleft”

CALL“move”

BLOCK

CALL“turnright”

CALL“turnright”

BLOCK

WHILE TRUE

BLOCK

IF_ELSENEXT_IS_EMPTY

BLOCK BLOCK

CALL “skip”

IF_ELSENEXT_IS_WALL

CALL “TurnAround”

CALL “StepAside”

BLOCKBLOCK

Page 22: BL Program

An Operation on Program

What would it mean to demobilize a program?

Page 23: BL Program

Statements in BL Program

INSTRUCTION identifier IS

END identifier

.

.

.

PROGRAM identifier IS

BEGIN

END identifier

Page 24: BL Program

State the Problem

global_procedure Demobilize ( alters Program& p ); /*! ensures p.name = #p.name and p.context = CONTEXT_DEMOBILIZE (#p.context) and

p.body = DEMOBILIZE (#p.body) !*/

Page 25: BL Program

State the Problem Continued…

math definition CONTEXT_DEMOBILIZE ( c: CONTEXT ): CONTEXT satisfies if c = empty_set then CONTEXT_DEMOBILIZE (c) = c else there exists n: IDENTIFIER, s: STATEMENT, rest_of_context: CONTEXT (c = {(n, s)} union rest_of_context and CONTEXT_DEMOBILIZE(c) = {(n, DEMOBILIZE(s))} union CONTEXT_DEMOBILIZE (rest_of_context))

Page 26: BL Program

Implementationprocedure_body Demobilize ( alters Program& p ){ object Program tmp; object Text name; object Statement body; // demobilize program body p.Swap_Body (body); Demobilize (body); tmp.Swap_Body (body);

Page 27: BL Program

Implementation Continued…

}

// demobilize statements in program context while (p.Size_Of_Context () > 0) { p.Remove_Any_From_Context (name, body); Demobilize (body); tmp.Add_To_Context (name, body); } // restore value of p (to be preserved) p.Swap_Name (name); tmp.Swap_Name (name); p &= tmp;

Page 28: BL Program

Implementationprocedure_body Demobilize ( alters Program& p ){ object Program tmp; object Text name; object Statement body; p.Swap_Body (body); Demobilize (body); tmp.Swap_Body (body);

while (p.Size_Of_Context () > 0) { p.Remove_Any_From_Context (name,

body); Demobilize (body); tmp.Add_To_Context (name, body); }

p.Swap_Name (name); tmp.Swap_Name (name); p &= tmp;}