Re: Implementing a stack-based interpreter

John Carter <john.carter@tait.co.nz>
22 Jul 2005 20:18:18 -0400

          From comp.compilers

Related articles
Implementing a stack-based interpreter cvrebert@gmail.com (2005-07-17)
Re: Implementing a stack-based interpreter ar.mcdonald@virgin.net (Alex McDonald) (2005-07-22)
Re: Implementing a stack-based interpreter mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2005-07-22)
Re: Implementing a stack-based interpreter gah@ugcs.caltech.edu (glen herrmannsfeldt) (2005-07-22)
Re: Implementing a stack-based interpreter john.carter@tait.co.nz (John Carter) (2005-07-22)
Re: Implementing a stack-based interpreter Jan.Bogaerts@telenet.be (Jan Bogaerts) (2005-07-22)
Re: Implementing a stack-based interpreter anton@mips.complang.tuwien.ac.at (2005-07-26)
Re: Implementing a stack-based interpreter dot@dotat.at (Tony Finch) (2005-07-26)
Re: Implementing a stack-based interpreter marcov@stack.nl (Marco van de Voort) (2005-08-03)
Re: Implementing a stack-based interpreter mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2005-08-05)
Re: Implementing a stack-based interpreter rossen.radev@gmail.com (aquarin) (2005-08-05)
[3 later articles]
| List of all articles for this month |
From: John Carter <john.carter@tait.co.nz>
Newsgroups: comp.compilers
Date: 22 Jul 2005 20:18:18 -0400
Organization: Xtra
References: 05-07-070
Keywords: interpreter
Posted-Date: 22 Jul 2005 20:18:18 EDT

On Sun, 17 Jul 2005 13:52:46 -0400, cvrebert wrote:




> I'm trying write an interpreter for a programming language but haven't
> been able to find anything on how to write a stack-based interpreter.


* Trick one, don't reinvent the wheel. There are quite a few simple stack
machines out there, compile to one of them. (If suitable.)


So if you have decided on making your own custom, slightly off round
wheel...


* Trick Two. Don't make up your destination stack language
first. Write your compiler to stack language first. As you try
implement each syntactic element, invent whatever stack primitives
that will magically do the job for you.


* Trick Three. Don't do it all in your own language. The average real
CPU machine instruction is very simple, very dumb, with lots of fiddly
special purpose registers and addressing modes.


This simply doesn't hold with a stack based interpretor. Keep
addressing modes dead simple. Everything is on the stack. No
registers.


However, if you are writing your own custom language, odds on its
_very_ domain specific. So why not implement the occasional
instruction that is really very smart? It's a trade off, a big fat
function in your interpreted language translated into lots and lots of
dumb operators, or a few very very smart operators written in lots and
lots of C++ or C or what ever you are using.


The one I did had freaky vaguely fuzzy/flow sorts of
variables. Instead of implementing operations on them in terms of lots
and lots of basic integer / floating point ops... I created a few very
highlevel ops that did most of the work in C++. Much Faster, Much
simpler.


Hint.


Think very hard about dataflow and ownership of objects and
memory. Memory leaks abound if you don't. Use a GC'd implementation
language if you can.
--


John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
New Zealand


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.