(Chapa 1)


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

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. A lua-mode for Emacs

# Downloading and unpacking:
# (find-es "lua5" "lua-mode.el")
# http://luaforge.net/projects/lua-mode/
# http://luaforge.net/frs/download.php/2471/lua-mode-20070608.tar.gz
tar -C ~/elisp/ -xvzf \
;; Relevant code in my .emacs:
;; (find-angg ".emacs" "add-to-alist")
;; (find-angg ".emacs" "auto-mode-alist")
(add-to-list 'load-path "~/elisp/")
(add-to-alist 'auto-mode-alist '("\\.lua$" . lua-mode))

;; (find-angg ".emacs" "lua-mode")
(setq lua-indent-level 2)
(setq lua-electric-flag nil)
(defun lua-abbrev-mode-off () (abbrev-mode 0))
(add-hook 'lua-mode-hook 'lua-abbrev-mode-off)

2. 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

3. 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")

4. 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'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-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/... 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).