Cours coroutines in Lua, tutoriel & guide de travaux pratiques en pdf.
Lua [Ierusalimschy et al., 1996, Figueiredo et al., 1996] is a lightweight scripting language that supports general procedural programming with data description facilities. It is dynamically typed, lexically scoped, interpreted from bytecodes, and has automatic memory management with garbage collection. Lua was originally designed, and is typically used, as an extension language, embedded in a host program.
Lua was designed, from the beginning, to be easily integrated with software written in C, C++, and other conventional languages. Lua is implemented as a small library of C functions, written in ANSI C, and compiles virtually unmodiﬁed in all currently available plataforms. Along with the Lua interpreter, this library provides a set of functions (the C API) that enables the host program to communicate with the Lua environment. Through this API a host program can, for instance, read and write Lua variables and call Lua functions. Besides allowing Lua code to extend a host application, the API also permits the extension of Lua itself by providing facilities to register C functions to be called by Lua. In this sense, Lua can be regarded as a language framework for building domain-speciﬁc languages.
Lua coroutine facilities implement the concept of asymmetric coroutines, more commonly denoted as semi-symmetric or semi-coroutines [Marlin, 1980, Dahl et al., 1972]. Asymmetric coroutine facilities are so called because they involve two types of control transfer operations: one for (re)invoking a coroutine and one for suspending it, the latter returning control to the coroutine invoker. An asymmetric coroutine can be regarded as subordinate to its caller, the relationship between them being similar to that between a called and a calling routine. A different control discipline is implemented by symmetric coroutine facilities, which provide a single transfer operation for switching control to the indicated coroutine. Because symmetric coroutines are capable of passing control between themselves, they are said to operate at the same hierarchical level. The following arguments justify why Lua offers asymmetric coroutines, instead of providing symmetric facilities or both.