By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,994 Members | 1,080 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,994 IT Pros & Developers. It's quick & easy.

Csharp simple script interpreter

P: n/a
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

Feb 3 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 3 Feb, 09:44, "Joel" <fr.joel.ja...@gmail.comwrote:
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.

Feb 3 '07 #2

P: n/a
If your script language is very simple, you could build your own interpreter
using a combination of RegEx and the various Parse functions. If it is too
complicated for that, you can find a parser, in C#, at
http://www.frontiernet.net/~fredm/pa...istWebPage.htm .
"DeveloperX" <nn*****@operamail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
On 3 Feb, 09:44, "Joel" <fr.joel.ja...@gmail.comwrote:
>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.

Feb 3 '07 #3

P: n/a
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

"Joel" <fr***********@gmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
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

Feb 3 '07 #4

P: n/a


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


Feb 4 '07 #5

P: n/a
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.

"Joel" <fr***********@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
>

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


Feb 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.