a short lua overview

14
Lua A Short Lua Overview Albrecht Zimmermann 20th April 2005 Albrecht Zimmermann A Short Lua Overview

Upload: mikhail-miguel

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 1/14

Lua

A Short Lua Overview

Albrecht Zimmermann

20th April 2005

Albrecht Zimmermann A Short Lua Overview

Page 2: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 2/14

Lua

The Language

Programming Constructs

C Interaction

What is Lua

Lightweight script language

Extension to existing program in C to configure program,modify run-time behaviour

Embedded in host program, not designed to be used

stand-alone

Dynamically typed

Albrecht Zimmermann A Short Lua Overview

Page 3: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 3/14

Lua

The Language

Programming Constructs

C Interaction

Syntactic Conventions

Identifiers consist of digits, letters, underscore - cannot

begin with digits

21 keywords, which are reserved, cannot be used as

identifiers (the usual: and, end, for  etc.)

Case-sensitive: and  is keyword, And  identifier

Identifiers starting with an underscore reserved for internal

Lua variables

Albrecht Zimmermann A Short Lua Overview

Page 4: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 4/14

Lua

The Language

Programming Constructs

C Interaction

Types and Typing

8 basic types:

nil - type of the value nil which is different from any other

valueboolean - has values true, false 

number - double-precision floating pointstring - array of charactersfunction - first-class value, means that functions can bestored in variables, passed as argument, returneduserdata - arbitrary C data, basically raw memory content,can be manipulated only through C APIthread - independent threads of executiontable - associative arrays, i.e. index does not have to benumber, only structure, index can be used astable.index, can be nested

Albrecht Zimmermann A Short Lua Overview

Page 5: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 5/14

Lua

The Language

Programming Constructs

C Interaction

Types and Typing cont.

Dynamic typing: type of variable depends on stored value

Re-assignment of different value changes type

Assignment with =

Multiple assignments possible

Albrecht Zimmermann A Short Lua Overview

Page 6: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 6/14

Lua

The Language

Programming Constructs

C Interaction

Types and Typing - Examples

a = 1 - type number

a = "hello" - type string

a = { item1="abc" } - type table, a[item1]=a.item1=abc

x, y = 2, "there" - x=2, y="there"

Expressions on the left and values on the right areevaluated before assignment

String concatenation a = "hello".." world" - a = "hello world"

x = function() print("hello") end - calling x() would now print

hello

Value nil means variable has no value and thus does not

exist, checking whether a variable has the value nil  is

essentially existence check

Albrecht Zimmermann A Short Lua Overview

Th L

Page 7: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 7/14

Lua

The Language

Programming Constructs

C Interaction

Chunks and Blocks

Chunk - list of statements, each optionally followed by a

semicolon

Sequentially executed, can be stored in files or as string in

host program, have local scope, can define variables

Block - Chunk, which may be delimited by do and end

Helps to control variable scope

Albrecht Zimmermann A Short Lua Overview

The Language

Page 8: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 8/14

Lua

The Language

Programming Constructs

C Interaction

Control Structures

for variable = start_val, stop_val [, step_size] do block end

for variable [, variable] in expression-list do block end

e.g. for table traversal:

for key,value in table do print(key,value) end

while expression do block end

repeat block until expression

break or return can be used to end loop

Have to be last statement in block (followed by end), so if

break somewhere in the middle wanted, explicit blockdo break end

if expression then block {elsif expression then block}

[else block] end

Albrecht Zimmermann A Short Lua Overview

The Language

Page 9: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 9/14

Lua

The Language

Programming Constructs

C Interaction

Functions

Definition:

Name = function ([parameterlist]) block end

function Name ([parameterlist]) block endlocal function Name ([parameterlist]) block end - this dies

once the block is exited

Call:

function foo(a,b,c) print(a,b,c) end - examplefoo() - will print nil nil nil 

foo(1,2,3,4) - will print 123function foo(...) block end - variable list of parameters isput into table arg , table traversal methods used to accessentries

Albrecht Zimmermann A Short Lua Overview

The Language

Page 10: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 10/14

Lua

The Language

Programming Constructs

C Interaction

Functions - cont.

Return values:

foo() - no return valuex = foo() - adjusted to one return valuex, y = foo() - adjusted to two values

{foo()} - creates table of all returned valuesInside function: return [value] [, value]For variable return list: return unpack(value-array)

Albrecht Zimmermann A Short Lua Overview

The Language

Page 11: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 11/14

Lua

The Language

Programming Constructs

C Interaction

Libraries

Lua includes libraries for

Basic functions

String manipulation

Table manipulation

Mathematical functions

Input/Output methods

Operating system facilitiesDebugging facilities

Albrecht Zimmermann A Short Lua Overview

The Language

Page 12: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 12/14

Lua

The Language

Programming Constructs

C Interaction

C API

Declared in lua.h

Whole state of a Lua interpreter (global variables, stack)

stored in structure of type lua_State, has to be passed

to each function in the library, except for lua_open, which

returns such a pointer

Create: lua_State *lua_open(void);, Delete: void

lua_close(lua_State *L);

Virtual stack used for communication with host program,

each entry a Lua value (nil, number,...)

Not real stack, reference to any stack entry possible

Lua chunks loaded with int lua_load

Calling functions: first push function on stack, then

arguments in direct order, finally call void lua_call

Albrecht Zimmermann A Short Lua Overview

The Language

Page 13: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 13/14

Lua

g g

Programming Constructs

C Interaction

C API cont.

Extending Lua with C functions:

Must be of type lua_CFunction:

typedef int (*lua_CFunction) (lua_State *L);

Arguments are passed from Lua to the function in direct

order on the stack

Results are pushed by the function on the stack in direct

order, function returns number of results

For registering there is a macro

Albrecht Zimmermann A Short Lua Overview

The Language

Page 14: A Short Lua Overview

8/3/2019 A Short Lua Overview

http://slidepdf.com/reader/full/a-short-lua-overview 14/14

Lua

g g

Programming Constructs

C Interaction

In-Depth Information

Lua 5.0 reference:

http://www.lua.org/manual/5.0/manual.html

Lua Wiki including tutorials:

http://lua-users.org/wiki/

Albrecht Zimmermann A Short Lua Overview