-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (54 loc) · 1.03 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"dt.com/lox/ast"
)
func main(){
if len(os.Args) > 2 {
fmt.Println("Usage: golox [script]");
os.Exit(64)
} else if len(os.Args) == 2 {
runFile(os.Args[1]);
} else {
runPrompt();
}
}
func check(e error) {
// Good enough for now
if e != nil {
panic(e)
}
}
func runFile(fileName string){
bytes, err := ioutil.ReadFile(fileName)
check(err)
run(string(bytes));
}
func runPrompt(){
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
line, _ := reader.ReadString('\n')
if line == "" { break; }
run(line);
}
}
func run(source string){
tokens, err := ast.Scan(source);
check(err)
// For now, just print the tokens.
for _, t := range tokens {
fmt.Println(t.ToString())
}
}
func reportError(line int, message string){
report(line, "", message)
}
var hadError = false; // global for now
func report(line int, where, message string){
fmt.Println("[line %v] Error %v:%v", line, where, message)
hadError = true;
}