Scripting Languages

Started by
5 comments, last by Spiral Guru 23 years, 9 months ago
If I want to develop a scripting language to assist a game I''m making, what do I need to research? Do I invent a syntax, and write a compiler? Do I generate C++ for the scripts, and pass it to the compiler for it to do its work? Can I use an already existing script (such as Java script)? If so, how? I understand this cannot be explained quickly or easily, but I am not new to coding, so please, some pointers in the right direction maybe?
Advertisement
www.flipcode.com has an excellent tutorial on implementing scripting languages, you could have a look there.

Here it is .


Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Well...

First make sure that a script lang is what u want. Ive coded some script interpreters before.

Its a lengthy job and as a new programmer your script parser will probably not be as robust as you would like.. which means not only will it probably fall over (often caused by pointers going passed ends of strings and such), but you''ll find that as you go your script develops a syntax of its own and this can be annoying.

What I mean is while coding you realise that... say your method of tokenising the string breaks at spaces... then you cant use tabs or anything else. You may also have to stick to one space between terms. Admittedly each of these problems by themselves are not much to worry about and require little coding to fix, but its the shear number of little fixes/patches and recodes you will probably do which will take up the time.

As my computer science lecturer''s favourite saying on a directed research project I am doing goes:

The time taken to write a program is roughly equal to the sum total of the little things which are, in essence, easy to do.

Makes sense to me



regards,

GeniX
regards,GeniXwww.cryo-genix.net
If you plan on using Java for your scripting purposes, on www.gamasutra.com was an article which outlined just that.

Also, you might take a look at python (www.python.org) it''s a pretty cool scripting language which has a good C/C++ interface.

BTW, the beauty of a scripting language should be that you don''t have to recompile anything (sometimes this can''t be avoided). You just want to have your program execute a script/scriptmethod. If the script code changes, the behaviour of the program should change. The really hard part is interfacing the scripting system with the run-time code (If you want to access functionality of the run-time code from within the script).

MK42
I've written a script engine for my game. And yes, it's a LOT of work! You have to know almost everything about compilers and interpreters and LR(1) parsers and backpatching and whatnot.

But praise the lord , because there are a lot of tools available to help you with that: check out Flex and Bison. The first is a lexical analyzer generator, that generates C code that parses your input text into "tokens" (or: words), and the second is a "compiler generator", that generates C code that uses the tokens generated by flex to actually do something with the input.

But if you're too lazy to learn all that stuff, there are some open-source script engines available. You can use mine, but I wouldn't recommend it because it's full of bugs . I'm working on incorporating the Mozilla java-script engine in my game (instead of my crappy engine, heh). You can download the source at Mozilla too. You can link in your own objects so you can interface the java-script code with your app and vice versa.

So if you want to use an already existing language I'd say go with java-script (which I recommend ), but if you really want to create your own language, use Flex and Bison (and prepare for a couple of months of coding and debugging).

Dormeur.


Edited by - dormeur on July 14, 2000 9:37:51 AM
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
If you''re dead serious about writing your own scripting engine. I would suggest using ANTLR as your parser generator(www.antlr.org). It''s a lot easier to work with than flex/bison. You can also get a host of grammars (C/C++/Java) that work with ANTLR. Unfortunately, the tool is in JAVA (but can generate C++ parsers nonetheless), which means you have to install the JDK to use it.

Trust me, this utility is a real gem...

MK42

You don''t have to know everything about LALR parsers, grammers, and certainly not bison and flex. Check out some Sweeney''s ramblings about scripting languages for "language features" you might want. I''m about 3/4 of the way done with a scripting language whose parser is entirely in C(no flex/bison), the actual slowdown for me was the compilation part, not the parsing.


A good introduction to parsing context free grammers which takes an unassuming pose rather than the pompous "Thou shalt have a Ph.D." approach of some is a here. It gets you through most of the parsing stuff. I would say the best benefit from it is a good explanation of BNF''s, why some tools overdo it, and how one can, with a proper BNF, simply write a program in the language of your choice FROM the BNF(I have a 1 to 1 correspondance of functions to my BNF).


You might SERIOUSLY want to consider Java, as most of the work is done for you, and I''ve been told that JNI makes it possible to interface with C/C++ code, and Jave VM''s are getting pretty efficient.


If you think about it, its not as hard as it appears, and I think its fun to write your own(gives some perspective compiler writing). It can made too difficult, and I would caution you to watch out for that, remember: Keep It Simple Stupid


This topic is closed to new replies.

Advertisement