Skip to main content
GameDev.net gamedev.net
🔒 Locked

script engine // parse scripts

Started by SiS-Shadowman May 17, 2006 at 9:24 AM 17 replies 900+ views
Original Post
SiS-Shadowman
SiS-Shadowman
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 :)
SiS-Shadowman
SiS-Shadowman
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 :)
jkleinecke
jkleinecke
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.
SiS-Shadowman
SiS-Shadowman
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.
JohnnyCasil
JohnnyCasil
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
SiS-Shadowman
SiS-Shadowman
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? )
Telastyn
Telastyn
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.
SiS-Shadowman
SiS-Shadowman
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 -.-
SiS-Shadowman
SiS-Shadowman
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]
Deyja
Deyja
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. :)
SiS-Shadowman
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
Deyja
Deyja
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.
Verg
Verg
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.