-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.go
177 lines (153 loc) · 3.6 KB
/
builtins.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
// Copyright ©2017-2020 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package goblin
import (
"fmt"
"reflect"
"sort"
"time"
"unicode/utf8"
"github.com/richardwilkes/goblin/util"
)
func (s *scope) loadBuiltins() {
s.Define("len", func(v interface{}) int64 {
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Interface {
rv = rv.Elem()
}
k := rv.Kind()
if k == reflect.String || k == reflect.Array || k == reflect.Slice || k == reflect.Map {
return int64(rv.Len())
}
return 0
})
s.Define("keys", func(v interface{}) []interface{} {
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Interface {
rv = rv.Elem()
}
if rv.Kind() == reflect.Map {
k := rv.MapKeys()
keys := make([]interface{}, len(k))
for i := range k {
keys[i] = k[i].Interface()
}
return keys
}
return []interface{}{}
})
s.Define("range", func(args ...int64) []int64 {
count := len(args)
if count == 0 {
return []int64{}
}
var min, max int64
if count == 1 {
max = args[0] - 1
if max < 0 {
return []int64{}
}
} else {
min = args[0]
max = args[1]
}
if max < min {
max = min
}
arr := make([]int64, 1+max-min)
for i := min; i <= max; i++ {
arr[i-min] = i
}
return arr
})
s.Define("sort", sort.Slice)
s.Define("sleep", func(spec string) {
if d, err := time.ParseDuration(spec); err == nil && d > 0 {
timer := time.NewTimer(d)
select {
case <-timer.C:
case <-(*s.interrupt):
timer.Stop()
s.Interrupt() // Re-interrupt, since we just ate the previous one
}
}
})
s.Define("toString", func(v interface{}) string {
if str, ok := v.(string); ok {
return str
}
if b, ok := v.([]byte); ok {
return string(b)
}
return fmt.Sprint(v)
})
s.Define("toInt", func(v interface{}) int64 {
return util.ToInt64(reflect.ValueOf(v))
})
s.Define("toFloat", func(v interface{}) float64 {
return util.ToFloat64(reflect.ValueOf(v))
})
s.Define("toBool", func(v interface{}) bool {
return util.ToBool(reflect.ValueOf(v))
})
s.Define("toChar", func(s rune) string {
return string(s)
})
s.Define("toRune", func(s string) rune {
if s == "" {
return 0
}
r, _ := utf8.DecodeRuneInString(s)
return r
})
s.Define("toByteSlice", func(s string) []byte {
return []byte(s)
})
s.Define("toRuneSlice", func(s string) []rune {
return []rune(s)
})
s.Define("toBoolSlice", func(v []interface{}) []bool {
var result []bool
util.ToSlice(v, &result)
return result
})
s.Define("toIntSlice", func(v []interface{}) []int64 {
var result []int64
util.ToSlice(v, &result)
return result
})
s.Define("toFloatSlice", func(v []interface{}) []float64 {
var result []float64
util.ToSlice(v, &result)
return result
})
s.Define("toStringSlice", func(v []interface{}) []string {
var result []string
util.ToSlice(v, &result)
return result
})
s.Define("typeOf", func(v interface{}) string {
t := reflect.TypeOf(v)
if t == nil {
return "<nil>"
}
return reflect.TypeOf(v).String()
})
s.Define("defined", func(str string) bool {
_, err := s.Get(str)
return err == nil
})
s.Define("print", fmt.Print)
s.Define("println", fmt.Println)
s.Define("printf", fmt.Printf)
s.DefineType("int", int64(0))
s.DefineType("float", 0.0)
s.DefineType("bool", true)
s.DefineType("string", "")
}