Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
EnTerr
Roku Guru

"IF" after statement = Syntax Error?

Well this is weird - seems that "if" statement following another statement on the same line causes "Syntax Error. (compile error &h02)". Example:
dice = rnd(6): if dice < 2 then
? "You get nothing"
end if

I don't see any other statement behaving like that (else, end if, while, end while, for, end for etc don't seem to mind sharing a line with buddies). Probably a compiler issue?

But why would i want to do such a strange thing, you may ask? I have a use case, i want to profile my code and i can do this simply by inserting in the beginning of every line a single call to a function that collects the stats. That works no matter what the rest of the line, even if empty or commented out... bar IF. It's way easy to "instrument" and remove this in text editor using rectangle-block select. Here is how that looks like:
sub _profile(next_line, tm, acc):
t = acc[next_line-1]
if t = invalid then t = 0
acc[next_line-1] = t + tm.TotalMilliseconds()
tm.mark()
end sub

function xml_parse(xml as string):
_ = []: ti = createobject("roTimespan")
_profile(LINE_NUM, ti, _): leafs = []
_profile(LINE_NUM, ti, _): tags = xml.tokenize("<")
_profile(LINE_NUM, ti, _): startN = 0
_profile(LINE_NUM, ti, _): if tags.count() > 0 and left(xml, 1) <> "<" 'line 183
_profile(LINE_NUM, ti, _): startN = 1
_profile(LINE_NUM, ti, _): if tags[0].trim() <> "": 'line 185
_profile(LINE_NUM, ti, _): leafs.push(tags[0])
_profile(LINE_NUM, ti, _): end if
_profile(LINE_NUM, ti, _): end if
...

And here is how compiler grumbles:
*** ERROR compiling /pkg:/source/something.brs:
Syntax Error. (compile error &h02) in ...8n/pkg:/source/something.brs(183)
Syntax Error. (compile error &h02) in ...8n/pkg:/source/something.brs(185)
0 Kudos
17 REPLIES 17
EnTerr
Roku Guru

Re: "IF" after statement = Syntax Error?

Any clue why it says "syntax error" ?
0 Kudos
TheEndless
Channel Surfer

Re: "IF" after statement = Syntax Error?

For what it's worth, it just plain looks like a syntax error to me.. 😛
That is some bizarre looking code...
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
belltown
Roku Guru

Re: "IF" after statement = Syntax Error?

It's probably just the way the interpreter works. The BrightScript Reference states:

Each line may contain a single statement, or a colon ( : ) may be used to separate multiple statements on a single line.


In order for an IF statement to occur on a single line, you have to use the single-line form of the IF statement, yet you're trying to use the multi-line form. It's giving you a syntax error on that line because it's assuming you're using the single-line form of the IF statement, which has a statement following the conditional expression, yet you have no statement there (on that line).
0 Kudos
EnTerr
Roku Guru

Re: "IF" after statement = Syntax Error?

"TheEndless" wrote:
For what it's worth, it just plain looks like a syntax error to me.. 😛
That is some bizarre looking code...
Pray tell, how does that look like a syntax error?
Writing `a=1 : b=2` vs broken on two lines is concern of programming style. Unlike me and you though, compilers/interpreters do not opine on coding style 😉
0 Kudos
EnTerr
Roku Guru

Re: "IF" after statement = Syntax Error?

"belltown" wrote:
It's probably just the way the interpreter works. The BrightScript Reference states:
Each line may contain a single statement, or a colon ( : ) may be used to separate multiple statements on a single line.

Aha! That's a good try at explanation, i went through that. Until i tried
i = 5: for j = 1 to 5
? i + j
end for
- and see that it works. As well as all the other compound statements. Bolding out those words is creative reading, it does not mean only simple statements are allowed.
0 Kudos
belltown
Roku Guru

Re: "IF" after statement = Syntax Error?

I think that's because the interpreter considers that FOR is a statement, ? is a statement, and END FOR is a statement, so you have 3 separate statements. There are several references to the "END FOR statement" in the documentation but no references to an "ENDIF statement". ENDIF is part of a multi-line IF statement, and there are no other references in the documentation to any other "multi-line" statements that exist.
0 Kudos
EnTerr
Roku Guru

Re: "IF" after statement = Syntax Error?

"belltown" wrote:
I think that's because the interpreter considers that FOR is a statement, ? is a statement, and END FOR is a statement, so you have 3 separate statements. There are several references to the "END FOR statement" in the documentation but no references to an "ENDIF statement". ENDIF is part of a multi-line IF statement, and there are no other references in the documentation to any other "multi-line" statements that exist.

I don't understand what do you mean - or are you being "devil's advocate" here?
First, there is no difference between "END FOR" and "ENDFOR" and same is the case for "ENDIF" and "END IF", "END WHILE" and "ENDWHILE".
Second, "END WHILE statement" is also not mentioned but that does not mean this does not work:
i = 1 : while i < 4
? i : i = i + 1
end while
0 Kudos
belltown
Roku Guru

Re: "IF" after statement = Syntax Error?

I probably didn't explain myself very well. My explanation had nothing to do with the difference between "ENDFOR" and "END FOR". And although the manual refers to "Block Statements" such as "WHILE-END WHILE", as far as how the BrightScript interpreter operates, I think it's looking at "WHILE condition" as being a separate statement in its own right for parsing purposes, as is "END WHILE" or "ENDWHILE".

In most (higher-level) languages:
FOR condition
blah
blah
blah
END FOR


would be considered a single "statement", albeit a compound one.

I'm just postulating that maybe BrightScript (possibly due to its simplistic BASIC origins), may at some level (at least as far as "continuation" is concerned), consider each of the above lines to be a separate "statement".

You can't split these "statements" across lines.

That's why you can write:
FOR condition
but not:
FOR
condition


or:
END FOR
but not:
END
FOR


or:
a="hello"
but not:
a=
"hello"


or:
WHILE i < 4
but not:
WHILE
i < 4


What makes IF unique in BrightScript is that it has two forms, as a single-line statement or a multi-line statement.

The line continuation character allows "multiple statements on a single line".
IF a=b print "Hello"
is a valid statement (the single-line form of an IF statement), so therefore can appear after a continuation character.

However,
IF a=b
is not a valid statement (in the same way that FOR by itself is not a valid statement), therefore cannot appear after a continuation character.

In the case of IF, the language specifically defines a multi-line form and allows IF a=b to be used in that way, but in that case multiple lines are used. Since, by definition, multiple lines cannot occupy a single line, a multiple line statement cannot be used after the line continuation character given the above definition of the line continuation character (multiple statements on a single line).
0 Kudos
EnTerr
Roku Guru

Re: "IF" after statement = Syntax Error?

"belltown" wrote:
... although the manual refers to "Block Statements" such as "WHILE-END WHILE", as far as how the BrightScript interpreter operates, I think it's looking at "WHILE condition" as being a separate statement in its own right for parsing purposes, as is "END WHILE" or "ENDWHILE".
...
I'm just postulating that maybe BrightScript (possibly due to its simplistic BASIC origins), may at some level (at least as far as "continuation" is concerned), consider each of the above lines to be a separate "statement".

Ok, so you think that maybe FOR and WHILE block statements are different from the block form of IF (aka "multiline-IF"). The manual calls all of them "block" statements at some point or another - but let's check with the interpreter too:
BrightScript Debugger> for i=1 to 10
A block (such as FOR/NEXT or IF/ENDIF) was not terminated correctly. (compile error &hb5) in $LIVECOMPILE(1190)
BrightScript Debugger> while True
A block (such as FOR/NEXT or IF/ENDIF) was not terminated correctly. (compile error &hb5) in $LIVECOMPILE(1191)
BrightScript Debugger> if True then
A block (such as FOR/NEXT or IF/ENDIF) was not terminated correctly. (compile error &hb5) in $LIVECOMPILE(1192)

Fine, so they are the same kind of animal. Now onto another question raised - are "END FOR" and "END WHILE" separate statements from FOR and WHILE? Is it possible that compiler does not match block opening to block closing (as a single compound statement) but instead does it on the fly at runtime. That's a valid thinking and might be the case in some other language - but not in BrSc. We can check - if that's the case, unmatched closing tags (or interlaced tags) will be run-time and not compile-time error:
BrightScript Debugger> for i=1 to 2: end for: end for
Syntax Error. (compile error &h02) in $LIVECOMPILE(1193)
BrightScript Debugger> while False: end while: end while
Syntax Error. (compile error &h02) in $LIVECOMPILE(1194)
BrightScript Debugger> ? "".len
Syntax Error. (runtime error &h02) in $LIVECOMPILE(1167)
The last one was just to demonstrate that BS interpreter has such a thing as "Syntax Error (runtime error)", which is different from the normal "Syntax Error (compile error)".

There is no semantic justification of why block IF is allergic to preceding statement on the same line. The only reason i can think of is some problem/bug/quirk inside the parser.
0 Kudos