-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (47 loc) · 783 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"github.com/i-am-g2/Gox/gox"
)
func main() {
if len(os.Args) > 2 {
fmt.Println("Usage gox [script]")
os.Exit(64)
} else if len(os.Args) == 2 {
runFile(os.Args[1])
} else {
runPrompt()
}
if gox.HadError {
os.Exit(65)
}
}
func runPrompt() {
for {
fmt.Print("> ")
scanner := bufio.NewReader(os.Stdin)
line, _, _ := scanner.ReadLine()
run(string(line))
gox.HadError = false
}
}
func runFile(path string) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
os.Exit(64)
}
run(string(bytes))
if gox.HadError {
os.Exit(65)
}
}
func run(source string) {
scanner := gox.NewScanner(source)
tokens := scanner.ScanTokens()
for i := 0; i < len(tokens); i++ {
fmt.Println(tokens[i])
}
}