口译员:处理包含/导入

发布于 2024-08-27 23:03:54 字数 407 浏览 4 评论 0原文

我已经用 C++ 构建了一个解释器,到目前为止一切正常,但现在我陷入了导入/包含/但是你想调用它函数的设计。

我考虑了以下内容:

  • 在标记化过程中处理包含:当代码中找到 include 时,会使用指定的文件名递归调用标记化函数。然后将 included 文件的标记化代码添加到 include 的先前位置。 缺点:没有条件包含(!)

  • 在解释过程中处理包含:我不知道如何。我所知道的是 PHP 必须 这样做,因为条件包含是可能的。

现在我的问题是:

  • 我应该怎么做?
  • 现代解释器(Python/Ruby)如何处理这个问题?他们允许有条件包含吗?

I've built an interpreter in C++ and everything works fine so far, but now I'm getting stuck with the design of the import/include/however you want to call it function.

I thought about the following:

  • Handling includes in the tokenizing process: When there is an include found in the code, the tokenizing function is recursively called with the filename specified. The tokenized code of the included file is then added to the prior position of the include.
    Disadvantages: No conditional includes(!)

  • Handling includes during the interpreting process: I don't know how. All I know is that PHP must do it this way as conditional includes are possible.

Now my questions:

  • What should I do about includes?
  • How do modern interpreters (Python/Ruby) handle this? Do they allow conditional includes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2024-09-03 23:03:54

如果您有一个简洁的设计并且您知道自己在做什么,那么这个问题很容易解决。否则会非常困难。我已经编写了至少 6 个解释器,它们都具有此功能,而且相当简单。

  1. 您的解释器需要维护一个了解所有已定义的全局变量、函数、类型等的环境。您可能更愿意将其称为“符号表”。

  2. 您需要定义一个读取文件并更新环境的内部函数。根据您的语言设计,您可能会或可能不会在读入内容时进行一些评估。我的解释器非常动态,并且会在读入后立即评估每个定义。

  3. 如果您构建结构,您的生活将变得更加轻松你的解释器分层:

    • Tokenizer(将输入分解为标记)
    • 解析器(一次读取一个标记,转换为抽象语法树)
    • 评估器(读取抽象语法并更新环境)

抽象语法树确实是关键。如果你有这个,当你在输入中遇到 import/include 构造时,你只需进行递归调用并获得更抽象的语法。您可以在解析器或求值器中执行此操作。如果您想要条件导入,则必须在求值器中执行此操作,因为只有求值器可以计算条件。

我的解释器的源代码位于网络上。其中两个是用C语言编写的;其他的都是用标准机器学习编写的。

This problem is easy to solve if you have a clean design and you know what you're doing. Otherwise it can be very hard. I have written at least 6 interpreters that all have this feature, and it's fairly straightforward.

  1. Your interpreter needs to maintain an environment that knows about all the global variables, functions, types and so on that have been defined. You might feel more comfortable calling this the "symbol table".

  2. You need to define an internal function that reads a file and updates the environment. Depending on your language design, you might or might not do some evaluation the moment you read things in. My interpreters are very dynamic and evaluate each definition as soon as it is read in.

  3. Your life will be infinitely easier if you structure your interpreter in layers:

    • Tokenizer (breaks input into tokens)
    • Parser (reads one token at a time, converts to abstract-syntax tree)
    • Evaluator (reads the abstract syntax and updates the environment)

The abstract-syntax tree is really the key. If you have this, when you encounter the import/include construct in the input, you just make a recursive call and get more abstract syntax back. You can do this in the parser or the evaluator. If you want conditional import, you have to do it in the evaluator, since only the evaluator can compute a condition.

Source code for my interpreters is on the web. Two of them are written in C; the others are written in Standard ML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文