-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (66 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bufio"
"fmt"
"io"
"os"
"github.com/chettriyuvraj/lox-crafting-interpreters/pkg/scanner"
)
/* var hadError bool = false - might not need this because of Go's multiple return types*/
func main() {
if len(os.Args) < 2 {
runPrompt()
} else if len(os.Args) == 2 {
err := runFile(os.Args[1])
fmt.Println(err)
} else {
fmt.Println("Usage: go run main.go [file]")
}
}
func runPrompt() error {
fmt.Println("Run prompt!")
reader := bufio.NewReader(os.Stdin)
for {
s, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
run(s)
return nil
}
return err
}
run(s)
}
return nil
}
func runFile(filePath string) error {
fmt.Println("Run file!")
/* TODO: Is this the best way to get source code from file (?) */
b, err := os.ReadFile(filePath)
if err != nil {
return err
}
sc := scanner.Scanner{Source: string(b)}
tokens, err := sc.ScanTokens()
if err != nil {
return err
}
fmt.Println(tokens)
return nil
}
func run(source string) error {
fmt.Println("running run!")
sc := scanner.Scanner{Source: source}
tokens, err := sc.ScanTokens()
if err != nil {
return err
}
fmt.Println(tokens)
return nil
}
func handleError(line int, message string) {
report(line, "", message)
}
func report(line int, where string, message string) {
fmt.Printf("[Line %d] Error %s: %s", line, where, message)
}