-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (50 loc) · 938 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"github.com/drewhayward/glox/lox"
)
func runFile(path string, debug bool) {
file, err := os.Open(path)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
rs := lox.NewRuntimeState()
rs.Debug = debug
err = rs.Run(string(data))
if err != nil {
os.Exit(1)
}
}
func runPrompt(debug bool) {
scanner := bufio.NewScanner(os.Stdin)
rs := lox.NewRuntimeState()
rs.Debug = debug
print("> ")
for scanner.Scan() {
line := scanner.Text()
rs.Run(line)
print("> ")
}
}
func main() {
debug := flag.Bool("v", false, "Debug parsing and lexing")
flag.Parse()
args := flag.Args()
if len(args) == 0 {
fmt.Println("Usage: glox [script]")
runPrompt(*debug)
} else if len(args) == 1 {
runFile(args[0], *debug)
}
}