PDA

View Full Version : Writing an interpreter...



Darkhog
03-06-2011, 10:13 AM
I designed script language and now need to write interpreter for it. It has lot to do with Pascal Game Development, because it'll eventually be part of game creation studio (similar to tools like MultiMedia Fusion or Stencyl). But first I have to write script interpreter, then I'll take care of engine and IDE, then I'll need to make easy point&click code generator and level maker.

So how to start it? Parsing, as far as I know is easiest part. It'd be more complicated to actually interpret code. I could make array with all possible procedures and loop through it using for to check if current procedure is in it, but I can imagine it'd be kinda slow, especially considering fact that user would be able to define his own procedures and functions and there can be lot of those. Is any faster way to do it. It will be used for scripting games, so it have to be fast.

Oh, and I don't want to use existing solution like Lua or Python because while they're fast, my language is designed specially for games and I don't have to use tricks I'd would have to try with them (and besides there is already game creation system/library/whatever for Lua called LOVE and for python we have pyGame. I don't want to make another one). Plus my game language (code name: G4MECode, game creation studio itself would have different name) have nice feature that allows to have variables with any unicode characters with it, even if it won't be too hard it'll have ability to use numbers at begining of variable, so we'll be able to have variable named 123ąexd??¬µłđĸ. But maybe better I'll make thread about studio itself in Your Projects.

Carver413
03-06-2011, 01:17 PM
quite an ambitious project are you sure your up for it? just to make the interpreter the way you envision it could take year's to perfect. to make your scripting language fast enough you will need to tokenize your code in a way that you can use a lookup table to find the command without having to searching for it. so basicly you would need to build a compiler to compile the script into byte codes then you would need to build a byte code interpreter to read the byte code and look the address of the function. another approach is to use the scripts to tell your game engine how to configure itself rather then actually using them to run the game. this is the way I am doing it and is much simpler approach. you dont need to worry to much about how fast your scripting engine is if it is only used to define the game logic.

pstudio
03-06-2011, 11:23 PM
My suggestion: read a book on compiler design (eg. Dragon Book). Writing an interpreter is pretty much the same as writing a compiler. This is by no means an easy job. It'll take a lot of time and I hope you realize that.

chronozphere
03-06-2011, 11:44 PM
My suggestion would be: Don't do this, unless your main goal is education (and you have enough time ;) ).

To be honest, I don't think one person can offer something better than Lua or Python as a game scripting language. You say that it is geared specifically towards games, but that doesn't convince me. How would you reach that goal? I, as a user of a scripting language in a game engine, would rather see a normal object-oriented language that I know how to work with, than a very specialized language with non-standard syntax. You'd need more fundemental idea's than unicode variables, to really justify such a project.

But my guess is that your main goal is to have fun and to learn, rather than building a polished product for others within a small time frame. So, as others said, reading books on compiler design can really help alot. Make a good plan for all the phases of interpretation and compilation. You could start to make a barebone by making a bunch of abstract classes that show how the main process works. The rest of the work would be to derive subclasses and incrementally add features and details.

Good luck with it! :)

harrypitfall
04-06-2011, 01:37 AM
Well, I coded myself a script engine from zero, including a 'pseudo-compiler' (bytecode), works good at all with good speed, I called "TupiScript" and uses a hybrid syntax of xbase, basic and pascal.
Later, I just found a way to use JavaScript natively (almost) from the Windows OS, with full support of pascal objects using COM/OLE/Dispatch.
If you want to learn, do it, 'cause worth a lot and will improve your logic. For professional work, there is already good script engines out, full of resources that is better to use.

paul_nicholls
04-06-2011, 04:29 AM
If you STILL want to have a go writing an interpreter, I would start with Jack Crenshaw's tutorials...they helped me make a compiler (to byte code), and then I wrote an interpreter for the byte code :)

http://compilers.iecc.com/crenshaw/

cheers,
Paul