-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.go
37 lines (32 loc) · 1.19 KB
/
instructions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package brainfuck
type InstructionType string
var (
moveLeft InstructionType = "<"
moveRight InstructionType = ">"
increment InstructionType = "+"
decrement InstructionType = "-"
openLoop InstructionType = "["
closeLoop InstructionType = "]"
input InstructionType = ","
output InstructionType = "."
)
// DefaultOperationHandlers are set of commands and corresponding actions that are executed
// by default when brainfuck program starts.
var DefaultOperationHandlers = map[InstructionType]func(*Brainfuck){
moveLeft: (*Brainfuck).MoveLeft,
moveRight: (*Brainfuck).MoveRight,
increment: (*Brainfuck).Increment,
decrement: (*Brainfuck).Decrement,
openLoop: (*Brainfuck).OpenLoop,
closeLoop: (*Brainfuck).CloseLoop,
input: (*Brainfuck).Read,
output: (*Brainfuck).Write,
}
// AddInstruction adds given instruction and operation to be performed into the brainfuck program.
func (bf *Brainfuck) AddInstruction(command InstructionType, handler func(*Brainfuck)) {
bf.CmdOperationMapping[command] = handler
}
// RemoveInstruction deletes given command from the brainfuck program.
func (bf *Brainfuck) RemoveInstruction(command InstructionType) {
delete(bf.CmdOperationMapping, command)
}