Scanner working for single character tokens.
This commit is contained in:
parent
3d0f3b95d5
commit
d8dd8fd49a
6 changed files with 241 additions and 0 deletions
37
golox/internal/runner/runner.go
Normal file
37
golox/internal/runner/runner.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue