[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [ANN] LuaFish 0.1 - lua lpeg parser, macros, and static type checking
- From: David Manura <dm.lua@...>
- Date: Sun, 19 Aug 2007 03:20:22 +0000 (UTC)
LuaFish ( http://lua-users.org/wiki/LuaFish ) provides various Lua modules for
parsing Lua 5.1 source code, manipulating abstract syntax trees (AST), applying
LISP-style macros, and doing static type checking.
LuaFish is similar in application to Metalua[1], Cheese[2] (the other food), and
LuaParse[3] but is based on LPeg[4]. Warning: this initial 0.1 version is
fairly experimental.
One of the included macro examples illustrates an interesting initial idea of
performing *static* type checking in Lua. The macros operate at compile time on
the AST to associate a type with a lexical. The macros use standard Lua
function call syntax, so there is no change to the grammar but only to the
semantics. It is instructive to follow through the following test case.
<snip>
-- Test of static type checking via the LuaFish
-- LPeg-based macro processor.
--
-- This file must be loaded using the macro-enabled
-- version of dofile:
--
-- local mdofile = require "luafish.macro" . dofile
-- mdofile "typem.lua"
--
-- NOTE: SETTYPE and TYPE are macros evaluated
-- at compile time. These respectively assign a static type
-- to a lexical and retrieve that type.
local x, c = 3, 'ok'
SETTYPE(x, 'integer')
SETTYPE(c, 'string')
local function test(y)
SETTYPE(y, 'integer')
assert(TYPE(y) == 'integer')
end
-- demonstration of the TYPE macro.
assert(TYPE(x) == 'integer')
assert(TYPE(test) == 'function')
test(x) -- ok, types match
-- compile error: "argument y type is string but expecting integer"
-- test('ok')
-- type of arithmetic expression is deduced at compile time
local x = 1
SETTYPE(x, 'number')
assert(TYPE(1+2+x*x) == 'number')
print 'done'
</snip>
[1] Metalua - http://luaforge.net/projects/metalua/
[2] Cheese - http://luaforge.net/projects/cheese/
[3] LuaParse - http://luaparse.luaforge.net/
[4] LPeg - http://www.inf.puc-rio.br/~roberto/lpeg.html