-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (86 loc) · 1.53 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
package main
import (
"flag"
"fmt"
"github.com/zfjagann/gamma/interp"
"github.com/zfjagann/gamma/parse"
"github.com/zfjagann/gamma/sexpr"
"io"
"os"
)
func main() {
fname := flag.String("f", "-", "specify a file to run")
flag.Parse()
if *fname == "-" {
os.Exit(repl(true, os.Stdin))
} else {
input, err := os.Open(*fname)
if err != nil {
fmt.Println(err)
os.Exit(255)
}
os.Exit(repl(false, input))
}
}
func repl(interactive bool, input io.Reader) int {
parser := parse.NewParser(input)
eval := interp.NewInterpreter(interp.DefaultEnvironment)
for {
if interactive {
fmt.Print("scheme00> ")
}
input, err := parser.Parse()
if err != nil {
if err == io.EOF {
return 0
}
fmt.Println(err)
if !interactive {
return 1
}
fmt.Println()
continue
}
output, err := eval.Evaluate(input)
if err != nil {
if err == interp.Exit {
return 0
}
fmt.Println(err)
if !interactive {
return 2
}
} else if output != sexpr.Null {
fmt.Printf("%v\n", output)
}
}
}
func run(fname string) {
input, err := os.Open(fname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
parser := parse.NewParser(input)
eval := interp.NewInterpreter(interp.DefaultEnvironment)
for {
input, err := parser.Parse()
if err != nil {
if err == io.EOF {
os.Exit(0)
}
fmt.Println(err)
os.Exit(2)
}
output, err := eval.Evaluate(input)
if err != nil {
if err == interp.Exit {
return
}
fmt.Println(err)
os.Exit(3)
} else {
fmt.Printf("%v\n", output)
}
}
}