Sunday, July 22, 2012

Evaluation and circular evaluation

Take an interpreted programming language (say javascript), then implement an interpreter of another programming language (say BASIC, if anybody still remembers it :D ). Doable.

Now what about implementing a javascript interpreter in javascript? Or a python interpreter in python?
Also doable ... but why?

'' The Metacircular Evaluator ...
Our evaluator for Lisp will be implemented as a Lisp program. It may seem circular to think about evaluating Lisp programs using an evaluator that is itself implemented in Lisp.
Given the evaluator, we have in our hands a description (expressed in Lisp) of the process by which Lisp expressions are evaluated. One advantage of expressing the evaluator as a program is that we can run the program. This gives us, running within Lisp, a working model of how Lisp itself evaluates expressions...
'' [source]

'' PyPy is a fastcompliant alternative implementation of the Python language (2.7.2). ...
PyPy's sandboxing is a working prototype for the idea of running untrusted user programs.'' [source]

'' Its name is based on the mythical figure of Narcissus, who fell in love with himself. This relates to the fact that this JavaScript engine is a metacircular interpreter, because the engine itself is also written in JavaScript, albeit using non-standard extensions that are specific to SpiderMonkey. ''  [source]

Fact1: A meta-interpreter must have a representation for all the instructions of its language: a data structure (a tree possibly) is used to represent code.
Fact2: JSON is used to serialize data and de-serialize it, also across different languages (e.g. Javascript and python).
Question: Would it be possible to write/modify a meta-circular interpreter for python and one for javascript, so that I could take a program in python, represent it as a data-structure, pass it to javascript via JSON, then run it as javascript code?
That would be a quick way to translate python into javascript, and the data-structure passed in JSON would probably be like an AST (abstract syntax tree)...

No comments:

Post a Comment