Scripting language

Published August 14, 2008
Advertisement
One of my biggest weaknesses has always been writing exception safe code. I realised last night that my expression tree builder was leaking memory all over the place when errors were discovered.

I had code like this:

Node *Term(CompileData &Data,bool Get){    Node *N=Prim(Data,Get);    while(true)        {        switch(Data.Sc.Type)            {            case scMul: N=new BinaryNode(bnMul,N,Prim(Data,true)); break;            case scDiv: N=new BinaryNode(bnDiv,N,Prim(Data,true)); break;            default: return N;            }        }}


Obviously, Prim() can be the source of many exceptions, thrown by the scanner in the case of an illegal character, an undefined symbol thrown from the symbol table, a parse error thrown by Prim() itself, etc etc.

I put a debugging counter in that is incremented by Node's constructor and decremented by its destructor and sure enough, if I ran a script with errors in, at program exit there were a bunch of undeleted nodes.

Fine in the driver program, but in game, I may not necessarily want to terminate the whole game if a script contains errors.

Solutions has been to whack together a reference-counting smart pointer and use that throughout the expression parser:

ref_ptr Term(CompileData &Data,bool Get){    ref_ptr N=Prim(Data,Get);    while(true)        {        switch(Data.Sc.Type)            {            case scMul: N=new BinaryNode(bnMul,N,Prim(Data,true)); break;            case scDiv: N=new BinaryNode(bnDiv,N,Prim(Data,true)); break;            default: return N;            }        }}


ref_ptr<> is just a little template class that is a bit less safe than boost::shared_ptr, but a bit more lax in syntax (e.g. can assign a new expression directly rather than through an explicit.

It might end up leaking if you run out of memory - still don't really understand all that properly, but whatever I throw at the compiler now, the NodeCounter is always zero after the call to Expr::Gen() or Expr::Calc().

I'll post a more detailed description of the scripting language soon - just want to get a few more fundamental bits working.
Previous Entry New PC
Next Entry Scripting
0 likes 4 comments

Comments

Daerax
another one huh
August 14, 2008 03:04 PM
Aardvajk
Quote: Original post by Daerax
another one huh


Yep. I've been following your logic language over the last couple of weeks. Interesting stuff.

I'm just writing a script system for my game.
August 14, 2008 05:29 PM
Daerax
Mine is just a scripting language as well. I would be interested to see how you implement your language in a game.

The logic language is another project I am just polishing before i release.
August 14, 2008 08:03 PM
Aardvajk
I haven't actually decided how to do communication between the scripting and the game yet. Big decision, that one.

At the moment, I am building a static lib that contains the compiler and the virtual machine and am testing with some dummy console printing functions. It's basically along the Java model of compile to assembly-style bytecode then execute with a virtual machine. Means can have a very complex script lanaguage if I want but still be quite efficient during the game's runtime.

I'll post more details tonight.
August 15, 2008 01:37 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement