Page 1 of 1

Allow a line to start with (

Posted: Sat Aug 06, 2011 6:27 am
by netmaestro
This compiles without problems:

Code: Select all

n|32>>1
Is there a reason why this shouldn't compile too?

Code: Select all

(n|32)>>1
I ask because >> takes precedence over | in the order of operations and there are times when I need to override this using brackets. All of which is fine until I try to put (n|32)>>1 in a macro which might need to serve as input to another macro. By itself it won't compile and I can't use n=(n|32)>>1 because that will change the value of n in the resulting formula. See the difficulty? This shows why it's a problem:

Code: Select all

n=4
Debug (n|32)>>1
Debug n|32>>1
Surely macros would be much more flexible if you could begin a line with brackets. I'm currently working on some SHA2 implementations and there's some pretty fancy bitshifting involved. If this worked it would simplify things a lot. Thanks for listening!

Also, if anyone knows how I can overcome this without the feature request (which could take some time if ever) please, post in here :mrgreen: There are some pretty skilled macro masters lurking around here. Unfortunately, I'm not one of them.

Re: Allow a line to start with brackets

Posted: Sat Aug 06, 2011 6:48 am
by Danilo
netmaestro wrote:This compiles without problems:

Code: Select all

n|32>>1
It compiles because the result goes to n.
Its like "n + 1" is a shortcut for "n = n + 1".
and "n|32>>1" is a short version of "n = n | 32 >> 1".
netmaestro wrote:Is there a reason why this shouldn't compile too?

Code: Select all

(n|32)>>1
Yes. The reason is that this is an expression only, but there
is no left hand side where the result of this expression goes to.
Without the lhs the expression alone makes no sense.
netmaestro wrote:Surely macros would be much more flexible if you could begin a line with brackets. I'm currently working on some SHA2 implementations and there's some pretty fancy bitshifting involved. If this worked it would simplify things a lot. Thanks for listening!
It already works with macros:

Code: Select all

Macro doIt(n)
(n|32)>>1
EndMacro

x = doIt(12)
The following does not work:

Code: Select all

Macro doIt(n)
(n|32)>>1
EndMacro

doIt(12)
It does not work because there is no left hand side.
Where do you expect the result of the expression
if you dont say where it should go?

Without brackets it works only because its a shortcut
and n is also the lhs, where the result of the expression
is stored.

It is the same with the following expressions:
1 + 2
(1 + 2)

Doesnt make sense if the expression stands alone
without specifying a target where the result goes to.

Re: Allow a line to start with brackets

Posted: Sat Aug 06, 2011 6:55 am
by netmaestro
Thanks! I appreciate the help. Maybe you can show me how to convert these c macros to Purebasic and I'll be eternally grateful:
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
They were written by Christopher Devine. I'm not too proud to learn, if there's anything you can offer. Thanks in advance.

Re: Allow a line to start with brackets

Posted: Sat Aug 06, 2011 7:04 am
by Danilo
netmaestro wrote:Thanks! I appreciate the help. Maybe you can show me how to convert these c macros to Purebasic and I'll be eternally grateful:
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
They were written by Christopher Devine. I'm not too proud to learn, if there's anything you can offer. Thanks in advance.
Just straight forward:

Code: Select all

;#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
Macro SHR(x,n)
  ((x & $FFFFFFFF) >> n)
EndMacro

;#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
Macro ROTR(x,n)
  (SHR(x,n) | (x << (32 - n)))
EndMacro

x = SHR(%1000,2)
Debug Bin(x)

x = ROTR(%1000,7)
Debug Bin(x)

Re: Allow a line to start with brackets

Posted: Sat Aug 06, 2011 7:20 am
by netmaestro
Danilo, thanks so much for your help. Every time I tried to start a line in a macro with a bracket I got a syntax error. I've probably been at it for too many hours today, I'll try to find my (probably stupid) error tomorrow. Thanks again, it's people like you that keep this forum strong.

Re: Allow a line to start with brackets

Posted: Sat Aug 06, 2011 7:25 am
by Danilo
No problem... sleep well... gn8 ;)