Implement `--debug` flag.

master
Icedream 2017-09-07 12:04:26 +02:00
parent 5a8760ae23
commit 3164588546
Signed by: icedream
GPG Key ID: 1573F6D8EFE4D0CF
1 changed files with 17 additions and 6 deletions

15
main.go
View File

@ -26,6 +26,8 @@ var (
app = kingpin.New("gobfy", "Yet another interpreter for Brainfuck programs.") app = kingpin.New("gobfy", "Yet another interpreter for Brainfuck programs.")
argInput = app.Arg("input", "The source file of the program to execute.").Required().ExistingFile() argInput = app.Arg("input", "The source file of the program to execute.").Required().ExistingFile()
flagDebug = app.Flag("debug", "Indicates whether to display information about the current state before executing each instruction.").Bool()
) )
const ( const (
@ -42,6 +44,8 @@ type Processor struct {
Data []byte Data []byte
DataPointer int DataPointer int
Debug bool
stdin *bufio.Reader stdin *bufio.Reader
instructionPointer int instructionPointer int
@ -82,12 +86,14 @@ func (p *Processor) Execute() {
for p.instructionPointer < len(p.instructionBuffer) { for p.instructionPointer < len(p.instructionBuffer) {
instruction := p.instructionBuffer[p.instructionPointer] instruction := p.instructionBuffer[p.instructionPointer]
/*log.Printf("exec 0x%[2]x = %[1]q, data: 0x%[3]x = %[3]q (0x%[4]x), reserved data size: %[5]d B", if p.Debug {
log.Printf("exec 0x%[2]x = %[1]q, data: 0x%[3]x = %[3]q (0x%[4]x), reserved data size: %[5]d B",
instruction, instruction,
p.instructionPointer, p.instructionPointer,
p.Data[p.DataPointer], p.Data[p.DataPointer],
p.DataPointer, p.DataPointer,
len(p.Data))*/ len(p.Data))
}
switch instruction { switch instruction {
case InstMoveRight: case InstMoveRight:
@ -219,6 +225,11 @@ func main() {
} }
p := NewProcessor() p := NewProcessor()
if flagDebug != nil {
p.Debug = *flagDebug
}
p.Load(input) p.Load(input)
p.Execute() p.Execute()
} }