views:

142

answers:

2

I'm writing a small visualization tool in wpf, the idea is that average users can create interesting visualizations without being programming wizards.

I have a controller class that has methods like StartPath(double x, double y) and LineTo(x,y) CurveTo(...) etc.

The idea is that a user can type these commands into a textbox and have it draw the result on a canvas.

StartPath(0,0);
LineTo(30,50);
LineTo(50,40);
EndPath();

One Idea I had was to use a .cs template that has all the methods implemented, and has an additional Run() command with a replacement token inside. I load the template as a string, insert the user commands into the Run() method, use the new .net 4.0 compilation service to create an assembly on the fly, then load it and invoke its Run() method and access the exposed Path to draw it on a canvas.

Another one would be to actually just parse the textbox, error check it and call the appropriate methods.

Are there any other methods, especially with the new dynamic keyword?

+2  A: 

You don't need to use anything new from .NET 4.0. The ability to compile C# code in the framework has been present for ages. In fact, my Snippy tool does pretty much exactly what you say - it's a template that user code goes in. You're welcome to base your tool on mine, should you wish to. You can download the code from the C# in Depth site.

Aside from anything else, that way you won't require your users to have .NET 4.0.

As for dynamic - it doesn't really help in this case, unless you fancy letting your users write code in IronPython/IronRuby. C# still doesn't have a sort of "eval" call letting you just execute an arbitrary string.

Jon Skeet
I will have a look at it, thanks a lot.Thanks for clearing that up, I'm not up to date with the latest c# spec.
kitsune
Works nicely: http://dl.getdropbox.com/u/133877/test.jpgThanks again :)
kitsune
+1  A: 

I think you are better to try to define a LL1 language and generate a parser and a scanner and build your own interpreter.

Coco/R is a very stable and well known tool for this kind of job. Check this out is should not be difficult for what you have in mind:

http://www.scifac.ru.ac.za/coco/cshcoco.htm

If you want to use the new dynamic you will still have problem parsing the input command text. You could use dynamic to build your interpreter on top of the parser.

Hope this helps

Claudiu
Why would it be better to go to all the trouble of designing and implementing your own language which users would then have to learn, rather than using one which is already well-designed, robustly implemented and well-known? If C# happens not to be a good choice, it's easy enough to use Boo, IronPython, IronRuby etc...
Jon Skeet
if you want to have your own mini instruction set like goto(x,y) lineTo(x,y) you can not use an already existing language. The use has only to learn your business specific mini language to be able to issue commands. If you follow up the Coco/R link you can see that is easy to define the LL1 grammar. When a piece of input is parsed (at runtime) the generated parser will automatically call a piece of .Net code passing you any additional info that you may need. Let me know if you need a help to put something together.
Claudiu