Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Roberto Ierusalimschy

Rating No ratings yet

Lua is spreading more and more throughout different areas of software, from embedded systems and mobile devices to the Web and the Internet of Things. Besides, it has a major role in the game industry, where knowledge of Lua has become an indisputable asset. Lua is the language of choice for anyone who needs a scripting language that is simple, efficient, extensible, portable, and free. Programming in Lua is the official book about the language, providing a solid base to any programmer who wants to use Lua. Authored by Roberto Ierusalimschy, the chief architect of the language, it covers all aspects of Lua 5---from the basics to its API with C. The book is the main source of programming patterns for Lua, with numerous code examples that help the reader to make the most of Lua's flexibility and powerful mechanisms. Programming in Lua is targeted at people with some programming background, but does not assume any prior knowledge about Lua or other scripting languages. This Fourth Edition updates the book to Lua 5.3 and marks a complete reorganization of the text. Building on his many years of experience teaching Lua, Roberto has restructured the book to present the material in a growing order of complexity, allowing the reader to better absorb the character of the language.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 The definitive guide to Lua 5.3, written by the language's chief architect, teaching you both the language itself and how to embed and extend it through C. Best for programmers who already know another language and want a scripting engine that is small, fast, and designed to be extended. 【Book Arc】 - **Opening (~0%–10%)**: Establishes Lua's design philosophy—simplicity, extensibility, portability—and gets you running the interpreter, then covers the core value types: numbers (the new 64-bit integers vs. floats in 5.3) and strings (byte sequences, immutability, UTF-8 handling). - **Early (~10%–30%)**: The heart of the language: tables as the universal data structure, traversal with `pairs`/`ipairs`, multiple return values, proper tail calls, and the scoping rules (locals, `do` blocks, `goto`) that shape idiomatic Lua. - **Middle (~30%–50%)**: Practical programming with the standard libraries—pattern matching and string manipulation, date/time, file I/O—plus classic data structures (linked lists, queues, graphs) built from tables, and serialization techniques. - **Late (~50%–80%)**: The advanced language machinery: metatables and metamethods, environments, modules and packages, object-oriented programming via table fields, and the mechanisms that make Lua so flexible. - **Ending (~80%–100%)**: The C API—interfacing Lua with C/C++ and other languages, extending Lua with external libraries, and the embedding patterns that make Lua a "kit for building domain-specific languages." 【Key Takeaways】 - **Lua is a kit, not just a language** (Opening): Its extensibility is so central that many treat it as a framework for building domain-specific languages; Lua implements much of its own basic functionality through external libraries. - **Tables are the single universal data structure** (Early): Arrays, records, sets, objects, and modules are all tables; understanding anonymous tables, reference semantics, and garbage collection is the foundation for everything else. - **Lua 5.3's hallmark is the integer/float split** (Opening): 64-bit integers wrap around predictably on overflow, and the change created few incompatibilities because doubles represent integers exactly up to 2^53. - **Functions are first-class and multi-valued** (Early): Functions can return multiple results, be stored in table fields (the basis of OOP), and Lua performs proper tail-call elimination for state machines and recursion. - **Strings are immutable byte sequences** (Opening): Lua is eight-bit clean and can hold any binary data; the standard string library assumes one-byte characters but handles UTF-8 reasonably, with a dedicated utf8 library since 5.3. - **Patterns are a lightweight, powerful alternative to regex** (Middle): Lua's pattern matching supports character classes, repetition modifiers, and captures, enabling tasks like recursive XML transformation and URL encoding. - **Data structures are built, not built-in** (Middle): Linked lists, queues, double-ended queues, and graphs are all implemented from tables; often a simpler table representation beats a "textbook" structure. - **The C API is where Lua's real power lives** (Ending): Interfacing with C/C++, Java, C#, and Python is straightforward, and the API is the path to embedding Lua in games, embedded systems, and IoT devices. 【Reading Tips】 - **Deep-read the early chapters on tables and functions**—they are the conceptual core; skimming them will make metatables and OOP feel arbitrary later. - **Skim the standard-library chapters** (strings, I/O, dates) on a first pass and return to them as reference; the pattern-matching chapter deserves a slower second read. - **Treat the C API section as a separate project**: it assumes C fluency and is best read with a compiler open, experimenting with embedding a small interpreter. - **Do the exercises**, especially in the data-structures and serialization chapters—they expose edge cases (empty strings, queue index reset, cyclic tables) that the prose only hints at. - **Watch the version notes**: the book targets Lua 5.3, and the integer/float distinction is the main incompatibility with older code you may encounter. 【Coverage Limits】 This guide is based on stratified excerpts covering roughly the first half of the book in detail, with lighter coverage of the metatable, module, OOP, and C API chapters; specific chapter titles and later-chapter examples are not fully represented in the source material.
Page 11
ontributes to its small size. (Its Linux 64-bit executable, including all standard libraries, has 220 KB.) Efficiency: Lua has a quite efficient implementati...
View in text
Excerpt 2
an return multiple results. Several predefined functions in Lua return multiple values. We have already seen the function string.find, which locates a patter...
View in text
Excerpt 3
it starts with a one-line header with the string "P1"; then there is one line with the width and height of the drawing, in pixels. Finally, there is a sequen...
View in text
Excerpt 4
o that both indices return to zero when the queue is empty. Exercise 14.3: Modify the graph structure so that it can keep a label for each arc. The structure...
View in text
Excerpt 5
: the invariant state and the control variable. A stateless iterator generates the next element for the iteration using only these two values. A typical exam...
View in text
Excerpt 6
rom load using an auxiliary table. Before calling load, the server checks in the table whether the given string already has a translation. If it cannot find...
View in text
Excerpt 7
he calling function. (The returned table should not include environmental variables; instead, it should inherit them from the original environment.) IV The C...
View in text
Excerpt 8
s: void lua_geti (lua_State *L, int index, int key); void lua_seti (lua_State *L, int index, int key); Lua versions prior to 5.3 offered only ra
View in text
Tags
AI categories
Programming LanguageGoSoftware
ISBN: 8590379868
Publisher: lua.org
Publish Year: 2016
Language: English
Pages: 388
File Format: PDF
File Size: 3.2 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…