本文介绍了我使用垃圾回收器在语言上构建解释器。我需要一个解释器的垃圾收集器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个天真的问题,但在目前我看到的教程中拼写不清楚。



如果我在高级语言的顶部构建一个解释器(而不是C,C ++等),这有一个垃圾收集器......是否有必要为解释器本身做一个?



如果答案是肯定的。 ..它必须是同一种主机? (即:如果主机是标记扫描,解释器呢?),还是可以利用主机并让它管理所有这些?



选择主机有所作为?我计划使用F#/ .NET,但对Java和其他人来说是一样的吗?

解决方案

解释型语言的内存管理可以背负主机的内存管理,但是如果您正在实现非玩具解释器,则可能需要使用 用于从主机到解释世界的值,如果在解释世界中没有引用它们,应该被垃圾收集。 p>

如果你正在实现一个玩具解释器,没有人会注意到你是否使用强指针作为从主机世界到解释世界的管理引用。在这种情况下,我建议你不要打扰:作为一项功能,弱指针可用于所有先进的内存管理语言,但细节有所不同,它们的使用总是很棘手。 提供了所有品种的图片。在他的调查中,这种弱指针的使用属于全局垃圾收集:允许垃圾收集跨越进程边界或跨越Lisp-C / C ++ / Java外部接口边界,除了外部接口边界位于主机语言和解释性语言。

This is a naive question, but in the tutorials I have seen so far is not spelled clearly.

If I build a interpreter on top a high-level language (not C, C++, etc) and this have a garbage collector... is necessary to also make one for the interpreter itself?

And if the answers is yes... it must be the same kind of the host? (ie: If the host is mark-sweep, the interpreter too?), or is possible to leverage the host and let it manage all of this?

The selection of host make a difference? I plan to use F#/.NET, but is the same for Java and others?

解决方案

The memory management of the interpreted language can piggy-back on the memory management of the host, but if you are implementing a non-toy interpreter, you may need to use weak pointers for the administrative references from the host to values of the interpreted world that should be garbage-collected if there are no references to them left in the interpreted world.

If you are implementing a toy interpreter, no one may even notice if you use strong pointers for administrative references from the host world to the interpreted world. In this case, I would recommend you don't bother: weak pointers, as a feature, are available in all sufficiently advanced memory-managed languages, but the details differ and their use is always tricky. Bruno Haible's survey provides a picture of all the varieties. In his survey, this use of weak pointers falls under "Global garbage collection: Allow garbage collection to work across process boundaries, or across a Lisp - C/C++/Java foreign interface boundary", except that the foreign interface boundary is between the host language and the interpreted language.

这篇关于我使用垃圾回收器在语言上构建解释器。我需要一个解释器的垃圾收集器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:12