Scanner working for single character tokens.

This commit is contained in:
Mariano Uvalle 2023-05-06 22:34:12 +00:00
parent 3d0f3b95d5
commit d8dd8fd49a
6 changed files with 241 additions and 0 deletions

View file

@ -0,0 +1,37 @@
package runner
import (
"bufio"
"fmt"
"os"
)
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 {
return fmt.Errorf("could not read script file: %w", 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)
}
}