script engine // parse scripts

Started by
17 comments, last by GameDev.net 17 years, 11 months ago
it's me again *g i've begun to write an own script engine, this is my thrid approach so far, i didn't like the first two. i've written some simple classes, wich handle all the operations of a script, and execute them, but i have no clue, how i can read out a script and then preprocess the script. i have in mind that some kind of function reads out the script line by line, and then fills my ScriptLine array with the operation it needs to do there, and all the argument this operation needs.

struct Argument
{
	VarType _type; // Type of the Argument
	char *_name; // What Name the Argument has

	union {
		void *_ptr; // Pointer to it's Value ( in the stack )
		char *static_cdata; // Static char array
		int static_idata; // Static Integer
		float static_fdata; // Static Float
		bool static_bdata; // Static Bool
	}data;
};

class ScriptLine {
       int _type; // operation type
       std::vector<Argument*> _arg; // List of Arguments
};


this will be usefull, if i want to run the script very often, so that nothing needs to be done when executing this script, except than checking the operation type. how can i parse each line, and find out what needs to be done there? i had some simple grammar in mind, something like: blub = 5+4; blub = var2+3; blub = "i am a string"; SomeFunction( arg1, arg2, arg3 ); perhaps i'll make the language more powerfull, but only when this works. i've already heard of regular expressions, but i am not shure if this would be the right thing for this. i'm shure you can help me with this one :)
Advertisement
Well man you need a grammer parser, like Bison, Yacc, or PRECC... there's quite a few out there some free some not. You'll probably want a lexer too like Flex or Lex.

Good luck with that. I suggest you get a book on compilers too and start reading that first. Writing a scripting language from scratch is way more complicated than writing any other part of a game engine. If you're trying to make a game what you really want to do is use a language somebody already has unless you're wanting to do something really weird like Unreal's 'state' blocks which are pretty cool of course.

i'll look for that grammar parser, but i don't expect my language to be that powerfull, just something better than my last one ^^. i'm still no experienced programmer and i've much to learn. i'm writing it for practice and to be able to use it in my little game :)
I am also in the process of writing my own scripting language. A book that really helped me understand what was going on was 'Game Scripting Mastery'. It introduces you to several different iterations of a compiler, assembler, and virtual machine. It helped me write a finite state machine parser and lexer by hand (I will never do that again). Once I had some experience doing it from scratch I started learning lex and yacc (or flex, bison, etc... they are all pretty much the same).

Once you understand the basics to this stuff, its not too hard just tedious. Lex and Yacc really do help make things simpler (they spit out source code for you to use), I highly recommend both of them.
-----------------------------------Indium Studios, Inc.
isn't there any simplier solution? i don't need a complete language, i just want a basic "language" with a basic structure ^^

the grammar above would be my first approach, i wouldn't need more than 5 tokens per operation.
For a class assignment once we had to design our own instruction set for a processor and build an assembler and simulator. It was pretty simple and could easily be used for a scripting language if you just want something that is simple and works. The language would look like assembly language, but once you get the base down, you can add a lot of tricks to it. Then it is easy to parse.

For example, if you have this operation for example. This will add the values of $s1, and $s2 together and store it in $s0

add $s0, $s1, $s2


This is much easier to parse than a scripting language based off of a high level language. The first thing you will get is the operation, which can then give you how many arguments are going to be used. And then you know how many arguments you have to parse in. Much simplier than attempting a high level language, and it will be faster to implement. Plus you won't need to use lex or yacc. If you are interested in my method I can provide some more info for you.

Hope this helps
i'm shure that's easy to implement, but i would do it just with sscanf and write each token to a string. but i would really like to try new things and try to enhance my scripting language, but i think it's too difficult to try all these tools like yacc / lex etc...

does anyone know a good working regular expression parser? ( php got a good one, too bad it isn't integrated in c++, isn't it? )
boost::regex, and for scripting things, boost::spirit. Spirit is daunting at first, but actually pretty easy to deal with and similar to regexes once you get a little into it.

The problem is of course that string parsing isn't simple, even when what you're looking for is simple [to you]. Humans are particularly good pattern matchers.
i've got no idea how i should install boost. i've downloaded the source code. but the readme says i got to open a .bat file and then type in some commands, but the window opened by the bat won't stay open, it closes after some ms -.-
One of the unfortunate downsides to boost... Their website has better instructions, and there's a few threads on these forums specific to some of the problems.

This topic is closed to new replies.

Advertisement