diff options
author | Julien Dessaux | 2021-12-04 00:07:41 +0100 |
---|---|---|
committer | Julien Dessaux | 2021-12-04 00:07:41 +0100 |
commit | 2dbd22114b4906c6ae60de15f4c30a5ea38441c6 (patch) | |
tree | b8035d711eee7e69b33a0bc59f8cf034a1c53f74 /pkg | |
parent | Renamed headless_interpreter to gofunge98 (diff) | |
download | gofunge98-2dbd22114b4906c6ae60de15f4c30a5ea38441c6.tar.gz gofunge98-2dbd22114b4906c6ae60de15f4c30a5ea38441c6.tar.bz2 gofunge98-2dbd22114b4906c6ae60de15f4c30a5ea38441c6.zip |
Fixed standard input error handling
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/pointer/exec.go | 12 | ||||
-rw-r--r-- | pkg/pointer/intput-output.go | 23 | ||||
-rw-r--r-- | pkg/pointer/pointer.go | 2 |
3 files changed, 25 insertions, 12 deletions
diff --git a/pkg/pointer/exec.go b/pkg/pointer/exec.go index 8bb9dba..55020fa 100644 --- a/pkg/pointer/exec.go +++ b/pkg/pointer/exec.go @@ -177,9 +177,17 @@ func (p *Pointer) eval(c int, f *field.Field) (done bool, returnValue *int) { case ',': p.CharacterOutput(p.ss.head.Pop()) case '&': - p.ss.head.Push(p.DecimalInput()) + if v, err := p.DecimalInput(); err != nil { + p.Reverse() + } else { + p.ss.head.Push(v) + } case '~': - p.ss.head.Push(p.CharacterInput()) + if v, err := p.CharacterInput(); err != nil { + p.Reverse() + } else { + p.ss.head.Push(v) + } case 'y': n := p.ss.head.Pop() now := time.Now() diff --git a/pkg/pointer/intput-output.go b/pkg/pointer/intput-output.go index ac58d44..6a2423a 100644 --- a/pkg/pointer/intput-output.go +++ b/pkg/pointer/intput-output.go @@ -2,37 +2,42 @@ package pointer import ( "fmt" - "log" "os" ) var defaultInputLastChar *int = nil -func DefaultCharacterInput() int { +func DefaultCharacterInput() (int, error) { if defaultInputLastChar != nil { c := *defaultInputLastChar defaultInputLastChar = nil - return c + return c, nil } b := make([]byte, 1) i, err := os.Stdin.Read(b) if err != nil { - log.Fatalf("Error in DefaultCharacterInput { b: %c, i: %d, err: %+v }", b[0], i, err) + return 0, fmt.Errorf("Error in DefaultCharacterInput { b: %c, i: %d, err: %w }", b[0], i, err) } - return int(b[0]) + return int(b[0]), nil } -func DefaultDecimalInput() int { +func DefaultDecimalInput() (int, error) { var v int for { - c := DefaultCharacterInput() + c, err := DefaultCharacterInput() + if err != nil { + return 0, err + } if c >= '0' && c <= '9' { v = c - '0' break } } for { - c := DefaultCharacterInput() + c, err := DefaultCharacterInput() + if err != nil { + break + } if c >= '0' && c <= '9' { v = v*10 + c - '0' } else { @@ -40,7 +45,7 @@ func DefaultDecimalInput() int { break } } - return v + return v, nil } func DefaultCharacterOutput(c int) { diff --git a/pkg/pointer/pointer.go b/pkg/pointer/pointer.go index 4467235..1044cff 100644 --- a/pkg/pointer/pointer.go +++ b/pkg/pointer/pointer.go @@ -9,7 +9,7 @@ import ( var myRand = rand.New(rand.NewSource(time.Now().UnixNano())) -type InputFunction func() int +type InputFunction func() (int, error) type OutputFunction func(v int) type Pointer struct { |