I wrote a user-programmable calculator, and now I'm hitting the following problem.
Let's say a user writes the following in it:
fun(n) = fun(n-1)
and then tries to call fun(42) (or whatever number). Now obviously, the user's code will cause infinite recursion, which in turn causes the whole calculator to crash with a stack overflow.
How could I make the calculator quit the calculation gracefully?
I have looked at how Mathematica handles similar situations, and it just bails out saying
$IterationLimit::itlim: Iteration limit of 4096 exceeded.
I have tried a similar approach, but then, as the stack size is OS-dependent, how do I know what number to use as the iteration limit (yes, I found a number that "seems to work" by experimentation, but that doesn't feel right)?
Thanks for your time. The core of the calculator is written in C.