audiosignal-processingreaper

Working with a severely limited interpreted language


I'm coding in an embedded language called JS.

I want to be able to call three functions in any order. (ABC, ACB, BAC, BCA, CBA, CAB.)

The trick? The language doesn't have user-defined functions.

It does have a conditional and a looping construct.

I think I have three choices.

  1. Duplicate a whole bunch of code.
  2. Write a preprocessor (that would create all the duplicated code).
  3. Do a loop with three iterations, using an array to control which functionality gets called on each pass of the loop.

I hate #1. Duplicated code is nasty. How do I change anything without screwing up?

I guess #2 is OK. At least I don't have duplicated code in the source. But my output code is what I'll be debugging, and I wonder if I want to diverge from it. On the plus side, I could add a bunch of sugar to the language.

I think my best bet is #3.

Any other ideas? There is no goto. No functions. No existing preprocessor.

Funny thing about #3 is that it's essentially the infamous for/switch nightmare.


Solution

  • Perhaps some kind of mutant state-machine, viz:

    int CODEWORD=0x123;
    
    while (CODEWORD)
    {
        switch(CODEWORD&15)
        {
        case 1:
           /// case 1
           break;
        case 2:
           /// case 2
           break;
        case 3:
           //// case 3
           break;
        }
        CODEWORD=CODEWORD>>4;
    }
    

    DRY, no preprocessor, no array. for/switch seems somewhat unavoidable.