Shop OBEX P1 Docs P2 Docs Learn Events
Propeller Tool: Make IF A:= report an error. — Parallax Forums

Propeller Tool: Make IF A:= report an error.

Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
edited 2011-10-16 12:19 in General Discussion
This one has gotten me twice now during late night programming sessions..

Any chance that Propeller Tool could spot the following error on compile?

IF A:= instead of IF A ==

I can't think of any reason for the first example except to create aggravation :)

OBC

Comments

  • TorTor Posts: 2,010
    edited 2011-10-14 13:43
    I'm not familiar enough with Spin to know.. but is 'if a:=' invalid? Or is it valid, like in C? In C you can write 'if (a = operation())' { do_something();}
    and it will do three things:
    1: Perform a call to operation()
    2: Assign the return value from operation() to a
    3: If a is different from zero then do_something() will be performed.
    So it's a short form of
    a = operation();
    if (a) {do_something();}

    Granted, that's error prone in C too.. so some compilers, including GCC, will produce a warning unless you explicitly tell it that yes, you meant '=' and not '=='. So the above should be written as follows to avoid a warning:
    if ((a = operation()))

    Now, the above is all about C.. it may be totally irrelevant if 'if :=a' isn't even legal in Spin, or if it works differently. I guess I should simply load up a test program in my QuickStart.

    -Tor
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-10-14 14:03
    Could we add a warning for "if A >=" or "if A <=" also? :)

    I doubt if the Prop tool will ever have a warning feature like that, but maybe someone could write a Spin checker utlity. Another useful thing to check for is whether variables are used consistently between the calling program and the method that is called. This would help catch errors with pointers and floating point values.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-10-14 14:05
    It would be a mistake to make it an error. For example, this is perfectly valid and highly useful:
    if (a := strng[ptr++])
      sio.out(a)
    

    -Phil
  • TorTor Posts: 2,010
    edited 2011-10-14 14:10
    It would be a mistake to make it an error. For example, this is perfectly valid and highly useful:
    [..]
    Right, so just like C then. In GCC you would add an extra set of () around the expression to tell the compiler you meant to do so, no warning needed.

    -Tor
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-10-14 19:40
    Surely the tool could be smart enough to know the difference... no?
    It would be a mistake to make it an error. For example, this is perfectly valid and highly useful:
    if (a := strng[ptr++])
      sio.out(a)
    

    -Phil
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-10-14 21:06
    What exactly is the difference? I'm afraid don't see it. You wanted the compiler to error out on if a :=, and that's the form my example takes.

    -Phil
  • RavenkallenRavenkallen Posts: 1,057
    edited 2011-10-16 12:19
    That seems to be a very common mistake, even in C programming(Yes, i am reading up on C:)).
    := Is an assignment operator and,
    == is a comparison command
    I have had countless programs messed up by accidentally forgetting about that. Now i just check the whole program over looking for those commands specifically, to know if that was the cause of the error. And a lot of the time, it is.
Sign In or Register to comment.