12 April 2006

The Pawn (former Small) scripting language.

Pawn is a very nice scripting language, which we use in our server. It allows us to write the quests faster than if we'd write them in C, and some volunteers can write quests without being given access to the server source code.

One good thing about Pawn is that the syntax is very similar to the syntax of C and PHP. It is a typeless language, which means that you can not assign a type to a variable (such as char, short, float). Instead, each variable is a 32b integer (or 64b on a 64b machine).
Because the syntax is C like, pretty much any C programmer can learn it in just a few hours.
But because it doesn't have pointers, types and classes, beginners can learn it in just a few days.

The Documentation:
Pawn comes with two PDF manuals, one for the person who is going to embed it in an application, and the other for whoever wants to learn the language itself, without being bothered by the unnecessary details of embedding it.
Both manuals cover everything that needs to be covered, so you will not need to spend hours googling for tutorials.

Pawn is a compiled language. That is, it has a compiler that translates your Pawn code into its virtual machine code. The advantage is, of course, the execution speed. Interpreted languages are pretty slow, compared to compiled languages.
To make things faster, you can use a highly optimized JIT VM that is written in assembly. It works on Windows, Linux and FreeBSD, but only in the 32b mode. The assembly version is about 10 times faster than the C one.
The C virtual machine can be used on more operating system, and even on some embedded devices without an OS.

The interaction beteween the host program and the Pawn script is bidirectional; the script can call native C functions (via wrapper functions) and the native code can call the script routines. Furthermore, public variables (from C) can be accessed by the script.

Compared with other scripting languages, Pawn will give you the following benefits:
a. Easy to learn.
b. Very lightweight.
c. An X86 assemby virtual machine is available so the speed of the compiled script is close to a natively compiled language.
d. Easy to embed and expand through C functions.
e. Excellent documentation.
f. Familiar syntax (for most of the programmers).

The license:
Pawn comes under the zlib license, which is as liberal as it gets. You can use it in commercial programs, without having to pay anything, and you can statically link to it without having to reveal your source code.

If you are like me, you would like to see a sample of the syntax, so here is one example, from the official package:

/* word count: count words on a string that the user types */

print "Please type a string: "
new string[100]
getstring string, sizeof string

new count = 0

new word[20]
new index
for ( ;; )
word = strtok(string, index)
if (strlen(word) == 0)
printf "Word %d: '%s'\n", count, word

printf "\nNumber of words: %d\n", count

strtok(const string[], &index)
new length = strlen(string)

/* skip leading white space */
while (index < length && string[index] <= ' ')

/* store the word letter for letter */
new offset = index /* save start position of token */
new result[20] /* string to store the word in */
while (index < length
&& string[index] > ' '
&& index - offset < sizeof result - 1)
result[index - offset] = string[index]
result[index - offset] = EOS /* zero-terminate the string */

return result

As you can see, you don't have to terminate your instructions with a ";", akthough the compiler won't mind if you do so. However, empty statements after an if, such as: "if(variable);" are ignored.

If you would like to learn more about Pawn, visit the official website: http://www.compuphase.com/pawn/pawn.htm

The bottom line is, if you need a C like, lightweight, portable scripting language for your applications, give Pawn a chance, and you won't regret it.


Blogger Donny said...

Will have to check out pawn when I get to actually working on some code for myself looks promising.

Yes, I also enjoy seeing code with actual examples instead of theory's and things makes working with it much easier because you can actually figure out how it works instead of guessing and taking longer just because there was horrible documentation or examples, I deal with that almost every day the API documentation I use at work is garbage but I need it to write programs that work with GoldMine.

I sure do miss c code, delphi just doesn't cut it for me but if I use anything else people at my job would be "lost" so I stick with what they know since I can learn things pretty quickly.

13/4/06 01:20  
Blogger Radu said...

Yeah, I don't like Delphi (well, I never tried Delphi, just plain old Pascal in school).

On the other hand, nothing prevents you from having your own C side project, right? :)

14/4/06 02:21  
Anonymous Pratik said...

thats interesting! can you help me to know how to call C library function from pawn script?

18/6/10 05:39  

Post a Comment

<< Home