views:

282

answers:

12

I need to implement an interpreter for a programming language as part of a project I'm working on. I don't think the details of this project are too relevant, except that it requires me to implement an interpreter from scratch, I can't use an existing programming language (the requirements include supporting portable delimited continuations, and being able to write an interpreter for it in Javascript, and also in Java).

Obviously I would really rather avoid inventing a whole new programming language, so I'm hoping there is some very simple language I could copy, or at least draw inspiration from.

My first thought was Forth or a rudimentary Lisp-like language, however I'd really prefer the language have a syntax closer to more popular programming languages like Java, Python, or Ruby. In particular, this means supporting infix operators (a+b), and also a=b assignment of variables.

To put it another way, I'd like this language to feel reasonably familiar to people who program in PHP today, and I don't believe either Forth or Lisp meet this criteria.

Can anyone offer any suggestions for such a language?

A: 

Grrr: no Forth.

TinyBasic? K&R C? Early Lua?

Cameron Laird
+6  A: 

I think that Lisp and Forth have some of the easiest naive interpreters.

You can choose a simple dynamic language, the hardest part would be building the parser. For example a subset of JavaScript might work. The interpreter is basically traversing the AST and doing the operations of each node.

In any case, research existing scripting languages that can be embedded in your development environment, avoid at all costs rolling your own. Implementing compilers (in the broad sense) is very fun to do, but can be expensive to maintain in the long run.

juancn
I already ruled out lisp and forth unfortunately for the reasons described in my question.
sanity
why not make a lisp with infix syntax? that not really hard.
nickik
+2  A: 

Brainfuck? I mean it just has 8 commands which each map to a single character.

Daniel DiPaolo
Did one of those a couple of weeks ago...it was fun. There are a couple of extensions like pbrain that add to the project as well. I thought it would be interesting to write an optimizing JIT compiler for it, but then I got distracted by work...
Burton Samograd
Yeah, but isn't Brainfuck a language designed to be unpleasant to use? I'm looking for something people will want to use.
sanity
Yeah, I suppose it doesn't fully answer the question when you consider the "trying to design a language for people to actually use" portion ;)
Daniel DiPaolo
+1  A: 

Build a LISP interpreter first, this will be relatively simple.

You will gain a lot of experience in language parsing, without being hampered by additional feature requirements.

slomojo
I've already ruled out Lisp for the reasons explained in my question.
sanity
I know, however I still want to encourage you to do this, since I'm assuming this will be your first language interpreter, and I think there is much more to be gained from building a language that is relatively easy to parse, instead of a language you want other people to use. Don't worry about that at this stage, build something that you can learn from. Not to mention that a quick poke around Reddit and YCombinator reveals that Lisp is hardly unpopular at the moment.
slomojo
Do you think maybe Paul Graham may have something to do with LISPers congregating around YCombinator? =P
Josh Smeaton
That might account for YCombinator, but not Reddit, I think Twitter being rewritten in Scheme and the emergence of Haskell has had something to do with a new growth of interest in LISP and functional languages. But your guess is as good as mine.
slomojo
A: 

PCF

swegi
+1  A: 

I assume this is as much an exercise for your own edification as a desire for a useful final product.

In that case, I must agree with the others who have recommended variants on Lisp-like languages, at least for the first pass, especially if you haven't done this before.

Lua is a pretty popular scripting language for this sort of thing that requires small, not particularly performant user scripts.

You might also consider whether javascript itself (or a subset) meets your requirements.

Also consult the list here: http://en.wikipedia.org/wiki/Continuation#Programming_language_support

Paul McMillan
A: 

I recommend you start with a subset of lisp--basically car, cdr, cons, and quote. Make sure you have a basic scanner that handles invalid characters, and then invalid types (like cons requires 2 args, 2nd must be a list). You can get this done with a knowledge of basic data structures (linked lists alone could do it, but doubly linked or circular are much better).

Kizaru
+3  A: 

Tcl. The syntax is about as simple as Lisp, and it has an expr proc for infix arithmetic. It even uses {} for blocks so if you squint just right you can tell people it's a C-like language.

It doesn't have infix assignment a=b, but once you start going down the road of general infix notation, languages get really complex really fast, so I'm not sure how that's compatible with your other requirements.

Ken
I want to +1 a TCL answer, but writing a full TCL interpreter isn't really all that easy. You can get a modest core of the language pretty simply, but TCL contains many sublanguages, for instance, the expr command you mention. any real program must support expr, because it appears in the if command, so you actually do have to implement a C-like language on top of the shell like language that is the TCL endekalog
TokenMacGuy
Well, he did say "or at least draw inspiration from", and Tcl compatibility wasn't a hard requirement. Plus, since `expr` looks pretty much just like any other proc, it doesn't need to be fully implemented right away, like in most (more complicated) languages. I think you could even write it in Tcl itself, and share it between the two interpreters he needs to write!
Ken
@Token: I'll also add that those sublanguages are specific to the commands in question. Implementing a parser/interpreter for the language is easy. Implementing all the core commands... less so. Though, if you look on the wiki, you can probably find implementations of many of the commands using other, simpler commands.
RHSeeger
Also worth noting is that Tcl is, at least partially, a Lisp derivative (command arg ... arg), and can probably be ruled out as an option for the same reasons Lisp was.
RHSeeger
But the "sublanguages" we're talking about are for Tcl's *infix arithmetic*, which was the #1 reason Lisp was ruled out! The requirements taken literally look contradictory. I don't see how any language could possibly be both "infix everywhere" and "very simple" -- once you've added enough syntax to make it look like Java, it's pretty complex. Tcl at least has a very simple core, and a fairly simple and self-contained infix evaluator, and so straddles the two worlds. I can't think of another existing language that lives here.
Ken
+5  A: 

Smalltalk and Io both have wonderfully simple but expressive syntaxes.

munificent
A: 

Logo is a simple Lisp-like language without parentheses and a few hundred implementations.

Logo information on Wikipedia.

See this PDF for information about implementations: Logo tree.

Rainer Joswig
+4  A: 

This sounds like a job for Lua.

  • It's a small language, designed to be simple to implement
  • There are already several implementations in Java and at least works in progress for Javascript.
  • Its syntax meets your requirements (assignments, infix operators).

The work you'd have left is to implement delimited continuations, but you knew that already when you ruled out Lisp/Scheme.

Gilles