Programming a Script language

  • Thread starter Piotr 'Zenobius' Baranowski
  • Start date
P

Piotr 'Zenobius' Baranowski

Hello

I'm new to programming under C#.

I have question about suggestions, how to write a simple script
language, that can handle operations like 'if' statement.

Whats better, operating on variables [ like 'if a>b foo() else goo() ],
or maybe predefined functions [ like if(getA()>getB()) foo() else goo()]

And how to handle script interpretating, or optimising scripts speed?
Like compiling text to some type of ' native' program code.


I'm thinking about fast and simple script language, that will allow to
make templates of behavior in game [ change AI ].
 
J

Jimbo

Piotr said:
Hello

I'm new to programming under C#.

I have question about suggestions, how to write a simple script
language, that can handle operations like 'if' statement.

Whats better, operating on variables [ like 'if a>b foo() else goo() ],
or maybe predefined functions [ like if(getA()>getB()) foo() else goo()]

And how to handle script interpretating, or optimising scripts speed?
Like compiling text to some type of ' native' program code.


I'm thinking about fast and simple script language, that will allow to
make templates of behavior in game [ change AI ].

Yet Another Compiler Compiler (yacc) is a application for turning a
language grammar into code. It's an old C thing originally on Unix
systems, but ports were done to Java. Have a search on google and you
might come across a C# port of YACC.

Jimbo
 
J

John Vottero

Piotr 'Zenobius' Baranowski said:
Hello

I'm new to programming under C#.

I have question about suggestions, how to write a simple script
language, that can handle operations like 'if' statement.

Whats better, operating on variables [ like 'if a>b foo() else goo() ],
or maybe predefined functions [ like if(getA()>getB()) foo() else goo()]

And how to handle script interpretating, or optimising scripts speed?
Like compiling text to some type of ' native' program code.


I'm thinking about fast and simple script language, that will allow to
make templates of behavior in game [ change AI ].

How about using Microsoft.CSharp.Compiler.Compile()?
 
P

Piotr 'Zenobius' Baranowski

How about using Microsoft.CSharp.Compiler.Compile()?

This gives me whole C# lang support, right? :)


This should be easy to write lang - for players that want to change
their units behavior. Like:

if attacked() run() else stay()

or

if attackerNum>100 run() else fight

it should be compiled, to run fast, and then such compiled script,
loaded as resource, can be added to unit/units.

Any suggestions?
 
D

Daniel O'Connell [C# MVP]

Piotr 'Zenobius' Baranowski said:
This gives me whole C# lang support, right? :)


This should be easy to write lang - for players that want to change
their units behavior. Like:

if attacked() run() else stay()

or

if attackerNum>100 run() else fight

it should be compiled, to run fast, and then such compiled script,
loaded as resource, can be added to unit/units.

Any suggestions?

Just look up scripting languages and write an interpreter or compiler (or if
you are using .NET 2.0, complie to DynamicMethods). Its not the easiest
thing in the world, but its doable. You'll need a parser(you could use a
parser generator or write your own, I prefer the former) and a bit of
compiler knowhow. Developing a scripting language is well beyond what could
be explained here.

You might also want to look for python or lua bindings or consider existing
..NET languages like boo or nemerle that might help.
 
J

Jimbo

Piotr said:
This gives me whole C# lang support, right? :)


This should be easy to write lang - for players that want to change
their units behavior. Like:

if attacked() run() else stay()

or

if attackerNum>100 run() else fight

it should be compiled, to run fast, and then such compiled script,
loaded as resource, can be added to unit/units.

Any suggestions?

I am doing a game where a computer controlled player has to move army
units and make decisions about which territories to invade, when to
retreat etc.

My solution for having different behaviours for different players is to
use an implementation of the strategy pattern. This involves purely java
code (but its the same for c#) where I define the interface that all
behaviours must implement. The interface basically passes in the game
state so that the implemented behaviour can make its decision. Once
written the new class is compiled in the normal way. I then use
reflection to load the appropriate class into the game at run time. I
have a configutation file which tells the system which class to load and
use.

In theory this allows A. N. Other person to write a strategy and try it
out in the game.

Jimbo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top