[18 Aug 2008] Converting some lua extensions

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
Dwayne

[18 Aug 2008] Converting some lua extensions

Post by Dwayne »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 18 Aug 2008 23:21:00 +1000

Hi,

Have been slowly converting some lua extension modules for hollywood from Lua source but keep running in to a lot of examples that require the string modules. Andreas you have done an excellent job of making the language simple (basic esq.) and powerful at the same time but in my effort to learn Lua and to try and extend the Hollywood feature set at a basic level by implementing these modules I decide I would re-write the string implementation.

Now I had to attack this one slightly differently than some of the other modules I have been converting because it is actually a part of the core Lua implementation and is written in C. My first attempt at re-writing some the routines for gsub for example worked well with the the first example and it produced the expected result but a more complex example failed to re-produce the correct output.

I put this down to many things (my experience being the main one) however one common recurring piece of code kept on cropping up and I did my best to try and implement it in lua but I am not certain if it is working correctly. Perhaps someone can give me a better explanation than the one I have read regarding the following C syntax. Understanding the logic will hopefully allow me to re-implement this better than I currently have it hopefully solve my problem.

The statement is simple it is (obviously the variables are nonsensical for this example bt I was more interested in understanding the logic).

Code: Select all

a ? b : c
Any explanations would be greatly appreciated.

My attempt at trying to resolve this is to use if else statements

Code: Select all

if a
	c
else
	b
endif
Regards,

Dwayne
User avatar
Clyde
Posts: 351
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

[18 Aug 2008] Re: Converting some lua extensions

Post by Clyde »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 18 Aug 2008 15:30:18 +0200

Hi there!
a ? b : c

Any explanations would be greatly appreciated.

My attempt at trying to resolve this is to use if else statements

if a c else b endif
I am quite sure that it has to be the other way around: if "a" is true, than do "b". Otherwise (if "a" is false) do "c".

So in my opinion it has to look like

Code: Select all

if a
 	b
else
 	c
endif
Greetings Micha
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
User avatar
airsoftsoftwair
Posts: 5834
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[18 Aug 2008] Re: Converting some lua extensions

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 18 Aug 2008 15:33:04 +0200
Hi,

Have been slowly converting some lua extension modules for hollywood from Lua source but keep running in to a lot of examples that require the string modules. Andreas you have done an excellent job of making the language simple (basic esq.) and powerful at the same time but in my effort to learn Lua and to try and extend the Hollywood feature set at a basic level by implementing these modules I decide I would re-write the string implementation.

Now I had to attack this one slightly differently than some of the other modules I have been converting because it is actually a part of the core Lua implementation and is written in C. My first attempt at re-writing some the routines for gsub for example worked well with the the first example and it produced the expected result but a more complex example failed to re-produce the correct output.

I put this down to many things (my experience being the main one) however one common recurring piece of code kept on cropping up and I did my best to try and implement it in lua but I am not certain if it is working correctly. Perhaps someone can give me a better explanation than the one I have read regarding the following C syntax. Understanding the logic will hopefully allow me to re-implement this better than I currently have it hopefully solve my problem.

The statement is simple it is (obviously the variables are nonsensical for this example bt I was more interested in understanding the logic).

a ? b : c
Now, that's an easy one:

Code: Select all

// C code
r = a ? b : c;
corresponds to

Code: Select all

; Hollywood code
If a
 r = b
Else
 r = c
EndIf
Or shorter:

Code: Select all

r = IIf(a, b, c)
See also the Hollywood documentation to the IIf() command. But be careful: Contrary to C, Hollywood evaluates all expressions if you use IIf(). I.e. the following code would not work:

Code: Select all

// C code! divide a by 2 if a is not 0 else return 0
r = (a != 0) ? a / 2 : 0;
You can't write this as

Code: Select all

; Hollywood code
r = IIf(a <> 0, a / 2, 0)
because Hollywood will evaluate all expressions in any case. So if "a" is really 0 you would get a "division by zero" error with the above code. In this case you would have to do it explicitly, i.e.

Code: Select all

If a <> 0
 r = a / 2
Else
 r = 0
EndIf
Any explanations would be greatly appreciated.

My attempt at trying to resolve this is to use if else statements
No, it's just the other way around... see above :)
User avatar
airsoftsoftwair
Posts: 5834
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[18 Aug 2008] Re: Converting some lua extensions

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 18 Aug 2008 15:35:28 +0200

[...]

See also the Hollywood documentation to the IIf() command. But be careful: Contrary to C, Hollywood evaluates all expressions if you use IIf(). I.e. the following code would not work:

Code: Select all

// C code! divide a by 2 if a is not 0 else return 0
r = (a != 0) ? a / 2 : 0;

You can't write this as

; Hollywood code
r = IIf(a <> 0, a / 2, 0)
because Hollywood will evaluate all expressions in any case. So if "a" is really 0 you would get a "division by zero" error with the above code. In this case you would have to do it explicitly, i.e.

Code: Select all

If a <> 0
r = a / 2
Else
r = 0
EndIf
Aargh, bad example... of course the division must be

2 / a
Locked