I wrote an experimental function analyzer that allows me to bind simple functions together, so when changing variables, all functions that rely on these variables (and functions that rely on these functions, etc.) are updated at the same time. The way I do this, instead of immediately evaluating the function when it came in, I save this function. Only when the output value is requested to evaluate the function, and I evaluate it every time the output value is requested.
For instance:
pi = 3.14159 rad = 5 area = pi * rad * rad perim = 2 * pi * rad
I define "pi" and "rad" as variables (well, functions that return a constant) and 'area' and 'perim' as functions. Each time "pi" or "rad" changes, I expect the results of 'area' and "perim" to change in kind. Similarly, if there were any functions depending on 'area' or 'perim', the results of these changes would also change.
Everything works as expected. The problem here is when the user enters a recursion - random or deliberate. There is no logic in my grammar โ it's just an evaluator, so I cannot provide the user with a way to โbreak outโ of recursion. I would like to prevent it altogether, which means that I need a way to detect it and declare the abusive input invalid.
For instance:
a = b b = c c = a
Currently evaluating the last line throws a StackOverflowException (while the first two lines evaluate to "0" - the undeclared variable / function is 0). What I would like to do is to detect a cyclic logical situation and prevent the user from entering such an operator. I want to do this no matter how deeply circular logic is hidden, but I have no idea how to do this.
Behind the scenes, for example, input lines are converted to tokens through a simple scanner, then into an abstract syntax tree using a manual recursive descent analyzer, then AST is evaluated. C # language, but I'm not looking for a solution for the code - the logic itself will be fine.
Note: this is a personal project that I use to find out how parsers and compilers work, so it is not critical for the mission - however, the knowledge that I remove from this, I plan at some point to start working in real life, Any the help you guys can provide will be greatly appreciated. =)
Edit: In case anyone is interested, this blog post describes why I'm trying to find out, and what I'm going out of it.