Sign in to follow this  
SiS-Shadowman

script engine // parse scripts

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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? )

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 -.-

Share this post


Link to post
Share on other sites
isn't there just a compiled library out there wich supports the **** regular expressions? i've now downloaded tons of them, but either they require all the mfc garbage i don't need or they won't work and produce tons of compiler errors...

*edit*

i'm too stupid to install boost under windows, i don't know what i have to do after setting the paths for visual studio in the console, the manual doesn't help very well. the search doesn't help me either -.-

[Edited by - SiS-Shadowman on May 17, 2006 3:48:58 PM]

Share this post


Link to post
Share on other sites
Is one of your requirments that the scripting library support regexs? Then bind boost::regex to angelscript. Angelscript doesn't include ANY built in libraries because you can add anything to it so easily. :)

Share this post


Link to post
Share on other sites
i don't need regex in a scripting language. i want to create my own and use regex to parse the lines ( i know it's not possible because of the infinite thing, but i just need a simple syntax )

i don't get any of the librarys supporting regex running... the manuals aren't helping a bit

Share this post


Link to post
Share on other sites
boost::regex is certainly the best option for regexs in C++. I've been told it's interface is very similiar to java and .net's regex packages, but I've never used either of those two myself. If you have trouble getting it working, I'm sure if you posted a thread here about the specific problem you would get plenty of help.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiS-Shadowman
i don't need regex in a scripting language. i want to create my own and use regex to parse the lines ( i know it's not possible because of the infinite thing, but i just need a simple syntax )

i don't get any of the librarys supporting regex running... the manuals aren't helping a bit


It's too bad you can't seem to install boost... since boost::spirit is very nice for creating your own parser/script language.

It does seem a little scary at first; but if you follow Joel's docs from the start, and try the examples... your brain should open up to it and it will seem easy.

We've currently implemented a GUI scripting language for our apps... boost::spirit can do what you're asking.


Chad

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


You can write a state engine that processes your language's statements.

One would be a tokenizer that reads in a line string and determine from the characters what the token is (ie- a number, a variablename, a quote delimited string, punctuation/operators etc...).

You then gave another state engine that builds your statements (using the tokens that come from the tokenizer to transition your state engine). Its not too complicated if you stay away from nesting of control blocks, or parenthesized equations, etc....

You would either execute operations corresponding to the language or build bytecode. Things get weirder when you have things like ELSE statements (when you have to skip execution of statements and find the end of a code block...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this