Giving Some Back

Started by
33 comments, last by EDI 20 years ago
When I set off to make my scripting language I ''felt'' that i knew enough to do it, as for external resources I didint use any, so your guess is as good as mine, all i can say is be fimilar and comfortable with the knowledge of how code executes.

the major pitfalls that got me, were really circumstantial for my specific case *event locking*, though i would now live and die by these few rules....


if your planing to use infix expressions, save yourself some time and find some infix evaluator code online.

ability to traverse the final instruction code with knowledge of only one instruction, is a must, that is from a given instruction you must be able to go to that instructions nested parent,it''s first child, it''s previous sibling and it''s next sibling.

as mentioned above instruction execution should happen on a per instruction basis, the ExecuteInstruction function should make use of, and move an instruction pointer, based on the kind of instructions that get executed.

e.g.

if you execue a variable set command, then advance the isntruction to it''s next sibbling(if applicable)

if you execute an if command, evaluate the infix terms if true advance to the instruction''s first child(if applicable), otherwise advance to the next sibbling(if applicable).

and as with all things, obey the rules of K.I.S.S =D




Raymond Jacobs,

www.EDIGames.com

www.EtherealDarkness.com

Raymond Jacobs, Owner - Ethereal Darkness Interactive
www.EDIGames.com - EDIGamesCompany - @EDIGames

Advertisement
quote:Original post by Nerothos
I recently started my own scripting language. Do you have any good resources or ideas / major problems you encountered along the way that I could use as a base step? Thanks a lot!


If you are going to make a Command Based Language (like EDI has) wherein your game functions are written in C++ and are called from your compiled scripts via calls to those functions (commands), then you may be able to get by with what you already know. Just attack the job in small pieces, take your time, and pay attention to details. You'll do fine.

If you're making a full fledged Procedural Language (like I am) which is much more similar to something like straight C, then you're in for a rough ride. This is, without a doubt, the toughest programming challenge that I've set out to do. However, the applications of such a system will prove to be well worth it in my projects. Well designed Procedural Languages can do everything that Command Based Languages can do and then some with little or no run-time loss, are extremely flexible, and are 100% reusable.

But, on flip side, they take WAY longer to implement and will give you a lot of headaches along the way. I'm about half-way through the process and have been hacking away at it for 2 months. And, while I have no doubt what-so-ever that I will finish this task (I always do), It will likely take me 2 more months to complete...whereas EDI's Command Based Language took roughly 1-2 weeks to implement.

If you are serious about designing a scripting language then I HIGHLY suggest a new book called "Game Scripting Mastery" by Alex Varanese, especially if you're making a Procedural Language. Procedural Languages go into some pretty heavy Compiler Theory and should NOT be attacked without devoting a hefty amount of in-depth research and design time.

Also remember, as with any scripting system, try to implement as much is practically possible in straight C++ (for raw speed), and only use scripts where it makes sense to because of the developmental advantages.

[edited by - ChronosVagari on April 1, 2004 9:38:33 PM]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The only thing better than word-play is playing with words involving word-play. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I would just like to clarify,

the only thing that the scripting language i wrote lacks, is the ability to define and call script based functions.

the only reason i didint implement this was because there was really no need for it, and to have them, would cause the users of the scripting language to use it in ways that it should not be used *writing lots of code in script wherein it should belong in the engine*

with that said, chances are my language is about 20% away from where CV's will be when it's done, since implementing functions is not that hard. in fact it would have been one of the easier challenges that I faced. to give a litte orientation of language capability here is most the functionality my scripting language can handle.
#an entry pointMain(){	x=25;	if(x>30)	{		bln=false;	}	else	{		bln=true;		if(x==25&&bln==true)		{			v=0;			loop=true;			while(loop)			{				switch(v)				{				case 0:					v=v+1;					break;				case 1:					v="cool";					break;				case "cool":					v="wow";					break;				default:					loop=false;					break;				}			}						#call an internal function			yn=DoesEDIGamesRock();			if(yn==true)			{				#end the script				return;				}			else			{				#unreachable statement ;-)			}		}	}}  


Raymond Jacobs,

www.EDIGames.com

www.EtherealDarkness.com



[edited by - EDI on April 1, 2004 9:42:27 PM]

Raymond Jacobs, Owner - Ethereal Darkness Interactive
www.EDIGames.com - EDIGamesCompany - @EDIGames

Just to reiterate EDI''s above post, the only difference that I see in the actual script code is the comments. Mine uses the standard C // and /* */ syntax. Other than that, that code example would be exactly the same in my language.

But, for me, the ability to call functions defined within a script from other scripts as well as the host API is well worth the extra effort...plus, as a hobbiest, I like the challenge.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The only thing better than word-play is playing with words involving word-play. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The dragon book is a good resource, but it takes some patience to read it.
Also wikipedia.org has some pages on compiler design.

***
For Java games and Java related resources, go to http://www.javaengines.dk
***

Developer journal: Multiplayer RPG dev diary

quote:Original post by blackone
The dragon book is a good resource, but it takes some patience to read it.
Also wikipedia.org has some pages on compiler design.

***
For Java games and Java related resources, go to http://www.javaengines.dk
***


The Dragon Book is THE source for Compiler Theory. However, for the implementation and design of most scripting languages, the level of design in that book is overkill. Of course, if you''ve got a large enough team and ample developement time, then by all means enlighten us with a successor to Lua...hopefully using C syntax.

IMO the author of "Game Scripting Mastery" does a great job of teaching enough Compiler Theory to make a very good, solid, scripting system of your own design.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The only thing better than word-play is playing with words involving word-play. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thanks for all info! So far, I haven''t thought of exactly how I want the language itself, but I have gotten somewhere. My parser can handle almost all of the mathematical operations that C can (+, -, /, *, ^, =, %, ()), and it can handle 26 variables (A-Z or a-z, both are ==) now I have to add more variables and strings along with it. Any ideas on implementing real variables rather than just preset A to Z''s? Thanks!!
Jump in and crash the system...it''s quite humbling.
Disclaimer:
The techniques that I'm about to describe are very watered down. I left these details out because it would have taken me several pages to describe the process in adequate detail. With that said...on with the show!


When you read in your source file, instead of reading in one character, read in several characters and store them as a strings. You can still read one character at a time, but when you read in whitespace or a delimeter (like a comma) then you know that the string has ended. These strings will represent your variables, commands, etc.

As you read in the strings you figure out what type they represent based on the characters that are contained within them. For example, floats will contain a ., possibly a -, and the rest of the characters will be numbers. I suggest building functions that checks the string for what type it represents, so that you only have to go through the headache once. Make a function to check each type that your system supports. Strings are a bit more complicated to detect because they contain whitespace and other delimeters. However, you'll know that they are going to start with a " charater, so you'll just have to parse the string a little differently if the first character in the string is a ". Also, be careful to handle quotation marks inside your literal strings, so that you don't end your string too soon and screw everything up.

As you read in the strings, figure out their type, and build tables containing those strings as well as the necessary info that goes along with them (ie. command parameter count & types). Then when you get around to writing your compiled script file you'll first write the type, then the variable (or command, etc.), then the other necessary info. You do this so that your Virtual Machine, what loads and runs your scripts, will know what to do with the information due to it's type.

Anyway, if you have other questions, drop EDI and I another line in this thread. We took very different approaches to the same problem, so I'm sure that one (or both) of us will be able to help you out will your own approach.

[edited by - ChronosVagari on April 3, 2004 12:48:54 PM]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The only thing better than word-play is playing with words involving word-play. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CV is right,


the way i did it was to first ''normalize'' the language structure

for instance

x=v+1;
if(v==2)
{
//code
}


would become

x=v+1;if(v==2){//code}

by striping out spaces tabs and line breaks

this makes it much easier to interpret
less ''whitespace skipping'' code.

so in this example, i would start by reading each character into a temporary string buffer.

once i reached a '';'' i would analize what is in the buffer to decide what it is.

in this case,

x=v+1

you would then check this string against some interpretation tests.

for instance if the 2nd character is an ''='' then you know this instruction is an assignment.


as for diferentiating between type, as CV explained it''s simple

when you fine some special character (+,=,-,...etc)
then start reading to the right of it, until you either reach the end of the string or another special character.

in the event of

raymond="cool";

you would first read and save each character until you got to ''=''
then you would have ''raymond'' since this text doesnt have any quotes in it and it''s first character is a alphabetic character, you can assume it is a variable name or a function, if the string contains a ''('' you can asume it is a function,otherwise you can then fault the variable name against your variable store to get the actual variable value.
if the first character of the string is a ''"'' then you can assume it is a string value. if both of the previous tests are false you can assume it is a number.

hope that gives you some ideas=D



Raymond Jacobs,

www.EDIGames.com

www.EtherealDarkness.com

Raymond Jacobs, Owner - Ethereal Darkness Interactive
www.EDIGames.com - EDIGamesCompany - @EDIGames

How did you write your parser / syntax analysis? Did you use FLEX/BISON or create your own?

I tried a while ago, got a decent-ish VM up and running but fell down totally on the parsing side.

This topic is closed to new replies.

Advertisement