Another mysterious compiler issue
DHawthorne
Posts: 4,584
OK, scratching my head over this one ...
The following snippet generates a compiler (syntax) error:
Move the SEND_LEVEL line so it's before the sMainVolume assignment line, and it compiles fine. Leave it as written, but remove the sOldTunerFreq declaration, and it compiles fine. Changing the order of the LOCAL_VAR declarations has no effect. Remove the semicolons after the LOCAL_VAR declarations, and it also compiles fine .
Why? Clearly the compiler is having trouble determining where the declarations end and the code begins, but I thought semicolons were the definitive answer to that particular problem.
The following snippet generates a compiler (syntax) error:
DEFINE_DEVICE dvTP = 10001:1:0 DEFINE_VARIABLE VOLATILE INTEGER nVolume[3] ; DEFINE_CALL 'Panel Update' { LOCAL_VAR CHAR sOldTunerFreq[10] ; LOCAL_VAR CHAR sMainVolume[2][4] ; sMainVolume[1] = ITOA(nVolume[1]) ; SEND_LEVEL dvTP, 1, nVolume[1] ; }
Move the SEND_LEVEL line so it's before the sMainVolume assignment line, and it compiles fine. Leave it as written, but remove the sOldTunerFreq declaration, and it compiles fine. Changing the order of the LOCAL_VAR declarations has no effect. Remove the semicolons after the LOCAL_VAR declarations, and it also compiles fine .
Why? Clearly the compiler is having trouble determining where the declarations end and the code begins, but I thought semicolons were the definitive answer to that particular problem.
0
Comments
compiles without error as does this:
I was not aware that Netlinx would permit C style line terminations. Is there a technote or anything which explains when this is advisable?
Harold
We had this discussion here a while ago; there are times when the compiler doesn't properly recognize the end of a declaration (generally when the next line is a DEVCHAN [] notation), and the semicolon forces it to recognize that wasn't intended as part of your declaration. We surmised that some other mysterious compiler errors might be due to something similar. Since that discussion, I have been using them on all my projects.
check the studio help file
The keyword LOCAL_VAR specifies a variable that is static. A static variable?s value is initialized the first time the statement block in which it is declared is executed and retained after execution of the statement block has finished. To provide compatibility with the Axcess language, local variables may be declared right before the opening brace for DEFINE_CALL declarations only.
- Chip (Was so very happy to nuke DEFINE_CALLs the day Netlinx came out)
Is there a performance difference? I regularly use DEFINE_CALL whenever I don't need a value returned. Not exactly sure why this is, but I think it has to do with some of my early years programming experience.
Jeff
I found when I started to program in other languages I got lots of compilation errors because I wasn't used to using colons.
using DEFINE_FUNCTIONS and colons are a good habbit to get into if you ever plan to program in other languages and make your code more portable.
I doubt it. I just can't stand having to put "CALL 'SOME_FUNCTION'" all over my program when I can do "Some_Function ()" instead.
Just a personal taste thing with me.
- Chip
We have a note in our database for Studio 1.20 which has a similar problem. It was a compiler bug where you have to end the LOCAL_VAR line with a semicolon if a Array-Element assignment was done directly after the LOCAL_VAR.
But I found one more variation to get it to work: This is the way I declare LOCAL_VARs or STACK_VARs if I have more than one of a type.
In this notation it doesn't matter if a semicolon is used or not to end the LOCAL_VAR line.
I have any number of ways to make that piece of code work, that's not what I am looking for. I want to understand why it didn't work the first time, to avoid problems in the future that aren't so easy to see are happening.
I agree the compiler is having trouble but I think it knows that the declarations have ended and the code begins because if you comment out the line INTEGER a[1]; the error Symbol [A] not defined is generated for the line a[1]=5;
I certainly can?t explain it and I hope it's looked into. A confused compiler is never a good thing.
IF you add a second declaration line like:
STACK_VAR CHAR sOldTunerFreq2[10], sMainVolume2[2][4];
and end it in a semicolon, the illegal syntax error will be generated.
Isn?t programming fun?
Regarding the DEFINE_CALL vs. DEFINE_FUNCTION debate, I?m in the Chip, champ, and cbaily camp. I was immediately converted the first time Chip voiced his opinion some time ago. I?ll never write another DEFINE_CALL again.
Perhaps you all like DEFINE_FUNCTION because that is what 'real' programmers use. But, I will argue that the code is much more readable using DEFINE_CALL because they are named as literal strings. And with my editor color code scheme literal strings are very easy to locate in a program.