-
Notifications
You must be signed in to change notification settings - Fork 3
/
builtin.go
117 lines (116 loc) · 2.24 KB
/
builtin.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package lisp
func init() {
Add("none", func(t []Token, p *Lisp) (Token, error) {
if len(t) != 0 {
return None, ErrParaNum
}
return None, nil
})
Add("atom", func(t []Token, p *Lisp) (Token, error) {
if len(t) != 1 {
return None, ErrParaNum
}
x, err := p.Exec(t[0])
if err != nil {
return None, err
}
if x.Kind != List || len(x.Text.([]Token)) == 0 {
return True, nil
} else {
return False, nil
}
})
Add("eq", func(t []Token, p *Lisp) (Token, error) {
if len(t) != 2 {
return None, ErrParaNum
}
x, err := p.Exec(t[0])
if err != nil {
return None, err
}
y, err := p.Exec(t[1])
if err != nil {
return None, err
}
if x.Kind == Back || y.Kind == Back {
return None, ErrFitType
}
if x.Eq(&y) {
return True, nil
} else {
return False, nil
}
})
Add("car", func(t []Token, p *Lisp) (Token, error) {
if len(t) != 1 {
return None, ErrParaNum
}
x, err := p.Exec(t[0])
if err != nil {
return None, err
}
if x.Kind == List {
if len(x.Text.([]Token)) != 0 {
return x.Text.([]Token)[0], nil
}
return None, ErrIsEmpty
}
return None, ErrFitType
})
Add("cdr", func(t []Token, p *Lisp) (Token, error) {
if len(t) != 1 {
return None, ErrParaNum
}
x, err := p.Exec(t[0])
if err != nil {
return None, err
}
if x.Kind == List {
if len(x.Text.([]Token)) != 0 {
return Token{List, x.Text.([]Token)[1:]}, nil
}
return None, ErrIsEmpty
}
return None, ErrFitType
})
Add("cons", func(t []Token, p *Lisp) (Token, error) {
if len(t) != 2 {
return None, ErrParaNum
}
x, err := p.Exec(t[0])
if err != nil {
return None, err
}
y, err := p.Exec(t[1])
if err != nil {
return None, err
}
if y.Kind == List {
a := y.Text.([]Token)
b := make([]Token, len(a)+1)
b[0] = x
copy(b[1:], a)
return Token{List, b}, nil
}
return None, ErrFitType
})
Add("eval", func(t []Token, p *Lisp) (Token, error) {
if len(t) != 1 {
return None, ErrParaNum
}
ans, err := p.Exec(t[0])
if err != nil {
return None, err
}
return p.Exec(ans)
})
Add("quote", func(t []Token, p *Lisp) (Token, error) {
if len(t) != 1 {
return None, ErrParaNum
}
if t[0].Kind == Label {
return p.Exec(t[0])
}
return t[0], nil
})
}