-
Notifications
You must be signed in to change notification settings - Fork 3
/
decorate.go
151 lines (150 loc) · 3.21 KB
/
decorate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package lisp
func init() {
Add("pretreat", func(t []Token, p *Lisp) (Token, error) {
var (
x, y Token
err error
)
x, err = p.Exec(t[0])
if err != nil {
return None, err
}
if x.Kind == Macro {
f := x.Text.(*Hong)
n := f.Para
if len(n) != len(t)-1 {
return None, ErrParaNum
}
hold := make([]bool, len(n))
for i, z := range t[1:] {
y, err = p.Exec(z)
if err != nil {
return None, err
}
hold[i] = y.Bool()
}
return Token{Back, Gfac(func(t2 []Token, p2 *Lisp) (Token, error) {
if len(t2) != len(n) {
return None, ErrParaNum
}
m := make([]Token, len(n)+1)
m[0] = x
for i, t := range hold {
if t {
u, err := p.Exec(t2[i])
if err != nil {
return None, err
}
m[i+1] = u
} else {
m[i+1] = t2[i]
}
}
return p2.Exec(Token{List, m})
})}, nil
}
return None, ErrFitType
})
Add("default", func(t []Token, p *Lisp) (Token, error) {
var (
x, y Token
err error
)
x, err = p.Exec(t[0])
if err != nil {
return None, err
}
switch x.Kind {
case Macro:
g := x.Text.(*Hong)
n := g.Para
if len(n) < len(t)-1 {
return None, ErrParaNum
}
hold := t[1:]
return Token{Back, Gfac(func(t2 []Token, p2 *Lisp) (Token, error) {
if len(t2) > len(n) || len(t2)+len(hold) < len(n) {
return None, ErrParaNum
}
m := make([]Token, len(n)+1)
m[0] = x
copy(m[1:], t2)
copy(m[len(t2)+1:], hold[len(t2)+len(hold)-len(n):])
return p2.Exec(Token{List, m})
})}, nil
case Front:
f := x.Text.(*Lfac)
n := f.Para
if len(n) < len(t)-1 {
return None, ErrParaNum
}
hold := make([]Token, len(t)-1)
for i, z := range t[1:] {
y, err = p.Exec(z)
if err != nil {
return None, err
}
hold[i] = y
}
return Token{Back, Gfac(func(t2 []Token, p2 *Lisp) (Token, error) {
if len(t2) > len(n) || len(t2)+len(hold) < len(n) {
return None, ErrParaNum
}
q := &Lisp{dad: f.Make, env: map[Name]Token{}}
for i, z := range t2 {
y, err = p.Exec(z)
if err != nil {
return None, err
}
q.env[n[i]] = y
}
for i, j := len(n)-1, 1; i >= len(t2); i, j = i-1, j+1 {
q.env[n[i]] = hold[len(hold)-j]
}
return q.Exec(Token{List, f.Text})
})}, nil
}
return None, ErrFitType
})
Add("omission", func(t []Token, p *Lisp) (Token, error) {
var (
x, y Token
err error
)
if len(t) != 1 {
return None, ErrParaNum
}
x, err = p.Exec(t[0])
if err != nil {
return None, err
}
if x.Kind == Front {
f := x.Text.(*Lfac)
n := f.Para
return Token{Back, Gfac(func(t2 []Token, p2 *Lisp) (Token, error) {
if len(t2) < len(n)-1 {
return None, ErrParaNum
}
q := &Lisp{dad: f.Make, env: map[Name]Token{}}
for i := len(n) - 2; i >= 0; i-- {
y, err = p.Exec(t2[i])
if err != nil {
return None, err
}
q.env[n[i]] = y
}
z := make([]Token, 0, len(t2)-len(n)+1)
for i := len(n) - 1; i < len(t2); i++ {
y, err = p.Exec(t2[i])
if err != nil {
return None, err
}
z = append(z, y)
}
q.env[n[len(n)-1]] = Token{List, z}
return q.Exec(Token{List, f.Text})
})}, nil
}
return None, ErrFitType
})
}