问题
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?
回答1:
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.
回答2:
In general, no. But you might have exceptions, e.g. because the memory semantics of the interpreted language is different of the memory semantics of the language you are coding the interpreter in.
As an example, some languages are garbage collecting threads or tasks. If you code your interpreter for such a language in Java, you'll need to GC the tasks by yourself.
You probably want some weak hash tables.
来源:https://stackoverflow.com/questions/27472158/i-build-a-interpreter-on-a-language-with-a-garbage-collector-i-need-a-garbage-c