From b00c4506001ca1ea481d6408f1c1e2e096217113 Mon Sep 17 00:00:00 2001 From: AYM1607 Date: Sat, 6 May 2023 23:13:13 +0000 Subject: [PATCH] Wire up errors and error state. --- golox/cmd/golox/main.go | 6 ++++++ golox/internal/runner/runner.go | 11 +++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/golox/cmd/golox/main.go b/golox/cmd/golox/main.go index 0014a11..246275f 100644 --- a/golox/cmd/golox/main.go +++ b/golox/cmd/golox/main.go @@ -19,6 +19,12 @@ func main() { if errors.Is(err, runner.ErrInvalidScriptFile) { fmt.Println(err) os.Exit(1) + } else if errors.Is(err, runner.ErrScriptNotRunnable) { + fmt.Println(err) + os.Exit(65) + } else if err != nil { + fmt.Printf("Unexpected error: %v\n", err) + os.Exit(1) } default: runner.RunPrompt() diff --git a/golox/internal/runner/runner.go b/golox/internal/runner/runner.go index 9ee498a..54ccd2a 100644 --- a/golox/internal/runner/runner.go +++ b/golox/internal/runner/runner.go @@ -5,9 +5,12 @@ import ( "errors" "fmt" "os" + + lerrors "github.com/AYM1607/crafting-interpreters/golox/internal/errors" ) var ErrInvalidScriptFile = errors.New("could not read script file") +var ErrScriptNotRunnable = errors.New("could not run script") func RunPrompt() { s := bufio.NewScanner(os.Stdin) @@ -15,7 +18,9 @@ func RunPrompt() { for s.Scan() { line := s.Text() Run(line) - // TODO: resed hadError wherever it is set. + // TODO: Understand the implications of this. The book implies that it's + // to allow the users to keep issuing commands even if they make a mistake. + lerrors.HadError = false fmt.Print("> ") } } @@ -26,7 +31,9 @@ func RunFile(path string) error { return errors.Join(ErrInvalidScriptFile, err) } Run(string(fBytes)) - // TODO: check hadError and exit with a 65 code if so. + if lerrors.HadError { + return ErrScriptNotRunnable + } return nil }