script interpreter/compiler (custom language)

Hello,

I’m trying to do a custom language for AI scripting. This language will have a set of basic functions for detection and actions which are the exclusive entry points of my java AI engine, a simple expression evaluator for basic arithmetic/boolean operations and some control statements such as foreach and if/then/else.

I’m not trying to do java scripting at all but really a custom language.

So do you think there is already an existing free licence API that could help me doing this ?

thanks

There is a LUA version for java. And there’s beanshell… and uhm… something else, I’ve forgotten.

However, I suggest you take a look at janino. It’s java, it gets compiled(*) to byte code and it runs fast (it’s the same like your usual java stuff). You can also (besides complete classes) compile single methods or just expressions. It’s really awesome :slight_smile:

(* Compilation is very fast.)

ok but I want to prevent user from using java and only allow the use of my custom language exclusively.

I already had a look at beanshell and janino but AFAIK they are compiling java, which I do not want to do.

Then do a google for JLua and Groovy…They dont have java like syntax and are pretty powerful.

DP

er no… That’s not what I meant either.
:-/

What I’m looking for is a tool that helps me creating my own programming language and the interpreter or compiler that goes with. Like lex or yacc.
But I found an interresting pointer https://javacc.dev.java.net/
Someone knows it ?

JavaCC is one way, but what I think you’re really looking for is something like the Rhino Javascript Engine. It allows you to produce “standard” JavaScript code, but using only the core libraries you specify. Since the core libraries can be in Java, you can expose your AI to the Javascript programmer.

Does that help, or were you looking for something more along the lines of a Rete-based Rules Engine?

[quote]ok but I want to prevent user from using java and only allow the use of my custom language exclusively.

I already had a look at beanshell and janino but AFAIK they are compiling java, which I do not want to do.
[/quote]
beanshell has nothing to do with “compiling java” so I’m not sure what you’re saying here.

It is quite easy to “prevent” your users from using java from within beanshell: you could try searching the mailing list archives, or asking on the list for help in doing this.

IIRC unless you explicitly “allow full access to everything everywhere” then people writing beanshell scripts only have access to things you make available to them AND the default things that get imported into java (java.lang.). You might be able to disable access to java.lang. using a security manager or some beanshell-specific feature. I know that a lot of people are interested in doing this with beanshell; I don’t know how easy/successful it is.

However…WHY do you want to police access to java? You do realise that you will never prevent people from wiritng malicious code, e.g.:

while( true )
;

…so you need to put in introspection and stuff anyway, and once you’re doing that why not just introspect for any java stuff you want to prevent access to?

Also, there’s a lot of stuff you can disable in java by altering the security manager / security policy.

I havent looked at beanshell recently, but about 1.5 years ago I talked with the developer about limiting access to java specific stuff. He mentioned that he could add a feature where you could turn off access by package name. Since we never pursued beanshell, we didnt ask him to do it.

He may have added it on his own since then, or if not, I’m sure he would if you ask nice :slight_smile:

Of course while(true) ; sure is mean.

You need to trust scripters a little or spend lots of time figuring out all the holes you left behind.

It’s not that I don’t trust scripters or whatever. I just do not want them to play too much with memory or cpu because that’s not the purpose.
What I want is create a scripting language a bit like baldur’s gate’s.

This is exactly the kind of stuff I want them to write : (this is a draft of course)

** beginning of the ai script **
if (damageLevelOf(myself) > 80)
{
moveTo(nearestBuilding(ally, repairSation))
endScript
}

Unit lastAttacker=lastAttackerOf(myself)
if (lastAttacker)
{
attack(lastAttacker)
endScript
}

[ and so on… ]
** end of the ai script **

If you’re really interested, I have a complete course about writing a compiler:

  1. lexical check (spelling mistakes)
  2. syntax check
  3. type and name check for var’s and functions
  4. produce the code

This is exactly what you need to do if you want to write your own small language and interpet it afterwards. Instead of producing code as does a compiler, you need to run the specific commands in your program.

ATTENTION: this is a course given in 3rd year university so if you don’t want to really dig into this, then just forget what I’ve written.

If you’re interested, the slides (they are in french but java code is pretty self-explanatory) document everything from start to end.

I also have a nearly-complete program running that takes a source code and generates a tree-structure. It has several visitor techniques to run through the tree and check for sth. You just need to write a new visitor and call the stuff you need to call.

Gimme a quick msg if you’re interested.

This is the best lex/yacc doc/tute I’ve seen.

You really need to have some backround in fsm’s and language theory though.

Hm, make them add a ‘;’ at the end of each line and you compile it with a javac ?

[quote]If you’re really interested, I have a complete course about writing a compiler:

  1. lexical check (spelling mistakes)
  2. syntax check
  3. type and name check for var’s and functions
  4. produce the code

This is exactly what you need to do if you want to write your own small language and interpet it afterwards. Instead of producing code as does a compiler, you need to run the specific commands in your program.
[/quote]
Yes that’s what i figured out. I’ve got some background with that kind of stuff although I have not practiced since university.

Ok. I’ll see if I need them. Thanks. And by the I’m french so it’s ok.

Using javac would also allow users to use the entire java language which implies that the ai scripts would not be lightweight anymore.
Since the kind of game I’m working on is a turn based strategy game and that each unit could have a customised ai script, you probably guess what would happen…
So I must keep users inside boundaries of lightweight.

I think we will be able to help you more if you will exactly describe the roles in your game. Who is writing scripts, who is running them, who can be ‘bad’ and who is trusted, etc.

If person writing and running the script is the same, then two jvm solution might be possible. Run all dangerous scripts on one jvm and communicate with server/second jvm through remote calls (be it RMI or custom protocol). This may equal to running AI for units in client program instead on server for example. If you plan to run everything offline on server, then it might be also a solution - run jvm with minimal access rights, plus use some OS-specific tools to limit it’s memory/CPU usage (and kill it after timeout for example).

Please describe your use cases in detail.

[quote]I think we will be able to help you more if you will exactly describe the roles in your game. Who is writing scripts, who is running them, who can be ‘bad’ and who is trusted, etc.

[…]

Please describe your use cases in detail.
[/quote]
Ok here we go :

  • end user will be able to use this language to make his own ai scripts
  • this game is a turn based strategy game (standalone application) that will be playable offline but also through internet, the way warcraft can be played for instance.
  • the scripts should not be too complicated since they will be used by many units (maybee 100 per player) and it’s not a RPG so basic functions are : find enemy, attack one of them depending on some parameters, repair when too much damaged and so on…
  • scripts should be editable in-game for ease of use/test. So no external tools should be used.
  • ai is hierarchical

Nope. Can’t think of anything bad that would happen. What are you afraid of?

So all code will be written by you or by user and executed on user’s computer ? Then I see no problem at all. You cannot stop people from shooting themselves in the leg - even if they won’t be able to run out CPU/memory resources, they will program their units to make very stupid things.

Use on of existing scripting languages, create reasonable API to game, implement few macros to make programming easier and make sure that players cannot modify game internal resolution by scripts - and it will be ok.

Ok, here you go:

http://lamp.epfl.ch/teaching/compilation/2004/

In the slides we define a custom language called “EINS” (Eins is not Scala) based on the functional language SCALA.

On the site you will find the language grammar, the abstract grammar and the attributed grammar for name and type checking.

I’d start be defining the grammar of your language then implement the lexical check part. Then define the abstract grammar and construct the tree. Finally write 4 visitors, one for printing the tree, one for checking name, one for checking type and finally the last one for creating the executable script. (or make function calls in your case).

If you want a running project with the tree construction already implemented, I can send it to you.

ok merci :wink: