1 / 28

BL Program

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).

skule
Download Presentation

BL Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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)

  3. The Math Model • math subtype PROGRAM is ( name: IDENTIFIER context: CONTEXT body: STATEMENT )exemplar pconstraintroot (p.body).kind = BLOCK

  4. sequence of statements New Instruction INSTRUCTION identifier IS END identifier

  5. Math Model Continued… • What pieces of information do we need to keep track of for a new BL instruction? • name (IDENTIFIER) • body (BLOCK STATEMENT)

  6. Math Model Continued… • math subtype CONTEXT isfinite set of ( name: IDENTIFIER body: STATEMENT )exemplar cconstraint . . . • c is a (partial) function • for each (name, body) pair in c,name is not one of primitivesnor the empty string, and bodyis a BLOCK

  7. Program Component • Type • Program_Kernel is modeled by PROGRAM • Initial Value • IS_INITIAL_PROGRAM (self) self.name = empty_string andself.context = empty_set and IS_INITIAL_STATEMENT (self.body)

  8. Program SteerClear Draw a picture of this BL program’s abstract view: BEGIN WHILE true DO IF next-is-empty THEN skip ELSE IF next-is-wall THEN TurnAround ELSE StepAside END IF END IF END WHILE END SteerClear PROGRAM SteerClear IS INSTRUCTION StepAside IS IF random THEN turnright ELSE turnleft END IF move END StepAside INSTRUCTION TurnAround IS turnright turnright END TurnAround

  9. p.name p.context p.body Program SteerClear (cont’d) p = ( , , )

  10. p.name p.context p.body Program SteerClear (cont’d) p = ( “SteerClear”, , )

  11. p.name p.context p.body Program SteerClear (cont’d) p = ( “SteerClear”, { }, )

  12. p.name p.context p.body Program SteerClear (cont’d) p = ( “SteerClear”, { , }, )

  13. p.name p.context p.body Program SteerClear (cont’d) p = ( “SteerClear”, { ( , ), ( , ) }, )

  14. p.name BLOCK BLOCK IF_ELSE RANDOM CALL “move” CALL “turnright” CALL “turnright” BLOCK BLOCK CALL “turnright” CALL “turnleft” p.context p.body Program SteerClear (cont’d) p = ( “SteerClear”, { ( “TurnAround”, ), ( “StepAside”, ) }, )

  15. p.name BLOCK BLOCK IF_ELSE RANDOM CALL “move” CALL “turnright” CALL “turnright” BLOCK BLOCK BLOCK WHILE TRUE CALL “turnright” CALL “turnleft” BLOCK IF_ELSE NEXT_IS_EMPTY p.context BLOCK BLOCK p.body CALL “skip” IF_ELSE NEXT_IS_WALL BLOCK BLOCK CALL “TurnAround” CALL “StepAside” Program SteerClear (cont’d) p = ( “SteerClear”, { ( “TurnAround”, ), ( “StepAside”, ) }, )

  16. 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 ()

  17. P= ( “Timid”, { ( “TurnAround”, ), (“StepAside”, ) }, BLOCK BLOCK IF RANDOM CALL “move” CALL “turnright” CALL “turnright” BLOCK BLOCK BLOCK CALL “turnright” CALL “turnleft” WHILE TRUE BLOCK IF_ELSE NEXT_IS_EMPTY BLOCK BLOCK CALL “skip” IF_ELSE NEXT_IS_WALL BLOCK BLOCK ) CALL “TurnAround” CALL “StepAside” Swap_Name What effect will the following statements have on p, the program object for program SteerClear? object Text name; name = “Timid”; p.Swap_Name (name); name = “SteerClear”

  18. P= ( “Timid”, { ( “TurnAround”, ), (“StepAside”, ) }, BLOCK BLOCK IF_ELSE RANDOM CALL “move” CALL “turnright” CALL “turnright” BLOCK BLOCK CALL “turnright” CALL “turnleft” BLOCK BLOCK b = WHILE TRUE IF RANDOM BLOCK IF_ELSE NEXT_IS_EMPTY BLOCK BLOCK BLOCK CALL “skip” IF_ELSE NEXT_IS_WALL CALL “move” ) BLOCK BLOCK CALL “TurnAround” CALL “StepAside” Swap_Body What effect will the following statements have on object p from the previous slide? 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);

  19. P= ( “SteerClear”, { ( “TurnAround”, ), (“StepAside”, ) }, BLOCK BLOCK IF_ELSE RANDOM CALL “move” CALL “turnright” CALL “turnright” BLOCK BLOCK CALL “turnright” CALL “turnleft” BLOCK WHILE TRUE BLOCK IF_ELSE NEXT_IS_EMPTY BLOCK BLOCK CALL “skip” IF_ELSE NEXT_IS_WALL BLOCK BLOCK ) CALL “TurnAround” CALL “StepAside” Context Operations What result is produced by these statements if p has its original value? p.Size_Of_Context () ? p.Is_In_Context (“StepAside”) ?

  20. P= ( “SteerClear”, { (“StepAside”, ) }, What result is produced by these statements if p has the value shown on the previous slide? BLOCK IF_ELSE RANDOM CALL “move” BLOCK BLOCK CALL “turnright” CALL “turnleft” object Text n; object Statement b; p.Remove_From_Context (“TurnAround”, n, b) ? BLOCK WHILE TRUE BLOCK IF_ELSE NEXT_IS_EMPTY BLOCK BLOCK n=“TurnAround” b = BLOCK CALL “skip” IF_ELSE NEXT_IS_WALL CALL “turnright” CALL “turnright” BLOCK BLOCK ) CALL “TurnAround” CALL “StepAside” Context Operations (cont’d)

  21. P= ( “SteerClear”, { ( “TurnBack”, ), (“StepAside”, ) }, BLOCK BLOCK IF RANDOM CALL “move” CALL “turnright” CALL “turnright” BLOCK BLOCK CALL “turnright” CALL “turnleft” BLOCK WHILE TRUE BLOCK IF_ELSE NEXT_IS_EMPTY BLOCK BLOCK n=“” b = BLOCK CALL “skip” IF_ELSE NEXT_IS_WALL BLOCK BLOCK ) CALL “TurnAround” CALL “StepAside” Context Operations (cont’d) What result is produced by these statements if p and b have the values shown on the previous slide? n = “TurnBack”; p.Add_To_Context (n, b) ?

  22. An Operation on Program What would it mean to demobilize a program?

  23. PROGRAM identifier IS BEGIN END identifier INSTRUCTION identifier IS END identifier . . . Statements in BL Program

  24. 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) !*/

  25. 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))

  26. Implementation procedure_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);

  27. 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; }

  28. Implementation procedure_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; }

More Related