Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm working on a small Unix shell, and am contemplating the idea of writing a script interpreter. While reading about the subject I inevitably hear of functional programming, lambda calculus, and find out about the whole fascination around Lisp.

Before I jump into this I have some questions.

  • Which language should I use?

I am curious about functional programming, so that would be a great opportunity to start. I want my shell to have as little 3rd party dependencies as possible. I am wondering whether I should look for a compiled language. I would like being able to distribute it more easily. Is this a correct approach? if so, which language would you recommend?

  • How do you embed an interpreter in your program?

The way I see it is having the interpreter run in a second separate process. As far as I know, two processes communicating, are either listening to a pipe or sending signals to one another. Is this a realistic approach? Is there a particular language that handles this part? Are there other ways of embedding the interpreter?

share|improve this question

migrated from stackoverflow.com Sep 28 '11 at 18:02

This question came from our site for professional and enthusiast programmers.

    
You may want to try to build your shell on top of SCSH first. –  SK-logic Sep 28 '11 at 9:59
1  
Parts of this are largely impossible to answer. "Which language should I use?" is always answered by "the one you know best." –  S.Lott Sep 28 '11 at 17:45
    
The point is that I don't know any functionnal language, I'm looking something specific that would be compiled not interpreted, embedded in a C program, able to manipulate low level functions and suited for writing interpreters. I'm mostly looking for feedback and experience of people having already done that –  rahmu Sep 28 '11 at 17:48
    
Cross-posting the exact same question to multiple SE sites is discouraged. I'm going to ask a Stack Overflow moderator to migrate your original question here so they can be merged. –  Anna Lear Sep 28 '11 at 18:00

2 Answers 2

up vote 2 down vote accepted

You should probably use ANSI C. It's universal across all platforms. It's low level and has system-level interfaces for everything on all platforms which will be required for a scripting runtime or programming language.

To write a script interpreter, I would suggest reading the Dragon Book [1] first. It's pretty math heavy but the basics are easy enough to pick up. If you are interested in LISP/Scheme, read SICP [2]. Both books describe interpretation and compilation in depth in two different contexts. SICP will change your life if you are interested in programming languages.

Regarding embedding, your script interpreter should probably have an "eval" function which takes a string in your language, executes it and returns a result. That is the only entry point you should need for embedding.

UNIX/Linux/OSX does all the communication stuff for you using pipes etc. Just concentrate on something which talks to stdin and stdout and you will be sorted. Even works (mostly) on Windows.

Start really simple. This is as simple as you need to start ([3]). It implements a complete LISP interpreter in C in not much code at all.

share|improve this answer

You might want to acquire a copy of "Writing Interactive Compilers and Interpreters" by P.J. Brown, now considered a classic in the field (published in 1982). New copies are expensive, but you can pick a used copy for under $5.

Another book on the same topic is Writing Compilers and Interpreters: A Software Engineering Approach by Ronald Mak, published in 2009.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.