-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (63 loc) · 1.36 KB
/
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
63
64
65
66
67
68
69
70
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/AnshVM/golox/Environment"
"github.com/AnshVM/golox/Error"
"github.com/AnshVM/golox/Interpreter"
"github.com/AnshVM/golox/Parser"
"github.com/AnshVM/golox/Resolver"
"github.com/AnshVM/golox/Scanner"
)
func run(i *Interpreter.Interpreter, source string) {
scanner := Scanner.NewScanner(source)
tokens := scanner.ScanTokens()
parser := Parser.NewParser(tokens)
stmts := parser.Parse()
resolver := Resolver.NewResolver(i)
if Error.HadError {
return
}
resolver.Resolve(stmts)
if Error.HadError {
return
}
i.Interpret(stmts)
}
func runFile(i *Interpreter.Interpreter, path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return errors.New(Error.CANNOT_READ_FILE)
}
run(i, string(data))
if Error.HadError {
os.Exit(65)
}
if Error.HadRuntimeError {
os.Exit(70)
}
return nil
}
func runPrompt(i *Interpreter.Interpreter) {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
line, _ := reader.ReadString('\n')
line = line[:len(line)-1]
run(i, line)
Error.HadError = false
Error.HadRuntimeError = false
}
}
func main() {
globals := Environment.Environment{Values: make(map[string]any)}
interpreter := Interpreter.NewInterpreter(&globals)
if len(os.Args) == 2 {
runFile(interpreter, os.Args[1])
} else {
runPrompt(interpreter)
}
}