My Applesoft BASIC in JavaScript page is updated. It now actually compiles the BASIC program to JavaScript before running... although the executor, library, and I/O are all linked in during compilation so it's not emitting anything re-usable. Sorry.

Maybe this is old hat, but when I was writing a software rasterizer several years ago I came up with a trick for getting optimized variations for different render states (textured, flat shaded, Gouraud shaded, wireframe, z-write, z-test, cull-ccw, etc) without hand-writing multiple permutations of the rasterizer as is recommended by LaMothe and others.
It requires an optimizing C++ compiler that supports template functions. The trick is to pack as much state as possible into a single POD value like a 32-bit integer, write your function so that all of the if() tests for render state look at the integer for branching decisions, and call a template function that takes this value as a template parameter in a constant context like a switch statement.
Here's the base function full of nasty branches:
result_t func(int flags, const data_t& data) {
if (flags & OPT_A) {
// ...
}
if (flags & OPT_B) {
if (flags & OPT_C) {
// ...
}
else {
// ...
}…