commit 3d9d7cfdca7b4caac7f5b2409f3fef55b60f1c1c
Author: jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Fri Sep 11 15:37:03 2020 -0600
Commit: jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Sep 15 17:09:08 2020 -0600
add support for shorthand while
---
source/cartPatcher.cpp | 4 +++-
test/cartpatchertests.cpp | 9 +++++++--
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/source/cartPatcher.cpp b/source/cartPatcher.cpp
index 2365a83..3067304 100644
--- a/source/cartPatcher.cpp
+++ b/source/cartPatcher.cpp
@@ -34,6 +34,8 @@ const char * patchLuaFunction = R"#(
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 >>> ")
@@ -43,7 +45,7 @@ const char * patchLuaFunction = R"#(
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
diff --git a/test/cartpatchertests.cpp b/test/cartpatchertests.cpp
index a86b87f..3ffd5f5 100644
--- a/test/cartpatchertests.cpp
+++ b/test/cartpatchertests.cpp
@@ -33,7 +33,7 @@ TEST_CASE("Cart Patcher/Preprocessor tests") {
CHECK(patched == "--this is a comment");
}
- SUBCASE("shorthand if statment converted"){
+ 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");
@@ -42,10 +42,15 @@ TEST_CASE("Cart Patcher/Preprocessor tests") {
CHECK(patched == "if thingIsTrue() then doADifferentThing() end\n");
}
- SUBCASE("shorthand if else statment converted"){
+ 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");
+ }
}