Presentation is loading. Please wait.

Presentation is loading. Please wait.

بسم الله الرحمن الرحیم.

Similar presentations


Presentation on theme: "بسم الله الرحمن الرحیم."— Presentation transcript:

1 بسم الله الرحمن الرحیم

2 Lua Real embeddable Script Language

3 History Lua was created in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, members of the Computer Graphics Technology Group (Tecgraf) at the Pontifical Catholic University of Rio de Janeiro, in Brazil.

4 What is Lua dynamic language scripting language Embedded
in C/C++, Java, Fortran, C#,Perl, Ruby, Ada, etc. portability simplicity small size

5 Like basic Types: "nil" "boolean" "number""string" "table" "function" "thread" “userdata” Comment --one Line comment --[[ Multi-line comments]] multiple assignments a,b=10, Operators * / % = # and or not == ~= >= <= < > () []

6 function name ( args ) body [return values] end
do block end if exp then block {elseif exp then block} [else block] end while exp do block end repeat block until exp for var = start, end [, step] do block end for vars in iterator do block end Break local a=12 Local a=function (args) body [return values] end

7 boolean b=true b=false number I=12.3 table t = {} t = {"yes", "no", "?"} t = {x=5, y=10} t = {x=5, y=10; "yes", "no"} t[“x”]=”rahim”; t.x=”firouzi” string s=”rahim” s='firouzi' S= [=[ multi line string ]=]

8 OOP Use table for oop Example obj→func(p1,p2) ====> Obj:func(p1,p2) == obj[“func”](obj,p1,p2) Use table for module io.print(p1) == io[“print”](p1) OOP example local Vector = {} Vector.__index = Vector function Vector:new(x, y, z) The constructor return setmetatable({x = x, y = y, z = z}, Vector) end function Vector:magnitude() Another method return math.sqrt(self.x^2 + self.y^2 + self.z^2)

9 Useful function type(var) print( … ) ipairs (t) pairs (t) _G _VERSION

10 lib string string.len (s) string.sub (s, i [, j]) string.upper (s)
string.lower (s) string.format (s [, args]) IO io.read (formats) io.write (values) Math math.sqrt (x) math.sin (a) math.cos (a)

11 lib file io.open (fn [, m]) file:close () file:read (formats)
file:lines () file:write (values) file:flush () OS os.execute (cmd) os.exit ([code]) os.time ([tt]) os.date ([fmt [, t]])

12 How to run lua program Interpretor Lua sudo apt-get install luaX.X lua
Embedded in other programming Use lib

13 Embedded An embedded style language is a kind of computer language whose commands appear intermixed with those of a base language. Lua example:adobe lightroom Python example:Gimp ,Blander Scheme example:gdb,gEDA JavaScript → ECMAScript example: web browsers TCL example:NS-2

14 Embedded Use stack to communication by lua Make state
Add c function in state Run lua script for preset Call lua function by c

15 embedded Main program Lua program Function link Call function return

16 C example #include <stdio.h>
#include <lua.h> //Lua main library (lua_*) #include <lauxlib.h> //Lua auxiliary library (luaL_*) int main(void) { //create a Lua state lua_State *L = luaL_newstate(); //load and execute a string if (luaL_dostring(L, "function foo (x,y) return x+y end")) { lua_close(L); return -1; } //push value of global "foo" (the function defined above) //to the stack, followed by integers 5 and 3 lua_getglobal(L, "foo"); lua_pushinteger(L, 5); lua_pushinteger(L, 3); lua_call(L, 2, 1); //call a function with two arguments and one return value printf("Result: %d\n", lua_tointeger(L, -1)); //print integer value of item at stack top lua_close(L); //close Lua state return 0; wikipedia

17 Add function static int hello (lua_State *L) {
double f = luaL_checknumber(L, 1); lua_pushstring(L, “hello world”); return 1; } lua_pushcfunction(L, hello); lua_setglobal(L, “hello”); ...

18 Try

19 Tanks


Download ppt "بسم الله الرحمن الرحیم."

Similar presentations


Ads by Google