Your Digital Media Has Never Looked So Good

 
EnTerr
** Valued Community Member **
Topic Author
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

"IF" after statement = Syntax Error?

Mon Aug 25, 2014 2:47 am

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)
 
EnTerr
** Valued Community Member **
Topic Author
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 10:29 am

Any clue why it says "syntax error" ?
 
User avatar
TheEndless
** Valued Community Member **
Posts: 9231
Joined: Mon Oct 04, 2004 10:15 am
Location: US
Contact:

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 11:29 am

For what it's worth, it just plain looks like a syntax error to me.. :P
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)
 
belltown
Posts: 1465
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 12:17 pm

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).
https://github.com/belltown/
 
EnTerr
** Valued Community Member **
Topic Author
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 1:07 pm

TheEndless wrote:
For what it's worth, it just plain looks like a syntax error to me.. :P
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 :wink:
 
EnTerr
** Valued Community Member **
Topic Author
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 1:13 pm

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.
 
belltown
Posts: 1465
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 1:33 pm

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.
https://github.com/belltown/
 
EnTerr
** Valued Community Member **
Topic Author
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 2:51 pm

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
 
belltown
Posts: 1465
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 3:55 pm

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).
https://github.com/belltown/
 
EnTerr
** Valued Community Member **
Topic Author
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 7:05 pm

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.
Last edited by EnTerr on Fri Sep 26, 2014 7:10 pm, edited 1 time in total.
 
belltown
Posts: 1465
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 7:09 pm

You totally missed my point.

I give up.
https://github.com/belltown/
 
EnTerr
** Valued Community Member **
Topic Author
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: "IF" after statement = Syntax Error?

Fri Sep 26, 2014 7:41 pm

belltown wrote:
You totally missed my point.
I give up.

You mean i did not respond to the end of your message when you stretched word meanings, by taking block-IF being called in some doco places "multi-line" (to distinguish it from one-liner-IF) and then combined it with your own musing that only single-line statements can be combined with ":" for a single line - to come up with this?
"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)."

I chose not to, because you did a silly word-play there. And also since i showed that WHILE and FOR are as "multi-line" as IF is, thus the 1st example
i = 5: for j = 1 to 5
    ? i + j
end for
shouldn't work by your logic since "multiple line statement cannot be used after the line continuation character" and "multiple lines cannot occupy a single line".
Well, we tried it and it works, so umm... it seems you mis-interpret the meaning of ":" to reach desired conclusion (block-IF does not work after ":" ) ?

PS. I like that you venture off the beaten path, like question whether FOR/ENDFOR is a single or two separate statements (btw, the first programming language i learnt was a version of BASIC where FOR and NEXT were two separate statements and matching was done runtime, so funny things may happen jumping in and out of loops with GOTO). But you should check your theories too, not select one just because they fits the intended conclusion
 
User avatar
Komag
Posts: 799
Joined: Fri Aug 22, 2014 3:42 am

Re: "IF" after statement = Syntax Error?

Sat Sep 27, 2014 2:40 am

I've wrangled with single line vs multi line IF handling, and it is definitely a unique beast. Different combinations of nested IF's, some single line and some multi-line, have to be handled with kid gloves I've found. Things that "should" work sometimes didn't. I don't know if I would call it a bug, just picky maybe.
 
EnTerr
** Valued Community Member **
Topic Author
Posts: 3834
Joined: Sun Jan 02, 2011 2:41 am

Re: "IF" after statement = Syntax Error?

Sat Sep 27, 2014 12:09 pm

Komag wrote:
I've wrangled with single line vs multi line IF handling, and it is definitely a unique beast. Different combinations of nested IF's, some single line and some multi-line, have to be handled with kid gloves I've found. Things that "should" work sometimes didn't. I don't know if I would call it a bug, just picky maybe.

It seems very tricky at first indeed. But "there is method to the madness" and when you get the knack of it, there should be no trouble.

Surely it could have been explained better - or at all - in the documentation. Currently there is no explanation whatsoever on the differences, instead one is left to guess it from the two examples. I suspect whoever wrote thought there could be only one way to guess it, well obviously duh... aa-and that's not the case! But it is easy to have blind spots when one is so close to the subject matter, can't blame them.

At first i wondered too - how does interpreter decide which one to stick with - could it be by look-ahead to see if `then` or `:` or `else` are used on the same line? That is not it though - `then` is unabashedly optional, can be skipped and rather it checks to see if there is another statement on the same line AFTER the IF. You can put any simple statement (that is, a non-block statement - FOR/WHILE are excluded, at least in current incarnation) on the same line following IF and that makes it a 1-liner; otherwise a block-IF will be assumed.

It was not a wise choice since causes confusion. If i were designing the language (and had to stick with BASIC), i would have distinguished the two IF forms more - likely by mandating that having "THEN" afterwards means 1-line IF and multi-line one is without `then` - just to be extra clear on who is who. But it is too late to fix it now.

Let me try to formalize the rules:
  • At least one statement following IF on the same line determines it to be a 1-line-IF, otherwise it is a block-IF.
  • THEN is optional after (any kind of) IF and does not affect the meaning
  • Only simple-statements can partake in one-line-IFs (no block IF/FOR/WHILE)
  • Multiple statements can follow IF on the same line, followed by an optional ELSE clause
  • If ELSE keyword is present in 1-line-IF, it must be followed by one or more statements

PS. I just realized i am 1 letter short of spelling "ATOMIC" in acrostic. Any ideas for a "C"-rule? :mrgreen:
 
User avatar
Komag
Posts: 799
Joined: Fri Aug 22, 2014 3:42 am

Re: "IF" after statement = Syntax Error?

Mon Sep 29, 2014 9:20 am

I know all that now (good summary!), but there's something odd going on when mixing/nesting one-line versions and multi-line versions. I can't remember exactly the example I had to re-work in my program - I should try to recreate the problem I found...

(for ATOMIC...)
(Care must be taken to...)

Who is online

Users browsing this forum: No registered users and 6 guests