-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
77 lines (68 loc) · 1.34 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
package main
import (
"bufio"
"fmt"
"github.com/kedebug/LispEx/repl"
"github.com/kedebug/LispEx/scope"
"io/ioutil"
"os"
"time"
)
const version = "LispEx 0.1.0"
func LoadStdlib() (string, error) {
lib, err := ioutil.ReadFile("stdlib.ss")
if err != nil {
return "", err
}
return string(lib), nil
}
func EvalFile(filename string) error {
lib, err := LoadStdlib()
if err != nil {
return err
}
exprs, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
fmt.Println(repl.REPL(string(lib)+string(exprs), scope.NewRootScope()))
return nil
}
func try(body func(), handler func(interface{})) {
defer func() {
if err := recover(); err != nil {
handler(err)
}
}()
body()
}
func main() {
if len(os.Args) > 1 {
if err := EvalFile(os.Args[1]); err != nil {
fmt.Println(err)
}
return
}
lib, err := LoadStdlib()
if err != nil {
fmt.Println(err)
return
}
env := scope.NewRootScope()
repl.REPL(lib, env)
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s (%v)\n", version, time.Now().Format(time.RFC850))
for {
fmt.Print(">>> ")
line, _, _ := reader.ReadLine()
try(
func() {
r := repl.REPL(string(line), env)
if len(r) > 0 {
fmt.Println(r)
}
},
func(e interface{}) { fmt.Println(e) },
)
}
}