From 9793da932c07907e0f58d76fb66b40ae7c3d37da Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Mon, 22 Feb 2016 02:26:29 +0100 Subject: [PATCH] Add a "base" builtin. --- eval/builtin_func.go | 16 ++++++++++++++++ eval/eval_test.go | 1 + 2 files changed, 17 insertions(+) diff --git a/eval/builtin_func.go b/eval/builtin_func.go index 40463b44..ac291f74 100644 --- a/eval/builtin_func.go +++ b/eval/builtin_func.go @@ -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) { diff --git a/eval/eval_test.go b/eval/eval_test.go index 8a1824bd..9c9a26da 100644 --- a/eval/eval_test.go +++ b/eval/eval_test.go @@ -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 {