Implement `--debug` flag.
parent
5a8760ae23
commit
3164588546
23
main.go
23
main.go
|
@ -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 {
|
||||||
instruction,
|
log.Printf("exec 0x%[2]x = %[1]q, data: 0x%[3]x = %[3]q (0x%[4]x), reserved data size: %[5]d B",
|
||||||
p.instructionPointer,
|
instruction,
|
||||||
p.Data[p.DataPointer],
|
p.instructionPointer,
|
||||||
p.DataPointer,
|
p.Data[p.DataPointer],
|
||||||
len(p.Data))*/
|
p.DataPointer,
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue