-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (60 loc) · 1001 Bytes
/
main.go
File metadata and controls
79 lines (60 loc) · 1001 Bytes
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
package main
import (
"bufio"
"flag"
"fmt"
"os"
scan "github.com/ByteHunter/glox/scanner"
)
func main() {
os.Exit(RunMain())
}
func RunMain() int {
flag.Parse()
if flag.NArg() > 1 {
fmt.Println("Error: Too many arguments!")
return 64
}
if flag.NArg() == 1 {
runFile(flag.Arg(0))
} else {
runInteractive()
}
return 0
}
func runFile(fileName string) error {
fileData, err := os.ReadFile(fileName)
if err != nil {
return err
}
source := string(fileData)
run(source)
return nil
}
func runInteractive() error {
for {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("> ")
line, prefix, err := reader.ReadLine()
if err != nil {
return err
}
if prefix {
fmt.Println("Prompt size was too large!")
}
source := string(line)
if len(source) == 0 {
break
}
run(source + "\n")
}
return nil
}
func run(source string) error {
if len(source) == 0 {
return nil
}
scanner := scan.NewScanner(source)
scanner.ScanTokens()
return nil
}