-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (138 loc) · 3.19 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bufio"
goerrors "errors"
"fmt"
"io"
"os"
"strings"
"github.com/Drumstickz64/golox/ast"
"github.com/Drumstickz64/golox/errors"
"github.com/Drumstickz64/golox/interpreting"
"github.com/Drumstickz64/golox/parsing"
"github.com/Drumstickz64/golox/resolving"
"github.com/Drumstickz64/golox/scanning"
)
func main() {
if len(os.Args) == 1 {
RunPrompt()
} else if len(os.Args) == 2 {
RunFile(os.Args[1])
} else if len(os.Args) == 3 {
switch os.Args[2] {
case "scan":
TestScanning(os.Args[1])
case "parse":
TestParsing(os.Args[1])
case "run":
RunFile(os.Args[1])
default:
errors.LogUsageMessage()
}
} else {
errors.LogUsageMessage()
}
}
func RunFile(path string) {
source := LoadSource(path)
interpreter := interpreting.NewInterpreter()
resolver := resolving.NewResolver(interpreter)
statements, errs := Build(source)
for _, err := range errs {
fmt.Fprintln(os.Stderr, err)
}
if len(errs) > 0 {
os.Exit(65)
}
if err := Run(resolver, interpreter, statements); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(70)
}
}
func LoadSource(path string) string {
source, err := os.ReadFile(path)
if err != nil {
errors.LogCliError(err, 66)
}
return string(source)
}
func RunPrompt() {
reader := bufio.NewReader(os.Stdin)
interpreter := interpreting.NewInterpreter()
resolver := resolving.NewResolver(interpreter)
PromptLoop:
for {
fmt.Print("> ")
line, err := reader.ReadString('\n')
if err != nil {
if goerrors.Is(err, io.EOF) {
break
}
errors.LogCliError(err, 74)
}
line = strings.TrimSpace(line)
if line == "" {
continue PromptLoop
}
statements, errs := Build(line)
for _, err := range errs {
fmt.Fprintln(os.Stderr, err)
continue PromptLoop
}
if err := Run(resolver, interpreter, statements); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
func Build(source string) ([]ast.Stmt, []error) {
scanner := scanning.NewScanner(source)
tokens, errs := scanner.ScanTokens()
if len(errs) > 0 {
return nil, errs
}
parser := parsing.NewParser(tokens)
statements, errs := parser.Parse()
if len(errs) > 0 || parser.HadError {
return nil, errs
}
return statements, errs
}
func Run(resolver *resolving.Resolver, interpreter *interpreting.Interpreter, statements []ast.Stmt) error {
if hadError := resolver.Resolve(statements); hadError {
return nil
}
return interpreter.Interpret(statements)
}
func TestScanning(pth string) {
source := LoadSource(pth)
scanner := scanning.NewScanner(source)
tokens, errs := scanner.ScanTokens()
for _, err := range errs {
fmt.Fprintln(os.Stderr, err)
}
if len(errs) > 0 {
os.Exit(65)
}
for _, tok := range tokens {
fmt.Println(tok)
}
}
func TestParsing(pth string) {
panic("Parsing test currently unavailable")
// source := LoadSource(pth)
// scanner := scanning.NewScanner(source)
// tokens, errs := scanner.ScanTokens()
// for _, err := range errs {
// fmt.Fprintln(os.Stderr, err)
// }
// if len(errs) > 0 {
// os.Exit(65)
// }
// parser := parsing.NewParser(tokens)
// expression, err := parser.Parse()
// if err != nil {
// fmt.Fprintln(os.Stderr, err)
// os.Exit(65)
// }
// fmt.Println(ast.NewPrinter().Print(expression))
}