Search

mercredi 8 février 2017

Self-interpreter: switch on instruction code (4)

Context

Implementing a switch in BrainFuck is not that complex: case values should be sorted (a bit easier to implement), and then
  1. Set else bit to 1
  2. Decrease the switch condition by first case value
    1. If not null, continue with other values
    2. If null, go to else bit, reset and process
  3. Finally, go to else bit and process the default case

Initial state

  • Memory: 0, 0, 0, instructions, 0, IP, instruction
  • Cursor: on instruction
  • Input: any

Process

  • Set else bit to 1
  • Execute the switch
    • Decrease current condition with total decrease (since beginning) equals to the current case
    • Value is not null: process next case
    • Value is null: process current case, reset else bit
The global syntax looks like
[
  set else bit
  >+<
  decrease condition
  [
    next case : decrease condition
    ...
  ]>[- reset else bit
    current case implementation
  ]<
]

Note: for test purposes, the current instruction processing will be a simple display on output of the current instruction, and reset. Non-instruction characters will be simply ignored

Code 

  parse current instruction
  [
    >+<--------
    [
      -
      [
        -
        [
          -
          [
            >+[-<------->]+<
            [
              --
              [
                >+++[-<------->]+<-
                [
                  --
                  [
                    not an instruction
                    >-<[-]
                  ]>[-
                    instruction: loop
                    +++++++++[-<++++++++++>]<+++.[-]>
                  ]<
                ]>[-
                  instruction: while
                  +++++++++[-<++++++++++>]<+.[-]>
                ]<
              ]>[-
                instruction: right
                ++++++++[-<++++++++>]<--.[-]>
              ]<
            ]>[-
              instruction: left
              ++++++++[-<++++++++>]<----.[-]>
            ]<
          ]>[-
            instruction: print
            ++++++[-<+++++++>]<++++.[-]>
          ]<
        ]>[-
          instruction: dec
          ++++++[-<+++++++>]<+++.[-]>
        ]<
      ]>[-
        instruction: read
        ++++++[-<+++++++>]<++.[-]>
      ]<
    ]>[-
      instruction: inc
      ++++++[-<+++++++>]<+.[-]>
    ]<

Code (minified)

[>+<--------[-[-[-[>+[-<------->]+<[--[>+++[-<------->]+<-[--[>-<[-]]>[++++++++[-<++++++++++>]<+++.[-]>]<]>[++++++++[-<++++++++++>]<+.[-]>]<]>[+++++++[-<++++++++>]<--.[-]>]<]>[+++++++[-<++++++++>]<----.[-]>]<]>[+++++[-<+++++++>]<++++.[-]>]<]>[+++++[-<+++++++>]<+++.[-]>]<]>[+++++[-<+++++++>]<++.[-]>]<]>[+++++[-<+++++++>]<+.[-]>]<

Final state

  • Memory: 0,instructions, 0, IP, 0
  • Cursor: on last 0
  • Input: unchanged
  • Output: unchanged (except for test purposes)
Note: the root switch will be modified in the future, to take incative_flag into account

Example

Live 'Instruction read / fetch / switch / print' example, that reads code to execute until it reaches separator, then use the fetch loop to get instructions, switch on different instructions, then display instructions using fetch loop. By the way, it's also a nice 'BrainFuck code minifier' - implemented in BrainFuck!

Back to previous step
Go to next step

Aucun commentaire:

Enregistrer un commentaire