使用严格限制的解释语言

时间:2021-02-22 20:43:04

I'm coding in an embedded language called JS.

我用一种名为JS的嵌入式语言编写代码。

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

我希望能够以任何顺序调用三个函数。 (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. 复制一大堆代码。

  3. Write a preprocessor (that would create all the duplicated code).
  4. 编写预处理器(将创建所有重复的代码)。

  5. Do a loop with three iterations, using an array to control which functionality gets called on each pass of the loop.
  6. 执行一个包含三次迭代的循环,使用数组来控制在循环的每次传递中调用哪些功能。

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

我讨厌#1。重复的代码是令人讨厌的。如何在不搞砸的情况下改变任何东西?

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.

我猜#2还可以。至少我在源代码中没有重复的代码。但我的输出代码是我将要调试的,我想知道我是否想要与它分道扬..从好的方面来说,我可以在语言中添加一堆糖。

I think my best bet is #3.

我认为我最好的选择是#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的有趣之处在于它本质上是臭名昭着的/切换梦魇。

3 个解决方案

#1


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.

干,没有预处理器,没有数组。 for / switch似乎有点不可避免。

#2


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.

您可以使用C预处理器而不是编写自己的预处理器。这至少会让你试一试,看看它是否是一个可行的解决方案。

#3


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.

技术上最好的解决方案(假设您可以访问代码或开发人员)是修改JS语言以执行您真正需要的操作。

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

如果做不到这一点,最好的解决方案取决于您未解释的问题的各个方面:

  1. are the 'functions' recursive?
  2. 递归的'功能'是什么?

  3. are there function parameters?
  4. 有功能参数吗?

  5. do you need (are you likely to need) other control structures not provided in JS?
  6. 你需要(你可能需要)JS中没有提供的其他控制结构吗?

  7. does the function call order depend on runtime parameters?
  8. 函数调用顺序取决于运行时参数吗?

  9. are you skilled and confident enough to design and implement a preprocessor language that meets your current and projected requirements?
  10. 您是否熟练且有足够的信心来设计和实现满足当前和预计要求的预处理器语言?

  11. is implementing a preprocessor going to save you / coworkers time in the long run?
  12. 从长远来看,是否正在实施预处理器以节省您/同事的时间?

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.

如果5.和其他人的答案都是“是”,那么您的选项#2就是正确答案。否则......像#1或#3这样丑陋的解决方案实际上可能是个更好的主意。

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.

编辑:如果您没有源代码访问权限且开发团队无法满足您的需求,请考虑寻找开源替代方案。

#1


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.

干,没有预处理器,没有数组。 for / switch似乎有点不可避免。

#2


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.

您可以使用C预处理器而不是编写自己的预处理器。这至少会让你试一试,看看它是否是一个可行的解决方案。

#3


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.

技术上最好的解决方案(假设您可以访问代码或开发人员)是修改JS语言以执行您真正需要的操作。

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

如果做不到这一点,最好的解决方案取决于您未解释的问题的各个方面:

  1. are the 'functions' recursive?
  2. 递归的'功能'是什么?

  3. are there function parameters?
  4. 有功能参数吗?

  5. do you need (are you likely to need) other control structures not provided in JS?
  6. 你需要(你可能需要)JS中没有提供的其他控制结构吗?

  7. does the function call order depend on runtime parameters?
  8. 函数调用顺序取决于运行时参数吗?

  9. are you skilled and confident enough to design and implement a preprocessor language that meets your current and projected requirements?
  10. 您是否熟练且有足够的信心来设计和实现满足当前和预计要求的预处理器语言?

  11. is implementing a preprocessor going to save you / coworkers time in the long run?
  12. 从长远来看,是否正在实施预处理器以节省您/同事的时间?

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.

如果5.和其他人的答案都是“是”,那么您的选项#2就是正确答案。否则......像#1或#3这样丑陋的解决方案实际上可能是个更好的主意。

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.

编辑:如果您没有源代码访问权限且开发团队无法满足您的需求,请考虑寻找开源替代方案。