Context
Implementing a switch in BrainFuck is not that complex: case values should be sorted (a bit easier to implement), and then- Set else bit to 1
- Decrease the switch condition by first case value
- If not null, continue with other values
- If null, go to else bit, reset and process
- 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
]<
]
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
Back to previous step
Go to next step
Aucun commentaire:
Enregistrer un commentaire