Hi, I am implementing a small variation of the Monkey programming language in Rust as an exercise (I don’t have much experience in Rust). I have the parser and the lexer, and the first version of an evaluator. However, it seems that no matter what I do, I always get stack overflow with the following program:
let counter = fn (x : int) -> int {
if (x < 900) {
return counter(x + 1);
} else {
return x;
}
};
counter(2);
Here is the code of the evaluator (simplified for this example): https://github.com/tomaz1502/monkey-rs/blob/rec_limit/src/mods/lib/evaluator.rs
I checked and the AST is being correctly generated by the parser. I don’t think I am making to…
Hi, I am implementing a small variation of the Monkey programming language in Rust as an exercise (I don’t have much experience in Rust). I have the parser and the lexer, and the first version of an evaluator. However, it seems that no matter what I do, I always get stack overflow with the following program:
let counter = fn (x : int) -> int {
if (x < 900) {
return counter(x + 1);
} else {
return x;
}
};
counter(2);
Here is the code of the evaluator (simplified for this example): https://github.com/tomaz1502/monkey-rs/blob/rec_limit/src/mods/lib/evaluator.rs
I checked and the AST is being correctly generated by the parser. I don’t think I am making too many redundant clones in the evaluator. The main component is the `LocalContext`, which is a `HashMap<String, EvalResult>` storing the values of the constants defined by the user, paired with a reference to the parent context. It is quite surprising to me that it is overflowing with only 900 recursive calls.
One curious thing is that this other implementation of the language has the same behavior in this program.
Does anyone notice anything suspicious in my code? This example is in the repository. If you want to run it you can just do `cargo run ./examples/b.mk` from the root.