??Lua - PowerPoint PPT Presentation

About This Presentation
Title:

??Lua

Description:

Lua Lua Lua 1994 ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 27
Provided by: 209859
Category:
Tags: lua

less

Transcript and Presenter's Notes

Title: ??Lua


1
??Lua
  • ????????????

2
???Lua
  • Lua?????????,?1994?,??????????????????????,Lua??
    ????????? ???
  • ???????? PHP?Perl?JavaScript ???,Lua?????????
    ????,Lua????????????????? ???? ??
  • Lua????????
  • ??? ?????? ?????????????
  • ??? ?API?????????? ????

3
????????
  • Lua??????????,?????????????Lua??????????????????,?
    ??? Java?Python ??????????(?????????????),?????,??
    ???????
  • Lua??????,??????,?? ???????(???????
    )???????,???????????????????,???????????
  • Lua????????C/C,Java,.NET,???????PHP,Ruby??

4
Lua???????
  • Lua??????,???????????
  • ?????????,Lua??????

????
Lua
????
Lua
5
Lua??????????
  • Lua????????,????????? ???????????,???????????
    ?????????????

6
????
  • 1.??????Lua??????
  • 2.????????Lua??(??),????,???Lua????,?????
  • 3.??Lua??????????Lua??(?????????????????)?
  • 4.?????Lua???

7
Lua??????????
  • ?????????,?Lua???????????????
  • ???????????Lua??????
  • ?????????????Lua????
  • Lua???????????

8
???????demo1.c
  • include ltlua.hgt //Lua?????
  • include ltlualib.hgt //Lua???
  • include ltlauxlib.hgt //Lua????
  • char code "for i0, 5 do print(\'Hello,
    world!\') end"
  • int main()
  • lua_State s luaL_newstate() //???????
  • luaL_openlibs(s) //??Lua???
  • luaL_dostring(s, code) //??????????
  • lua_close(s) //?????
  • return 0

9
?????
  • ??gcc o demo1 demo1.c llua
  • ??./demo1
  • ????
  • Hello, world!
  • Hello, wordl!
  • Hello, world!
  • Hello, world!
  • Hello, world!
  • ??Lua????????,???API,????????Lua???,??????????????

10
??????
  • ???????Lua?????,?????Lua???????????????
  • ????????????,Lua?????????,??????????????,??????Lua
    API?????,?Lua?????????????
  • ????????????????Lua??,???Lua????????,????SQL????,
    ?????????????

11
???????
  • ??f (a, b, c)

c
b
??lua_call(s, 3, 1)?
a
f
f(a, b, c)


?????? ????????
?????????? ??????
12
???????
  • Lua??????????,Lua API????????,????????,?????????,?
    ???????????,??????????????
  • ??????????Lua?????,????????????,???????,??????????
    ??????lua_register???Lua???(????????Lua????????),?
    ???Lua??????
  • ????????Lua????,????????,????lua_call,???Lua??????

13
Lua??????
  • ?Lua???????N???,???? 1 N ???????,???? -1 -N
    ???????,?????????
  • ???????????????Lua????,??????????,?????????????

-1
lt????gt
6
-2
lt??gt
5
-3
1, "ab", nil
4
-4
"hello"
3
-5
false
2
-6
123
1
14
?????????demo2.c
  • include ltlua.hgt
  • include ltlualib.hgt
  • include ltlauxlib.hgt
  • int divide(lua_State s) //?Lua?????????
  • double a lua_tonumber(s, -2) //???????
  • double b lua_tonumber(s, -1) //???????
  • int quot (int)a / (int)b
  • int rem (int)a (int)b
  • lua_pushnumber(s, quot) //?????????
  • lua_pushnumber(s, rem) //?????????
  • return 2 //????????

15
?demo2.c?demo2.lua
  • int main()
  • lua_State s luaL_newstate()
  • luaL_openlibs(s)
  • lua_register(s, "div, divide) //????????
  • luaL_dofile(s, "demo2.lua") //?????????
  • lua_close(s)
  • return 0
  • demo2.lua
  • a 13
  • b 5
  • q, r div(a, b) //????
  • print(q, r)

16
???????
  • ??
  • 2 3
  • ?????,??Lua??????????????int f(lua_State
    s),??????????,????????????????????Lua???????,???
    ???????????,Lua????????????,??????????
  • ??,?Lua?????????????,??Lua???????,???????????????

17
????????demo3.c
  • include ltlua.hgt
  • include ltlualib.hgt
  • include ltlauxlib.hgt
  • int main()
  • lua_State s luaL_newstate()
  • luaL_openlibs(s)
  • luaL_dofile(s, "demo3.lua")
  • lua_getglobal(s, "show") //?Lua??????
  • lua_pushstring(s, "It is from C") //??????
  • lua_call(s, 1, 1) //??Lua??

18
?demo3.c?demo3.lua
  • const char result lua_tostring(s,
    -1) //????????
  • printf("C has got s\n", result)
  • lua_pop(1) //?????
  • lua_close(s)
  • return 0
  • demo2.lua
  • show function(m)
  • print('Lua has got ' .. m)
  • return 'It is from Lua'
  • end

19
???????
  • ??
  • Lua has got It is from C
  • C has got It is from Lua
  • ??,???????????????Lua?????,?Lua???????????????????
    ,???Lua??????????????,????????????Lua????

20
Lua????
  • ????,???????,?????????????????????
  • ???????,??????,?????????,???????
  • ??????????????????
  • ???????????,??????????
  • ???????????????

21
????/????
  • ??a 3 x, y, z 12, 'Hello', true
  • ????
  • ??? nil nil
  • ?? number 123 3.14159 1.6e-9
  • ?? - / (??) -(?)
  • ?? boolean true false
  • ??or and not
  • ??? string 'www.feedsky.com' "?????"
  • ??..(??) (??)
  • ???????
  • gt lt gt lt

22
?
  • Lua???(table)?????????????
  • t 1234, nil, 'something', true, 'nested',
    1.414
  • table??????,???-?????,?????,?????1?????????????
  • rec name ????', favorite '????',
    32 true
  • ??????
  • rec.name rec'favorite' rec8-3
  • ?????,??????????????????????????

23
??
  • ?????Lua?????,???????,???????????,??????????????
  • r2p function(x, y)
  • abs math.sqrt((x x) (y y))
  • arg math.atan2(x, y)
  • return abs, arg
  • end
  • ????
  • rho, theta r2p(3, 4)
  • ????????,???????????

24
???????
  • ??(coroutine)???????????????,???CPU??????(??)?????
  • ??????(yield)?I/O???????
  • ??????(yield)?I/O(?????)??
  • ??,?????????????????,??????????,????????,???????
    ????????
  • ??????????,?????????????????????????????????,???
    ??????????????????????,???OS??????
  • ???????????????????,?????????????,??????????

25
??
  • Lua????????????????????,????????????
  • ??????Lua,?????????????????????
  • ???????????,???????,Lua?????????????????

26
http//lych.yo2.cn?????
  • ??,???????blog
Write a Comment
User Comments (0)
About PowerShow.com