Archive for March, 2008

Implementing Verified Collections using Irony Script.NET

Friday, March 28th, 2008

This entry will start a series of posts about Script.NET language features.

The first feature I would like to introduce is Contracts and Mutanic objects. I will start with an example. It will implement a verified collection. The collection will satisfy following requirement: it should contain no more than 10 items.

In the example I will use a Stack of integers. (Stack<int> in C#):

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

I will override two functions of stack: Push and Pop. For this purpose I will use DataMutant. DataMutant is a class that is able to decorate any existing .NET class. Because Script.NET is dynamically typed language there is no need to care about function signatures. Moreover, Script.NET uses Late binding for searching object’s functions.

To create a DataMutant object I will use special language construct:

mObject=[Push->Push,PopCheck->Pop];

It will create an object with two fields Push and PopCheck. Because, these fields are function pointers (they implement IInvokable interface) it is possible to invoke them as well: mObject.Push(20).

One more step is to decorate original stack: mObject.Mutate(stack).

One more thing that should be mentioned is function’s contracts. They are represented by three conditions: pre-, post- and invariant. Their semantics is very simple: pre condition should be satisfied before function body is being executed. post conditions should be satisfied after function body execution. invariant condition should preserve the value between function execution.

All above said is summarized on the following code snippet. Further instructions are at the bottom of the post.

Verified Collections
Code for this example
To run an example proceed the following steps:

  • 1. Open Code for this example and copy it
  • 2. Open Script.NET
  • 3. Paste the Code
  • 4. Press “Evaluate”
  • 5. Change the number 5 in for statement to 15
  • 6. Run example once again
  • 7. See the precondition exception

New features of Script.NET

Friday, March 28th, 2008

The upcoming release of Script.NET language will bring to the language a set of new features.

Among them are:

  • *using statement
    Here is example:
    using(Console)
    {
    using(Math)
    {
    //Call Console.WriteLine(Math.Pow(2,10).ToString());
    WriteLine(Pow(2,10).ToString());
    }
    }
    ;
  • *new syntax for generic types, like list = new List<|string|>(); for simplifying grammar;
  • *exception handling;
    try
    {
    throw new Exception(’Exception test’);
    }
    catch(e)
    {
    msg = e.Message;
    }
    finally
    {
    Console.WriteLine (’Message:’+msg);
    }
  • *new metafeatures;
  • *run-time extensibility with custom Contexts, Scopes, Functions.
  • *debugger. now you will be able to debug script.net programs:
    Download Debugger Preview.

Script.NET on Irony Platform

Tuesday, March 25th, 2008

Good news for Script.NET users. I have started reworking the language run-time because of the large feedback about first release. Hopefuly the new version will be more stable and efficient. The grammar of the language was also changed. The changes are minor and will not impact the majority of the exisiting code.

The preview of the future release was published:DownloadPreview

Info:

Irony is a new-generation .NET compiler construction kit. It utilizes the full potential of c# 2.0 and .NET Framework to implement a completely new and streamlined technology of compiler construction. Unlike most existing yacc/lex-style solutions Irony does not employ any scanner or parser code generation from grammar specifications written in a specialized meta-language. In Irony the target language grammar is coded directly in c# using operator overloading to express grammar constructs. Irony’s scanner and parser modules use the grammar encoded as c# class to control the parsing process

Script.NET is a language that provides scripting functionality in .NET applications, embedding custom functionality into CLR applications. It is very simple, yet powerful enough to satisfy usual needs for scripting your application.

Tricky excercises in C#

Thursday, March 20th, 2008

Created On: 13 November 2007 @ 9:23

Task1

Modify this class without changing method Main, so the output will be:

Before Method Main
Don’t change this method
After Method Main

t1.gif

 

Solution

Task2

What output will be generated by the following code snippet:

t3.gif

Answer:
Derived
Derived

Task 3

How to change the class A, so the example will work?

t4.gif

Solution

Task 4

A set of Type Conversion in C#:

t6.gif

Task 5

What will be the outputed?

task5.jpg

Answer: Will not compile.

Task 6

You have assigned junior developer in your team to implement an adapter class for Windows Forms Button which will implement a certain IClickable iterface.It should participate in the following usage scenario:

Usage Scenario

He proposed the following solution:

Task 6

What is wrong with it? See, Solution.

C# Tasks for Interview and Fun

Thursday, March 20th, 2008

Created On: 13 November 2007 @ 9:23

Here I would like to present a set of tricky tasks in C#.

Task1

Modify this class without changing method Main, so the output will be:

Before Method Main
Don’t change this method
After Method Main

t1.gif

 

Solution

Task2

What output will be generated by the following code snippet:

t3.gif

Answer:
Derived
Derived

Task 3

How to change the class A, so the example will work?

t4.gif

Solution

Task 4

A set of Type Conversion in C#:

t6.gif

Task 5

What will be the outputed?

task5.jpg

Answer: Will not compile.

Task 6

You have assigned junior developer in your team to implement an adapter class for Windows Forms Button which will implement a certain IClickable iterface.It should participate in the following usage scenario:

Usage Scenario

He proposed the following solution:

Task 6

What is wrong with it? See, Solution.