Creating an interactive shell for .NET apps and embed scripting languages like python/iron python into it

StackOverflow https://stackoverflow.com/questions/808692

Question

I was learning python using the tutorial that comes with the standard python installation. One of the benefits that the author states about python is "maybe you’ve written a program that could use an extension language, and you don’t want to design and implement a whole new language for your application" - My question is how would i go about designing a program (using c#) that can be extended using Python interactively(for this to be possible, i would imagine that i would need to create some sort of a "shell" or "interactive" mode for the .net program) ?

Are there any pointers on how to design .NET programs that have an interactive shell. I would then like to use python script in the shell to "extend" or interact with the program.

EDIT: This question partly stems from the demo give by Miguel de Icaza during PDC 2008 where he showed the interactive csharp command prompt, C# 4.0 i think also has this "compiler as a service" feature. I looked at that and thought how cool would it be to design a windows or web program in .NET that had a interactive shell.. and a scripting language like python could be used to extend the features provided by the program.

Also, i started thinking about this kind of functionality after reading one of Steve Yegge's essays where he talks about systems that live forever.

Was it helpful?

Solution

This sounds like a great use of IronPython.

It's fairly easy to set up a simple scripting host from C# to allow calls into IronPython scripts, as well as allowing IronPython to call into your C# code. There are samples and examples on the CodePlex site that show how to do this very thing.

Another good site for examples and samples is ironpython.info

And here is a page dedicated to an example answering your very question, albeit in a generic DLR-centric way -- this would allow you to host IronPython, IronRuby, or whatever DLR languages you want to support.

I've used these examples in the past to create an IronPython environment inside a private installation of ScrewTurn Wiki - it allowed me to create very expressive Wiki templates and proved to be very useful in general.

OTHER TIPS

I was looking solution for the same problem, and found IronTextBox: http://www.codeproject.com/KB/edit/irontextbox2.aspx

It needs a little tuning for current versions, but seems to be everything I needed. First made it compile, and then added variables I wanted to access from shell to the scope.

Python as an extension language is called "Embedding Python".

you can call a python module from c++ by bascially calling the python intepreter and have it execute the python source. This is called embedding.

It works from C and C++, and will probably work just as well from C#.

And no, you do not need any kind of "shell". While Python can be interactive, that's not a requirement at all.

Here is a link to a blog post about adding IronRuby to script a C# application.

http://blog.jimmy.schementi.com/2008/11/adding-scripting-to-c-silverlight-app.html

The principles would also work well for using IronPython.

If your goal is to avoid learning a new language you can use CSScript.Net and embedded scripts written in C# or VB into you application. With CSScript you get full access to the CLR. Three different models of script execution are supported so that you can execute script that refers to objects in your current app domain, execute using remoting, or execute as a shell.

Currently I am using CCScript as "glue" code for configuring application objects somewhat similar to using Boo.

This link tasks you to a code project article that provides a good overview.

I don't know what you mean with

"extend" or interact with the program

so I can't answer your question. Can you give an example?

There is an open source interactive C# shell in mono: http://www.mono-project.com/CsharpRepl

When you like python, .Net and language extension, you will probably like Boo over Iron python. Boo comes with an open source interactive shell too.

I disagree with

"you don’t want to design and implement a whole new language for your application"

It's not as hard as it used to be to create a simple DSL. It won't take you days to implement, just hours. It might be an interesting option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top