views:

170

answers:

3

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.

+3  A: 

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.

Dave Gamble
Right. There's no switch. so I'll have to fake that with the conditional, which is a ternary. I like that it avoids the array.
Nosredna
Just edited; was shifting bytes not nibbles.
Dave Gamble
Also, be warned that this code will execute 3, then 2, then 1!
Dave Gamble
That's OK. I'll probably have a setup array that looks up the codeword based on which ordering the user selects in the GUI anyway.
Nosredna
+3  A: 

You might be able to use the C preprocessor instead of writing your own. That would at least let you try it to see if it's a workable solution.

Daniel Yankowsky
Yeah, I could do that. The thing that's stopping me is that the embedded language can be changed on the fly, which is pretty cool. A preprocessor would muck with the flow, but might really help keep the code sane.
Nosredna
+1  A: 

The technically best solution (assuming that you have access to the code or the developers) is to modify the JS language to do what you really need.

Failing that, the best solution depends on aspects of the problem that you haven't explained:

  1. are the 'functions' recursive?
  2. are there function parameters?
  3. do you need (are you likely to need) other control structures not provided in JS?
  4. does the function call order depend on runtime parameters?
  5. are you skilled and confident enough to design and implement a preprocessor language that meets your current and projected requirements?
  6. is implementing a preprocessor going to save you / coworkers time in the long run?

If the answers to 5. and enough of the others are "yes", then your option #2 is the right answer. Otherwise ... an ugly solution like your #1 or #3 might actually be a better idea.

EDIT: If you don't have source code access and the development team is not responsive to your needs, consider looking for an open-source alternative.

Stephen C
Good questions. 1. No. 2. Not passed in, obviously, because there are no user-functions, but they will access global variables and memory. 3. Not need, but would like. 4. Yes. The user can change at any time. 5. Yes. 6. Not sure. That's what I'm stewing over.
Nosredna
Not practical to modify the language. Source is closed and executable changes often. And there's a real-time debugger I'd have to hook into as well. Maybe I should email Justin Frankel and tell him I want a job working on it. :-)
Nosredna
Or look for an open source alternative.
Stephen C