I just stumbled into a really odd bug

I’m having trouble with…

The weirdest error I ever got.

The error I’m getting is:…

*"*startup line 3875: It is illegal to fall in to an *else statement; you must goto or finish before the end of the indented block."

My code looks like this:

*label for_loop_sanitize
*set sanitizei +1
*if (sanitizei <= (length(to_read)))
	*if ((to_read#sanitizei) = symbol)
		*comment Found an ${symbol} at ${i}position! exchanging it for ${new_symbol}!
		*set substrg (substrg&new_symbol)
		*goto for_loop_sanitize
		*set substrg (substrg&(to_read#sanitizei))
		*goto for_loop_sanitize
	*set {ret} substrg

The line 3875 of the aforementioned error is the loop at the example.

The error apparently is caused by the “*comment” Command located just above the *else statement.

Apparently, the ChoiceScript interpreter do not really ignore the *comments, but treats them as commands! (“ignore-able” commands, but still passive of proper indentation and syntax relevancy nonetheless)

I dunno. Just thought it would be nice to share it with the community if your guys ever get stuck in an mysterious error message on a part of the code that you fail to locate any flaw…


Its not a bug. That is an error. You shouldn’t have *comment in-between the end of the *if block and the start of the *else block


Was just going to say this. My understanding is CS is still reading the *comment command, it just doesn’t act on what is past it so it’ll think your coding indentation is off. Move it level with the tabs on your previous *if or in line with the text below *else and it should be ok.


I understand where @Nahim_Kerman is coming from. This is considered an odd behaviour when compared to other programming languages. A comment is ignored no matter where it is at.

Technobabble for anyone interested:

When executing a piece of code, there’s a process through which your code, which is just plain text, is turned into a set of instruction for your computer. In this process there’s a few stages. To simplify: tokenization, parsing and interpretation.

Each stage is usually done all at once, meaning the full text is tokenized and then the list of tokens is turned into an AST (abstract syntax tree) before being interpreted (executed).

ChoiceScript does not do that. It tokenize and interprets line by line. The interpreter keeps track of a few states (for example, the expected indentation level) but other than that it has no access to the surrounding context.

In other programming languages, comments would have been stripped off from the AST prior to execution. But ChoiceScript does not ignore comments, it executes them, it’s just that the comment command doesn’t do anything.

While it may seem obvious if you only know ChoiceScript, if you have experience with other programming languages this behaviour is counter-intuitive.

So when you add a comment command after *if, the interpreter effectively closes the if-ladder. The following *else is interpreted independently of the previous if which is not possible.


This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.