# Lua Programming Language

- Page: https://shared.btbytes.com/p/lua
- Date: 2020-01-21
- Tags: programming-language, lua

## Learn

- [Programming in Lua](http://www.dcc.ufrj.br/~fabiom/lua/) -- class    notes.
- [Lua - good, bad and ugly parts](http://notebook.kulchenko.com/programming/lua-good-different-bad-and-ugly-parts)
- [Lua 5.1.4 Annotated Source](https://stevedonovan.github.io/lua-5.1.4/)
* <https://www.luafaq.org/>

## Lua associated languages

[Pallene + Lua](https://news.ycombinator.com/item?id=18038619). [Paper](http://www.inf.puc-rio.br/~roberto/docs/pallene-sblp.pdf) .Found via janet lang's gitter channel. It is a "Friendly Fork" of Titan Language. Pallene is a moon of saturn?

[Terra](http://terralang.org/) -- low level lua like language.

[teal](https://github.com/teal-language/tl); The compiler for Teal, a typed dialect of Lua

[Luau](https://luau-lang.org/)  is a fast, small, safe, gradually typed embeddable scripting language derived from Lua.

## Links and references

- [Lua.Space](http://web.archive.org/web/20151214030857/http://lua.space/) -- lua community blog; started Dec 2015.
- [What's different about Lua](http://news.ycombinator.com/item?id=1786280)
- [Is Lua interesting, from a programming language design perspective?](http://stackoverflow.com/questions/2100902/is-lua-interesting-from-a-programming-language-design-perspective/2102399#2102399)
- [Lua date manipulation](https://gist.github.com/272082)
- [Lua - world's most infuriating language](http://www.slideshare.net/jgrahamc/lua-the-worlds-most-infuriating-language).
- [How to lua and c - a short novel](http://web.archive.org/web/20181227123136/https://sepisoad.com/blog/how%20to%20lua%20and%20c%20-%20a%20short%20novel.html)
- [lua's nil](https://lobste.rs/s/1xlmb3/interactive_lua_development_with_fennel#c_oplrcb)
-  [Lua links by Tao of Mac](https://taoofmac.com/space/dev/lua)

## Cool Lua stuff

- [Performance counter access with inline assembler in Lua](https://gist.github.com/btbytes/52d33c22cc60ce4f476f)
- [Luvi](https://github.com/luvit/luvi) A project in-between luv and luvit. The goal of this is to make building luvit and derivatives much easier. Luvi has a somewhat unique, but very easy workflow for creating self-contained binaries on systems that don't have a compiler:
    + [Using luvi to ship binaries](http://web.archive.org/web/20160304034142/https://www.distelli.com/blog/using-lua-for-our-most-critical-production-code)
    + [Holy build box](http://phusion.github.io/holy-build-box/)
    + [lit](https://github.com/luvit/lit) - Toolkit for developing, sharing, and running luvit/lua programs and libraries.
    + [example of luvit code - IRC activity bot](https://github.com/technomancy/luvit-irc-activity-bot) by `technomancy`.

- [Fengari](https://fengari.io/) is the Lua VM written in JavaScript. It uses JavaScript's garbage collector so that interoperability with the DOM is non-leaky.
- [The Fennel programming language](https://fennel-lang.org/) . [note](https://shared.btbytes.com/p/fennel).
- [bakpakin/moonmint: A Web Framework for Lua](https://github.com/bakpakin/moonmint)
- [Urn: A Lisp implementation for Lua | Urn](https://urn-lang.com/)
- [rxi/lume: Lua functions geared towards gamedev](https://github.com/rxi/lume) -- "lua's missing standard library" according to \@technomancy
* [rochus-keller/LjTools: LuaJIT 2.0 bytecode parser, viewer, assembler and test VM](https://github.com/rochus-keller/LjTools)
* [I build Lua programs in prod. I use fennel or moonscript as the frontend, and ev... | Hacker News](https://news.ycombinator.com/item?id=21678604); Dec 2019.

2020-06-04 [oasislinux/oasis: a small statically-linked linux system](https://github.com/oasislinux/oasis); uses lua for setup, building, package spec etc.,

[Mako Server](https://makoserver.net/) <q>This tiny, ready-to-run application server packs a punch with its super compact and efficient Lua web framework and its non-blocking asynchronous sockets. Plus, it comes with everything you need built-in, including an integrated database, SMTP, HTTP client/server, and even IoT protocols like MQTT and industrial protocols like OPC-UA and Modbus.</q>

## People

- Calvin Rose -- [bakpakin.com](https://bakpakin.com/) (creator of Fennel lang, moonmint web framework, etc.,)
* [Stefano Trettel](https://github.com/stetre) -- `moon*` libs for graphics, etc.,


## Compiling Lua source files

Let's say you have a lua program -- `hello.lua`.

~~~{.lua}
print("Hello Lua!")
~~~

The source code can be compiled into a binary file using `luac`

~~~{.bash}
$ luac -o hello.luac hello.lua
~~~

The file hello.luac can then be used in place of the source file. Using
the binary file has some advantages like: faster loading, simple source
hiding etc.,

To run a `.luac` file (this is my own convention, the Lua docs do not
seem to prefer any particular suffix for the compiled file), you still
need to call the lua interpreter,

~~~{.bash}
$ lua hello.luac
~~~

Creating standalone executables for lua
---------------------------------------

A Lua source file can be converted into a executable using different
methods. `srlua` appears to be the popular one.

Download `srlua` from
[here](http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/) and build it using
`make`. Copy `glue` and `srlua` files into a convenient location, say
`~/bin`.

You can now create standalone executable from the Lua source file
--`hello.lua` thus:

~~~{.bash}
$ ~/bin/glue ~/bin/srlua hello.lua hello
$ chmod +x hello
$ ./hello
Hello Lua!
~~~

Installing new software packages
--------------------------------

[Luarocks](http://luarocks.org) is the deployment and package management
system for Lua. Think CPAN for Perl and PyPI for Python.

I have installed Lua in my `$HOME` directory. That is, `lua` interpreter
resides in `$HOME/bin`, include files in `$HOME/include` etc.,

So, to install Luarocks:

~~~{.bash}
$ export PREFIX=$HOME
$ ./configure --prefix=$PREFIX --sysconfdir=$PREFIX/luarocks --force-config
~~~

Installing new lua packages is as simple as:

~~~{.bash}
$ cd
luarocks install luasocket
~~~

where `luasocket` is a new package that
is fetched from the luarocks mirrors and installed.

To make this package available to the Lua interpreter export `LUA_PATH`
thusly:

~~~{.bash}
export LUA_PATH="/opt/local/share/lua/5.1//?.lua;/opt/local/lib/luarocks/?.lua;;"
~~~

Another `LUA_PATH` that makes more sense:

```
?;?.lua;/usr/local/lua/?/?.lua
```

Also, to explicitly add paths to a script:

~~~{.lua}
# cat t_paths.lua
package.path = package.path.. ";/opt/local/share/lua/5.1/?.lua"
package.cpath = package.cpath.. ";/opt/local/lib/lua/5.1/?.so"
local s = require "socket"
print(s._VERSION)
~~~

Incidentally, on the day I was learning aobut `luarocks`, luaforge.net,
the main lua modules repository was down. Friendly folks on the `#lua`
IRC channel told me to add mirrors to the luarocks configuration.

~~~{.bash}
$ vim ~/luarocks/config.lua
~~~

I replaced the luaforge.net line with

    http://www.luarocks.org/repositories/rocks/",

## Misc. Notes

Setting up `LUA_PATH` and `LUA_CPATH` is always more complicated than it
needs to be. Remember not to have any spaces after the path seperator
`;`.

[LuaDist](http://luadist.org/) appears promising to set up `virtualenv`
like sandboxed environments (and more..) for lua.


## Lua application packaging

[luastatic](https://github.com/ers35/luastatic) README tells us that it can package lua applications into executables for windows, macos & linux. A few applications that use luastatic to do their packaging are given. It will be nice to find build setups which can compile to all the three OSes using github CI pipeline.


--

See also: [Luarocks](https://shared.btbytes.com/p/luarocks).
