Programming ROBERTO IERUSALIMSCHY in Lua 2 nd edition Lua.org Last update: Wed Jan 13 12:07:33 UTC 2010 Programming in Lua Property of Ian Bloss <[email protected]> Property of Ian Bloss <[email protected]> Programming in Lua Second Edition Roberto Ierusalimschy PUC-Rio, Brazil Lua.org Rio de Janeiro Property of Ian Bloss <[email protected]> Programming in Lua, Second Edition byRobertoIerusalimschy ISBN85-903798-2-5 Copyright(cid:13)c 2006,2003byRobertoIerusalimschy. Allrightsreserved. [email protected]. BookcoverandillustrationsbyDimaquina. LualogodesignbyAlexandreNako. TypesettingbytheauthorusingLATEX. Although the author used his best efforts preparing this book, he assumes no responsibility for errors or omissions, or for any damage that may result from theuseoftheinformationpresentedhere. Allproductnamesmentionedinthis bookaretrademarksoftheirrespectiveowners. CIP(cid:150)BibliotecadoDepartamentodeInforma·tica,PUC-Rio Ierusalimschy,Roberto I22 ProgramminginLua/RobertoIerusalimschy. (cid:150)2nded. (cid:150)RiodeJaneiro,2006. xviii,308p. : 25cm. Includesindex. ISBN85-903798-2-5 1. Lua(Programminglanguage). I.Title. 005.133(cid:150)dc20 Property of Ian Bloss <[email protected]> to Ida, Noemi, and Ana Lucia Property of Ian Bloss <[email protected]> Property of Ian Bloss <[email protected]> Contents Preface xiii I The Language 1 1 GettingStarted 3 1.1 Chunks 4 1.2 SomeLexicalConventions 5 1.3 GlobalVariables 6 1.4 TheStand-AloneInterpreter 7 2 TypesandValues 9 2.1 Nil 10 2.2 Booleans 10 2.3 Numbers 10 2.4 Strings 11 2.5 Tables 13 2.6 Functions 17 2.7 UserdataandThreads 17 3 Expressions 19 3.1 ArithmeticOperators 19 3.2 RelationalOperators 20 3.3 LogicalOperators 21 3.4 Concatenation 22 3.5 Precedence 22 3.6 TableConstructors 22 vii Property of Ian Bloss <[email protected]> viii Contents 4 Statements 27 4.1 Assignment 27 4.2 LocalVariablesandBlocks 28 4.3 ControlStructures 30 4.4 breakandreturn 34 5 Functions 35 5.1 MultipleResults 36 5.2 VariableNumberofArguments 39 5.3 NamedArguments 42 6 MoreAboutFunctions 45 6.1 Closures 47 6.2 Non-GlobalFunctions 50 6.3 ProperTailCalls 52 7 IteratorsandtheGenericfor 55 7.1 IteratorsandClosures 55 7.2 TheSemanticsoftheGenericfor 57 7.3 StatelessIterators 58 7.4 IteratorswithComplexState 60 7.5 TrueIterators 61 8 Compilation,Execution,andErrors 63 8.1 Compilation 63 8.2 CCode 67 8.3 Errors 67 8.4 ErrorHandlingandExceptions 69 8.5 ErrorMessagesandTracebacks 70 9 Coroutines 73 9.1 CoroutineBasics 73 9.2 PipesandFilters 76 9.3 CoroutinesasIterators 79 9.4 Non-PreemptiveMultithreading 81 10 CompleteExamples 87 10.1 DataDescription 87 10.2 MarkovChainAlgorithm 91 II Tables and Objects 95 11 DataStructures 97 11.1 Arrays 97 Property of Ian Bloss <[email protected]> ix 11.2 MatricesandMulti-DimensionalArrays 98 11.3 LinkedLists 100 11.4 QueuesandDoubleQueues 100 11.5 SetsandBags 101 11.6 StringBuffers 103 11.7 Graphs 104 12 DataFilesandPersistence 107 12.1 DataFiles 107 12.2 Serialization 109 13 MetatablesandMetamethods 117 13.1 ArithmeticMetamethods 118 13.2 RelationalMetamethods 120 13.3 Library-De(cid:2)nedMetamethods 122 13.4 Table-AccessMetamethods 122 14 TheEnvironment 129 14.1 GlobalVariableswithDynamicNames 129 14.2 Global-VariableDeclarations 131 14.3 Non-GlobalEnvironments 132 15 ModulesandPackages 137 15.1 TherequireFunction 138 15.2 TheBasicApproachforWritingModules 141 15.3 UsingEnvironments 143 15.4 ThemoduleFunction 144 15.5 SubmodulesandPackages 145 16 Object-OrientedProgramming 149 16.1 Classes 151 16.2 Inheritance 152 16.3 MultipleInheritance 154 16.4 Privacy 156 16.5 TheSingle-MethodApproach 158 17 WeakTables 161 17.1 MemoizeFunctions 163 17.2 ObjectAttributes 164 17.3 RevisitingTableswithDefaultValues 165 III The Standard Libraries 167 18 TheMathematicalLibrary 169 Property of Ian Bloss <[email protected]>