Advertisement

Writing a scripting language

Started by September 14, 2004 03:45 PM
43 comments, last by GameDev.net 20 years, 4 months ago
Quote:

realy?
i have had people tell me that it isnt posible


I wrote mine in some weeks too and even if it's not top-notch it's already quite advanced.

It has C syntax/scope, function, fully iterative, namespace, interfaces with C native functions, multithread native, output quite efficient bytecode (with optional multi-pass optimizer (5x AngelScript 1.8.0c speed, 3x GameMonkey speed computing the classic Mandelbrot set)).

It's fully C++ from scratch and the parser is *very* solid and has very good and precise warning/error reporting (I'm setting my standards to VS.Net being the top). The VM is just a stupid switch() like a Z80 emulator would have so it took some hours to write...

And I just hate (don't ask me, I really got allergic since KDE makefiles) everything GNU, they must have setup a "bloat teaching" school for the GNU developers. I'm sure those parser generators are very good (provided you have some weeks ahead of you to learn how to use them, have a look at GameMonkey's compiler I just can't believe how many errors slip in silently) but if you want to learn writing something then just write it. My opinion... I see too much hobby projects turning in LEGO games lately. That's no fun really. Sure, the goal is to DO something. Well, sometimes it isn't.

That said the things that gave me the most troubles:
-Parser: Cool/light scope handling with multiple implicit scopes was tricky.
-Linker: Some tricky checks for custom user types from different script objects.

So I guess it's quite possible but I'm not a code newbie either \:. Sorry to sounds arrogant, I just mean that in my 17 years of coding I must have made many indirect tries at it.
Praise the alternative.
check out the following:

Virtual Machine Design and Implementation in C++ by Blunden (Wordware)
Compilers: Primciples, Techniques and Tools by Aho et al (AdWes)
and Game Scripting Mastery by Varanese (Prima)

also, if you want to write an OO language (like UnrealScript) then an understanding of an Object Model Layout will help:

Inside the C++ Object Model by Stanley Lippman

[Edited by - algorhythmic on September 17, 2004 4:01:10 AM]
[email=algorhythmic@transcendenz.co.uk]algorhythmic[/email] | home | xltronic | whatever you do will be insignificant, but it is very important that you do it - mahatma gandhi
Advertisement
you might not want to use any vm. you can directly compile on the fly your scripts to x86 bytecode (or whatever your target processor(s) is).
you'll need to parse and compile your scripts to x86 asm, and use or make a runtime JIT assembler, like softwire for example. (http://softwire.sourceforge.net/), although softwire is quite slow if you assemble big sources. most of the time will probably be spent in the compilation anyway, not in the assembly, so you can probably use the lib directly.
if you want to do something nifty and non-standard like the UnrealScript system you will need to write your own VM though. I'm playing around with such a system using a quick and dirty LUA+luabind model to share object instance between script and engine. Once I've got the model fully defined I'll write my own VM+compiler system...
[email=algorhythmic@transcendenz.co.uk]algorhythmic[/email] | home | xltronic | whatever you do will be insignificant, but it is very important that you do it - mahatma gandhi
Quote:
Original post by CodeTitan
Or can you just write it in C/C++?


Sure can, take a look at:
http://spirit.sourceforge.net/
or
http://hapy.sourceforge.net/

Define your grammer, and parse it, all in C++.

And of course don't forget Boost.
I used one of the boost tokenize classes in the early stages of my script language.
Then I switched to a Boost::Spirit based compiler, which works very well.
IMHO Boost::Spirit is an absolutely incredible parsing framework (and check out Boost::Pheonix, Spirit's companion library), especially since it is entirely contained in header files, no object files at all!

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement
You could check out this tutorial series that I wrote:

http://www.peroxide.dk/tuts_scr.shtml

It should provide you with a good starting point for a C-style scripting language. First understand and master everything I write about in my tutorials, then expand the language with whatever features you need.

But as other has said - creating a full language and VM is quite a lot of work (but still very possible), so consider yourself warned :)

- Kasper
Quote:
Original post by swiftcoder
IMHO Boost::Spirit is an absolutely incredible parsing framework

I thought Boost::Spirit is a very interesting proof of concept (the concept being advanced hackery of C++ language) but is absolutely unusable in real life. Compilation times being one of the major problems (although it should improve with the newer compilers). I also thought the architecture was an ugly piece of crap. I didn't understand much about templates back then, so I might want to go back to Spirit and reevaluate it again. In any case, to each his own [smile]
I've written script interpreters and compilers for my own virtual machine. Even though there are tons out there, it is still a lot of fun. In fact, they are one of my most favorite things to write. I highly recommend the Let's Build a Compiler series that has already been mentioned.
I think that one of the best things to do is to write a bnf form of the language you want to write. Even if you aren't going to use yacc or bison on this bnf it lets you get the language straight before doing anything.

As for learning lex and yacc (I use flex and bison), they aren't the easiest things to learn in the world but defiantly worth it. I first started writing my own parser / lexer and I though I did fairly well, but there is always a lot of duplicate code - even just if statements for error checking. I soon changed to lex / yacc because it was much better.

What I like about using lex and yacc is that you can be confident that it will only accept your language, I won't go into the details but it is mathematically proven. As well as the fact that you can't get a language acceptor any simpler than the bnf that describes it - and this is essentially what yacc uses to generate the parsing code.

Wikipedia Backus Naur Form

This topic is closed to new replies.

Advertisement