crafting-interpreters/golox/internal/runner/runner.go

41 lines
671 B
Go
Raw Normal View History

package runner
import (
"bufio"
2023-05-06 22:53:19 +00:00
"errors"
"fmt"
"os"
)
2023-05-06 22:53:19 +00:00
var ErrInvalidScriptFile = errors.New("could not read script file")
func RunPrompt() {
s := bufio.NewScanner(os.Stdin)
fmt.Print("> ")
for s.Scan() {
line := s.Text()
Run(line)
// TODO: resed hadError wherever it is set.
fmt.Print("> ")
}
}
func RunFile(path string) error {
fBytes, err := os.ReadFile(path)
if err != nil {
2023-05-06 22:53:19 +00:00
return errors.Join(ErrInvalidScriptFile, err)
}
Run(string(fBytes))
// TODO: check hadError and exit with a 65 code if so.
return nil
}
func Run(source string) {
s := NewScanner(source)
tokens := s.ScanTokens()
for _, t := range tokens {
fmt.Println(t)
}
}