Search

mercredi 8 février 2017

Self-interpreter: inactive and direction flags (8)

Context

As a reminder, here is the final memory map
0 0 0 {instrs} 0 inst_ptr 0 0 0 inactive_flag direction_flag memory_ptr  0 0 0 0 0 {mem}
Let's now see how to handle inactive_flag and direction_flag
  • Inactive_flag: when not null, execution of instructions should be suspended, only '[' and ']' will taken into account.
  • While will increase the inactive_flag and if null update direction_flag.
  • Loop will decrease the inactive_flag

Initial state

  • Memory: see above
  • Cursor: on instruction, next to inst_ptr
  • Input: any

Process

  • Set else bit
  • If inactive flag is not null
    • Reset else bit  
    • Check if instruction is ]
      • Decrease inactive flag
    • If not, check if it is [
      • Increase inactive flag
      • If inactive flag is null, reset direction flag
    • If not, ignore instruction
  • If not null: process instruction normally 
Update instruction pointer: instead of  moving to next instruction, we need to check if direction_flag is not null (go back) or not (go forward). An easier way to do that is to go back to steps back if direction flag is not null, and then go one step forward in all cases.

Code snippet for inactive case

parse current instruction
>+<[>->+>
[
  check if instruction is while (increase flag) or loop (decrease flag)
  <<++++++++[-<------->]+<
  [
    --
    [>-<
      clear instruction read
      [-]
    ]>[-
      instruction: loop
      decrease inactive_flag
      >>-<<
    ]<
  ]>[-
    instruction: while
    increase inactive_flag
    >>+
  
    if inactive_flag = 0 then reset direction_flag
    [<-]<[>>[-]<<-<]>+
    <
  ]<
  set instruction read to 1 to consider it as a non instruction
  +>>-
]
<[-<]
+<
[
 
and continue

Code snippet for instruction pointer update

increase or decrease instruction counter based on direction_flag
>>>>[-<<+<<<-->>>>>]<<[->>+<<]<<<++>

Final state

  • Memory: see above
  • Cursor: on instruction, next to inst_ptr
  • Input: unchanged
  • Output: unchanged

Aucun commentaire:

Enregistrer un commentaire