Csharp simple script interpreter

J

Joel

In the course of my project, I must include some custom logic and
would like to integrate a small script language in my application for
that purpose.
In C++, I used the LUA script language and I know that bindings exists
for a LUA/c# integration, but only using the binary DLL.
However, I would prefer a native c# solution and for a small language,
not a huge interpreter like Python.

Has anybody seen such implementation ?
Thanks,

Joel
 
D

DeveloperX

In the course of my project, I must include some custom logic and
would like to integrate a small script language in my application for
that purpose.
In C++, I used the LUA script language and I know that bindings exists
for a LUA/c# integration, but only using the binary DLL.
However, I would prefer a native c# solution and for a small language,
not a huge interpreter like Python.

Has anybody seen such implementation ?
Thanks,

Joel

Would C# be appropriate for your scripting requirements? It's really
simple to dynamically compile and load code into a running
application. I hadn't heard of LUA, I'm just looking at the docs now
and I can't see anything it does well that you can't do in C# if that
makes sense. There's a good tutorial on CodeProject that I can't find
which shows how to type in code, have it compiled and even list any
compilation errors.
 
L

Lebesgue

Writing a simple C# syntax based interpreter in C# is a simple task. I
occasionaly implement one in my projects when I find use for this kind of
functionality.

What you need is to use CSharpCodeProvider to compile the prepared source
code which looks basically like this:

you'll declare this class in your project:

public class ScriptBase
{
public virtual void Execute() {}
}

you'll prepare a "template" from which you'll create the source code you are
going to compile dynamically:
string template =
@"public class Script : YourNamespace.ScriptBase
{
public override void Execute()
{
$script comes here$;
}
}";

what you do is string source = template.Replace("$script comes here$",
script); and compile this source using the compiler you get from the code
provider by calling its CreateCompiler method. Make sure you include your
executing assembly (the one that contains class ScriptBase) in the
ReferencedAssemblies.

Then you'll check for errors, take the compiled assembly from the
CompilerResults, and use reflection to create an instance, cast it to
ScriptBase and call its Execute() method - which will execute you script.
You can also implement some binding to your running program (passing a
reference to some "bridge" object to the execute method or constructor) or
methods in the ScriptBase class which you will be able to call in your
scripts, depending on your needs.

this article has source code for expression evaluation that does what I have
described above: http://www.codeproject.com/csharp/runtime_eval.asp
There is an article on C# interpreter that uses a slightly different
approach on codeproject as well: http://www.codeproject.com/csharp/csi.asp
 
J

Joel

Thank you all for your extensive answers.

As a (mainly) C/C++ programmer, it is quite strange for me to use my
own "internal" compiler in r/t mode.
I will study this solution.

Do you think that this solution is also available on mobile devices ?
I am inclined to think "yes" because it is more "language" dependent
than framework dependent, but... (I had a lot of trouble finding the
available metods in compact framework).

Joel
 
L

Lebesgue

Unfortunately, this is not supported on mobile devices. The reason is that
there is no C# compiler (csc.exe) that runs on mobile device - you compile
your device applications on desktop.
Some time ago I had an idea to try to convert some freely available parser
for compact framework, create a basic C# compiler (or try to port mono
implementation to CF) and add this functionality to the CF, since (not only)
I really miss dynamic code compilation on compact framework.
 

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