
KFORTH Interpreter Dialog
This dialog allows you to experiment with KFORTH programs. You can also develop new genetic programs, and verify that they compile.
File Buttons
- Load: First load a KFORTH program (*.kf).
- Compile: Compiles the current program.
- Save: Save's the current program.
NOTE: If you modify something in the Kforth Source pane you will be prompted to save if you try to exit.
Debugger Buttons
- Step: Executes the next KFORTH instruction. Will "step into" any subroutine call. (Function key: F7)
- Step Over: Executes the next instruction, but if the instruction is one of call, if, or ifelse then this button will continue execution until this instruction returns. (Function key: F6)
- Run: Runs the program until it terminates (or a breakpoint reached). The interpreter has special protections against stack overflows and infinite loops. The error, "Step Limit Exceeded", means your program exceeded about 10 million steps. The error, "Stack Size Limit Exceeded", means you have exceeded a stack size of about 10,000 elements. (Function key: F5)
- Reset: Resets the interpeter to the start of the program.
- Breakpoint: Add or remove a breakpiont. You can click on an instruction in the Kforth Instruction pane, and add or remove a breakpoint BEFORE this instruction. A little veritical bar will appear before the instruction, which is how we indicate a breakpoint. (Function key: F9)
- Clear Breakpoints: Clear all breakpoints. (Function key: F8).
- Instructions: Displays a list of KFORTH instructions. The current instruction will be shown in the KFORTH Instruction help dialog. You can also click on an instruction and press this button.
The KFORTH Interpreter allows the special organism instructions like OMOVE, LOOK, EAT, etc... Inside of the interpreter these instructions are just no-op's. But at least you can examine real genetic programs and single step through them (even though they won't actually do anything).
KFORTH MACHINE
This section of the dialog shows the current state of the kforth machine. All 10 registers R0 ... R9 are shown. CB is the current code block being executed. PC is the current program counter. Both CB and PC are 0 based numbers. Code block 0, refers to the code block with the label "main:". PC is relative to the current code block being executed.
The data stack shows the elements currently on the data stack. The top most item represents the "top" of the stack.
The call stack shows the saved locations for cb and pc. This stack allows KFORTH to return from a subroutine call.
KFORTH INTERPRETER
This dialog allows you to execute generic KFORTH program (that is, KFORTH programs that do not use CELL instructions). However "dummy" versions of the CELL instructions are provided, so that genetic programs may be explored using this dialog without getting compiler errors. You can even step thru the code, but since all the CELL instructions "fail" you won't learn much.
KFORTH Instruction Dialog
If you click on [Instructions] this pop-up dialog will appear:
This dialog lets you browse the list of available instructions.
Function Key Summary
- F5 - Run
- F6 - Step over
- F7 - Step
- F8 - Clear all breakpoints
- F9 - Add/remove breakpoint
The mutations tab.

Detailed information about these mutations available here.
Here is how the [MUTATE!] button and other fields are used to implement mini-evolution.
MUTATE_BUTTON(TIMES, POPULATION, TRIALS) { STEP_LIMIT = 10,000 ; fixed constant, prevents infinite loops PROGRAM p1 ; program we start and end with PROGRAM p ; program used during trials PROGRAM best ; best program so far integer best_score ; best score so far integer best_steps ; best number of steps integer avg_score ; average score across # of TRIALs integer avg_nsteps ; average steps taken over # of TRIALs bool first_time ; on first member of population p1 = <KFORTH Disassembly> for(i=0; i < TIMES; i++) { first_time = true for(j=0; j < POPULATION; j++) { p = Mutate(p1) avg_score = 0 avg_nsteps = 0; for(k=0; k < TRIALS; k++) { rr = Run(p, STEP_LIMIT) avg_score += rr.score avg_nsteps += rr.steps } avg_score = avg_score / TRIALS avg_nsteps = avg_nsteps / TRIALS if first_time or avg_score > best.score or (avg_score == best_score and avg_nsteps < best_nsteps) { best = p best_score = avg_score best_nsteps = avg_nsteps first_time = false } } p1 = best } <KFORTH Disassembly> = p1 } |
The Run() function runs the program for up to 10,000 steps and returns whatever is on the top of the data stack. An empty data stack is treated as 0. Returns two things: the score and the number of steps required.
Scoring is based on the 16-bit KFORTH integer returned from the program. The larger the value, the better the score.
The Mutate() function returns a copy of the provided program, except it has been mutated. The algortihm is described here.
The protections tab.

Protections are explained here.
Lookup Symbol: This field allows you to lookup a symbol in the source code pane and place its code block number into the Protected Code Blocks field.
This is handy when you are developing a new program. It is annoying to constantly count code block numbers to just set the protections.