Archived

This topic is now archived and is closed to further replies.

Extrarius

Lisp as a Scripting Language

Recommended Posts

Extrarius    1412
I need a scripting language, and after looking at many languages used for that purpose, I''ve decided none of them fit my needs so I want to make my own. Since the Lisp revolution here on GameDev, I''ve found that I like Lisp. Unfortunately, I''m not a Lisp guru, so I don''t think I could write a game engine in it. Thus, I''d like to create a Lisp interpreter for my engine which will be written in C++. I''ve looked around on the net (using google and visiting many, many links from lisp.org), but I can''t find any information on how to write such an interpreter. Lisp is easy enough to create a tree from, but I''m not sure how to handle the untyped variables, and how to handle lists in lists, etc. I would prefer to support as much of the Common Lisp standard as possible. I''ve found the source code for a few interpreters, but generally they are very messy C that is difficult to understand. Does anybody know of any good papers, sites, books, etc or have any pointers or advice on writing a Lisp interpreter?

Share this post


Link to post
Share on other sites
SiCrane    11839
Didn''t you buy "Essentials of Programming Languages"? If so, the samples are (relatively) easy to adapt to C++. Admittedly the language they develop isn''t Lisp, but it is semantically similar (some would argue a Lisp dialect). I know from experience that it''s similar enough to write a Scheme interpreter from it. Then again, I worked off of the first edition of the book. Your milage may vary with the second edition.

Share this post


Link to post
Share on other sites
Extrarius    1412
I guess I''ll start reading it then. I''d been too busy learning and playing with Lisp to spend time on ''more serious'' stuff until now =-) Lisp is a lot of fun, especially with so many awesome projects created in it, and a lot of them open source.

Share this post


Link to post
Share on other sites
bishop_pass    109
I don''t understand.

Why don''t you just use a Lisp package, call your C++ code, and do a callback to your Lisp code? In other words, let Lisp carry the program, with C++ reserved for whatever thing it is that you want C++ to be reserved for.

Share this post


Link to post
Share on other sites
Extrarius    1412
quote:
Original post by bishop_pass
I don''t understand.

Why don''t you just use a Lisp package, call your C++ code, and do a callback to your Lisp code? In other words, let Lisp carry the program, with C++ reserved for whatever thing it is that you want C++ to be reserved for.


I''m much stronger with C++, and I don''t know how to do that kind of thing in Lisp. Also, I feel implementing a Lisp interpreter in C++ will improve my understanding of Lisp.

Share this post


Link to post
Share on other sites
bishop_pass    109
quote:
Original post by Extrarius
I''m much stronger with C++, and I don''t know how to do that kind of thing in Lisp. Also, I feel implementing a Lisp interpreter in C++ will improve my understanding of Lisp.

I think your time would be better spent researching what I suggested, and yield a better product. Lisp can compile itself into machine code. Will your implementation do that? Also, there''s a lot of other things in a good quality Lisp implementation in addition to compilation: p-lists, gc, symbol tables, bignums, complex numbers, ratios, etc.

Share this post


Link to post
Share on other sites
Extrarius    1412
Another reason is that every Lisp compiler I''ve found is only free for personal use, and I already own a C++ compiler I can use for commercial use. A commercial License for the Lisp compilers I''ve looked at costs more than I can afford.

Also, if I use Lisp as the base language, I''ll have 3 parts instead of only 2 - C++, Lisp with full access to the engine, and Lisp without access to the engine. I need the latter because the point of the scripting engine is to allow anybody to easily make some modifications without exposing the entire engine to them. If I use C++ as the base, I eliminate the middle layer of Lisp. I might lose some speed because I''m interpreting Lisp, but maybe not as there are libraries that making creating machine code easy, which I could use to compile my Lisp code. I probably won''t go that route though, because part of the reason I want an interpreter is so that I have more control over when and how the code runs.

Share this post


Link to post
Share on other sites
bishop_pass    109
I thought Corman Lisp was only $250. And you don''t need to pay for it until you''re ready to distribute.

As for the other arguments, I believe they aren''t valid arguments, but excuses to not research the techniques I discussed, instead favoring the idea of writing your own interpreter. While there is nothing wrong with doing that, I believe in the end the better product would be the result of what I am suggesting.

Share this post


Link to post
Share on other sites
Extrarius    1412
$250 is a lot of money for a poor college student.

You''re right that they aren''t very strong arguments, but I really don''t have any good reasons for wanting to write an interpreter other than doing it. For now, that is my real goal. My next goal in line is to make an engine, and it will ''need'' a scripting langauge so it makes sense to use the one I''ve already spent time to finish. I really don''t want to spend the time it will take for me to get good with Lisp before starting my engine. By writing an interpreter first, and then using it only lightly as a scripting language I''ll be able to get more comftorable with it and then I might use it in my next project or two... Maybe the final engine will be entirely in Lisp, but for now my goal is to gently ease into it.

Share this post


Link to post
Share on other sites
Fruny    1658
My experience is that, as far as implementation goes, the simplest scripting languages are Forth-derivatives (stack-based, postfix notation). Granted its not quite Lisp, but it''ll help you get there.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
SiCrane    11839
First, let me make one modification to me statement: "Essentials of Programming Languages" will take you through basically every part except for lexing/parsing the language. (You have to wait till chapter 11 and even then the coverage is suboptimal). I recommend writing the lexer and parser with some derivitives of lex and yacc. A good book for coverage is "lex & yacc" by Levine, Mason and Brown. However, what you need to lex and parse Lisp isn''t that involved and you can probably get away with just using the online documentation for flex and bison if you happen to use either of those.

That being said, it''s not hard to create a functioning Scheme interpreter. The only tricky part I remember was closing the environment on the creation of a lambda expression. Once you get the lambda expressions down, everything else just flies by. I even had my implementation automaticaly recognize arbitrarily deep car/cdr concatinations. (Well, maybe not arbitrarily deep, I think if you had more than 10,000 or so a''s or d''s it would dump core.)

Share this post


Link to post
Share on other sites