Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add int2str builtin function #634

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ func Rnd(args ...runtime.Object) runtime.Object {
return runtime.Float(rand.Float64())
}

func Int2str(args ...runtime.Object) runtime.Object {
if len(args) != 1 {
return runtime.Errorf("wrong number of arguments. got=%d, want=1", len(args))
}
number, ok := args[0].(runtime.Int)
if !ok {
return runtime.Errorf("%s arguments not supported", args[0].Type())
}
if !number.IsInt64() {
return runtime.Errorf("Provided argument is not 64bit-integer")
}
return &runtime.String{Value: []rune(fmt.Sprintf("%d", number.Int64()))}
}

func Int2char(args ...runtime.Object) runtime.Object {
if len(args) != 1 {
return runtime.Errorf("wrong number of arguments. got=%d, want=1", len(args))
Expand Down Expand Up @@ -185,6 +199,7 @@ func makeSet(lt runtime.ListType, args ...runtime.Object) func(...runtime.Object
func init() {
runtime.AddBuiltin("lengthof", Lengthof)
runtime.AddBuiltin("rnd", Rnd)
runtime.AddBuiltin("int2str", Int2str)
runtime.AddBuiltin("int2bit", Int2Bit)
runtime.AddBuiltin("int2float", Int2Float)
runtime.AddBuiltin("float2int", Float2Int)
Expand Down
40 changes: 40 additions & 0 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,46 @@ func testBool(t *testing.T, obj runtime.Object, expected bool) bool {
return true
}

func TestBuiltinFunctionInt2str(t *testing.T) {

tests := []struct {
input string
expected runtime.Object
}{
{`int2str(2, 4)`, runtime.Errorf("wrong number of arguments. got=2, want=1")},
{`int2str("wrong")`, runtime.Errorf("string arguments not supported")},
{`int2str(2.4)`, runtime.Errorf("float arguments not supported")},
{`int2str(9223372036854775808)`, runtime.Errorf("Provided argument is not 64bit-integer")},
{`int2str(9223372036854775807)`, runtime.NewString("9223372036854775807")},
{`int2str(0)`, runtime.NewString("0")},
{`int2str(-9223372036854775808)`, runtime.NewString("-9223372036854775808")},
{`int2str(-9223372036854775809)`, runtime.Errorf("Provided argument is not 64bit-integer")},
}

for _, tt := range tests {

val := testEval(t, tt.input)

switch expected := tt.expected.(type) {
case *runtime.Error:
err, ok := val.(*runtime.Error)
if !ok {
t.Errorf("object is not runtime.Error. got=%T (%+v)", val, val)
continue
}
if err.Error() != expected.Error() {
t.Errorf("wrong error message. got=%s, want=%s", err.Error(), expected.Error())
}
case *runtime.String:
if !expected.Equal(val) {
t.Errorf("wrong runtime.String. got=%v, want=%v", val, expected)
}
default:
t.Errorf("test error, unhandeled type:%T", expected)
}
}
}

func TestBuiltinFunctionInt2char(t *testing.T) {

tests := []struct {
Expand Down