Menu

Code review - Fix for Script where the fix now can determine that a constant exists

Anobium
2021-10-11
2021-10-16
  • Anobium

    Anobium - 2021-10-11

    This is a fix for scripting where the compiler will now correctly handle that a constant exists.

    There is no change to the scope - it is that IF CONSTANT or IF DEF(CONSTANT) now works are expected. This has been a very long standing issue.


    The following code shows the issue. The existence of the MYNONEXISTANTCONSTANT is ignore in the existing compiler. The result is that SCRIPTNUMBER in the ASM will always be 2 - regardless if MYNONEXISTANTCONSTANT is defined or not.

    The new compiler build #1042 resolves.

    #chip 16f88
    '#chip mega328p
    
    #DEFINE MYNONEXISTANTCONSTANT
    
    #SCRIPT
    
     'Set SCRIPTNUMBER to 255 so we know it had changed
      SCRIPTNUMBER = 255
    
      IF AVR Then
      'ONLY Set when AVR
    '    Warning "AVR"
        SCRIPTNUMBER = 55
      END IF
    
      IF PIC Then
        'ONLY Set when PIC
    '    Warning "PIC"
        SCRIPTNUMBER = 170
      END IF
    
    
      IF DEF(MYNONEXISTANTCONSTANT) Then
        'ONLY Set when MYNONEXISTANTCONSTANT exists
          SCRIPTNUMBER = 1
      END IF
    
      IF NODEF(MYNONEXISTANTCONSTANT) Then
          'ONLY Set when MYNONEXISTANTCONSTANT does not exist
          SCRIPTNUMBER = 2
      END IF
    
    #ENDSCRIPT
    
    'Main program to create ASM to shown in the ASM the value of SCRIPTNUMBER
        #ifdef AVR
          ldi SysValueCopy,SCRIPTNUMBER
        #ENDIF
    
        #ifdef PIC
          movlw SCRIPTNUMBER
        #ENDIF
    
        'USART settings for USART1
        #define USART_BAUD_RATE 9600
        #define USART_TX_BLOCKING
        #define USART_DELAY OFF
    
        HSerPrint ""
    

    As this is a huge change to the compiler there is a need to disable if this causes issues. Add the following constant to the program to disable this change - then report via the forum. What will go wrong? Dunno.... but, someone may have write a script that has been passive up to now that will suddenly burst into life.

    To disable, add

    #DEFINE DISABLECHANGE1042
    

    Enjoy

     
  • Anobium

    Anobium - 2021-10-11

    I have been asked of show the working commands. This code shows the commands in the code below show the use

    Case 1: IF and IF DEF() are the same operation
    Case 2: IF NOTDEF() has no other equivalent

    Example is code - change the chip to see the difference.

    #chip 16f88
    '#chip mega328p
    
    #SCRIPT
    
      if NODEF(PIC) then
        warning "Not a PIC"
      end if
      if PIC then
        warning "PIC using IF"
      end if
      if DEF(PIC) then
        warning "PIC using IF DEF()"
      end if
    
      if NODEF(AVR) then
        warning "Not an AVR"
      end if
      if AVR then
        warning "AVR using IF"
      end if
      if DEF(AVR) then
        warning "AVR using IF DEF()"
      end if
    
    #ENDSCRIPT
    

    Enjoy

     
  • stan cartwright

    stan cartwright - 2021-10-12

    IF CONSTANT or IF DEF(CONSTANT) now works are expected. This has been a very long standing issue.
    Again, not bothered me. I use gcb in like simple mode. The problems that have been sorted I never found. Maybe I was lucky I never found the problems. If var suits me. What's the difference between a defined constant and a dimmed var? ie a dimmed var stays constant unless changed. Is this a different topic?
    It's like sinclair basic vs bbc basic... if that's when you first learned "basic"... then there's commodore basic. It's what you could afford at the time. MS basic wasn't around until pc's like amstrad. Forgot amstrad 464 basic. :)

     
    • Anobium

      Anobium - 2021-10-12

      Stan, you are totally right. This is a library developer issue that is fixed. Before the fix there were many hoops we had to implement to understand if a constant existed.

      So, your points are totally valid for most users.

       
  • William Roth

    William Roth - 2021-10-13

    An observation. It seems that the existence of a constant can only be tested IF the constant has a null value.

    '--- This Works as Expected  ---
    #DEFINE MYCONSTANT
    
    #Script
      IF MYCONSTANT then
        Warning "MYCONSTANT DEFINED"
      END IF
    #ENDSCRIPT
    '----------------------------
    
    '--- This does *NOT* Work as expected
    #DEFINE MYCONSTANT 0
    
    #Script
      IF MYCONSTANT then
        Warning "MYCONSTANT DEFINED"
      END IF
    #ENDSCRIPT
    '----------------------------
    
    'If the constant is not a Null value then
    'existence cannot be tested for
    
     
    • Anobium

      Anobium - 2021-10-13

      Good spot. Needs to be sorted out.

       
  • William Roth

    William Roth - 2021-10-13

    This also does not work as expected. "MyConstant" clearly exists and is defined with a value of 255. However the script determines that MyConstant is NOT defined.

    '--- This does *NOT* Work as expected
    #DEFINE MYCONSTANT 255
    
    #Script
      IF NODEF(MYCONSTANT) then
        Warning "MYCONSTANT NOT DEFINED"
      END IF
    #ENDSCRIPT
    '----------------------------
    
     
    • Chris Roper

      Chris Roper - 2021-10-13

      should that not read
      IF NDEF rather than IF NODEF
      I am not near a copy of GCBASIC to test so just an observation.

       

      Last edit: Chris Roper 2021-10-13
      • Anobium

        Anobium - 2021-10-16

        It is NODEF. :-)

         
  • William Roth

    William Roth - 2021-10-13

    Somewhat related ....

    If #ENDSCRIPT is missing from the script construct ... G+Stools hangs instead of issuing an error or warning.

     
    • Anobium

      Anobium - 2021-10-14

      #ENDSCRIPT issued fixed in build 1043

      Evan

       
  • Anobium

    Anobium - 2021-10-16

    Update SCRIPT capability in build 1045.

    This ensure script functions DEF() and NODEF() work as expected.

    '#chip 16f88
    #CHIP MEGA328P
    
    #DEFINE MYCONSTANT 0
    
    #Script
      IF PIC Then
        Warning "PIC1"
      End IF
    
      IF AVR Then
        Warning "AVR1"
      End IF
    
      IF DEF(PIC) Then
        Warning "PIC2"
      End IF
    
      IF DEF(AVR) Then
        Warning "AVR2"
      End IF
    
      IF NODEF(PIC) Then
        Warning "!PIC"
      End IF
    
      IF NODEF(AVR) Then
        Warning "!AVR"
      End IF
    
      IF MYCONSTANT then
        Warning "MYCONSTANT EXISTS1"
        Warning "MYCONSTANT value = "MYCONSTANT
      END IF
    
      IF DEF(MYCONSTANT) then
        Warning "MYCONSTANT EXISTS2"
        Warning "DEF(MYCONSTANT) value = "MYCONSTANT
      END IF
    
    
      IF NODEF(MYCONSTANT) then
        Warning "MYCONSTANT DOES NOT EXIST"
      END IF
    
      Warning "end"
    
    #ENDSCRIPT
    

    Yields - which means a script can now detect constants.

    Warning: AVR1
    Warning: AVR2
    Warning: !PIC
    Warning: MYCONSTANT EXISTS1
    Warning: MYCONSTANT value = 0
    Warning: MYCONSTANT EXISTS2
    Warning: DEF(MYCONSTANT) value = 0
    Warning: end
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.