From 91f72e6d17181951af7307ea30d8d7e548c0913c Mon Sep 17 00:00:00 2001 From: Reuh Date: Thu, 31 Aug 2017 19:40:12 +0200 Subject: [PATCH] Updated README and tests --- README.md | 24 +++++++++++-- test/test.lua | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1d81568..14d76e6 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,12 @@ local list = [ -- table comprehension (kind of) if i%2 == 0 then continue -- continue keyword end - i + i -- implicit push end ] +local count = [for i=1,10 i] -- single line statements + local a = if condition then "one" else "two" end -- statement as expressions ```` @@ -86,6 +88,8 @@ All theses operators can also be put right of the assigment operator, in which c If you feel like writing hard to understand code, right and left operator can be used at the same time. +**Please note** that the Lua code `a=-1` will be compiled into `a = 1 - a` and not `a = -1`! Write good code, write spaced code: `a = -1` works as expected. + ##### Default function parameters ```lua function foo(bar = "default", other = thing.do()) @@ -170,7 +174,7 @@ Add one or more value to the returned value list. If you use a `return` afterwar This keyword is mainly useful when used through implicit `push` with table comprehension and statement expressions. -**Please note** that, in order to stay compatible with vanilla Lua syntax, any `push` immediatly followed by a `"string expression"`, `{table expression}` or `(paranthesis)` will be interpreted as a function call. Preferably use implicit `push` in these cases. +**Please note** that, in order to stay compatible with vanilla Lua syntax, any `push` immediatly followed by a `"string expression"`, `{table expression}` or `(paranthesis)` will be interpreted as a function call. Preferably use implicit `push` when you can. ##### Implicit `push` ```lua @@ -232,6 +236,22 @@ Values returned by the function will be inserted in the generated table in the o The table generation function also have access to the `self` (or its alias `@`) variable, which is the table which is being created, so you can set arbitrary fields of the table. +##### One line statements +```lua +if condition() + a() +elseif foo() + b() + +if other() + a() +else -- "end" is always needed for else! + c() +end +``` + +`if`, `elseif`, `for`, and `while` statements can be writtent without `do`, `then` or `end`, in which case they contain a single statement. + ### Preprocessor Before compiling, Candran's preprocessor is run. It execute every line starting with a _#_ (ignoring whitespace) as Candran code. For example, diff --git a/test/test.lua b/test/test.lua index 10c81fa..215b850 100644 --- a/test/test.lua +++ b/test/test.lua @@ -290,6 +290,70 @@ local a = :(arg1) end return a(2, 3) ]], 5) +test("short anonymous method parsing edge cases", [[ +-- Taken from the file I used when solving this horror, too tired to make separate tests. +x = "" +function a(s) + x = x .. tostring(s or "+") +end +k=true +while k do + k=false + cap = {[0] = op, a} + a(tostring(h)) + if true then + a() + if false then + a = x, (a) + c() + end + a() + end + a() +end +a() +a("l") +let h = (h) + a("h") +end +h() +a("lol") +if false then exit() end +a("pmo") +if true then + if false + a = (h) + + a() + a("pom") +end +a("lo") +a("kol") +if false then + j() + p() +end +do + b = [ + k = () end + if false + k = (lol) + error("niet") + end + + k() + a()] +end +if a() then h() end +local function f (...) + if select('#', ...) == 1 then + return (...) + else + return "***" + end +end +return f(x) +]], "nil++++lhlolpmo+pomlokol++") -- let variable declaration test("let variable declaration", [[ @@ -406,6 +470,36 @@ t2 = {"foo", "bar"} return table.concat([push unpack(t1); push unpack(t2)]) ]], "heyhopfoobar") +-- one line statements +test("one line if", [[ +a = 5 +if false + a = 0 +return a +]], 5) +test("one line if-elseif", [[ +a = 5 +if false + a = 0 +elseif true + a = 3 +elseif false + a = -1 +return a +]], 3) +test("one line for", [[ +a = 0 +for i=1,5 + a = a + 1 +return a +]], 5) +test("one line while", [[ +a = 0 +while a < 5 + a = a + 1 +return a +]], 5) + -- results local resultCounter = {} local testCounter = 0