-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.go
185 lines (149 loc) · 4.26 KB
/
core.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package slang
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/spy16/sabre"
)
// Case implements the switch case construct.
func Case(scope sabre.Scope, args []sabre.Value) (sabre.Value, error) {
if len(args) < 2 {
return nil, errors.New("case requires at-least 2 args")
}
res, err := sabre.Eval(scope, args[0])
if err != nil {
return nil, err
}
if len(args) == 2 {
return sabre.Eval(scope, args[1])
}
start := 1
for ; start < len(args); start += 2 {
val := args[start]
if start+1 >= len(args) {
return val, nil
}
if sabre.Compare(res, val) {
return sabre.Eval(scope, args[start+1])
}
}
return nil, fmt.Errorf("no matching clause for '%s'", res)
}
// MacroExpand is a wrapper around the sabre MacroExpand function that
// ignores the expanded bool flag.
func MacroExpand(scope sabre.Scope, f sabre.Value) (sabre.Value, error) {
f, _, err := sabre.MacroExpand(scope, f)
return f, err
}
// Throw converts args to strings and returns an error with all the strings
// joined.
func Throw(scope sabre.Scope, args ...sabre.Value) error {
return errors.New(strings.Trim(MakeString(args...).String(), "\""))
}
// Realize realizes a sequence by continuously calling First() and Next()
// until the sequence becomes nil.
func Realize(seq sabre.Seq) *sabre.List {
var vals []sabre.Value
for seq != nil {
v := seq.First()
if v == nil {
break
}
vals = append(vals, v)
seq = seq.Next()
}
return &sabre.List{Values: vals}
}
// TypeOf returns the type information object for the given argument.
func TypeOf(v interface{}) sabre.Value {
return sabre.ValueOf(reflect.TypeOf(v))
}
// Implements checks if given value implements the interface represented
// by 't'. Returns error if 't' does not represent an interface type.
func Implements(v interface{}, t sabre.Type) (bool, error) {
if t.T.Kind() == reflect.Ptr {
t.T = t.T.Elem()
}
if t.T.Kind() != reflect.Interface {
return false, fmt.Errorf("type '%s' is not an interface type", t)
}
return reflect.TypeOf(v).Implements(t.T), nil
}
// ToType attempts to convert given sabre value to target type. Returns
// error if conversion not possible.
func ToType(val sabre.Value, to sabre.Type) (sabre.Value, error) {
rv := reflect.ValueOf(val)
if rv.Type().ConvertibleTo(to.T) || rv.Type().AssignableTo(to.T) {
return sabre.ValueOf(rv.Convert(to.T).Interface()), nil
}
return nil, fmt.Errorf("cannot convert '%s' to '%s'", rv.Type(), to.T)
}
// ThreadFirst threads the expressions through forms by inserting result of
// eval as first argument to next expr.
func ThreadFirst(scope sabre.Scope, args []sabre.Value) (sabre.Value, error) {
return threadCall(scope, args, false)
}
// ThreadLast threads the expressions through forms by inserting result of
// eval as last argument to next expr.
func ThreadLast(scope sabre.Scope, args []sabre.Value) (sabre.Value, error) {
return threadCall(scope, args, true)
}
// MakeString returns stringified version of all args.
func MakeString(vals ...sabre.Value) sabre.Value {
argc := len(vals)
switch argc {
case 0:
return sabre.String("")
case 1:
nilVal := sabre.Nil{}
if vals[0] == nilVal || vals[0] == nil {
return sabre.String("")
}
return sabre.String(strings.Trim(vals[0].String(), "\""))
default:
var sb strings.Builder
for _, v := range vals {
sb.WriteString(strings.Trim(v.String(), "\""))
}
return sabre.String(sb.String())
}
}
func threadCall(scope sabre.Scope, args []sabre.Value, last bool) (sabre.Value, error) {
if len(args) == 0 {
return nil, errors.New("at-least 1 argument required")
}
res, err := sabre.Eval(scope, args[0])
if err != nil {
return nil, err
}
for args = args[1:]; len(args) > 0; args = args[1:] {
form := args[0]
switch f := form.(type) {
case *sabre.List:
if last {
f.Values = append(f.Values, res)
} else {
f.Values = append([]sabre.Value{f.Values[0], res}, f.Values[1:]...)
}
res, err = sabre.Eval(scope, f)
case sabre.Invokable:
res, err = f.Invoke(scope, res)
default:
return nil, fmt.Errorf("%s is not invokable", reflect.TypeOf(res))
}
if err != nil {
return nil, err
}
}
return res, nil
}
func isTruthy(v sabre.Value) bool {
if v == nil || v == (sabre.Nil{}) {
return false
}
if b, ok := v.(sabre.Bool); ok {
return bool(b)
}
return true
}