alan-if / alan

ALAN IF compilers and interpreters
https://alanif.se
Other
19 stars 3 forks source link

Division by Zero Crashes all Terps #5

Closed tajmone closed 5 years ago

tajmone commented 5 years ago

I was testing an adventure that implements verbs to carry out the four basic operations of arithmetic and noticed that divide by zero crashes all the interpreters.

Possibly it's a bug, for there should be some failsafe mechanism to prevent similar illegal operations — after all, this was a legit example where the context could provide such cases.

Here's the code:

THE kitchen IsA LOCATION
END THE kitchen.

SYNTAX 'divide' = 'divide' (num1) 'by' (num2)
  WHERE num1 IsA integer
    ELSE "You can only use numbers with this verb!"
  AND num2 IsA integer
    ELSE "You can only use numbers with this verb!"

ADD TO EVERY integer
  VERB 'divide'
    When num1
      DOES
        "Sure, $1 divided by $2 equals" SAY num1 / num2. "."
  END VERB 'divide'.
END ADD TO integer.

Start at kitchen.

Tested with the following command:

> divide 4 by 0

Which caused all three terps to crash (WinArun, Gargoyle and ARun). Arun would show the response up to "Sure, 4 divided by 0 equals" before crashing.

Of course, the easy fix was adding the following Check:

  Verb 'divide'
    Check num2 <> 0
      Else "You can't divide by zero!"

But having the interpreter check for a division by zero would be better — at least it should exit with an error, instead of just crashing silently, so that the error message could help the author trace the error and its nature.

thoni56 commented 5 years ago

This is definitely a bug. I'll add an interpreter check for this so that the author can add that check.

Although Alan as a language tries to prevent run-time errors, there are a few for which there seems to be no good way to handle. The preferable solution would be to have the interpreter say something like "You can't do divisions by zero", but since there is no way to know if the player even is to know about the math going on, I think an "Application Error" would be appropriate.

tajmone commented 5 years ago

I also think that the producing an Application Error is better, for it would emphasize the need on the author side to better check the code (otherwise, if the game just carried on, it would be soon forgotten and the player wouldn't make a report about this).

thoni56 commented 5 years ago

Thanks for a excellent problem report that became the test case straight off!