foxygit / fake-08-xbox360 Log in
commit 2cd560b95eb8c8b864aa529d931216fd73fb4d32
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Wed Sep 23 18:20:19 2020 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Sep 23 18:20:19 2020 -0600

    Add tonum support (mostly)\nFixed point support still needed to be accurate
---
 source/p8GlobalLuaFunctions.h             |  27 +++++++
 test/carts/screenshots/tonumtest2_f01.png | Bin 0 -> 707 bytes
 test/carts/tonumtest2.p8                  |  30 ++++++++
 test/endtoendtests.cpp                    | 124 ++++++++++++++++++++++++++----
 4 files changed, 165 insertions(+), 16 deletions(-)

diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index 72d0e25..36f4288 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -85,6 +85,33 @@ function tostr(val, hex)
 	end
 end

+function tonum(val)
+	--todo: fix these cases (real fix is fixed point support- might do a bandaid):
+	--tonum('99999')      -- -32768
+	--parse it, and clamp to pico 8 max value
+
+	local number = tonumber(val)
+	--limit to 4 decimal places to emulate fixed point for now
+	if string.match(val, "%.") then
+		return round(number, 4)
+	end
+
+	--if number >= 32768 then
+		--number = -32768
+	--end
+
+	return number
+end
+
+function round(x, decimals)
+	-- hacky band aid until true fixed point support
+	-- https://steamcommunity.com/app/573090/discussions/0/1639792569839228614/
+    local n = 10^(decimals or 0)
+    x = x * n
+    if x >= 0 then x = math.floor(x + 0.5) else x = math.ceil(x - 0.5) end
+    return x / n
+end
+
 ---------------------------------
 --Debug
 ---------------------------------
diff --git a/test/carts/screenshots/tonumtest2_f01.png b/test/carts/screenshots/tonumtest2_f01.png
new file mode 100644
index 0000000..435fa6e
Binary files /dev/null and b/test/carts/screenshots/tonumtest2_f01.png differ
diff --git a/test/carts/tonumtest2.p8 b/test/carts/tonumtest2.p8
new file mode 100644
index 0000000..d1c4a63
--- /dev/null
+++ b/test/carts/tonumtest2.p8
@@ -0,0 +1,30 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _draw()
+ cls()
+ r1 = tonum('12345')      -- 12345
+ print(r1)
+ r2 = tonum('-12345.67')  -- -12345.67
+ print(r2)
+ r3 = tonum('0x0f')       -- 15
+ print(r3)
+ r4 = tonum('0x0f.abc')   -- 15.6709
+ print(r4)
+ r5 = tonum('0b1001')     -- 9
+ print(r5)
+
+ r6 = tonum('32767')      -- 32767
+ print(r6)
+ r7 = tonum('99999')      -- -32768
+ print(r7)
+ r8 = tonum('xyz')        -- nil
+ print(r8)
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 341323c..10f6b27 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -70,16 +70,14 @@ TEST_CASE("Loading and running carts") {
         SUBCASE("No error reported"){
             CHECK(vm->GetBiosError() == "");
         }
-        SUBCASE("Frame count updated with each UpdateAndDrawCall()")
-        {
+        SUBCASE("Frame count updated with each UpdateAndDrawCall()"){
             vm->UpdateAndDraw(0, 0);
             vm->UpdateAndDraw(0, 0);
             vm->UpdateAndDraw(0, 0);

             CHECK(vm->GetFrameCount() == 3);
         }
-        SUBCASE("check lua state")
-        {
+        SUBCASE("check lua state"){
             bool globalVarLoaded = vm->ExecuteLua(
                 "function globalVarTest()\n"
                 " return a == 1\n"
@@ -97,8 +95,7 @@ TEST_CASE("Loading and running carts") {
         SUBCASE("No error reported"){
             CHECK(vm->GetBiosError() == "");
         }
-        SUBCASE("sceen matches screenshot")
-        {
+        SUBCASE("sceen matches screenshot"){
             vm->UpdateAndDraw(0, 0);

             CHECK(verifyScreenshot(vm, "carts/screenshots/pset00-test_f01.png"));
@@ -112,8 +109,7 @@ TEST_CASE("Loading and running carts") {
         SUBCASE("No error reported"){
             CHECK(vm->GetBiosError() == "");
         }
-        SUBCASE("sceen matches screenshot")
-        {
+        SUBCASE("sceen matches screenshot"){
             vm->UpdateAndDraw(0, 0);

             CHECK(verifyScreenshot(vm, "carts/screenshots/pset3pix_f01.png"));
@@ -127,8 +123,7 @@ TEST_CASE("Loading and running carts") {
         SUBCASE("No error reported"){
             CHECK(vm->GetBiosError() == "");
         }
-        SUBCASE("sceen matches screenshot")
-        {
+        SUBCASE("sceen matches screenshot"){
             vm->UpdateAndDraw(0, 0);

             CHECK(verifyScreenshot(vm, "carts/screenshots/psetall_f01.png"));
@@ -142,8 +137,7 @@ TEST_CASE("Loading and running carts") {
         SUBCASE("No error reported"){
             CHECK(vm->GetBiosError() == "");
         }
-        SUBCASE("sceen matches screenshot")
-        {
+        SUBCASE("sceen matches screenshot"){
             vm->UpdateAndDraw(0, 0);

             CHECK(verifyScreenshot(vm, "carts/screenshots/cliptest_f01.png"));
@@ -157,8 +151,7 @@ TEST_CASE("Loading and running carts") {
         SUBCASE("No error reported"){
             CHECK(vm->GetBiosError() == "");
         }
-        SUBCASE("sceen matches screenshot")
-        {
+        SUBCASE("sceen matches screenshot"){
             vm->UpdateAndDraw(0, 0);

             CHECK(verifyScreenshot(vm, "carts/screenshots/memorytest_f01.png"));
@@ -172,8 +165,7 @@ TEST_CASE("Loading and running carts") {
         SUBCASE("No error reported"){
             CHECK(vm->GetBiosError() == "");
         }
-        SUBCASE("sceen matches screenshot")
-        {
+        SUBCASE("sceen matches screenshot"){
             vm->UpdateAndDraw(0, 0);

             CHECK(verifyScreenshot(vm, "carts/screenshots/cartdatatest_f01.png"));
@@ -181,6 +173,106 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("tonum test cart"){
+        vm->LoadCart("carts/tonumtest2.p8");
+
+        SUBCASE("can parse positive int"){
+            vm->UpdateAndDraw(0, 0);
+
+            bool parsedCorrectly = vm->ExecuteLua(
+                "function r1test()\n"
+                " return r1 == 12345\n"
+                "end\n",
+                "r1test");
+
+            CHECK(parsedCorrectly);
+        }
+        SUBCASE("can parse negative decimal") {
+            vm->UpdateAndDraw(0, 0);
+
+            bool parsedCorrectly = vm->ExecuteLua(
+                "function r2test()\n"
+                " return r2 == -12345.67\n"
+                "end\n",
+                "r2test");
+
+            CHECK(parsedCorrectly);
+        }
+        SUBCASE("can parse hex") {
+            vm->UpdateAndDraw(0, 0);
+
+            bool parsedCorrectly = vm->ExecuteLua(
+                "function r3test()\n"
+                " return r3 == 15\n"
+                "end\n",
+                "r3test");
+
+            CHECK(parsedCorrectly);
+        }
+
+       SUBCASE("can parse binary literal") {
+            vm->UpdateAndDraw(0, 0);
+
+            bool parsedCorrectly = vm->ExecuteLua(
+                "function r5test()\n"
+                " return r5 == 9\n"
+                "end\n",
+                "r5test");
+
+            CHECK(parsedCorrectly);
+        }
+        SUBCASE("can parse large positive number") {
+            vm->UpdateAndDraw(0, 0);
+
+            bool parsedCorrectly = vm->ExecuteLua(
+                "function r6test()\n"
+                " return r6 == 32767\n"
+                "end\n",
+                "r6test");
+
+            CHECK(parsedCorrectly);
+        }
+        SUBCASE("unparseable string returns nil") {
+            vm->UpdateAndDraw(0, 0);
+
+            bool parsedCorrectly = vm->ExecuteLua(
+                "function r8test()\n"
+                " return r8 == nil\n"
+                "end\n",
+                "r8test");
+
+            CHECK(parsedCorrectly);
+        }
+
+        //currently failing
+        SUBCASE("can parse hex with decimal") {
+            vm->UpdateAndDraw(0, 0);
+
+            bool parsedCorrectly = vm->ExecuteLua(
+                "function r4test()\n"
+                " return r4 == 15.6709\n"
+                "end\n",
+                "r4test");
+
+
+            CHECK(parsedCorrectly);
+        }
+        /*
+        SUBCASE("too large int overflows") {
+            vm->UpdateAndDraw(0, 0);
+
+            bool parsedCorrectly = vm->ExecuteLua(
+                "function r7test()\n"
+                " return r7 == -32768\n"
+                "end\n",
+                "r7test");
+
+            CHECK(parsedCorrectly);
+        }
+        */
+
+        vm->CloseCart();
+    }

     delete vm;
 }
\ No newline at end of file