Chapa 1)


Edrx's page on Lua, Forth, and in-betweens

Main links (2023):
My init file (with lots of cruft).
A very terse tutorial on Lua made with eepitch blocks.
eoo.lua: my implementation of OO in Lua. It is very REPL-friendly, sort of like SmallTalk.
All my recent programs in Lua are documented using test blocks.
Some examples: a repl for TikZ, luatree (for Maxima), Pict2e-lua, emlua.
Some classes written with eoo:
    Tos, for creating tostring-like functions.
    Repl1, Repl2, Repl3: several REPLs.
    LpegRex2: some tests for lpegrex,
    Gram1 (screenshot): build lpeg grammars incrementally.
    ELpeg1: similar, but with many features from lpegrex; see this.
"Boostrapping a Forth in 40 lines of Lua code" (PDF) - my article in the Lua Gems book.
My presentation at the EmacsConf 2020 mentions that eev borrowed some ideas from Forth.
Dednat6: an extensible (semi-)preprocessor for LuaLaTeX that understands diagrams in ASCII art
BlogMe3 - an extensible language for generating HTML

The rest of this page is very old.

April, 2011: Lua bindings for Retro. See:


January, 2011: http://0branch.com/.

December, 2008: the Lua Gems book was published (finally!8-), with my article about "bootstrapping a Forth in 40 lines of Lua code" in it. The bootstrap-ish Forth described in it, "MiniForth", is not really usable, but Marc Simpson's RubyForth (e-scripts, ref) is, and my port of it to Lua (tests) is half-done.

I have not cleaned the rest of this page yet! There are still some fossil parts in it - some 4 or 5 years old -, and most parts are not dated...

Quick index:


1. Running the Lua API interactively from GDB

The relevant code is in this GDB script: PP.gdb. Some functions in it call global Lua functions - PP and PPeval - that are defined in my $LUA_INIT file.

The first GDB transcript below uses neither PP.gdb nor PP; the second one uses everything. The e-script that I used to generate them is here, and here is a screenshot of Emacs running it (click to enlarge):

# (find-es "lua5" "lua-api-from-gdb")
# (find-angg ".lua51/PP.gdb")
# (find-angg ".emacs" "eepitch-gdb-lua")
# (find-eevarticlesection "eepitch-gud")
echo 'math.sin(0)' > /tmp/foo.lua
Current directory is /home/edrx/usrc/lua-5.1.2/src/
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) set args /tmp/foo.lua
(gdb) br math_sin
Breakpoint 1 at 0x80673ef: file lmathlib.c, line 32.
(gdb) run
Starting program: /home/edrx/usrc/lua-5.1.2/src/lua_O0 /tmp/foo.lua

Breakpoint 1, math_sin (L=0x8071008) at lmathlib.c:32
(gdb) call lua_pushstring(L, "print")
(gdb) call lua_gettable(L, -10002)
(gdb) call lua_pushstring(L, "Foo!")
(gdb) call lua_pushstring(L, "Bar!")
(gdb) p lua_gettop(L)
$1 = 4
(gdb) call lua_call(L, 2, 0)
Foo!    Bar!
(gdb) p lua_gettop(L)
$2 = 1
Current directory is /home/edrx/usrc/lua-5.1.2/src/
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) source ~/.lua51/PP.gdb
(gdb) quickstart
Breakpoint 1 at 0x80673ef: file lmathlib.c, line 32.
math_sin (L=0x8071008) at lmathlib.c:32
(gdb) $1 = 1
(gdb) PPeval "a = 32"
(gdb) PPeval "=   a, a+2"
32      34
(gdb) PPeval "=  {a, a+2}, 40"
table: 0x80781b0        40
(gdb) PPeval "== {a, a+2}, 40"
 {1=32, 2=34} 40
(gdb) depth
$2 = 1
(gdb) lua_pusheval "20 + 42"
(gdb) depth
$3 = 2
(gdb) PP -1
(gdb) depth
$4 = 2

2. Gavin Wraith's functional syntax patch

# (find-es "lua5" "risclua")
# (find-es "lua5" "minimal_GCW_SUGAR")
# (find-es "lua5" "install-5.1.3")
# (find-es "lua5" "install-5.2")
# (find-angg "bin/patch-lua-5.1.3")
# (find-angg "bin/patch-lua-5.2")

3. Ldb

# (find-es "davinci" "ldb-rewriting-functions")
# (find-es "lua5" "ldb-from-tgz")
# (find-es "lua5" "ldb")

(From this point on everything is very old...)

Lua for Debian:


Forth-like languages interpreted on top of Lua:

  • An outdated project: Flua. It has access to the standard C library and to extensions written in C and in other languages (see jcw (rmt)'s project Minotaur). I'm planning to use Flua for the computational part of my ideas about skeletons of mathematical proofs, and for adding cLIeNUX (rmt)-like extensions to the Hurd.
  • A follow-up to Flua: miniforth, and the bare beginnings of a technical report on its main ideas. It has the same goals as Flua, and it is much more modular; the part that generates bytecodes for an inner interpreter written in C is kept separate from the rest.
  • Note that both Flua and miniforth are far from being really usable.

Flua, a Forth-like language written in Lua+C+Nasm:

Important local links:

I gave a minicourse about Lua in 2004 (link?) and I'll give it again (much updated, of couse!) at the beginning of October/2005, at http://www.c3sl.ufpr.br/secomp/ (rmt)... Here is the current summary (in Portuguese only, and still without links):

Curso de Lua

Resumo: Uma "introdução em profundidade" à linguagem de programação Lua. Como a linguagem é bastante simples (o manual de referência completo, incluindo a descrição de todas as funções e da API em C, tem 65 páginas), será possível abordar todos os aspectos principais da linguagem, e mais algumas extensões, ferramentas, e várias técnicas de uso.

  • Instalando e rodando o Lua
  • O núcleo do Lua:
    • Tipos de dados; expressões e statements. Sintaxe. ";"s implícitos.
    • Variáveis globais e locais; escopo.
    • Tabelas associativas; como arrays, classes e variáveis globais são implementados via tabelas.
    • Funções como objetos; closures; blocos de código; propagação de erros.
  • Bibliotecas padrão:
    • Suporte a I/O e arquivos.
    • dofile, dostring e require.
    • Operações sobre tabelas; modos de percorrer tabelas.
    • Operações sobre strings; regexps em Lua.
    • Carregando bibliotecas extras com loadlib.
  • As entranhas do Lua:
    • A biblioteca "debug".
    • Byte-compilação, a máquina virtual interna do Lua, a pilha.
    • Rodando o Lua passo a passo com gdb; examinando o conteúdo da pilha. Seguindo passo a passo algumas funções Lua definidas em C. A API do Lua.
    • Executando scripts e funções Lua a partir de C.
  • Algumas bibliotecas e extensões: bitlib, pcre, posixlib, luagtk.
  • Uma ferramenta para ligar Lua e C mais facilmente: tolua.

Pré-requisitos (aluno): inglês técnico básico (para entender a documentação), alguma familiaridade com C (para quem quiser acompanhar a parte final do curso).