Lua编程语言第二版

需积分: 10 1 下载量 169 浏览量 更新于2024-07-20 收藏 1.45MB PDF 举报
"Programming_in_Lua_2nd_Edition.pdf" 《Programming in Lua》是第二版的Lua编程指南,由Roberto Ierusalimschy编写,他在巴西PUC-Rio大学工作,并在Lua.org上活跃。这本书专注于介绍Lua编程语言,旨在帮助读者深入理解其语法、特性和用法。 Lua是一种轻量级的脚本语言,广泛应用于游戏开发、嵌入式系统以及各种软件中的配置和扩展。第二版的出版日期为2010年1月13日,书中内容可能涵盖了自第一版以来的更新和改进。 此书的内容可能包括以下几个关键知识点: 1. **基础语法**:Lua语言的基础,如变量、数据类型(包括数字、字符串、布尔值、表、函数和nil)、控制结构(如if语句、循环)以及操作符。 2. **表和元表**:Lua的表是其核心特性,是一种动态类型的关联数组,可以存储任意数据。元表则允许用户自定义表的行为,例如实现类和继承的概念。 3. **函数和闭包**:Lua支持函数作为一等公民,可以将函数赋值给变量、作为参数传递、从其他函数返回。闭包是Lua中的一个重要概念,它允许函数访问并操作其外部作用域的变量。 4. **模块和包管理**:Lua提供了简单的模块系统,用于组织代码和实现命名空间。包管理器帮助导入和管理这些模块。 5. **垃圾回收**:Lua自动处理内存管理,通过垃圾回收机制回收不再使用的对象,简化了程序员的工作。 6. **协程(Coroutines)**:Lua支持协程,这是一种轻量级的并发模型,允许在单线程环境中实现协作式多任务。 7. **C接口**:Lua提供了一套C API,使得开发者可以将Lua嵌入到C/C++程序中,或者用C/C++编写扩展库供Lua调用。 8. **错误处理和调试**:讨论如何在Lua代码中处理错误,以及使用调试工具进行问题定位。 9. **性能优化**:讲解如何编写高效的Lua代码,以及在必要时如何利用C扩展来提升性能。 10. **应用案例**:可能包含一些实际项目中的应用示例,展示Lua在游戏开发、网络编程、脚本编写等领域的实际应用。 这本书适合已经有一定编程基础,想要学习或深入理解Lua语言的读者。通过阅读,读者不仅可以掌握Lua的基本语法,还能了解到高级特性和最佳实践,从而在实践中更好地应用Lua。
2019-07-09 上传
Contents Preface xiii I The Language 1 1 Getting Started 3 1.1 Chunks 4 1.2 Some Lexical Conventions 5 1.3 Global Variables 6 1.4 The Stand-Alone Interpreter 7 2 Types and Values 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 Userdata and Threads 17 3 Expressions 19 3.1 Arithmetic Operators 19 3.2 Relational Operators 20 3.3 Logical Operators 21 3.4 Concatenation 22 3.5 Precedence 22 3.6 Table Constructors 22 vii Property of Ian Bloss <ianlinkcd@gmail.com> viii Contents 4 Statements 27 4.1 Assignment 27 4.2 Local Variables and Blocks 28 4.3 Control Structures 30 4.4 break and return 34 5 Functions 35 5.1 Multiple Results 36 5.2 Variable Number of Arguments 39 5.3 Named Arguments 42 6 More About Functions 45 6.1 Closures 47 6.2 Non-Global Functions 50 6.3 Proper Tail Calls 52 7 Iterators and the Generic for 55 7.1 Iterators and Closures 55 7.2 The Semantics of the Generic for 57 7.3 Stateless Iterators 58 7.4 Iterators with Complex State 60 7.5 True Iterators 61 8 Compilation, Execution, and Errors 63 8.1 Compilation 63 8.2 C Code 67 8.3 Errors 67 8.4 Error Handling and Exceptions 69 8.5 Error Messages and Tracebacks 70 9 Coroutines 73 9.1 Coroutine Basics 73 9.2 Pipes and Filters 76 9.3 Coroutines as Iterators 79 9.4 Non-Preemptive Multithreading 81 10 Complete Examples 87 10.1 Data Description 87 10.2 Markov Chain Algorithm 91 II Tables and Objects 95 11 Data Structures 97 11.1 Arrays 97 Property of Ian Bloss <ianlinkcd@gmail.com> ix 11.2 Matrices and Multi-Dimensional Arrays 98 11.3 Linked Lists 100 11.4 Queues and Double Queues 100 11.5 Sets and Bags 101 11.6 String Buffers 103 11.7 Graphs 104 12 Data Files and Persistence 107 12.1 Data Files 107 12.2 Serialization 109 13 Metatables and Metamethods 117 13.1 Arithmetic Metamethods 118 13.2 Relational Metamethods 120 13.3 Library-Dened Metamethods 122 13.4 Table-Access Metamethods 122 14 The Environment 129 14.1 Global Variables with Dynamic Names 129 14.2 Global-Variable Declarations 131 14.3 Non-Global Environments 132 15 Modules and Packages 137 15.1 The require Function 138 15.2 The Basic Approach for Writing Modules 141 15.3 Using Environments 143 15.4 The module Function 144 15.5 Submodules and Packages 145 16 Object-Oriented Programming 149 16.1 Classes 151 16.2 Inheritance 152 16.3 Multiple Inheritance 154 16.4 Privacy 156 16.5 The Single-Method Approach 158 17 Weak Tables 161 17.1 Memoize Functions 163 17.2 Object Attributes 164 17.3 Revisiting Tables with Default Values 165 III The Standard Libraries 167 18 The Mathematical Library 169 Property of Ian Bloss <ianlinkcd@gmail.com> x Contents 19 The Table Library 171 19.1 Insert and Remove 171 19.2 Sort 172 19.3 Concatenation 173 20 The String Library 175 20.1 Basic String Functions 175 20.2 Pattern-Matching Functions 177 20.3 Patterns 180 20.4 Captures 183 20.5 Replacements 185 20.6 Tricks of the Trade 189 21 The I/O Library 193 21.1 The Simple I/O Model 193 21.2 The Complete I/O Model 196 21.3 Other Operations on Files 199 22 The Operating System Library 201 22.1 Date and Time 201 22.2 Other System Calls 203 23 The Debug Library 205 23.1 Introspective Facilities 205 23.2 Hooks 210 23.3 Proles 211 IV The C API 215 24 An Overview of the C API 217 24.1 A First Example 218 24.2 The Stack 221 24.3 Error Handling with the C API 225 25 Extending Your Application 229 25.1 The Basics 229 25.2 Table Manipulation 231 25.3 Calling Lua Functions 235 25.4 A Generic Call Function 236 26 Calling C from Lua 241 26.1 C Functions 241 26.2 C Modules 244 Property of Ian Bloss <ianlinkcd@gmail.com> xi 27 Techniques for Writing C Functions 247 27.1 Array Manipulation 247 27.2 String Manipulation 249 27.3 Storing State in C Functions 251 28 User-Dened Types in C 259 28.1 Userdata 260 28.2 Metatables 262 28.3 Object-Oriented Access 265 28.4 Array Access 267 28.5 Light Userdata 268 29 Managing Resources 269 29.1 A Directory Iterator 269 29.2 An XML Parser 271 30 Threads and States 281 30.1 Multiple Threads 281 30.2 Lua States 285 31 Memory Management 293 31.1 The Allocation Function 293 31.2 The Garbage Collector 295 Index 299
2023-06-01 上传

function Currency:setLastAddSkillPoint(val) m_currency.m_iLastAutoAddSkillPointTime = val end function Currency:getLastAddSkillPoint() return m_currency.m_iLastAutoAddSkillPointTime end function Currency:addLastAddSkillPoint(val) m_currency.m_iLastAutoAddSkillPointTime = m_currency.m_iLastAutoAddSkillPointTime + val end function Currency:getYuxi() return m_currency.m_iYuxi end function Currency:addYuxi(val) m_currency.m_iYuxi = m_currency.m_iYuxi + val end function Currency:getUniversal() return m_currency.m_iUniversal end function Currency:addUniversal(val) m_currency.m_iUniversal = m_currency.m_iUniversal + val end function Currency:setGuildCoin(val) m_currency.m_iGuildCoin = val end function Currency:getGuildCoin() return m_currency.m_iGuildCoin end function Currency:addGuildCoin(val) m_currency.m_iGuildCoin = m_currency.m_iGuildCoin + val end --TacticsCoin-- function Currency:setTacticsCoin(val) m_currency.m_iTacticsCoin = val end function Currency:getTacticsCoin() return m_currency.m_iTacticsCoin end function Currency:addTacticsCoin(val) m_currency.m_iTacticsCoin = m_currency.m_iTacticsCoin + val end --全民战役-- function Currency:setWarContribution(val) m_currency.m_iWarContribution = val end function Currency:getWarContribution() return m_currency.m_iWarContribution end function Currency:addWarContribution(val) m_currency.m_iWarContribution = m_currency.m_iWarContribution + val end function Currency:setMilitaryRank(val) m_currency.m_iMilitaryRank = val end function Currency:getMilitaryRank() return m_currency.m_iMilitaryRank end 翻译成TS

2023-05-27 上传