瀏覽代碼

Function tonumber now supports 3-7 and 9 based numbers.

jernejk 11 年之前
父節點
當前提交
56d6ab43bb

+ 1 - 2
src/MoonSharp.Interpreter.Tests/MoonSharp.Interpreter.Tests.csproj

@@ -59,8 +59,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
-    <Reference Include="nunit.framework, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
-      <SpecificVersion>False</SpecificVersion>
+    <Reference Include="nunit.framework">
       <HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
     </Reference>
     <Reference Include="System" />

+ 10 - 0
src/MoonSharp.Interpreter.Tests/TestMore/301-basic.t

@@ -310,6 +310,12 @@ is(tonumber('  3.14  '), 3.14)
 is(tonumber(111, 2), 7)
 is(tonumber('111', 2), 7)
 is(tonumber('  111  ', 2), 7)
+is(tonumber('78', 9), 71)
+is(tonumber('111  ', 3), 13)
+is(tonumber('111', 4), 21)
+is(tonumber('1234', 5), 194)
+is(tonumber('54321', 6), 7465)
+
 a = {}
 is(tonumber(a), nil)
 
@@ -321,6 +327,10 @@ error_like(function () tonumber('111', 200) end,
            "^[^:]+:%d+: bad argument #2 to 'tonumber' %(base out of range%)",
            "function tonumber (bad base)")
 
+error_like(function () tonumber('17', 6) end,
+           "^[^:]+:%d+: bad argument #1 to 'tonumber' %(invalid character%)",
+           "function tonumber (bad base)")
+
 is(tostring('text'), 'text', "function tostring")
 is(tostring(3.14), '3.14')
 is(tostring(nil), 'nil')

+ 25 - 8
src/MoonSharp.Interpreter/CoreLib/BasicModule.cs

@@ -201,8 +201,9 @@ namespace MoonSharp.Interpreter.CoreLib
 			}
 			else
 			{
-				//!COMPAT: tonumber supports only 2,8,10 or 16 as base
-				DynValue ee;
+                //!COMPAT: tonumber supports only 2,8,10 or 16 as base
+                //UPDATE: added support for 3-9 base numbers
+                DynValue ee;
 
 				if (args[0].Type != DataType.Number)
 					ee = args.AsType(0, "tonumber", DataType.String, false);
@@ -211,12 +212,28 @@ namespace MoonSharp.Interpreter.CoreLib
 
 				int bb = (int)b.Number;
 
-				if (bb != 2 && bb != 8 && bb != 10 && bb != 16)
-				{
-					throw new ScriptRuntimeException("bad argument #2 to 'tonumber' (base out of range)");
-				}
-
-				uint uiv = Convert.ToUInt32(ee.String.Trim(), bb);
+			    uint uiv = 0;
+                if (bb == 2 || bb == 8 || bb == 10 || bb == 16)
+			    {
+                    uiv = Convert.ToUInt32(ee.String.Trim(), bb);
+                }
+			    else if (bb < 10 && bb > 2) // Support for 3, 4, 5, 6, 7 and 9 based numbers
+			    {
+			        foreach (char digit in ee.String.Trim())
+			        {
+			            int value = digit - 48;
+			            if (value < 0 || value >= bb)
+			            {
+                            throw new ScriptRuntimeException("bad argument #1 to 'tonumber' (invalid character)");
+                        }
+
+                        uiv = (uint)(uiv * bb) + (uint)value;
+			        }
+                }
+			    else
+			    {
+                    throw new ScriptRuntimeException("bad argument #2 to 'tonumber' (base out of range)");
+                }
 
 				return DynValue.NewNumber(uiv);
 			}

+ 2 - 2
src/MoonSharp.Interpreter/MoonSharp.Interpreter.csproj

@@ -21,7 +21,7 @@
     <SccProvider>
     </SccProvider>
     <TargetFrameworkProfile>Client</TargetFrameworkProfile>
-    <NuGetPackageImportStamp>cd9758df</NuGetPackageImportStamp>
+    <NuGetPackageImportStamp>b17ae9da</NuGetPackageImportStamp>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -297,7 +297,7 @@
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
     <PropertyGroup>
-      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
+      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
     </PropertyGroup>
     <Error Condition="!Exists('..\packages\Antlr4.4.3.0\build\Antlr4.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Antlr4.4.3.0\build\Antlr4.props'))" />
     <Error Condition="!Exists('..\packages\Antlr4.4.3.0\build\Antlr4.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Antlr4.4.3.0\build\Antlr4.targets'))" />

+ 1 - 0
src/MoonSharp.RemoteDebugger/MoonSharp.RemoteDebugger.csproj

@@ -20,6 +20,7 @@
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>

+ 1 - 2
src/MoonSharpTests/MoonSharpTests.csproj

@@ -60,8 +60,7 @@
     <Prefer32Bit>false</Prefer32Bit>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="nunit.framework, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
-      <SpecificVersion>False</SpecificVersion>
+    <Reference Include="nunit.framework">
       <HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
     </Reference>
     <Reference Include="System" />