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