Add a "base" builtin.

This commit is contained in:
Qi Xiao 2016-02-22 02:26:29 +01:00
parent 5bbb8bef13
commit 9793da932c
2 changed files with 17 additions and 0 deletions

View File

@ -83,6 +83,8 @@ func init() {
&BuiltinFn{"lt", wrapFn(lt)},
&BuiltinFn{"gt", wrapFn(gt)},
&BuiltinFn{"base", wrapFn(base)},
&BuiltinFn{"=", eq},
&BuiltinFn{"deepeq", deepeq},
@ -506,6 +508,20 @@ func gt(ec *EvalCtx, nums ...float64) {
}
}
var ErrBadBase = errors.New("bad base")
func base(ec *EvalCtx, b int, nums ...int) {
if b < 2 || b > 36 {
throw(ErrBadBase)
}
out := ec.ports[1].Chan
for _, num := range nums {
out <- String(strconv.FormatInt(int64(num), b))
}
}
var ErrNotEqual = errors.New("not equal")
func eq(ec *EvalCtx, args []Value) {

View File

@ -184,6 +184,7 @@ var evalTests = []struct {
String("a"): NewList(strs("1", "2")...)}),
String("foo"),
}, nomore},
{"base 16 42 233", strs("2a", "e9"), nomore},
}
func strs(ss ...string) []Value {