[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [Q] simple translator from Lua to Java Script and PHP
- From: Doug Rogers <rogers@...>
- Date: Thu, 13 Nov 2008 14:33:24 -0500
Javier Guerra Giraldez wrote:
> for example, i usually do:
> if a and a.b then.....
> to avoid evaluating 'a.b' until i know that 'a' is not null.
This is a common idiom in Lua so I would also expect its behavior to be
retained in translation. However, if the target environment does not
expect that behavior, then Vlad is well within his design rights to
dispose of it.
The designers of Ada decided to use Vlad's way, but they provided an out
by reusing the 'then' and 'else' keywords:
-- Evaluates A, B, both, in an undefined order:
if A and B then ...
if A or B then ...
-- Always evaluates A first, then B only if necessary. That is, B
-- is evaluated when A is true for 'and', when A is false for 'or':
if A and then B then ...
if A or else B then ...
It's pretty, but it's Ada's way, not Lua's way. For one thing, Ada
strictly enforces the type to be Boolean. And Ada programmers must be
aware of the first form; it can be surprising to see the compiler
generate code to test B first in order to take advantage of an
optimization opportunity.
Also, Lua allows nil to be treated as false (indeed, originally nil was
identically false). Though I initially refrained from using the 'a = a
or 1' construct, I long ago succumbed to the allure of its brevity; my
own code now relies on this feature of The Language We Love.
I would concur with Javier, though. Unless you have tight control on the
programming environment, you might want to revisit this.
By the way, when I run this under Ubuntu Linux x86, the output for
test1_lua2js.lua is lacking call_func1() at the top. I tweaked the names
to be lua2js_rules.mlua (and .luac) and replaced the path in the top of
each to be my own - /home/rogers/opt/metalua. Below is a transcript;
notice the missing call_func1("ab","c","d",44) that exists in
test1_lua2js.lua.
$ which metalua
/home/rogers/opt/metalua/bin/metalua
$ metalua -o lua2js_rules.luac lua2js_rules.mlua # tweaked names
$ which lua
/home/rogers/opt/metalua/bin/lua
$ lua
Lua 5.1.3 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> ^D
$ lua lua2js_front.lua test1_lua2js.lua
if( js_iop_or( js_iop_lt( arr1['field1'], arr2['field1'])
, js_iop_lt( arr2[33][15], arr1['field2'])
)
)
{
call_func2('xyz') ;
}
else if
( js_iop_lt(a,b)
)
{
call_func3('abc') ; call_func4('abc') ;
}
else
{
call_func5(123) ;
}/*end of if*/
It's likely operator error on my part since I'm new to metalua.
Doug
--
Innovative Concepts, Inc. www.innocon.com 703-893-2007 x220