jrincayc / ucblogo-code

Berkeley Logo interpreter
https://people.eecs.berkeley.edu/~bh/logo.html
GNU General Public License v3.0
190 stars 34 forks source link

Unclear output of IF "FALSE #182

Open jasonsikes opened 7 months ago

jasonsikes commented 7 months ago

The IF command outputs the result of the instruction list if the condition is TRUE.

That enables the user to, for example:

MAKE "MESSAGE IF :USERNAME = "jsmith ["Welcome!]
PRINT :MESSAGE

What is the output if the condition is FALSE?

Of course, my example IS bad programming practice and probably should result in an error. It's just that the error message is unclear; an empty list is a list.

? show if "false ["hello]
Macro returned [] instead of a list
brianharvey commented 7 months ago

Oh gosh it's a terrible error message, because a user of IF shouldn't have to know what a macro is! Really the message should be IF DIDN'T OUTPUT TO SHOW. Sorry!

The case of an IF with no else clause, when a value is expected, could be caught in advance with IF (VAL_STATUS == VALUE_OK) at coms.c line 236. That would be the usual thing, although Logo's general practice is to report errors as late as possible so that side effects happen. But this could be viewed as a syntax error I guess.

jasonsikes commented 7 months ago

I suppose it could be a syntax error, but I agree that the most sensible error should be IF didn't output to SHOW.

It seems that after IF checks the condition, it outputs a list to be run as a macro. If the condition is FALSE the list will be empty. So UCBLogo treats:

IF tf instructionlist

as if the user wrote:

IFELSE tf instructionlist []

After some poking around, I found similar issues in a few other places. This is probably the simplest example:

SHOW RUN []
Macro returned [] instead of a list

It also happens when using IFT and IFF.

Oh, and don't feel sorry. UCBLogo is an excellent interpreter and language. As you guys probably know, I'm building my own interpreter, and I'm poking around UCBLogo testing edge cases.