Utter coolness (my interpreter)

Started by
13 comments, last by GameDev.net 19 years, 3 months ago
Ok, so this may not be much for you people who write your own compiler every day, but for me it's a pretty cool achievement. I decided to write a language that would allow me to extend and script my graphics framework as well as type commands at runtime. Here's what I have so far (after four days of work, spending about one hour every day):

>> 5
5

>> (add 2 3)
5

>> (add 10 (sub 3 1))
12

>> (let x (add 10 (sub 4 1)))
13

>> x
13

>> (add x 2)
15

>> (let y x)
13

>> (let x 10)
10

>> (add x y)
23

>> (sub x y)
-3

Essentially a calculator with variables. All that's supported at the moment is add (+), sub (-) and let (assignment). The interpreter already supports macros and functions under the hood, I just have to expose them to the language. I decided to use s-expressions because they're trivial to parse (I hate parsers!) and allow for pretty amazing macro functionality. I will soon add support for logic, functions, conditionals, and of course loops. After that I plan to expose the macro functionality as well as creating lists and preventing evaluation. From there I have a basic lisp interpreter! I can implement the rest of the language constructs in the language itself! Compiler class was not a requirement in my uni. Oh well, better late than never. I'm having a blast doing this, I just wish I had more time. I decided to use boost.variant to implement the lists in the interpreter and it made my code incredibly ugly. I'll have to refactor it later. Or may be I'll just let it be, add more constructs and then write a new compiler in the language itself! I encourage everyone who didn't tackle this yet to do it. First, you'll have a blast creating your own language. Second, you'll get a chance to learn Lisp. Third, you'll have a great language to use for your future projects. And the bast part: you wrote it yourself!
Advertisement
looks kinda like scheme or lisp

*edit*: Doh, didn't read the whole post.
daerid@gmail.com
Yeah, it looks similar due to s-expressions and will have some similar features. Yesterday I checked the size of the .lib file that implements the features above and it's already 17(!) megs in release mode. Man, heavy template use sure does increase the size. Most of that is thrown out during linking the executable but this is still not acceptable. I guess I'll use the current interpreter as a bootstrap to create a self-hosting compiler.
The nice thing about what you've done is that, if you like, you can write a front end later on while you keep working on the back end based on S-expressions. Transforming the parser to the S-expression mode isn't nearly as difficult as trying to suss your language with a new language from scratch. You can also write test scripts in your S-expression language and prove that your parser works.

Historical note: S-expressions were originally intended to be a middle ground language for M-expressions (as I just described). McCarthy dropped M-expressions since S-expressions were good enough for LISP.

*pat on back*
Quote:Original post by flangazor
The nice thing about what you've done is that, if you like, you can write a front end later on while you keep working on the back end based on S-expressions.

Yeah, I thought about that already. I don't envision myself doing that though. S-expressions are hard on the eyes for someone who's not used to them, but once you get over the barrier they're actually pretty simple to understand. I think the reason why people have trouble with Lisp is the special characters introduced by reader macros (', #', etc.) Unless you know what those mean, Lisp syntax doesn't look all that uniform and to the uninitiated it looks like Perl.

I'm actually more concerned about converting the interpreter to a compiler at a later stage. I have an aversion to interpreters and virtual machines. I'd much rather have a compiler that compiles whatever is possible at deployment time and include a runtime version of the compiler to deal with dynamic aspects of the language. The best part is that the functionality that's needed to have a usable version of the language is fairly limited, therefor a compiler would be relatively small. The rest can be implemented in the language itself.

I think you'll enjoy this book. I got it today and I can't put it down. It's up there with Adelman and Sussman.
Cool, I was planning on doing something similar on the .NET platform. Was going to be a virtual machine, but ideally I JIT compile what I can into .net code. I still want it to be running on as a VM, although I could get around to completely compiled if I ever actually made a release.

In a few weeks I will see how far I have gotten, I am still working through SICP, just got up to the Metacircular Evaluator section, but I am pretty certain I could start working on some of the basics (cons cells) right now. But I want to finish the book first, I am in no rush anyways.
You might want to take a look at DotLisp.
I found this really cool book that has all kinds of C++ algorithms including a simple C like interpeter. For the life of me I can't recall the exact title. I just know the book is about 4-5 years old, has about 1000+ pages (mostly due to code and code explanations), and has an orange-red cover. Maybe later I will find the book and the title for you... I think the title was "Applied C++ Algorithms and Data Structures" but I am not too sure.
Seen this?

This topic is closed to new replies.

Advertisement