Wire up errors and error state.

This commit is contained in:
Mariano Uvalle 2023-05-06 23:13:13 +00:00
parent f682b8468f
commit b00c450600
2 changed files with 15 additions and 2 deletions

View file

@ -19,6 +19,12 @@ func main() {
if errors.Is(err, runner.ErrInvalidScriptFile) { if errors.Is(err, runner.ErrInvalidScriptFile) {
fmt.Println(err) fmt.Println(err)
os.Exit(1) 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: default:
runner.RunPrompt() runner.RunPrompt()

View file

@ -5,9 +5,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
lerrors "github.com/AYM1607/crafting-interpreters/golox/internal/errors"
) )
var ErrInvalidScriptFile = errors.New("could not read script file") var ErrInvalidScriptFile = errors.New("could not read script file")
var ErrScriptNotRunnable = errors.New("could not run script")
func RunPrompt() { func RunPrompt() {
s := bufio.NewScanner(os.Stdin) s := bufio.NewScanner(os.Stdin)
@ -15,7 +18,9 @@ func RunPrompt() {
for s.Scan() { for s.Scan() {
line := s.Text() line := s.Text()
Run(line) 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("> ") fmt.Print("> ")
} }
} }
@ -26,7 +31,9 @@ func RunFile(path string) error {
return errors.Join(ErrInvalidScriptFile, err) return errors.Join(ErrInvalidScriptFile, err)
} }
Run(string(fBytes)) Run(string(fBytes))
// TODO: check hadError and exit with a 65 code if so. if lerrors.HadError {
return ErrScriptNotRunnable
}
return nil return nil
} }