Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
How to create a my own script engine using c#,
i have Syntax highlighting textbox written in C# for that i need script exective
please help me
Posted

This is quite possible.

You can use CodeDOM to compile C# code on temporary basis using the compiler always bundled with all versions of re-distributable .NET framework.

Please see some main ideas in my past solutions. You can find skeleton descriptions of all key techniques:
Create WPF Application that uses Reloadable Plugins...[^],
Dynamically Load User Controls[^],
code generating using CodeDom[^],
AppDomain refuses to load an assembly[^],
Create WPF Application that uses Reloadable Plugins...[^].

Some of the aspects I considered in the above posts are may be to complex for your purpose. Please don't be afraid; you case could be more simple. Please read and analyze the techniques against your use cases.

Fill free the ask your follow-up question (please, after you at least look through those posts) — the problem is not too easy to understand but perhaps is not the hardest in design and implementation.

—SA
 
Share this answer
 
Comments
dubbakakishore 5-Aug-11 5:34am    
thank u for ur responds
but i need like this
see the simple script
// test file

int x;
int i,j,k;
int ii[100],ij, ik;
float f, g;
float ff[10];
char s [ 100 ] , b, #9; #9; c;
char s2[100];
// ---------------------------------------------------------
func int test{};
int y;
print "at top of test\n";
s[0]='a';
s[1]=0;
print "s = ", s, " s[0] = ", s[0], "\n";
s="I am the cat " + "Hi World\n";
s2=s;
print "s = ", s, " s2 = ", s2, " newline\n";
print "Hi world\n";
print "Hi world tab\t cr\r world\n";
x=43;
y=5;
ii[34]=x + y;
ii[x+y]=5;
print "ii[34] = ", ii[34], "\n";
print ">x +10 = < ", x + 10, " y="y, "ii[2]=", ii[2], " ii[34] =", ii[34], "\n";
print "(4-2)*5 = ", (4-2) *5, "\n";
b='a';
print "b = ", b, "\n";
x=5;
y=10;
print "x=", x, " y=",y,"\n";
if 1;
print "before if break\n";
break;
print "after if break\n";
end
while x > 0;
x=x-1;
print "x=", x, "\n";
// continue;
if x > 2;
print " x > 2\n";
else
print " x is not > 2\n";
end
print "before the break\n";
break;
print "past the break\n";
end
print "at the end, past the while\n";
end

// ----------------------------------------------------
func int foo1{};
set 5;
end
// ----------------------------------------------------
func int foo2{int x, int y};
set x*y;
end
// ----------------------------------------------------
func int main{};
// locals
int y, z;
float m;
print "top of main\n";
print "call test next\n";
test{};
m=(1.0 + 0.5) * 20.0;
print "m=",m,"\n";
x=foo1{};
y=10;
z= (10 + y)*x;
print "z = ", z, "\n";
print "x=", x, " y=",y,"\n";
while x > 0;
x=x-1;
if x > 2;
print " x > 2\n";
else
print " x is not > 2\n";
end
print "x=", x, "\n";
end
print "at the end, past the if/while\n";
x=5;
y=6;
x=foo2{x,y};
print "x of foo2=", x, "\n";
end
Sergey Alexandrovich Kryukov 5-Aug-11 14:55pm    
What do you mean "but"? No ifs no buts -- the approach I described will work. I actually developed a CodeCOM calculator -- it works. Do you have any particular doubt? -- I'll try to explain.
--SA
Manfred Rudolf Bihy 5-Aug-11 10:23am    
From OP's comment I guess it's really a language interpreter/compiler that will be need here.
Nice links BTW! 5+
Sergey Alexandrovich Kryukov 5-Aug-11 14:57pm    
Thank you, Manfred.
Of course, what I described totally covers this task. I don't know why OP fails to see it -- I explained nearly all of it in my referenced solutions (see also my comment above).
--SA
BillWoodruff 8-Aug-11 21:01pm    
Hi SA, You just might be interested in the 'DataSet.Compute' facility in .NET I came across recently in the course of answering another question see 'edit #3' here: http://www.codeproject.com/Answers/237284/Csharp-Matlab-type-command-window. The place where I found this technique was:http://stackoverflow.com/questions/174664/operators-as-strings/175262#175262

In this case, I suspect, that what you can do with DataSet.Compute will not quite meet the OP's needs, but the scope of what can be done with it I think is very suitable for certain strategies.

best, Bill
... edit #3 ...

Turns out their may be a simpler way, taking advantage of something already in .NET, please see:[^].

... end edit #3 ...

... edit #2 ...

In this case, your example 'script' code is close enough to C#, that the 'cost' of creating a mini-parser that would turn it into 'legal' C# would not be that high, in my opinion. But, then, you have the issue of whether your intent is to have a finite/static tool that reliably handles a small sub-set of C#/.NET FrameWork functionality, or whether you intend for this specialty script language to be extensible, to allow defining and using new constructs built-up of what you provide the end-user.

You can see in your responses here that some folks are assuming that by 'script' you mean an interpreter, like BASIC, LOGO, PostScript, and, other responses, like mine, have assumed you mean a compiled .exe that you feed a file, and get back results, or cause side-effects, like printing.

There is a long 'distaff' tradition in programming of creating mini-languages within languages, whose most modern recent form is the buzzword "domain specific languages." From Forth to Logo, etc. Maybe we can even view the languages that .NET supports as being 'faces' on the underlying CIL/JIT, FrameWork, engines.

Nothing wrong if your intent is to build your own flavour language, interpreted, or compiled, specific to an industry, a target audience (as LOGO, a 'friendly-face' on LISP with Turtle Graphics, was for education).

If you clarify your intent, and your ambitions, further, I think you'll get even clearer responses.

... end edit #2 ...

I'd like to point out that the example of your script shown here is your own creation, not C#. By the way, what language puts a semi-colon after the 'if' statement ?

So, if you want to use the facilities in C# required to compile an assembly, and then execute methods in it:
C#
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
You are going to have to first write a parser to translate your script language into valid C#.

When you get that done, you can find numerous examples of how to compile and execute the C# code such as:[^]. There are also examples here on CP of how to execute "snippets" of text in C#.

... edit #1 ...

Also see MS's "How to programmatically compile code using C# compiler," a detailed walk-through on using the CSharpCodeProvider[^].

... end edit #1 ...

Another strategy you might consider is to make your script language VB, and look into using the VB library in C#, specifically its 'Eval function.

Time for some homework :)

good luck, Bill
 
Share this answer
 
v4
Comments
Manfred Rudolf Bihy 5-Aug-11 10:23am    
Good answer! 5+
Sergey Alexandrovich Kryukov 5-Aug-11 14:59pm    
Good point, my 5.
Why creating a language? The question itself makes sense -- C# script with interpreter/compiler. I've fully implemented such thing and explained in detail in my answer -- please see.
--SA
BillWoodruff 5-Aug-11 17:37pm    
@SAK, thanks, reading your response, and the other responses here, have caused me to add a second edit to my post. This discussion makes me think about my experience in the seventies with Forth 'devotees' (yes, I am that old), which was both computer language and 'religion' for those used it. best, Bill
Sergey Alexandrovich Kryukov 5-Aug-11 20:13pm    
Bill, I've noticed your comment only by luck. I would get e-mail notification only if you replied to my comment, not to your post (wrong level). Interesting post. I'm familiar with Forth, too, just a bit.
You see, .NET CodeDOM and application domains is a special phenomenon by itself. I went so far to create a conceptually different Computer Algebra System, where you can write symbolic code using C# (or any other non-trivial .NET language), so the input symbolic language can be the same language used to create the system itself (my last implementation was C#). One of the applications of the system is symbolic calculator: you type C# (or other language) in edit box, build and run it, and the symbolic result is displayed (as both symbolic and numeric ways). Effectively, this is pretty much like having "symbolic calculation interpreter). Not having to invent and process a special language is a very valuable feature and pretty fresh approach. Of course, for traditional numeric interpreter this is not a problem at all.

Thank you.
--SA
You can play with compiler generators. It's quite complex, but powerful. One written in C# is COCO/R, but there are others out there.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 5-Aug-11 10:24am    
Or maybe even ANTLR. 5+
Sergey Alexandrovich Kryukov 5-Aug-11 15:00pm    
I see no sense. Anyone could use CodeDOM to invoke available compilers which are bundled with .NET Framework for free, part of the re-distributable. Please see my solution.
--SA
Catalin Serafimescu 5-Aug-11 15:10pm    
CodeDOM is only for .NET-based languages (C# and VB, I think). And it's slow (search google).
For anything custom you need a parser and an interpreter.
Not necessarily useful for this question, but it's good to know available options. BTW, to create a custom language is not trivial, but it can be done, if the project worth it.
So you want to build an interpreter.
Basically you have two options:
  1. The hard-path: hand craft your parser using the lanbguage of your choice. It requires good programming skills (you should master recursion, at least) as well as a good understanding of parsing techniques.
  2. The not-such-hard-but-still-hard-path: use a compiler generation tool, one of the best is, in my opinion, ANTLR[^], albeit Java is its natural target language, you may use it with C# too (see Code Generation Targets[^].
 
Share this answer
 
Comments
Manfred Rudolf Bihy 5-Aug-11 10:25am    
Good answer! 5+
I love ANTLR BTW :)
Sergey Alexandrovich Kryukov 5-Aug-11 15:01pm    
I see no sense. Anyone could use CodeDOM to invoke available compilers which are bundled with .NET Framework for free, part of the re-distributable.
Please see my solution.
--SA
CPallini 6-Aug-11 3:10am    
CodeDOM is not 'developer friendly' if I remember well (I have to admit I have no experience of its use), even on CodePlex there are projects using alternative approaches. ANTLR and ANTLRWorks are GREAT tools, and, yes, 'developer friendly', definitely.
However, I could change my mind: please write the needed article: "CodeDOM for The Perfect Idiot Carlo" and I'll give it a try. :-)
What do you mean by a script engine? What does it do? Where does it get its input from? Where does it send its output to?
 
Share this answer
 
Comments
dubbakakishore 5-Aug-11 5:31am    
i will write my own script with some own difined function like
SET_PTF
for this function i will write my own code
eg: i will send a command thiough serial port
input is from a file or a string
output must be a bytecode or running the application
dubbakakishore 5-Aug-11 5:39am    
i need to execute this fllowing script sir


// test file

int x;
int i,j,k;
int ii[100],ij, ik;
float f, g;
float ff[10];
char s [ 100 ] , b, #9; #9; c;
char s2[100];
// ---------------------------------------------------------
func int test{};
int y;
print "at top of test\n";
s[0]='a';
s[1]=0;
print "s = ", s, " s[0] = ", s[0], "\n";
s="I am the cat " + "Hi World\n";
s2=s;
print "s = ", s, " s2 = ", s2, " newline\n";
print "Hi world\n";
print "Hi world tab\t cr\r world\n";
x=43;
y=5;
ii[34]=x + y;
ii[x+y]=5;
print "ii[34] = ", ii[34], "\n";
print ">x +10 = < ", x + 10, " y="y, "ii[2]=", ii[2], " ii[34] =", ii[34], "\n";
print "(4-2)*5 = ", (4-2) *5, "\n";
b='a';
print "b = ", b, "\n";
x=5;
y=10;
print "x=", x, " y=",y,"\n";
if 1;
print "before if break\n";
break;
print "after if break\n";
end
while x > 0;
x=x-1;
print "x=", x, "\n";
// continue;
if x > 2;
print " x > 2\n";
else
print " x is not > 2\n";
end
print "before the break\n";
break;
print "past the break\n";
end
print "at the end, past the while\n";
end

// ----------------------------------------------------
func int foo1{};
set 5;
end
// ----------------------------------------------------
func int foo2{int x, int y};
set x*y;
end
// ----------------------------------------------------
func int main{};
// locals
int y, z;
float m;
print "top of main\n";
print "call test next\n";
test{};
m=(1.0 + 0.5) * 20.0;
print "m=",m,"\n";
x=foo1{};
y=10;
z= (10 + y)*x;
print "z = ", z, "\n";
print "x=", x, " y=",y,"\n";
while x > 0;
x=x-1;
if x > 2;
print " x > 2\n";
else
print " x is not > 2\n";
end
print "x=", x, "\n";
end
print "at the end, past the if/while\n";
x=5;
y=6;
x=foo2{x,y};
print "x of foo2=", x, "\n";
end

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900