本文介绍了解释器如何运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读所有已编译与已解释的文章,似乎已编译意味着机器将直接运行已编译的代码,而已解释的机器将由解释程序运行代码.但是,如果解释器在机器上,则解释器如何运行该代码?难道它还没有最终不得不将其解释转换为机器代码并仍然让机器运行它吗?归根结底,所有东西最终都必须是机器代码,机器才能正常运行?似乎解释只是意味着它一次在一行语言中运行,而编译意味着一次遍历所有语言.在那之后,几乎是一样的权利吗?

Reading all the compiled vs interpreted articles it seems like compiled means the machine will run the compiled code directly whereas interpreted, the interpreter will run the code. But how does the interpreter run the code if it's on a machine? Doesn't it still end up having to convert what it's interpreting into machine code and STILL having the machine run it? At the end of the day, everything has to end up being machine code in order for the machine to run it right? It seems like interpreted just means that it's running through the language one line at a time whereas compiled means going thru it all at once. After that, it's pretty much the same right?

推荐答案

相关:

Related: How programs written in interpreted languages are executed if they are never translated into machine language?

否,不需要将其转换为机器代码.这些指令仅向解释器本身提供指令 ,然后解释器自行执行.

No, it doesn't need to convert it to a machine code. The instructions merely provide instructions to the interpreter itself, which the interpreter then executes itself.

考虑一个真正愚蠢的语言",它由以下说明组成:

Consider a really dumb "language" that consists of the following instructions:

我们可以实现一个这样的解释器"(用C#编写):

We could implement an "interpreter" like this (written in C#):

public static void ExecuteStatements(List<string> instructions)
    {
        int result = 0;
        foreach (string instruction in instructions)
        {
            string[] action = instruction.Split(' ');

            int number = int.Parse(action[1]);

            switch (action[0].Trim().ToLower())
            {
                case "add":
                    result += number;
                    break;
                case "subtract":
                    result -= number;
                    break;
                case "divide":
                    result /= number;
                    break;
                case "multiply":
                    result *= number;
                    break;
            }
        }

        Console.WriteLine("Result: " + result);
    }

ExecuteStatements 方法将被编译为机器代码.另外,我们有一个像这样的文本文件:

The ExecuteStatements method will be compiled to machine code. Separately, we have a text file like this:

结果将是100.这些字符串实际上从未编译成任何东西-它们只是告诉解释器采取什么动作.

The result will be 100. The strings are never actually compiled to anything - they just tell the interpreter what actions to take.

很显然,这种语言"甚至还不是含糊的图灵完成的,但是重点是我们绝不会以某种方式将其翻译"成机器代码-解释器"会采取任何指定的动作.

Obviously, this "language" isn't even vaguely Turing-complete, but the point is that at no point are we somehow "translating" this into machine code - the "interpreter" just takes whatever action is specified.

实际上,我曾经作为Test Automation框架的一部分编写过一个解释器.当有人对API进行调用时,我们将拦截该调用,对其进行反射,以确定调用的内容和参数,然后将反射元数据序列化为JSON.然后,我们随后反序列化JSON,并使用反射来调用之前使用相同参数运行的任何方法.该引擎实际上不需要机器代码-它只是使用反射来找出要执行的调用.

I actually wrote an interpreter once as part of a Test Automation framework. When someone did a call against the API, we would intercept the call, use reflection on it to determine what the call was and what the parameters were, and serialize the reflection metadata to JSON. We then later deserialized the JSON and used reflection to call whatever methods had been run before with the same parameters. The engine didn't actually need machine code - it just used reflection to figure out what call to do.

这是关键的见解:解释后的代码本身实际上不执行任何操作-所有所做的工作是为解释器提供所需的操作.解释器已经知道"如何采取您可以使用解释语言执行的所有操作,因此不需要其他机器代码.

Here's the key insight: the interpreted code itself is quite literally doing nothing - all it's doing is feeding the interpreter which actions it needs to take. The interpreter already "knows" how to take all of the actions you can perform in the interpreted language, so no additional machine code is required.

打个比方,将您要解释的代码视为一个食谱,将解释器视为一个厨师.食谱中规定了添加1杯面粉并混合"之类的动作.厨师知道如何遵循食谱中的指示,然后自己执行.严格来说,食谱实际上并没有做任何事情-它只是坐在那里供厨师阅读,以便厨师可以知道要采取什么行动.无需为了真正完成食谱而真正成为厨师的菜式,它只需要一个知道如何遵循其指示的人即可.

As an analogy, think of the code you're interpreting as a recipe and the interpreter as a cook. The recipe specifies actions like "add 1 cup of flour and mix." The cook knows how to follow whatever directions he finds in the recipe and he performs them himself. Strictly speaking, the recipe isn't actually doing anything - it's just sitting there for the cook to read so that the cook can know what actions to take. There's no need for the recipe to actually be a cook in order for the recipe to be completed - it just needs someone who knows how to follow its directions.

TL; DR 您不需要将其翻译"为机器代码-您只需要具有足够的信息,以使解释器知道要采取什么措施即可.一个好的解释器已经知道"如何采取该语言可以执行的任何操作,因此无需创建任何其他机器代码.

TL;DR You don't need to "translate" it into machine code - you just need to have enough information for your interpreter to know what actions to take. A good interpreter already "knows" how to take whatever actions the language could implement, so there's no need to create any additional machine code.

这篇关于解释器如何运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!