4 ms·
It's a great language if you want to write a compiler for fun: You can add the different statements incrementally, so you quickly get to a point where you can c
by RedNifre 3y ago
It's a great language if you want to write a compiler for fun: You can add the different statements incrementally, so you quickly get to a point where you can compile your first program and then add more and more features to compile more and more.
Also, when I was implementing FOR, my first naive implementation was compiling the body first, then append the loop related things that check the conditional and jump backwards to the start of the body. "Hang on, what about the case when the condition is initially false?" I thought, only to discover that the BASIC standard actually specifies that a FOR loop runs at least once, even if the condition is false! Officially, the language is advertised as for beginners, but to me it looks like it was meant as a lingua franca of computers, as in not only easy to understand, but also easy to implement.
Here's a BASIC compiler for the HP 15-C calculator written in Idris: https://gitlab.com/michaelzinn/voyc/-/blob/master/src/Compile/CommodoreBasicCompiler.idr?ref_type=heads#L337 https://gitlab.com/michaelzinn/voyc/-/blob/master/src/Compil...
- yiransheng 3y agoTrying to compile Basic is actually a bit trickier than just implementing an interpreter indeed. Like the FOR statement example you mentioned. Another messy situation is non-lexical FOR/NEXT like here [1], where NEXT can be encountered before a FOR statement, fairly certain I did not get it entirely right there. Original Dartmouth BASIC had only 14 statement types, and it takes very little efforts to enumerate and implement all of them, even if you are just starting on a compiler journey and making things up suboptimally along the way. The bonus upside for choosing BASIC as your first compiler project is that you get decades of vintage test programs to play with like this one [2]. [1] https://github.com/yiransheng/basic_rs/blob/ddd64e2eacfc2b36354e32e2b6abdb97989bd3a2/src/compile/mod.rs#L74 https://github.com/yiransheng/basic_rs/blob/ddd64e2eacfc2b36... [2] Game of Life compiled to warm: http://subdued-afternoon.surge.sh/ http://subdued-afternoon.surge.sh/