-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalled_from_c.lua
More file actions
53 lines (43 loc) · 1.37 KB
/
called_from_c.lua
File metadata and controls
53 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- comments in lua start with '--'
-- use lua 5.1 for running this code, install with apt-get install lua5.1
-- later versions of lua don't have as many third party libraries available
-- simple for loop
start = 1
stop = 11
step = 2
for variable = start, stop, step do
if variable == 5 then
print ("middle")
else
print (variable)
end
end
print "---simple loop end---"
-- upvalue example
function counter (n)
local i = n
return function ()
i = i + 1
return i
end
end
-- here we initialize the counter functions, note that they are separate
-- and retain their internal values after each run
firstcounter = counter(1)
secondcounter = counter(22)
print ( firstcounter() )
print ( firstcounter() )
print ( secondcounter() )
print ( secondcounter() )
print ( firstcounter() )
-- c or c++ can exchange information through stack and initialize variables for us
-- the following statement assigns "no value from c" to 'passedvalue' variable only
-- if it doesn't exist
print ("the following value from c program:")
passedvalue = passedvalue or "no value from c"
print ( passedvalue )
-- a nice cheatsheet can be found at http://lua-users.org/wiki/LuaShortReference
-- also ever trusty http://www.lua.org/manual/5.1/
-- and http://lua-users.org/wiki/TutorialDirectory
-- and http://lua-users.org/wiki/SampleCode
-- and http://pgl.yoyo.org/luai/i/_