Jump to content

Script.NET: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m remove portal link
m change source to syntaxhighlight
Line 26: Line 26:


Here is an example:
Here is an example:
<source lang="javascript">
<syntaxhighlight lang="javascript">
//Create an AST for MessageBox.Show('Hello'); program
//Create an AST for MessageBox.Show('Hello'); program
ast = <[ MessageBox.Show('Hello'); ]>;
ast = <[ MessageBox.Show('Hello'); ]>;
//Add this AST at the end of the current program
//Add this AST at the end of the current program
prog.AppendAst(ast);
prog.AppendAst(ast);
</syntaxhighlight>
</source>


The '''<[''' ... ''']>''' operator and '''prog''' objects allow Script.NET to generate new scripts or modify existing scripts at runtime.
The '''<[''' ... ''']>''' operator and '''prog''' objects allow Script.NET to generate new scripts or modify existing scripts at runtime.
Line 44: Line 44:


''Example''. Creation and Usage of MObject:
''Example''. Creation and Usage of MObject:
<source lang="javascript">
<syntaxhighlight lang="javascript">
// Create Data Mutant Object
// Create Data Mutant Object
mobj = [ Text -> 'Hello from Mutant' ];
mobj = [ Text -> 'Hello from Mutant' ];
Line 53: Line 53:
// (Mutantic Assignment)
// (Mutantic Assignment)
form := mobj;
form := mobj;
</syntaxhighlight>
</source>


==Examples==
==Examples==
===Hello world===
===Hello world===
<source lang="javascript">
<syntaxhighlight lang="javascript">
MessageBox.Show('Hello World!');
MessageBox.Show('Hello World!');
</syntaxhighlight>
</source>


===Bubble sort with out output function===
===Bubble sort with out output function===


<source lang="javascript">
<syntaxhighlight lang="javascript">


a=[17, 0, 5, 3,1, 2, 55];
a=[17, 0, 5, 3,1, 2, 55];
Line 80: Line 80:


MessageBox.Show(s);
MessageBox.Show(s);
</syntaxhighlight>
</source>


===RSS Reader===
===RSS Reader===
<source lang="javascript">
<syntaxhighlight lang="javascript">
a = new XmlDocument();
a = new XmlDocument();
a.Load('http://www.codeplex.com/scriptdotnet/Project/ProjectRss.aspx');
a.Load('http://www.codeplex.com/scriptdotnet/Project/ProjectRss.aspx');
Line 90: Line 90:
foreach (n in a.SelectNodes('/rss/channel/item/title'))
foreach (n in a.SelectNodes('/rss/channel/item/title'))
MessageBox.Show(n.InnerText);
MessageBox.Show(n.InnerText);
</syntaxhighlight>
</source>


===Stack===
===Stack===
Stack limited to 20 elements using [[Design by contract]] feature
Stack limited to 20 elements using [[Design by contract]] feature
<source lang="javascript">
<syntaxhighlight lang="javascript">
function Push(item)
function Push(item)
[
[
Line 130: Line 130:


Console.WriteLine((string)mObject.PopCheck());
Console.WriteLine((string)mObject.PopCheck());
</syntaxhighlight>
</source>


==See also==
==See also==

Revision as of 15:59, 31 January 2021

Script.NET
Paradigmimperative, meta, scripting
DeveloperMicrosoft
Typing disciplinedynamic
Platform.NET Framework
LicenseFree
Websitewww.protsyk.com/scriptdotnet
Influenced by
JavaScript

Script.NET or S# is a metaprogramming language that provides scripting functionality in Microsoft .NET applications, allowing runtime execution of custom functionality, similar to VBA in Microsoft Office applications. The syntax of Script.NET is similar to JavaScript. It is designed to be simple and efficient scripting language allowing to customize .NET applications. The language has a true runtime interpreter, and it is executed without generating additional in-memory assemblies.

Script.NET is an open-source project.

Metaprogramming features

Script.NET has a special quotation operator <[ program ]> which returns the AST of a given program. Additionally, the AST of the current program may be accessed with the prog object.

Here is an example:

//Create an AST for MessageBox.Show('Hello'); program
ast = <[ MessageBox.Show('Hello'); ]>;
//Add this AST at the end of the current program
prog.AppendAst(ast);

The <[ ... ]> operator and prog objects allow Script.NET to generate new scripts or modify existing scripts at runtime.

Generalized objects

Script.NET includes so-called "Mutantic Framework" which introduces a special kind of "meta" objects for controlling objects of any type. It is composed of a set of classes, on top of which is the "DataMutant" class. It implements several principles of mutant object:

Definition

A 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) are pragmatically conditional.

There is a special operator := called Mutantic or Generalized assignment. Its purpose is to assign values of DataMutant fields to corresponding fields of an object of any type.

Example. Creation and Usage of MObject:

 // Create Data Mutant Object
 mobj = [ Text -> 'Hello from Mutant' ];
 // Set Additional Fields
 mobj{{Not a typo|.}}Top = 0;
 mobj{{Not a typo|.}}Left = 0;
 // Set corresponding fields of Windows Form object
 // (Mutantic Assignment)
 form := mobj;

Examples

Hello world

 MessageBox.Show('Hello World!');

Bubble sort with out output function

a=[17, 0, 5, 3,1, 2, 55];
for (i=0; i < a.Length; i=i+1)
 for (j=i+1; j <  a.Length; j=j+1)
   if (a[i] > a[j] )
   {
     temp = a[i]; 
     a[i] = a[j];
     a[j] = temp;
   }

s = 'Results:';
for (i=0; i < a.Length; i++)
  s = s + ',' + a[i];

MessageBox.Show(s);

RSS Reader

a = new XmlDocument();
a.Load('http://www.codeplex.com/scriptdotnet/Project/ProjectRss.aspx');
 
MessageBox.Show('CodePlex Script.NET RSS::');
foreach (n in a.SelectNodes('/rss/channel/item/title'))
  MessageBox.Show(n.InnerText);

Stack

Stack limited to 20 elements using Design by contract feature

function Push(item)
[
//Limit to 10 items
 pre(me{{Not a typo|.}}Count < 10 ); 
 post();
 invariant();
]
{
 //me is mutated object, 
 //stack in this case
 me.Push(item);
}

function Pop()
[//Check emptiness hardik
 pre(me{{Not a typo|.}}Count > 0);
 post();
 invariant();
]
{
 return me.Pop();
}

stack = new Stack<|int|>();

//Create Mutant hardik
//1. Set Functions, override stack{{Not a typo|.}}Push
mObject=[Push->Push,PopCheck->Pop];
//2. Capture object
mObject.Mutate(stack);

for (i=0; i<5; i++)
  mObject.Push(i);

Console.WriteLine((string)mObject.PopCheck());

See also

  • L Sharp - Lisp-like scripting language for .NET
  • Boo - a Python Like language for .NET platform
  • IronPython - an implementation of Python for the .NET platform, similar to Jython.
  • Nemerle - a high-level language for the .NET platform.