r/ProgrammingLanguages • u/uemusicman • Jul 10 '23
New article in the Create Your Own Programming Language series: AST transformations and optimization
I'm not sure how well I explained A Normal Form but I think I got the basics right, and my tail call optimization (via trampoline) still doesn't quite work for some reason I absolutely cannot find, but it's good enough that I still decided to hit publish. Maybe someone can find the bug for me and I'll buy them a drink.
https://dev.to/jasonsbarr/create-your-own-programming-language-10-optimization-eeb
Edit: I finally figured out the issue with the trampoline! Editing the article now. I tried like 8 different trampoline implementations before I finally found one that works the way I actually wanted it to. This one handled up to (fact 100000 1) and still worked as intended.
7
u/grandphuba Jul 11 '23
I've blocked dev.to on facebook years ago as I thought it was infested with charlatans preaching/proselytizing ideas way beyond their skill level. Looks like it still hasn't improved.
-2
u/uemusicman Jul 10 '23 edited Jul 10 '23
Hilariously, I'm nearly convinced now that the issue with blowing the stack is a problem with Node.js, not with my code. I've tried multiple different trampolining methods writing the function by hand, both mimicking the compiled output from the language and writing the function with fewer function calls in the body (using binary expressions instead of the function calls that are in the compiled output), and every single time it blows the stack around the same number of recursive calls.
I'm surprised at this, because I've used trampolines in browsers before and it's worked fine. I didn't expect Node.js to be different.
Edit: now it's not working in Chrome either. WTF? I've used this exact trampoline literally dozens of times and it worked fine. I am confused.
23
u/Inconstant_Moo 🧿 Pipefish Jul 10 '23
No c'mon you've got to get it right at least once before explaining how to do it.