Why can't I chain "!" or "~" operators?
Dennis Ferron
Posts: 480
I want to toggle a pin from high to low and back to high. I tried this:
!!outa[noparse][[/noparse]Reset6502]
And the Spin compiler doesn't like it. It also won't let me get away with:
!(!outa[noparse][[/noparse]Reset6502])
It seems to me that that ought to work; it should be a one line equivalent to:
!outa[noparse][[/noparse]Reset6502]
!outa[noparse][[/noparse]Reset6502]
I would also like to be able to say:
(outa[noparse][[/noparse]Reset6502]~)~~
But that doesn't work either.
Edit: Spin needs C-style preprocessor macros! My kingdom for a macro feature! Then I could just say #define TOGGLE(x) !x; !x
!!outa[noparse][[/noparse]Reset6502]
And the Spin compiler doesn't like it. It also won't let me get away with:
!(!outa[noparse][[/noparse]Reset6502])
It seems to me that that ought to work; it should be a one line equivalent to:
!outa[noparse][[/noparse]Reset6502]
!outa[noparse][[/noparse]Reset6502]
I would also like to be able to say:
(outa[noparse][[/noparse]Reset6502]~)~~
But that doesn't work either.
Edit: Spin needs C-style preprocessor macros! My kingdom for a macro feature! Then I could just say #define TOGGLE(x) !x; !x
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Theron Luhn
"There are two things that are infinate: Human Stupidity and the universe. I'm not sure about the latter." - Mark Twain
or reseting a 6502 as his code would suggest
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Who says you have to have knowledge to use it?
I've killed a fly with my bare mind.
The operators "!", "~", and "~~" return the value of the pin, during runtime, after the operation takes place. When you attempt to apply the operator again, you are telling the compiler you want to apply it to the result of the previous operation (a number) and not to the pin itself. That, of course, makes no sense to the compiler, and it flags the error.
Now, having said that, it's possible when compiling to keep track of two elements of an operation: the value and the "lvalue", where one exists. The "l" stands for "left", and relates to the "value" of a variable when it's encountered on the lefthand side of an assignment operator. The lvalue is just a reference to the original operand and not to its assigned value. Then, when a subsequent operation is performed, the compiler has to decide, based on context, whether to apply that operation to the value or to the lvalue.
Sometimes when interpretations like what you hoped for are implemented, ambiguities creep into the language's semantics. Spin's semantics have the advantage of being simple and unambiguous. And I'm sure that was done on purpose.
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.