472,986 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Csharp simple script interpreter

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
5 10526
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
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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ian Giblin | last post by:
I am an experienced C programmer, learning C++ by writinging a mathematical toolkit in the framework of a script interpreter. I am posting here to ask for advice (or references) on the object...
6
by: harish | last post by:
Hi, We are developing one application for smartphone. In this we are parsing XMLs and rendering it as UI elements using smartphone SDK. Everything is working fine. Now one more requirement has...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
1
by: Brian Blais | last post by:
Hello, I was wondering if there is a way to, within a script, jump into the interpreter. What I mean is something like the "keyboard" command in Matlab, where the script pauses and you get an...
2
by: tom.purl | last post by:
I use the pyGTD script to manage my todo lists and such. From Vim, I shell out a call to the gtd.py script, which updates my todo.txt file after update one of the related pyGTD files. Since I...
6
by: Alan Isaac | last post by:
I'm fairly new to Python and I've lately been running a script at the interpreter while working on it. Sometimes I only want to run the first quarter or half etc. What is the "good" way to do...
3
by: uzzi | last post by:
I don't know how to make a php script to work via cron...I want to make it run daily...My server API is CGI/Fast CGI.I have hosting from godaddy and in the help section i found that i have to specify...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
4
by: Chris8Boyd | last post by:
I am embedding Python in a MSVC++ (2005) application. The application creates some environment and then launches a Python script that will call some functions exported from the MSVC++ application....
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.