Professional Documents
Culture Documents
Programming in Lua
Part I. The Language
Chapter 1. Getting Started
1.1 – Chunks
Each piece of code that Lua executes,
such as a file or a single line in
interactive mode,
is a chunk.
More specifically, a chunk is simply a sequence of
statements.
a = 1
b = a*2
a = 1;
b = a*2;
a = 1 ; b = a*2
>
Thereafter,
each command that you type (such as print "Hello World")
executes immediately after you press <enter>.
To exit the interactive mode
and the interpreter,
just type end-of-file
(ctrl-D in Unix, ctrl-Z in
DOS/Windows),
or call the exit function,
from the Operating System library
(you have to type os.exit()<enter>).
In interactive mode, Lua usually interprets each line that you type
as a
complete chunk.
However, if it detects that the line cannot form a complete
chunk,
it waits for more input,
until it has a complete chunk.
When Lua is
waiting for a line continuation,
it shows a different prompt (typically >>).
Therefore, you can enter a multi-line definition,
such as the factorial
function,
directly in interactive mode.
Sometimes, however,
it is more
convenient to put such definitions in a file,
and then call Lua to run that file.
-- file 'lib1.lua'
return math.sqrt(n2)
end
return 2*x
end