From 2dc59c5f8be6da0fdb7ed498e751b7a684c5416f Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 24 Nov 2018 14:49:20 +0100 Subject: [PATCH] Adds tostring and type functions --- Upsilon/BaseTypes/ScriptIterator.cs | 2 +- Upsilon/StandardLibraries/BasicFunctions.cs | 14 +++++++++- .../BasicFunctionsTests.cs | 27 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Upsilon/BaseTypes/ScriptIterator.cs b/Upsilon/BaseTypes/ScriptIterator.cs index fdec204..3df5c82 100644 --- a/Upsilon/BaseTypes/ScriptIterator.cs +++ b/Upsilon/BaseTypes/ScriptIterator.cs @@ -46,7 +46,7 @@ namespace Upsilon.BaseTypes while (baseEnumerator.MoveNext()) { var key = baseEnumerator.Current; - if (key.Type == Type.Nil) + if (key == null || key.Type == Type.Nil) break; var value = BaseIterator.GetValueFromIndex(key); if (value.Type == Type.Nil) diff --git a/Upsilon/StandardLibraries/BasicFunctions.cs b/Upsilon/StandardLibraries/BasicFunctions.cs index 0c415f8..1c8c0be 100644 --- a/Upsilon/StandardLibraries/BasicFunctions.cs +++ b/Upsilon/StandardLibraries/BasicFunctions.cs @@ -37,7 +37,6 @@ namespace Upsilon.StandardLibraries return new PairsScriptIterator(table); } - [StandardLibraryScriptFunction("tonumber")] public ScriptNumber ToNumber(ScriptString obj) { @@ -51,5 +50,18 @@ namespace Upsilon.StandardLibraries return new ScriptNumberLong(long.Parse(str)); } } + + [StandardLibraryScriptFunction("tostring")] + public ScriptString ToString(ScriptType obj) + { + return new ScriptString(obj.ToString()); + } + + [StandardLibraryScriptFunction("type")] + public ScriptString Type(ScriptType obj) + { + return new ScriptString(obj.Type.ToString()); + } + } } \ No newline at end of file diff --git a/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs b/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs index 25d653d..59a915e 100644 --- a/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs +++ b/UpsilonTests/StandardLibraryTests/BasicFunctionsTests.cs @@ -73,5 +73,32 @@ return value Assert.Empty(script.Diagnostics.Messages); Assert.Equal(284, result); } + + [Fact] + public void ToStringTest() + { + const string input = @" +return tostring(100) +"; + var script = new Script(input, BoundScope, StaticScope); + Assert.Empty(script.Diagnostics.Messages); + var result = script.Evaluate(); + Assert.Empty(script.Diagnostics.Messages); + Assert.Equal("100", result); + } + + [Fact] + public void TypeTest() + { + const string input = @" +return type(100) +"; + var script = new Script(input, BoundScope, StaticScope); + Assert.Empty(script.Diagnostics.Messages); + var result = script.Evaluate(); + Assert.Empty(script.Diagnostics.Messages); + Assert.Equal("Number", result); + } + } } \ No newline at end of file