473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 10603
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.