KSP-KOS/KOS

If statement documentation discrepancy (or bug)

brona-ruzicka opened this issue · 2 comments

On the documentation page about Flow Control (source here), there is a note about the if statement saying this:

NOTE

The period (.) is optional after the end of a set of curly braces like so:

// both of these lines are fine
IF TRUE { PRINT "Hello". }
IF TRUE { PRINT "Hello". }.

In the case where you are using the ELSE keyword, you must not end the previous IF body with a period, as that terminates the IF command and causes the ELSE keyword to be without a matching IF:

// works:
IF X > 10 { PRINT "Large". }  ELSE { PRINT "Small". }.

// syntax error - ELSE without IF.
IF X > 10 { PRINT "Large". }. ELSE { PRINT "Small". }.

But in the language definition file is the if statement defined like this:

if_stmt        -> IF expr instruction EOI? (ELSE instruction EOI?)?;

That means it is completely valid to write a . (EOI) before the ELSE keyword and no syntax error will be thrown if you do so. It also causes unexpected behaviour in scripts which include something like this:

IF condA
  IF condB {
    statements...
  }.
ELSE {
  statements...
}

Now, if you were to add the curly brackets everywhere they can be to better understand the flow structure, the documentation says it would look like this:

IF condA {
  IF condB {
    statements...
  }
} ELSE {
  statements...
}

But how the scipt behaves currently is as if it was like this:

IF condA {
  IF condB {
    statements...
  } ELSE {
    statements...
  }
}

Tested in KSP 1.12, KOS 1.13.2

I am not quite sure if this is a bug or documentation issue. However, I recommend for the language definition not to be changed as it would change the behaviour of some scripts. I would remove the whole documentation note, or better modify it to say the contrary.

Also I've just noticed, that the documentation doesn't mention the option to have just one statement (instead of a whole block) as the body of an if statement. Like so:

IF cond
  statement.

or

IF a > b
  PRINT "a > b".

The fact you can omit the {} from an IF is documented just not on the flow control page, said information is found on the syntax page though yes it should likely be at least linked to on the flow control page.