-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 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
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
)
func main() {
if len(os.Args) == 1 {
repl()
} else if len(os.Args) == 2 {
runFile(os.Args[1])
} else {
fmt.Errorf("Usage: clox [path]\n")
os.Exit(64)
}
}
func repl() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("> ")
line, err := reader.ReadString('\n')
if err == io.EOF {
fmt.Println()
os.Exit(0)
}
if err != nil {
fmt.Printf("could not read from stdin: %v", err)
os.Exit(1)
}
fmt.Println()
interpret(line)
}
}
func runFile(path string) {
source, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("could not read file %s: %v", path, err)
os.Exit(1)
}
result := interpret(string(source))
if result == INTERPRET_COMPILE_ERROR {
os.Exit(65)
}
if result == INTERPRET_RUNTIME_ERROR {
os.Exit(70)
}
}