Borer Explorer v1.0.2

The scanner

Part of our code is adding something called a scanner. There are several functions which control the scanner, some of which I'll list here. You might want to think of the scanner as a small creature which has been set loose on top of borer, or within borer, to explore the program and execute code in different parts of the program.

In this example, the scanner is the blue square. You will see them start up in the top left corner. Because the body of our borer is a circular array, and we move the scanners in the + and - direction, some scanners move to the end of the body first. We add about 10 of these scanners, and they slowly age until they die (age 90). It's possible this is your first introduction to bio-programming, however basic, and we've created a little artificial life-form. It's nice to think of it this way, as we progress the program.

Creating the scanner

Here we add a single instance scanb (scanner begin) which carries all the scanners, and we do this once.
codex.push (
  function scanb( borer ) {
    if ( borer.scanners )
      return; // one instance
    borer.scanners = new bexScanners();
  }
);
and then we add scanm (scanner make). Note we only want 5 scanners active for now!
codex.push (
  function scanm( borer ) {
    if ( !borer.scanners )
      return;
    if ( borer.scanners.length() > 10 )
      return;
    var scanner = new bexScanner();
    borer.scanners.add( scanner );
  }
);

Motion

Once we have our scanners being created, we need to add motion to them. Since they sit on top of the borer's body, we can use that index and move them up and down.
codex.push (
  function scani( borer ) {
    if ( !borer.scanners )
      return 0;
    var i = borer.scanners.at();
    var range = borer.body.length;
    if ( i )
      borer.scanners.move( i, range );
  }
);
Note that we keep the scanner within the range of the body. There is never a situation where the scanner moves from the body of the borer, that is, it belongs to the borer. We might allow it to in a future version, but where does it go? That's a good question.

We can also move our scanner to the current position of the white square. The pos executes each unit of the body of the borer once each pass, so we can use it to lift the scanner and reposition it there.
codex.push (
  function scanp( borer ) {
    if ( !borer.scanners )
      return;
    var cursed = borer.scanners.cursed();
    if ( cursed )
      cursed.pos = borer.pos;
  }
);

Passing execution to the scanner

Lastly, we want the currently cursed scanner to be able to execute functions it finds. This only happens when the borer pos hits the scane function.
codex.push (
  function scane( borer ) {
    if ( borer.executing )
      return;
    borer.executing = 1;
    if ( !borer.scanners )
      return;
    var cursed = borer.scanners.cursed();
    var pos = cursed.pos;
    var code = borer.body[ pos ];
    var f = borer.codex[ code ];
    if ( f )
      f( borer );
    borer.executing = 0;
  }
);
Here we mark the borer as executing to prevent chain execution, which causes an error. We could in practice allow some chain execution, but we'll learn to control it in a later tutorial. That's it!