lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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.

-- 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')

-- 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'

[1] Metalua -
[2] Cheese -
[3] LuaParse -
[4] LPeg -