1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 17:59:30 +00:00

Updated README and tests

This commit is contained in:
Étienne Fildadut 2017-08-31 19:40:12 +02:00
parent 70d3aba121
commit 91f72e6d17
2 changed files with 116 additions and 2 deletions

View file

@ -38,10 +38,12 @@ local list = [ -- table comprehension (kind of)
if i%2 == 0 then if i%2 == 0 then
continue -- continue keyword continue -- continue keyword
end end
i i -- implicit push
end end
] ]
local count = [for i=1,10 i] -- single line statements
local a = if condition then "one" else "two" end -- statement as expressions 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. 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 ##### Default function parameters
```lua ```lua
function foo(bar = "default", other = thing.do()) 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. 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` ##### Implicit `push`
```lua ```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. 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 ### Preprocessor
Before compiling, Candran's preprocessor is run. It execute every line starting with a _#_ (ignoring whitespace) as Candran code. Before compiling, Candran's preprocessor is run. It execute every line starting with a _#_ (ignoring whitespace) as Candran code.
For example, For example,

View file

@ -290,6 +290,70 @@ local a = :(arg1)
end end
return a(2, 3) return a(2, 3)
]], 5) ]], 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 -- let variable declaration
test("let variable declaration", [[ test("let variable declaration", [[
@ -406,6 +470,36 @@ t2 = {"foo", "bar"}
return table.concat([push unpack(t1); push unpack(t2)]) return table.concat([push unpack(t1); push unpack(t2)])
]], "heyhopfoobar") ]], "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 -- results
local resultCounter = {} local resultCounter = {}
local testCounter = 0 local testCounter = 0