0

Is there an embeddable scripting language for .Net which supports some kind of yield (program stops and yields control back to host... With next run it continues where it left off)?

Furthermore it should be possible to save the state of the vm, and let it continue at some later point.

Edit1: I've looked at lua, and while one can access the globals from c#, the whole method to do it feels hacky.

Edit2: I'm also looking at ironruby and it seems one can query and set global variables, but not get all of them in one go.

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Toad
  • 15,593
  • 16
  • 82
  • 128
  • That part about yielding control, why do you need that? Wouldn't simply running the code in a background thread that doesn't stop work just as good? – Lasse V. Karlsen Nov 18 '10 at 13:57
  • @Lasse v. Karlsen: The yielding enables me to have well defined spots to save the whole state of the VM. In c++ there were some vm's where you could execute the script line by line, something like that would be good too. – Toad Nov 18 '10 at 14:29

6 Answers6

1

Write it in VB or C#, with a well-defined tree of classes that hold all your state. This tree will consist of a root class, with all your data in lists etc., that are serialisable.

At an appropriate point, XML-serialise everything out to a stream file.

On re-start, de-serialise from the saved file and start where you left off.

smirkingman
  • 6,167
  • 4
  • 34
  • 47
  • good idea actually. Too bad i lose the easy of changing functionality which i have with a script. Although the proposed cs-script might be a solution to that one. Btw what happens if one serializes an open db connection? Is that deserializable? – Toad Nov 22 '10 at 18:41
  • sometimes, a bit of thinking outside the box is helpful. No, an open db connection won't serialise, you'll have to 'remember' where you are (note the PK of open files maybe?) – smirkingman Nov 22 '10 at 19:42
1

Boo does.
There is a book on Making your own embedded DSL in boo. I highly recommended read (by Oren the great): http://www.manning.com/rahien/
Sample chapter: http://www.manning.com/rahien/Sample1.pdf
Here's how you can embed it:
http://boo.codehaus.org/Boo+as+an+embedded+scripting+language

basarat
  • 261,912
  • 58
  • 460
  • 511
1

bungee# is a C# script. Actually a programming language that is developen in c# and syntax is the same with C#. Bungee# will be an open source project in a short time but haven't published yet. you will be able to find the source on codeplex. I suppose it is the thing that u want. Because it supports almost every keyword in c# and there are methods that are defined and also you can write new methods in it. I hope you'll like it. bungee link is this but not published yet

Burcu Co
  • 83
  • 2
  • 6
0

Have you tried using C# as "scripting language"? See http://www.csscript.net for a library that provides easy integration of c# as script into applications. It uses the .NET built compiler to create and load a new assembly from a file or string. You can do anything you can do in your application as you have a full c#. Including yield. To pause the "VM" you can let the script run in a thread and suspend and resume it.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • gloor: I've looked at it. It's impossible to save the state of the script. It's basically nothing more then an 'on-demand' compiler which creates an assembly which you can execute. The hosting program can only access public methods and has no way to save any private variables (for saving state) – Toad Nov 22 '10 at 10:33
  • gloor: btw when I say 'yield' I don't mean the keyword as in c# per-se. It's just a means of stopping the script temporarily at some point giving control back to the hosting program, and the host program can then 'resume' the script at some later point. – Toad Nov 22 '10 at 10:35
  • Yes its only a C# compiler. But C# is so powerful that you can achieve what you want. If I understand you correctly you want to suspend your script at one point and resume it at a later point. So why not doing it as I proposed and suspend/resume the thread running the script? There is no need to access the private variables in this case. Furthermore, with a proper interface you can do the yield easily. If you really need saving the state you can still serialize them. – Remo Gloor Nov 22 '10 at 21:12
  • gloor* I don't want to only pause it. I want to save it's state in a database and resume it at some other point in time. ;^) – Toad Nov 24 '10 at 14:18
0

I'd suggest looking at lua again, if you feel something is hacky, just make sure you do it right, and then wrap it in a class that makes it less hacky for you afterwards. If that's a problem, then there's already existing managed lua solutions available to use. Sometimes you simply wont be able to find a non hacky solution because no one has bothered to make one yet.

William
  • 772
  • 7
  • 18
0

Depending on the context PowerShell might be a good candidate. Documentation on how to host PowerShell in a .Net application can be found here, PowerShell supports executing commands on a per command bases as well as adding chunks of code to the to be run PipeLine.

Saving the state of the vm sounds a bit tricky I have not found any evidence that this is possible with PowerShell. You can however use a CmdLet called Export-Clixml to save the state of any object you might be interested in.

PowerShell would be a good match if your scripting needs to interact with the environment, since PowerShell Modules seem to popup for everything but the kitchen sink. This is what a search on CodePlex turns up.

Bas Bossink
  • 9,388
  • 4
  • 41
  • 53