使用 Java 创建我自己的(非常简单的)编译器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22142761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:52:54  来源:igfitidea点击:

Create my own (very simple) compiler, using Java

javacompiler-constructionmachine-code

提问by Aviv Cohn

I want to try and create my own very basic language, with it's very basic compiler. All using Java.

我想尝试创建我自己的非常基本的语言,它是非常基本的编译器。全部使用Java。

For now, it will only need to enable to 'programmer' to print things to the screen.

现在,它只需要启用“程序员”即可将内容打印到屏幕上。

I had an idea for how to do this, and what I wanted to know is: Will this be considered an actual 'compiler', an actual 'language', and an actual 'virtual machine'? (All very, very basic of course).

我有一个关于如何做到这一点的想法,我想知道的是:这会被视为真正的“编译器”、实际的“语言”和实际的“虚拟机”吗?(当然都是非常非常基本的)。

My idea was to create a program which will serve as the 'IDE' (editor and compiler), and another one which will serve as the 'virtual machine'.

我的想法是创建一个用作“IDE”(编辑器和编译器)的程序,以及另一个用作“虚拟机”的程序。

This means that the IDE will not compile the written code to some existing machine code, like the Java Bytecode, but will actually compile to some kind of compiled code made up by me.

这意味着 IDE 不会将编写的代码编译为某些现有的机器代码,例如 Java 字节码,而是实际编译为我编写的某种编译代码。

This compiled code will only be understandable by my 'virtual machine' program, and will only be able to run inside this program. The 'virtual machine' program, will use high-level Java operations in order to understand and execute the compiled code.

这个编译后的代码只能被我的“虚拟机”程序理解,并且只能在这个程序中运行。“虚拟机”程序将使用高级 Java 操作来理解和执行编译后的代码。

The 'virtual machine' program will be a Java program, running on the JVM.

“虚拟机”程序将是一个运行在 JVM 上的 Java 程序。

My question is: Conceptually, is this considered a virtual machine, and 'machine code'? If not, is this still considered a 'programming language', even though it's compiled bytecode can only run inside a specific program?

我的问题是:从概念上讲,这是否被视为虚拟机和“机器代码”?如果不是,即使它编译后的字节码只能在特定程序中运行,它是否仍被视为“编程语言”?

回答by Xabster

It would be considered an interpreter, and the compiled code would be byte code.

它将被视为解释器,编译后的代码将是字节码。

To be a "virtual machine" you'd need to create ways of interacting with "everything" on the PC that it runs. Giving your users/developers a "print to screen" feature is not a virtual machine.

要成为“虚拟机”,您需要创建与其运行的 PC 上的“一切”进行交互的方式。为您的用户/开发人员提供“打印到屏幕”功能不是虚拟机。

http://en.wikipedia.org/wiki/Virtual_machine

http://en.wikipedia.org/wiki/Virtual_machine

Edit: You asked: "Also: Conceptually, would you say that a programming language that is only able to create programs that can run inside another program, is still considered a programming language?"

编辑:您问:“另外:从概念上讲,您是否会说只能创建可以在另一个程序中运行的程序的编程语言仍然被视为一种编程语言?”

That depends on the features of this "other program". If you're asking about the JVM: yes. The wikipedia definition of programming language can be found here:

这取决于此“其他程序”的功能。如果您问的是 JVM:是的。编程语言的维基百科定义可以在这里找到:

http://en.wikipedia.org/wiki/Programming_language

http://en.wikipedia.org/wiki/Programming_language

A programming language is an artificial language designed to communicate instructions to a machine

编程语言是一种人工语言,旨在向机器传达指令

And we say that a virtual machine fulfills this property. So yes, Java (and other languages that compile to bytecode run by a proper virtual machine) is a programming language.

我们说虚拟机满足这个属性。所以是的,Java(以及其他编译为由适当的虚拟机运行的字节码的语言)是一种编程语言。