-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (50 loc) · 970 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
60
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/evaporei/interpreter/scanner"
e "github.com/evaporei/interpreter/error"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func run(src string) {
s := scanner.New(src)
tokens := s.ScanTokens()
for _, token := range tokens {
fmt.Println(token)
}
}
func runFile(file string) {
bytes, err := os.ReadFile(file)
check(err)
contents := string(bytes)
run(contents)
if e.HadError {
os.Exit(65)
}
}
func runPrompt() {
reader := bufio.NewScanner(os.Stdin)
fmt.Print("> ")
for reader.Scan() {
text := strings.TrimSpace(reader.Text())
run(text)
fmt.Print("> ")
}
fmt.Println()
}
func main() {
if len(os.Args) > 2 {
fmt.Println("Usage: scanner [file]")
os.Exit(64)
} else if len(os.Args) == 2 {
runFile(os.Args[1])
} else {
runPrompt()
}
}