A Bit of a Byte 4 >> 32

Just starting out? Need help? Post your questions and find answers here.
User avatar
PartTimeCoder
User
User
Posts: 14
Joined: Sat Jun 03, 2017 10:26 am

A Bit of a Byte 4 >> 32

Post by PartTimeCoder »

Hi there

as part of my 11 year old daughters "stay at home" education for IT I was delighted when they were given a project in Lua, I quickly jumped on board and started coaching her in what Lua can do besides printing "Hello World" in a console, I grabbed the Lua 5.3 include that was posted here in the forums and began writing "Lessons", we now have the basic makings of a Lua driven game engine using PB's 2D commands exported to Lua and in my daughters mind she wrote it herself understanding every line of code which has been a fantastic bonding experience.

The next lesson I am writing (callback functions) makes use of C Lua functions that require the use of the LUA_REGISTRYINDEX pseudo-constant which is not defined in the Lua 5.3 pb include.

the definition trail is as such:

lua.h:

Code: Select all

#define LUA_REGISTRYINDEX       (-LUAI_MAXSTACK - 1000)
luaconf.h:

Code: Select all

@@ LUAI_MAXSTACK limits the size of the Lua stack.
** CHANGE it if you need a different limit. This limit is arbitrary;
** its only purpose is to stop Lua from consuming unlimited stack
** space (and to reserve some numbers for pseudo-indices).
*/
#if LUAI_BITSINT >= 32
#define LUAI_MAXSTACK           1000000
#else
#define LUAI_MAXSTACK           15000
#endif
luaconf.h:

Code: Select all

@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'.
*/
/* avoid undefined shifts */
#if ((INT_MAX >> 15) >> 15) >= 1
#define LUAI_BITSINT    32
#else
/* 'int' always must have at least 16 bits */
#define LUAI_BITSINT    16
#endif
as I understand from what I can Google, a int value on 32 and 64 bit systems is 4 bytes or 32 bits, so would the following interpretation of the above definitions be correct for PB 32/64 bit??

Code: Select all

  #LUAI_BITSINT = 32
  #LUAI_MAXSTACK = 1000000
  #LUA_REGISTRYINDEX    = -(#LUAI_MAXSTACK-1000)
The last thing I want is for my daughter to present a project full of bugs because I got the constant values wrong.
User avatar
PartTimeCoder
User
User
Posts: 14
Joined: Sat Jun 03, 2017 10:26 am

Re: A Bit of a Byte 4 >> 32

Post by PartTimeCoder »

NM, with the above deceleration I got an IMA, with the below dec all it's good, function called and Lua stack intact after call of lua_ref/ lua_rawgeti

Code: Select all

  #LUAI_BITSINT = 32
  #LUAI_MAXSTACK = 1000000
  #LUA_REGISTRYINDEX    = -#LUAI_MAXSTACK-1000
Hint: just removed the brackets "()" and everything works, for anyone wondering!, or anyone willing to expand with an explanation of why it didn't work?

I now have complete 2 way communication between PB and Lua, now the fun begins lol
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: A Bit of a Byte 4 >> 32

Post by infratec »

That's math for beginners :wink:

Code: Select all

#LUAI_MAXSTACK = 1000000
Debug -(#LUAI_MAXSTACK-1000)
Debug -#LUAI_MAXSTACK-1000
That's why :!:
Olli
Addict
Addict
Posts: 1267
Joined: Wed May 27, 2020 12:26 pm

Re: A Bit of a Byte 4 >> 32

Post by Olli »

infratec wrote:That's why
I do not think so : It is the difference between the syntax of PB and the one of LUA.

If you were right, why does not LUA do a math product ?

:D

My suggest is possible, but my english misses. I try :

LUA needs brackets to put an unique op result in the constant, as PB does it in a macro.

Code: Select all

macro add0(a, b)
(a + b)
endmacro

macro add1(a, b)
a + b
endmacro

debug 8* add0(3, 5)
debug 8* add1(3, 5)
I prefer see NM (and others) give the right technical words, and learn again, than to think having right is the most important thing.

regards
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: A Bit of a Byte 4 >> 32

Post by infratec »

@Olli

I Simpy don't understand what you are writing.

If a mathematically calculated value is wrong, than it is wrong.

Because LUA

Code: Select all

#define LUA_REGISTRYINDEX       (-LUAI_MAXSTACK - 1000)
directly translated results in

Code: Select all

Debug (-#LUAI_MAXSTACK-1000)
Which gives the same result as

Code: Select all

Debug -#LUAI_MAXSTACK-1000
And not like

Code: Select all

Debug -(#LUAI_MAXSTACK-1000)
Doesn't matter in which langauge you are programming, math is math.
Olli
Addict
Addict
Posts: 1267
Joined: Wed May 27, 2020 12:26 pm

Re: A Bit of a Byte 4 >> 32

Post by Olli »

infratec wrote:If a mathematically calculated value is wrong, than it is wrong.
:shock: You are right : let's erase Sqr(-1) in the world : nobody found the result for 300 years !!

What I discover, above, mathematically on LUA, seems to be a product :

Code: Select all

#define SOMETHING another
If I think ONLY mathematically, before thinking someone did a mathematical mistake (basis anymore), I can see a product :

Code: Select all

a b
or

Code: Select all

ab
Syntax... :D
User avatar
PartTimeCoder
User
User
Posts: 14
Joined: Sat Jun 03, 2017 10:26 am

Re: A Bit of a Byte 4 >> 32

Post by PartTimeCoder »

infratec wrote:That's math for beginners :wink:

Code: Select all

#LUAI_MAXSTACK = 1000000
Debug -(#LUAI_MAXSTACK-1000)
Debug -#LUAI_MAXSTACK-1000
That's why :!:
Obviously, my issue was not the "math for beginners" it was the syntax of the equation, but your follow up posts answered my question "(-#LUAI_MAXSTACK-1000)" so thanks.

Is it always this hostile here :?
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: A Bit of a Byte 4 >> 32

Post by Demivec »

PartTimeCoder wrote:Is it always this hostile here
@PartTimeCoder: There hasn't been any hostility. Notice the 'wink' in the response. Your question was just being answered.
PartTimeCoder wrote:Hint: just removed the brackets "()" and everything works, for anyone wondering!, or anyone willing to expand with an explanation of why it didn't work?
infratec wrote:That's math for beginners :wink:
What did you think the explanation would say? It was simply a question of math. The results didn't match because you didn't copy the expression correctly. There weren't any hidden operations going on.

At least one additional note on things relating a perception of 'hostility'. This is an international forum, in English. Individuals sometime use translations or their own use of language isn't perfect. Sometimes expressions and ideas in their native language don't translate well to English and come off either odd, strange, or even 'harsh'. One has to leave his pride at the door and realize their is a difference between open hostility and either miscommunication or simple misunderstanding. Many friendly attempts to help come from forum users of different skill levels as well.

You've been around the forum for a couple of years. Individuals who have used the forums generally find them helpful and forum users willing to assist any honest attempts to learn more or address any issues. I've been around for a bit longer than you and I find the forums to be very helpful through all of that time, just not every response.
Olli
Addict
Addict
Posts: 1267
Joined: Wed May 27, 2020 12:26 pm

Re: A Bit of a Byte 4 >> 32

Post by Olli »

We can say that one syntax basis on PB is the "equal" sign '='. LUA seems to accept others syntax, ruled by the "space" sign ' '. On PB, space sign is fully neutral. There are any rare exceptions where space sign ' ' must be used, but it is never alone.

i.e. : with '#' sign in macro concatenation :

Code: Select all

Macro Xs()
X# MacroExpandedCount
EndMacro

X1 = 3
X2 = 5
X3 = 7

Debug Xs() ; will display 3
Debug Xs() ; will display 5
Debug Xs() ; will display 7
But this is very rare.

All the exceptions of the equal sign use, have an equivalent with the sign '=' :

Code: Select all

a >> 1 ;       a = a >> 1  ;   a = (a >> 1)
a << 1
a + 1
a - 1 ;   etc...
All these op have the same mechanism :

Code: Select all

a {arithmetic op sign} b

equ to

a =       a {arithmetic op sign} b

or, with brackets

a =      (a {arithmetic op sign} b)
To conclude, as '=' sign is the main assignment/move tool on the PureBasic, it seems that the issue came from bracket on LUA which were considered as a possible math priority, as it is a syntax priority, due to the LUA space sign use.
Post Reply