Why does putting a 1- operator before TO throw an exception?

If you have found a bug and would like to report it, it's here...

Why does putting a 1- operator before TO throw an exception?

Postby bobgillies » 02 Mar 2017 21:33

Code: Select all
// global variables

tvar: board
tvar: laneMask
tvar: currentLane
tvar: queenCount
tvar: queenRank
tvar: safe
tvar: threats


///////////////////////////////////////////////////////////////////////////////
Object Class new: Queens ( numQueens, mutable lane)                       
Queens method: initialize( numQueens  -- )                                   
    //  Diagonal downleft, downright, and vertical lanes of threats.       
    ArrayBuffer newWith( 3 , 0 ) := lane
    numQueens := numQueens   
    " number of queens are " . numQueens .
//     numQueens 1 bitLeft 1- to laneMask
    numQueens 1 bitLeft to laneMask
     "  lanemask is" . laneMask .
    numQueens to queenCount
            0 to queenRank
            1 to currentLane
    // Test to buffer each row of lanes threats to facilitate threat validation.
    ArrayBuffer new to threats
;

 Queens method: queenPut  ( n  -- )                                         
    //   n = bitpattern. 0b0010 queen in the 3rd column of a 4x4 board.     
    //  Calculate the position on the vertical lane bit where the queen       
    //  is placed for that row.         Usage:   0b0010 board queenPut 

    // Test to visually validate the lane threats per row                   
    1 @lane at 2 @lane at 3 @lane at bitOr bitOr
    dup "\nlanethreats" . .  threats add
    // End Test                                                             

    1 @lane at n bitOr 1 swap @lane put                                     
    2 @lane at n bitOr 2 swap @lane put
    3 @lane at n bitOr 3 swap @lane put
    queenRank 1+ to queenRank
    //-----------------Shift each lane by 1 bit position.
    1 @lane at 2* laneMask 2* 1- bitAnd 1 swap @lane put                     
    2 @lane at 2/  2 swap @lane put                                         


Queens method: nextSpot  \ b1 b2 -- n                                       
    //      b1 = The bitwise combination of each lane of threats.           
    //      b2 = The lane's bitmask                               
    //       n = bitpattern result for next safe queen position.             
   
   
    1 @lane at  2 @lane at  3 @lane at bitOr bitOr laneMask                 
    //---------------- If the bitwise AND operaton results in a zero.       
                                                                             
    while ( dup )
         [ 2dup bitAnd   
         
    //-----------------Put a Queen on this row if it's safe from threats.                     
            ifZero: [ 2/                                                     
                       
                       "\nQueen is to be put next at col" . 
                       currentLane .
                       currentLane 1- laneMask bitRight to safe                   
                       currentLane 1+ to currentLane  break
                    ]             
    //----------------Otherwise keep advancing to the next lane.             
              else: [ 2/ currentLane 1+ to currentLane ]                     
         ]
    2drop  1 to currentLane
    threats last laneMask 2* 1- = if "\nNo legal moves left" . return then                         
;

// Just simple repetitions before making this an iterating function.

: nQueens \ n q ==
Queens new to board
board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
board nextSpot safe board queenPut
;

1 7 nQueens



Hi Franck, at line 20, I've commented out a line of words that used #1- to facilitate making a bit mask for the laneMask global variable because it cause an exception regarding "#bitOr does not understand a null string". That bitOr throwing the exception I've discovered resides at line 40.

So I've removed the #1- and retried the rest of the code in the line below it. That doesn't throw the exception (although the bitmask is now incorrect as a result). Why does a simple #1- before a #to storing a number into a global variable cause the exception?

Regards,

Bob
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: Why does putting a 1- operator before TO throw an except

Postby bobgillies » 03 Mar 2017 00:41

Upon further thought, the laneMask computed at initialization should be stored as a constant. But I still don't understand why #to causes an string null exception by the #bitOr at line 40 when #1- precides #to, but causes no string null exception when #bitLeft precedes #to.

It's seems like #to has a different behavior at the REPL as opposed to behavior defined in a method that I'm not forseeing.
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: Why does putting a 1- operator before TO throw an except

Postby Franck » 03 Mar 2017 10:58

Hi,

I will check tonight and come back to you.

Franck
Franck
 
Posts: 162
Joined: 29 Oct 2014 19:01

Re: Why does putting a 1- operator before TO throw an except

Postby Franck » 03 Mar 2017 18:41

What I see is that, if you use 1-, laneMask is 127.

So, into #nextSpot, you never enter into the ifZero: [ ... ] test and so you never set the "safe" value.
It's value remains null and you can't use null with #bitOr. #bitOr only works with integers.

Franck
Franck
 
Posts: 162
Joined: 29 Oct 2014 19:01

Re: Why does putting a 1- operator before TO throw an except

Postby bobgillies » 04 Mar 2017 04:32

I believe you're right. Thank you.

Bob
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26


Return to Report a bug

Who is online

Users browsing this forum: No registered users and 1 guest

cron