|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Script.NET
New version of Script.NET was released. (13 June 2008) It is now based on Irony Compiler Toolkit and has new run-time infrastructure. Please use following link to download it: Download Latest Version.
Script .NET is a simple scripting engine which can be embedded into .NET framework applications to perform custom functionality in run-time. It works like VBA in Microsoft Office Applications. The difference is that Script .NET may use custom object model, different for each application. Script .NET is written in C#. The key principles of Script .NET are:
The idea of Script .NET was born 17 September 2007. Then:
In current version I am using C# Compiler Tools from http://cis.paisley.ac.uk/crow-ci0/ to produce Lexical analyzer and language parser. Getting StartedAll you need to start working with Script .NET is to add reference to ScriptDotNet.dll, Example (in C# code):
using ScriptDotNet;
TypesFor the sake of simplicity there are only four commonly known build-in types:
There is also implicit type object which is .NET object. All other types are derived from it. Script .NET supports type importing. Which means you can add any .NET Framework type to the Script .NET programs and work with it. Note: If you haven’t added your time Script.NET run-time will search it in libraries loaded into current application domain. If the type was found then it will be cached for further use in Script Context. Constant's declarationBoolean = True | False
Object = null String constants must be in '' brackets, example: s='Hello'; VariablesAll variables in Script .NET are global and non-typed. They are stored in the Script Context. Script Context is a special structure which is used by Script Engine to store run-time information and interact with .NET. However, function body creates local Contexts and store declared variables there. Functions have access to global variables. Examples of variables:
X, x1, name_of_var. Variables may be used in expressions, statements. The type information is computed in run-time. ArraysThe build-in arrays has a .NET type object[] and can store any values. The element of array may be accessed in a usual way: Array[index]. ExpressionsThere is usual syntax of expressions, like: X = (y+4)*2; Expressions have following operators: StatementsA program in Script .NET is a sequence of statements. There are three usual statements: sequencing (;), loop, and branching. If ... Then ... Else ...if (Expression) statement else Statement Example:
if (x>0) y = y + 1 ; else y = y – 1; if (x>0) message = 'X is positive'; For ...for (Expression1;Expression2;Expression3) StatementExample:
sum=0; for(i=0; i<10; i++) sum = sum + a[i]; Foreach ... in ...foreach (Identifier in Expression) StatementThe result of Expression calculation must implement IEnumerable. Expression evaluates only once, before loop starts. Example:
arr=[1,2,3,4,5]; sum = 0; foreach(i in arr ) sum = sum + i; While ...while (Expression) StatementExample:
while (i>0) i = i-1; Switchswitch (expr){
case expr1: statement ... default: statement } Example:
switch (i){ case 1: MessageBox.Show('Hello!'); case 2: MessageBox.Show('?'); default: MessageBox.Show('No way'); } Break, ContinueHas usual meaning, may be used only inside the loopReturnUsed only inside function callFunctionsfunction (id1, id2, ... , idn) { The function is creates local context during execution. Example:
function fac(n){ if (n==1) return 1; else return n*fac(n-1); } MessageBox.Show(fac(5).ToString()); //pointer to a function Func_pointer = fac; Func_pointer(4); //Call function using pointer Reserved functionsevent – assign event to windows forms control Script ContextScript Context is an object stores run-time information: variables and import types. Using Script Context you can add .NET objects to use in the script. There are a number of functions:
Using .NET objects from Script.NETusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using ScriptDotNet; namespace Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Script s = Script.Compile(@"form.Text = 'Hello World'; MessageBox.Show('Hi'); b = new Button() "); s.AddObject("form", this); s.AddType("Button", typeof(Button)); s.AddType("MessageBox", typeof(MessageBox)); s.AddBuildInObject(typeof(Math)); s.Execute(); } } } Script.NET in .NET ApplicationsAll you need to start working with Script .NET is to add reference to ScriptDotNet.dll, using ScriptDotNet; ... Script s = Script.Compile( "MessageBox.Show('Hello .NET! This is Script .NET');"); s.AddType("MessageBox", typeof(MessageBox)); s.Execute(); Script TestScript Test is the application for testing Script.NET programs. You can use it to execute any scripting program and to observe results in Script Context. Mutantic FrameworkMutantic Framework introduces a special kind of "meta" objects for working with objects of any type. Definition: Mutant is a special object which could have all properties (fields, methods, etc), and may be converted to any type (or assigned to object of any type). The semantics of such conversion (or assignment) is pragmatically conditional, i.e. depends on user needs. Example. Creation and Usage of MObject: // Create Data Mutant Object a = [ Text -> 'Hello from Mutant' ]; // Set Additional Fields a.Top = 0; a.Left = 0; // Set corresponding fields of Windows Form object // (Mutantic Assignment) form := a; Meta ProgrammingScript .NET has a special quotation operator: <[ program ]> which returns AST of a given program. The AST of the current program may be accessed with prog object. Here is an example: Modification of current script //Create an AST for MessageBox.Show('Hello'); program ast = <[ MessageBox.Show('Hello'); ]>; //Add this AST at the and of the current program prog.AppendAst(ast); MessageBox.Show(prog.Code()); The <[ ... ]> operator and prog object allows Script.NET to generate new scripts or modify existing script at the run-time. Alternative Solutions:
External Links
|
||||||||||||||||||||||||||||||||||||||||