-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (49 loc) · 917 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
61
62
package main
import (
"bufio"
"fmt"
"golox-lang/lib/vm"
"golox-lang/lib/vm/interpretresult"
"io/ioutil"
"os"
)
func main() {
vm := vm.New()
vm.InitVM()
if len(os.Args) == 1 {
repl(vm)
} else if len(os.Args) == 2 {
runFile(os.Args[1], vm)
} else {
fmt.Print("Wrong number of arguments\n")
os.Exit(64)
}
vm.FreeVM()
}
func repl(vm *vm.VM) {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
line, err := reader.ReadString(byte(13))
if err != nil {
fmt.Print("\n")
break
}
vm.Interpret(line)
}
}
func runFile(path string, vm *vm.VM) {
fileContent, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("File reading error", err)
return
}
source := string(fileContent)
result := vm.Interpret(source)
if result == interpretresult.INTERPRET_COMPILE_ERROR {
os.Exit(65)
}
if result == interpretresult.INTERPRET_RUNTIME_ERROR {
os.Exit(70)
}
}