Click here to Skip to main content
11,703,632 members (68,668 online)
Click here to Skip to main content

Script.NET a language for embedding scripting functionality into CLR Applications

, 16 Dec 2008 CPOL 140K 2.3K 189
Rate this:
Please Sign up or sign in to vote.
Scripting language for .NET Framework 2.0. Supports native .NET Types, Dynamic casting, Meta programming.

Script.NET

S_.gif

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.


scripttest.gif

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:
  • Be simple
  • Be efficient
  • Be intuitive

The idea of Script .NET was born 17 September 2007. Then:
  • First alpha was released 03 October 2007.
  • 04 October – CodeProject Article submitted.
  • 21 October – WikiPedia article.
  • 19-20 Script.NET has been announced on Microsoft community forums and Google groups.
  • 22 October, CodePlex project had been launched.
  • 17 January 2008, DLR implementation of the language started.
  • 07 February 2008, The First working prototype of DLR MyL released.
  • 22 February 2008, Script.NET grammar was implemented with Irony. You can test it here.
  • 27 March 2008, Script.NET run-time was ported on Irony platform.

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 Started

All you need to start working with Script .NET is to add reference to ScriptDotNet.dll,

Example (in C# code):

using ScriptDotNet;
Script s = Script.Compile(
"MessageBox.Show('Hello .NET! This is Script .NET');");
s.AddType("MessageBox", typeof(MessageBox));
s.Execute(); 

Types

For the sake of simplicity there are only four commonly known build-in types:

Type Name .NET Analogue Initializer
double double x = 1.23;
long long x = 3;
string string s='Hello World!'
bool bool b = true
array object[] A = [1, 1+2, 'Hello', s]

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 declaration

Boolean = True | False
Object = null
String constants must be in '' brackets, example: s='Hello';

Variables

All 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.

Arrays

The 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].

Expressions

There is usual syntax of expressions, like:

X = (y+4)*2;
Y = a[5] + 8;
Z = Math.Sqrt(256);
P = new System.Drawing.Point(3,4);
'this is string' is string

Expressions have following operators:

+, -, *, / ,%, ! , | , & , != , > , < , is

There is also special operator new for creating instances of imported types.

Statements

A 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) Statement

Example:

sum=0;
for(i=0; i<10; i++) sum = sum + a[i];

Foreach ... in ...

foreach (Identifier in Expression) Statement

The 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) Statement


Example:
while (i>0) i = i-1;

Switch

switch (expr){
case expr1: statement
...
default: statement
}

Example:
switch (i){
case 1: MessageBox.Show('Hello!');
case 2: MessageBox.Show('?');
default: MessageBox.Show('No way');
}

Break, Continue

Has usual meaning, may be used only inside the loop

Return

Used only inside function call

Functions

function (id1, id2, ... , idn) {
Statement }

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 functions

event – assign event to windows forms control
eval – evaluates value of an expression
clear – clears all variables in context

Script Context

Script 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:

  • Get the value of variable with name id within Context:
    public object Lookup(string id)
  • Add .NET object to the Script context with specified name
    public void AddObject(string name, object value)
  • Add .NET type to the script context to use it in new expressions
    public void AddType(string name, Type value)
  • Add build in object, which means you can invoke its Methods without specifying the name.
    public void AddBuildInObject (object object)
    Example:
    In C# code (look example above)
    AddBuildInObject(typeof(Math))


    In Script.NET
    a = Pow(2,3); //Will call Math.Pow(2,3);
  • Removes build-in object
    public void RemoveBuildInObject (object object)
  • Gets the static field value
    public object GetStaticValue(string FullName)
    Example:
    x = Context.GetStaticValue( 'double.NaN' );

Using .NET objects from Script.NET

using 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 Applications

All 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 Test

Script 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.

scripttest.gif

Mutantic Framework

Mutantic 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 Programming

Script .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:

  • Java Script ActiveX control via COM interface
  • Lua via API
  • Python.NET native integration

External Links

License

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

Share

About the Author

Petro Protsyk
Architect
Netherlands Netherlands
Please visit my website for more articles

I'm software developer & Ph.D. student

You may also be interested in...

Comments and Discussions

 
QuestionLatest Version Usage Pin
Andre De Beer10-Apr-14 11:17
memberAndre De Beer10-Apr-14 11:17 
AnswerRe: Latest Version Usage Pin
Petro Protsyk17-Apr-14 23:01
memberPetro Protsyk17-Apr-14 23:01 
GeneralS# Pin
AYDIN EBRAHIMI HOMAY2-May-13 2:14
memberAYDIN EBRAHIMI HOMAY2-May-13 2:14 
GeneralMy vote of 5 Pin
lazaropi27-Sep-12 6:47
memberlazaropi27-Sep-12 6:47 
GeneralEmbedded Single Quote in String Literal Pin
kevinswarner21-Sep-10 4:49
memberkevinswarner21-Sep-10 4:49 
GeneralRe: Embedded Single Quote in String Literal Pin
twinbee30-May-11 19:57
membertwinbee30-May-11 19:57 
GeneralRe: Embedded Single Quote in String Literal Pin
Extrim31-May-11 21:58
memberExtrim31-May-11 21:58 
QuestionIronyScriptDotNet Pin
TheChindu28-Apr-09 1:16
memberTheChindu28-Apr-09 1:16 
AnswerRe: IronyScriptDotNet Pin
Extrim29-Apr-09 4:48
memberExtrim29-Apr-09 4:48 
GeneralI was looking for some thing like this Pin
TheChindu27-Apr-09 23:11
memberTheChindu27-Apr-09 23:11 
GeneralIssues with Math.Abs Pin
frozenpondlife11-Feb-09 3:07
memberfrozenpondlife11-Feb-09 3:07 
GeneralRe: Issues with Math.Abs Pin
frozenpondlife11-Feb-09 3:54
memberfrozenpondlife11-Feb-09 3:54 
GeneralRe: Issues with Math.Abs Pin
frozenpondlife11-Feb-09 4:08
memberfrozenpondlife11-Feb-09 4:08 
GeneralRe: Issues with Math.Abs Pin
Extrim11-Feb-09 6:45
memberExtrim11-Feb-09 6:45 
GeneralRe: Issues with Math.Abs Pin
frozenpondlife11-Feb-09 7:52
memberfrozenpondlife11-Feb-09 7:52 
GeneralRe: Issues with Math.Abs Pin
Extrim12-Feb-09 21:31
memberExtrim12-Feb-09 21:31 
GeneralRe: Issues with Math.Abs Pin
frozenpondlife14-Feb-09 5:26
memberfrozenpondlife14-Feb-09 5:26 
GeneralRe: Issues with Math.Abs Pin
frozenpondlife23-Feb-09 4:52
memberfrozenpondlife23-Feb-09 4:52 
GeneralRe: Issues with Math.Abs Pin
Extrim23-Feb-09 20:14
memberExtrim23-Feb-09 20:14 
GeneralRe: Issues with Math.Abs Pin
frozenpondlife1-Mar-09 1:20
memberfrozenpondlife1-Mar-09 1:20 
GeneralRe: Issues with Math.Abs Pin
Extrim5-Mar-09 2:02
memberExtrim5-Mar-09 2:02 
GeneralSimplifying use for person writing the script / security Pin
frozenpondlife19-Mar-08 4:31
memberfrozenpondlife19-Mar-08 4:31 
AnswerRe: Simplifying use for person writing the script / security Pin
Extrim19-Mar-08 5:13
memberExtrim19-Mar-08 5:13 
GeneralRe: Simplifying use for person writing the script / security Pin
frozenpondlife1-May-08 7:24
memberfrozenpondlife1-May-08 7:24 
GeneralI've been playing with this all day... Pin
martin_hughes14-Mar-08 11:09
membermartin_hughes14-Mar-08 11:09 
GeneralGreat Idea Pin
merlin9818-Feb-08 3:56
membermerlin9818-Feb-08 3:56 
Generalyet another bicycle Pin
George_Saveliev14-Jan-08 9:14
memberGeorge_Saveliev14-Jan-08 9:14 
GeneralCAUTION! Pin
sam.hill14-Jan-08 6:34
membersam.hill14-Jan-08 6:34 
GeneralRe: CAUTION! Pin
Extrim14-Jan-08 8:15
memberExtrim14-Jan-08 8:15 
GeneralPerfect tool for reports and such. Pin
Gevorg14-Jan-08 4:05
memberGevorg14-Jan-08 4:05 
I have been waiting for something like this for 3 years already.
For my desktop application i used to have vbscript files that generate HTML as reports and then they i would open up an IE to show report to user.

And usually there might be tight integration with passing objects and such between application and report. So this solution is perfect for .NET applications.

It allows me to distribute/modify reports on a fly.

My site - Body Jewelry
Not my site - Piercing info

GeneralRe: Perfect tool for reports and such. Pin
Extrim14-Jan-08 4:30
memberExtrim14-Jan-08 4:30 
GeneralRe: Perfect tool for reports and such. Pin
Z.J.Liu17-Dec-08 3:20
memberZ.J.Liu17-Dec-08 3:20 
GeneralF# Pin
JohnWillemse25-Oct-07 23:41
memberJohnWillemse25-Oct-07 23:41 
GeneralRe: F# Pin
Extrim25-Oct-07 23:45
memberExtrim25-Oct-07 23:45 
GeneralRe: F# Pin
Agnius Vasiliauskas15-Apr-08 10:06
memberAgnius Vasiliauskas15-Apr-08 10:06 
GeneralRe: F# Pin
thany.org7-Feb-08 23:18
memberthany.org7-Feb-08 23:18 
GeneralDLR Pin
Michal Blazejczyk9-Oct-07 7:58
memberMichal Blazejczyk9-Oct-07 7:58 
GeneralRe: DLR Pin
reinux14-Jan-08 9:05
memberreinux14-Jan-08 9:05 
GeneralRe: DLR Pin
Extrim17-Jan-08 7:01
memberExtrim17-Jan-08 7:01 
Generalpowershell Pin
Zajda824-Oct-07 23:09
memberZajda824-Oct-07 23:09 
GeneralRe: powershell Pin
Steve Hansen5-Oct-07 0:46
memberSteve Hansen5-Oct-07 0:46 
GeneralRe: powershell Pin
Zajda825-Oct-07 1:13
memberZajda825-Oct-07 1:13 
GeneralRe: powershell Pin
taras_b7-Oct-07 13:58
membertaras_b7-Oct-07 13:58 
GeneralRe: powershell Pin
Extrim7-Oct-07 20:15
memberExtrim7-Oct-07 20:15 
GeneralRe: powershell Pin
taras_b8-Oct-07 3:48
membertaras_b8-Oct-07 3:48 
GeneralRe: powershell Pin
Mystic Taz8-Oct-07 21:44
memberMystic Taz8-Oct-07 21:44 
GeneralRe: powershell Pin
Extrim8-Oct-07 21:52
memberExtrim8-Oct-07 21:52 
GeneralRe: powershell Pin
kfoust27-Nov-07 4:00
memberkfoust27-Nov-07 4:00 
GeneralRe: powershell Pin
taras_b27-Nov-07 11:25
membertaras_b27-Nov-07 11:25 
GeneralRe: powershell Pin
Keith Vinson31-Mar-08 14:39
memberKeith Vinson31-Mar-08 14:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 17 Dec 2008
Article Copyright 2007 by Petro Protsyk
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid