Running Lua on Mac OS X

Lua is an embeddable scripting language and has good reputation among many gaming engines. We will show you in this article how to install and test Lua.

Running Lua in your Mac is easy:

Now test if your installation is OK:

$ /usr/local/bin/lua -v
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

This example is an implementation of Fibonacci numbers with coroutines written in Lua.

function fibo()
  a, b = 0, 1
  while true do
    coroutine.yield(a)
    a, b = b, a + b
  end
end

co = coroutine.create(fibo)

for i = 1, 20 do
  print(coroutine.resume(co))
end

Copy and save it to fibonacci.lua and then run with lua fibonacci.lua to print the first 20 Fibonacci numbers. You can also download the code from here.

©2010 Rudá Moura