I'm building a little scripting language in C#, and last night I hit an interesting problem:

I need to add GOTO. Yes, really. This is for game logic scripting, so when the player walks back and forth between two rooms I can either call goToRoomA<->goToRoomB and grow a ridiculous stack, or I can build some ungainly chaining monstrosity, or I can just GOTO and blow away the stack.

So how do I implement this?

Right now my interpreter is using the stack in C# to represent context. For example, if it hits a curly brace then it calls a function named block(), and until that function returns it's in that code block. This is a very neat solution, but doesn't obviously allow complete stack obliteration.

Do I:

A) Implement my own stack representation instead, with maybe a bit ol switch for the contexts and the interpreter code that runs within them.

B) Manually manage falling out of scope everywhere

...

...

C) Implement GOTO as an exception and catch it back at the start of the interpreter callstack.

D) Something I haven't thought of yet?

I'm kinda partial to C, because it's funny. Thoughts anyone?

(I feel like @ChevyRay and @eniko are my best bets for thoughts on this, but I'm also keen to hear from anyone else)

Follow

@Farbs @ChevyRay @eniko absolutely nothing wrong with a goto, especially if it makes your code funnier

· · Web · 0 · 0 · 2
Sign in to participate in the conversation
coolkids.house

this is where the cool kids live