forked from zhemao/glisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
45 lines (38 loc) · 904 Bytes
/
utils.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
package glisp
import (
"errors"
"fmt"
"strconv"
"strings"
)
const Many = -1
var WrongType error = errors.New("operands have invalid type")
func WrongNumberArguments(funcname string, current int, expect ...int) (Sexp, error) {
exp := make([]string, len(expect))
for i, n := range expect {
if n == Many {
exp[i] = "..."
break
} else {
exp[i] = strconv.FormatInt(int64(n), 10)
}
}
return SexpNull, fmt.Errorf(`%s expect %s argument(s) but got %v`, funcname, strings.Join(exp, ","), current)
}
func WrongGeneratorNumberArguments(funcname string, current int, expect ...int) error {
_, err := WrongNumberArguments(funcname, current, expect...)
return err
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func copyFuncMap(fm map[int]*SexpFunction) map[int]*SexpFunction {
out := make(map[int]*SexpFunction)
for k, v := range fm {
out[k] = v
}
return out
}