“Hello World!” This was good, but it wasn’t enough. Introducing BrainPlus In addition to fast cell initializers, a means was required to construct functions, call them, and return values. A command would also be needed to indicate where a main program ends and where higher-memory functions begin.
Software called DeepCoder has solved simple programming challenges by piecing together bits of borrowed code. One advantage of letting an AI loose in this way is that it can search more.
![Programming Programming](https://image.slidesharecdn.com/anoverviewofself-programmingartificialintelligence-170119205600/95/self-programming-artificial-intelligence-1-638.jpg?cb=1485435945)
With the addition of these commands to BF and the change in program flow, I realized this language is no longer BF. Let’s call it BrainPlus (maybe a more politically correct name?). In addition, this new extension allows for future support of additional features, such as calling interrupts for graphics, networking, and disk functionality. BrainPlus includes a combination of commands from BF Extended Type I and Type III, in addition to the classic available in BF. The new commands include. And restore state. (Extended Type I, BrainPlus) Details Behind Function Calling The main program is terminated at the @ command.
This marks the end of the main program and the beginning of the higher-level address space, dedicated to functions. Functions are also terminated at the @ command, which in the case of functions, serves as a “return” statement.
A function is defined by any code residing after the main program’s terminating @ command. A function is terminated with a final @ command. A!@$b@.@ In this example, two functions are defined after the first @ command.
When running this program, the command ‘a’ calls the first function, which increments its first memory cell to 3 and stores the result. So far, this is the same as the first example. Next, the function calls another function ‘b’. Function ‘b’ increments it’s own first memory cell to 5 and outputs the value to the console. Execution is then returned back to function ‘a’, which has no other commands to execute. Finally, execution is returned back to the main program, where the storage value of 3 is retrieved and printed to the console. Functions are called with the command a through z, where the letter indicates the order of the function in the higher-level address space.
For example, ‘a’ will move program execution to the beginning of the first defined function. ‘b’ will move program execution to the second defined function. A total of 26 functions may be defined in a program (although, this can be extended by using upper-ASCII character codes). If no function exists for the executed function-call command (a-z), an error is thrown. Each function receives its own dedicated memory space of 256 bytes. The main program uses the range 0-255.
The first function uses the range 256-511. The next function uses the range 512-767, etc. This range is arbitrary (and could certainly be increased to 1000 or more cells), but is largely dictated by execution speed. Smaller memory ranges are faster to initialize. When a function is called, its memory space is initialized to 0. The parent program state is pushed onto a stack.
This includes the current instruction pointer (which points to the currently executing cell in the main program), the current memory pointer (which points to the active memory cell in the main program), and all other relevant state information for the parent. Note, a parent can be the main program or another function, which is why a stack is used to push and pop state information.
When an input ‘,’ command is executed from within a function, the function’s current memory cell gets a copy of the value of the parent memory at the starting pointer. The next input command that is executed within the function will copy the value of the next parent memory cell, advancing along the parent memory accordingly. This allows passing multiple values as input to a function.
When the ‘@’ command is executed, the function terminates. It is common to include a storage command ‘$’ at the end of a function so that the current cell value in the function’s memory is saved, and thus, accessible in the parent’s code. This allows passing a result value back to the parent. State information is popped off the function stack and restored to the parent.
The instruction pointer is set back to the parent’s calling command, and program execution continues. The main program can call a function. Functions can call functions. BrainPlus Example.+g22.-,-,$!,!,-g,+@7$66.!.!+.@7-.!!6-$$+$.@7+$6.!+.@[email protected][email protected]$6+.!-.@[email protected]@ “99 bottles of beer on the wall” “98 bottles of beer on the wall” “97 bottles of beer on the wall” “96 bottles of beer on the wall” “95 bottles of beer on the wall” “” “0 bottles of beer on the wall” Multiplication This one was tricky for me to do without BrainPlus. Actually, I couldn’t do it. But, once I had access to functions (specifically, a function for addition that I wrote myself), multiplying digits became easier. Here is a I wrote that multiplies a number by 2.
I wrote this in about 2 hours. You can see the function for addition in there.