-
Notifications
You must be signed in to change notification settings - Fork 0
/
goscm.go
95 lines (81 loc) · 1.73 KB
/
goscm.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
92
93
94
95
package goscm
import (
"reflect"
"bufio"
"fmt"
"strings"
"errors"
)
type SCMT interface {
Eval(*Environ) (SCMT, error)
String() string
}
type Func interface {
Apply(*Pair, *Environ) (SCMT, error)
}
type PreFunc interface {
Expand(*Pair, *Environ) (SCMT, error)
}
func NewSCMT(in interface {}) SCMT {
if in == nil {
return SCM_Nil
}
switch reflect.TypeOf(in).Kind() {
case reflect.Int:
return &PlainInt { Value: in.(int) }
case reflect.Bool:
return &Boolean { Value: in.(bool) }
default:
// TODO: We probably need to put an error here
return nil
}
}
func REPL(in *bufio.Reader, read func(*bufio.Reader) (SCMT, error), env *Environ) {
for {
r, err := read(in)
if err != nil && err.Error() == "EOF" {
break
}
if err != nil {
fmt.Println("(READ ERROR) " + err.Error())
continue
}
e, err := r.Eval(env)
if err != nil {
fmt.Println("(EVAL ERROR) " + err.Error())
continue
}
fmt.Println("=> " + e.String())
}
}
func EvalStr(str string, read func(*bufio.Reader) (SCMT, error), env *Environ) (SCMT, error) {
b := bufio.NewReader(strings.NewReader(str))
r, err := read(b)
if err != nil && err.Error() != "EOF" {
return SCM_Nil, err
}
return r.Eval(env)
}
func MapEval(list *Pair, env *Environ) (*Pair, error) {
if list == SCM_Nil {
return SCM_Nil, nil
}
new, err := list.Car.Eval(env)
if err != nil {
return SCM_Nil, nil
}
if reflect.TypeOf(list.Cdr) != reflect.TypeOf(&Pair{}) {
return SCM_Nil, errors.New("List is not nil-terminated")
}
tail, err := MapEval(list.Cdr.(*Pair), env)
if err != nil {
return SCM_Nil, nil
}
return Cons(new, tail), nil
}
func SCMTrue(s SCMT) bool {
if reflect.TypeOf(s) == reflect.TypeOf(&Boolean{}) {
return s.(*Boolean).Value
}
return true
}