-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (49 loc) · 893 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"
"fmt"
"io"
"io/ioutil"
"os"
)
var hadError = false
func run(source []byte, filename string) {
tokens := NewScanner(source, filename).ScanAll()
if tokens[0].kind == EOF {
return
}
parser := &Parser{current: 0, filename: filename, source: source, tokens: tokens}
expr := parser.Parse()
if hadError {
return
}
var p AstPrinter
p.Print(expr)
}
func runFile(path string) {
bytes, _ := ioutil.ReadFile(path)
run(bytes, path)
}
func repl() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print(ANSI_BOLD + "glox> " + ANSI_RESET)
bytes, err := reader.ReadBytes('\n')
if err == io.EOF {
fmt.Println(ANSI_RESET)
break
}
run(bytes, "?")
hadError = false
}
}
func main() {
args := os.Args[1:]
if len(args) == 0 {
repl()
} else if len(args) == 1 {
runFile(args[0])
} else {
fmt.Println("Usage: glox [script]")
}
}