If you visit and read ScottGu blog, you will know that Microsoft will ships a new .NET library called DLR (Dynamic Language Runtime) in very near future. DLR provides a set of "features on top of the CLR" designed explicitly for dynamic language scenarios like shared dynamic type system, language hosting model, and fast dynamic code. The source code of IronPython implementation, including the underlying DLR library, was published on CodePlex. Not only IronPhyton, Microsoft will also ship other dynamic language implementations:
- IronPython. This should be easy for us as we have many real Iron and Phyton in Indonesia
- IronRuby. At least still consistent with IronX pattern. John Lam was the first implementor.
- Javascript. Why it is not just IronJS?
- Dynamic VB. Why it is not just IronVB?
As DLR opens opportunity to create more embedded scripting language implementation, I think it will be nice if we also have IronLUA (Loo-ah in Brazil means "moon" or "bulan" in bahasa). LUA is an extension programming language designed to support general procedural programming. It offers good support for OO, functional and data-driven programming. With its increasing number of fans, I think the effort to implement "LUA on DLR" will be an interesting journey.
Current LUA implementation is a simple library, written in portable clean C (that is why everybody should know C). If you download it from www.lua.org, its distribution includes a stand-alone embedding program, that uses the LUA library to offer a complete LUA interpreter and binary compiler. LUA is a dynamically typed language. That means that variables do not have types; only values do. There are no type definitions in the language. All values carry their own type. There are eight basic types in LUA: nil , boolean, number, string, function, userdata, thread, and table.
LUA has its own concept for garbage collector, called metamethods for userdata, also called finalizers, that allow us to coordinate LUA’s GC with external resource management (such as closing files, network or database connections, or freeing memory). You can download LUA source codes and compile it straightforward using our beloved VSTS 2008 Beta 2 like I did in my vista machine:
Note : please compile LUA interpreter (LUA.EXE) and LUA compiler (LUAC.EXE) separately to start play with it.
I do like PowerShell, Ruby, Perl, and now I want to show you interesting LUA codes to compute N-th Fibonacci number (I hope I am not confusing my brain with too many scripting languages):
-- fibonacci function with cache
-- very inefficient fibonacci function
function fib(n)
N=N+1
if n<2 then
return n
else
return fib(n-1)+fib(n-2)
end
end
-- a general-purpose value cache
function cache(f)
local c={}
return function (x)
local y=c[x]
if not y then
y=f(x)
c[x]=y
end
return y
end
end
-- run and time it
function test(s,f)
N=0
local c=os.clock()
local v=f(n)
local t=os.clock()-c
print(s,n,v,t,N)
end
n=arg[1] or 24 -- for other values, do LUA.EXE fib.lua XX
n=tonumber(n)
print("","n","value","time","evals")
test("plain",fib)
fib=cache(fib)
test("cached",fib)
I am pretty sure those codes are intuitive enough for you including its general purpose value cache. To execute LUA codes, you can do in three ways (I did with both first and second ways):
- Just write into text format called fib.lua and interpret it by executing LUA.EXE fib.lua.
- Compile into binary format using LUA compiler (LUAC.EXE -o fib.out fib.lua) then execute using LUA.EXE.
- Embed LUA binary in any host application as LUA interpreter size is only 152KB and its compiler is 151KB.
Interesting to know that LUA only took 0.075 second for N=24!. You can compare it with the same codes in RUBY or IronRuby, let say:
def fib(n)
if n<2
n
else
fib(n-2)+fib(n-1)
end
end
print(fib(24), "\n");
I wish someone can run it with RUBY.EXE (native Ruby) or RBX.EXE (IronRuby, DLR) and report its execution time comparing to LUA. Honestly, I DONT WANT to do that as I can feel it slower :). IronRuby is a baby and it is interesting to know "how the baby grow from its early age". Like my 2nd son Razka, he learns new thing every day. Baby needs time to be mature, right?. ScottGu said that IronRuby has been architected to take advantage of a new DLR feature we call "Dynamic Sites" - which delivers a fast adaptive call-site method caching implementation. This baby also uses the lightweight-code generation features of the CLR that enables dynamic language implementations to create in-memory IL that is then JIT'd into native code at runtime (without ever having to save anything to disk). ScottGZu said this can yield much better runtime performance than interpreted code (like LUA?). The lightweight codegen feature ensures that once we are finished with the JIT'd code we can optionally garbage collect it to avoid leaking. Let see how ScottGu & team prove their statement. I will look on IronRuby source codes sometime and share with all of you here.
IronRuby baby is interesting but someone already took that project (John Lam). The most interesting thing for us in Indonesia is not being a follower anymore but being the innovator. What about IronLUA, LUA implementation on DLR? We can make IronLUA as our new baby without took new wifes. Anyone see a possibility to implement it?
I am very confident that one day INDC will ship something to the worldwide developer community. I want to let them hear the name of our country as innovator. Not the country of terrorism or maid exporter. Let them know that there are developers in Indonesia who will ship good products to their countries like what Pak Tahir has done and consistently does. With all of us here, we can do many thing.
Thx - R.A.M