[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Zero as False
- From: Paige DePol <lual@...>
- Date: Tue, 19 Nov 2013 16:07:40 -0600
Replying to myself just to add one change also required for making Zero as False work.
In the luaK_goiftrue function in lparser.c one line needs to be changed:
FROM: case VK: case VKNUM: case VTRUE: {
TP: case VTRUE: {
This way constants and numbers will be checked if they are numeric 0 or not.
Again, if anyone knows why this is a bad idea (other than because it changes the semantics of the language, but remember I am creating a new variant of Lua so that is not a problem) please let me know.
So far, however, it does seem to work quite well, and since 0 has been false for as long as I have been coding it just makes me happy to have it work that way in my scripting language as well! ;)
~pmd~
On Nov 18, 2013, at 12:30 PM, Paige DePol <lual@serfnet.org> wrote:
> So, I was poking around in the Lua 5.2 code and I found this in lobject.h:
>
> #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
>
> Since I am in the process of heavily hacking Lua (and in the process creating a scripting language for my game engine) I decided I really wanted numeric '0' to also be false.
>
> To that end I changed the above macro as follows:
>
> #define l_isfalse(o) (ttisnil(o) || (ttisnumber(o) && nvalue(o) == 0) || \
> (ttisboolean(o) && bvalue(o) == 0) )
>
> Not surprisingly this works very well, numeric zero is now a false value and logic statements now work as expected when the result of an expression is zero.
>
> I grep'd 'l_isfalse' and can not see anywhere that this change would adversely affect anything in the Lua core itself. So, if anyone else is working on a Lua variant and would like zero to be false, this seems to be the ticket!
>
> This, of course, breaks with the official Lua rules, however, I am not too concerned about that. I am using Lua as the base for a scripting language I am calling LUNiA (my game engine is called ONiA).
>
> Just thought I would share the discovery and see if anyone knows anything I missed that makes this change a bad idea.
>
> ~pmd~