commit bf8b1f1049a342f0f859f16ae6e1b383cd8aaaee
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Mar 20 06:46:45 2021 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Mar 20 06:46:45 2021 -0600
removed unused cart patcher
---
source/cartPatcher.cpp | 98 -----------------------------------------------
source/cartPatcher.h | 3 --
test/Makefile | 9 +----
test/cartpatchertests.cpp | 65 -------------------------------
4 files changed, 2 insertions(+), 173 deletions(-)
diff --git a/source/cartPatcher.cpp b/source/cartPatcher.cpp
deleted file mode 100644
index ca7c3d8..0000000
--- a/source/cartPatcher.cpp
+++ /dev/null
@@ -1,98 +0,0 @@
-#include <string>
-
-#include "cartPatcher.h"
-
-//extern "C" {
- #include <lua.h>
- #include <lualib.h>
- #include <lauxlib.h>
-//}
-
-//from https://github.com/benwiley4000/pico8-to-lua, which was in turn adapted from LovePotion:
-//https://github.com/gamax92/picolove/blob/master/cart.lua (zlib license)
-//it should be possible to do this kind of pattern matching with another library, or use z8Lua
-//but for now I have lua 5.3 working so I'm going with this since I know it works
-const char * patchLuaFunction = R"#(
-
- function patch_lua(lua)
- -- patch lua code
- lua = lua:gsub("!=","~=")
- lua = lua:gsub("//","--")
- -- rewrite shorthand if statements eg. if (not b) i=1 j=2
- lua = lua:gsub("if%s*(%b())%s*([^\n]*)\n",function(a,b)
- local nl = a:find('\n',nil,true)
- local th = b:find('%f[%w]then%f[%W]')
- local an = b:find('%f[%w]and%f[%W]')
- local o = b:find('%f[%w]or%f[%W]')
- local ce = b:find('--',nil,true)
- if not (nl or th or an or o) then
- if ce then
- local c,t = b:match("(.-)(%s-%-%-.*)")
- return "if "..a:sub(2,-2).." then "..c.." end"..t.."\n"
- else
- return "if "..a:sub(2,-2).." then "..b.." end\n"
- end
- end
- end)
- -- rewrite shorthand while statements
- lua = lua:gsub("while%s*(%b())%s*([^\n]*)\n", "while %1 do %2 end\n")
- -- rewrite assignment operators (longest operators first)
- lua = lua:gsub("(%S+)%s*>>>=","%1 = %1 >>> ")
-
- lua = lua:gsub("(%S+)%s*%.%.=","%1 = %1 .. ")
- lua = lua:gsub("(%S+)%s*^^=","%1 = %1 ^^ ")
- lua = lua:gsub("(%S+)%s*<<=","%1 = %1 << ")
- lua = lua:gsub("(%S+)%s*>>=","%1 = %1 >> ")
-
- lua = lua:gsub("(%S+)%s*([%+-%*/%%\\^|&])=","%1 = %1 %2 ")
-
- -- rewrite inspect operator "?"
- lua = lua:gsub("^(%s*)?([^\n\r]*)","%1print(%2)")
- -- convert binary literals to hex literals
- lua = lua:gsub("([^%w_])0[bB]([01.]+)", function(a, b)
- local p1, p2 = b, ""
- if b:find(".", nil, true) then
- p1, p2 = b:match("(.-)%.(.*)")
- end
- -- pad to 4 characters
- p2 = p2 .. string.rep("0", 3 - ((#p2 - 1) % 4))
- p1, p2 = tonumber(p1, 2), tonumber(p2, 2)
- if p1 then
- if p2 then
- return string.format("%s0x%x.%x", a, p1, p2)
- else
- return string.format("%s0x%x", a, p1)
- end
- end
- end)
-
- return lua
- end
-)#";
-
-
-const char* getPatchedLua(const char* unpatchedLua){
-
- lua_State* L;
-
- L = luaL_newstate();
- luaL_openlibs(L);
-
- luaL_dostring(L, patchLuaFunction);
-
- lua_getglobal(L, "patch_lua");
-
- lua_pushstring(L, unpatchedLua);
-
- lua_pcall(L, 1, 1, 0);
-
- const char* result = lua_tostring(L, -1);
-
- lua_pop(L,1);
-
- lua_close(L);
-
- return result;
-
-}
-
diff --git a/source/cartPatcher.h b/source/cartPatcher.h
deleted file mode 100644
index 40f5389..0000000
--- a/source/cartPatcher.h
+++ /dev/null
@@ -1,3 +0,0 @@
-#pragma once
-
-const char* getPatchedLua(const char* unpatchedLua);
diff --git a/test/Makefile b/test/Makefile
index c657846..9fe4053 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -7,14 +7,13 @@ all: runtests
testrunner.a: testroot.o \
- fontdatatests.o nibblehelperstests.o graphicstests.o audiotests.o cartpatchertests.o \
+ fontdatatests.o nibblehelperstests.o graphicstests.o audiotests.o \
carttests.o endtoendtests.o vmtests.o
g++ -I$(CURDIR)/include -L$(CURDIR)/lib \
testroot.o \
fontdata.o fontdatatests.o \
graphics.o graphicstests.o stringToDataHelpers.o mathhelpers.o \
Audio.o synth.o audiotests.o \
- cartPatcher.o cartpatchertests.o \
cart.o carttests.o nooplogger.o lodepng.o emojiconversion.o filehelpers.o utf8-util.o \
vm.o endtoendtests.o picoluaapi.o Input.o \
vmtests.o stubhost.o \
@@ -46,10 +45,6 @@ audiotests.o: audiotests.cpp ../source/Audio.cpp ../source/Audio.h \
../source/mathhelpers.h ../source/mathhelpers.cpp \
../source/PicoRam.h ../source/hostVmShared.h
g++ $(CFLAGS) audiotests.cpp ../source/Audio.cpp ../source/synth.cpp ../source/mathhelpers.cpp
-
-cartpatchertests.o: liblua.a cartpatchertests.cpp ../source/cartPatcher.cpp ../source/cartPatcher.h
- g++ $(CFLAGS) -I$(CURDIR)/include -L$(CURDIR)/lib \
- cartpatchertests.cpp ../source/cartPatcher.cpp
liblua.a:
@echo 'starting lua build\n' &&\
@@ -76,7 +71,7 @@ carttests.o: carttests.cpp ../source/cart.cpp ../source/cart.h \
nooplogger.cpp ../source/emojiconversion.cpp ../source/filehelpers.cpp \
../libs/utf8-util/utf8-util.cpp
-endtoendtests.o: cartpatchertests.o carttests.o graphicstests.o audiotests.o \
+endtoendtests.o: carttests.o graphicstests.o audiotests.o \
endtoendtests.cpp \
../source/vm.cpp ../source/vm.h \
stubhost.cpp stubhost.h ../source/host.h \
diff --git a/test/cartpatchertests.cpp b/test/cartpatchertests.cpp
deleted file mode 100644
index 82287ab..0000000
--- a/test/cartpatchertests.cpp
+++ /dev/null
@@ -1,65 +0,0 @@
-#include <string>
-
-#include "doctest.h"
-#include "../source/cartPatcher.h"
-
-TEST_CASE("Cart Patcher/Preprocessor tests") {
-
- SUBCASE("patching lua handles empty string"){
- std::string patched = getPatchedLua("");
-
- CHECK_EQ(patched.length(), 0);
- }
- SUBCASE("assignment operators expanded"){
- std::string operators[] = {
- "+", "-", "*", "/", "\\", "%", "^", "..",
- "|", "&", "^^", "<<", ">>", ">>>"
- };
-
- for (int i = 0; i < 14; i++) {
- std::string unpatched = "foo" + operators[i] + "=bar";
- std::string patched = getPatchedLua(unpatched.c_str());
-
- CHECK(patched == "foo = foo " + operators[i] + " bar");
- };
- }
- SUBCASE("!= converted"){
- std::string patched = getPatchedLua("thing != other");
-
- CHECK(patched == "thing ~= other");
- }
- SUBCASE("// c style comment converted"){
- std::string patched = getPatchedLua("//this is a comment");
-
- CHECK(patched == "--this is a comment");
- }
- SUBCASE("shorthand if statement converted"){
- std::string patched = getPatchedLua("if (pl.drilling>0) pl.drilling-=1\n");
-
- CHECK(patched == "if pl.drilling>0 then pl.drilling = pl.drilling - 1 end\n");
-
- patched = getPatchedLua("if (thingIsTrue()) doADifferentThing()\n");
-
- CHECK(patched == "if thingIsTrue() then doADifferentThing() end\n");
- }
- SUBCASE("shorthand if else statement converted"){
- std::string patched = getPatchedLua("if (cond2) print(\"cond2\") else print(\"not cond2\")\n");
-
- CHECK(patched == "if cond2 then print(\"cond2\") else print(\"not cond2\") end\n");
- }
- SUBCASE("shorthand while statement converted"){
- std::string patched = getPatchedLua("x=0 while(x<5) print(x) x+=1\n");
-
- CHECK(patched == "x=0 while (x<5) do print(x) x = x + 1 end\n");
- }
- SUBCASE("shorthand print statement converted"){
- std::string patched = getPatchedLua("?x\n");
-
- CHECK(patched == "print(x)\n");
- }
- SUBCASE("shorthand print statement with args converted"){
- std::string patched = getPatchedLua("?thing, 65, 32,c\n");
-
- CHECK(patched == "print(thing, 65, 32,c)\n");
- }
-}