The Script .NET

[Back]


  • Example 1. Bubble Sort
  • Example 2. Fibonnachi and Factorial
  • Example 3. Greatest Common Divisor
  • Example 4. Quick Sort
  • Example 5. RSS Reader
  • Example 6. Meta Programming Features
  • Example 7. Drawing on Windows Forms

Example. Bubble Sort:
         
         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);
        

Example. Fibonnachi and Factorial:
          function fib(n){
           if (n==1) return 1;
           else if (n==2) return 2;
           else return fib(n-1)+fib(n-2);
          }

          function fac(n){
           if (n==1) return 1;
           else return n*fac(n-1);
          }
          MessageBox.Show('Rez=' + (fac(5) + fib(3)));
        

Example. Greatest Common Divisor:
          function GCD(a,b){
           if (a>b) return GCD(a-b,b);
           else
            if (b>a) return GCD(a,b-a);
           else
             return a;
          }

          function GCD_fast(a,b){

           while ( a!=b )
           {
             if (a>b) a = a-b;
             else    
              if (b>a) b = b-a;
           }          
           return a;
          }

          MessageBox.Show('GCD='+GCD_fast(12,4));
        

Example. Quick Sort:
         function swap(array, a, b)
         {
           tmp=array[a];
           array[a]=array[b];
           array[b]=tmp;
         }
 
         function partition(array, begin, end, pivot)
         {
           piv=array[pivot];
           swap(array, pivot, end-1);	
           store=begin;
           for(ix=begin; ix < end-1; ix++) {
            if(array[ix]<=piv) {
              swap(array, store, ix);
              store++;
             }
           }
           swap(array, end-1, store);
           return store;
         }

         function qsort(array, begin, end)
         {
           if(end-1>begin) {
             pivot=begin+(end-begin) / 2;
             pivot=partition(array, begin, end, pivot);
             qsort(array, begin, pivot);
             qsort(array, pivot+1, end);
           }
         }

         a = [1,2,10,0,12,34,5,3,3,4,1,23,4];
         qsort(a, 0, a.Length);
         s='';
         for (i=0; i < a.Length; i++)
           s = s+' '+a[i];
         MessageBox.Show(s);
        


Example. RSS Reader:
  a = new XmlDocument();
  a.Load('http://www.codeplex.com/scriptdotnet/Project/ProjectRss.aspx');

  MessageBox.Show('Now:'+DateTime.Now.ToString());
  MessageBox.Show('Tomorrow:'+DateTime.Now.AddDays(1).ToString());
  MessageBox.Show('CodePlex Script.NET RSS::');

  foreach (n in a.SelectNodes('/rss/channel/item/title'))
     MessageBox.Show(n.InnerText);

        


Example. Meta Programming Features:
  
         //the same as simply
         // rez = <[ x=1; MessageBox.Show(x); ]>;
         // <[ program ]> - AST for program
         rez = eval('<[ x=1; MessageBox.Show(x); ]>');

         //Execute program in local context         
         rez.Execute(Context);
        
         //Execute in new Context, desktop version only
         rez.Execute( new ScriptContext() );
        


Example. Drawing on Windows Forms:

f =  new Form();
f.Width = 320;
f.Height = 240;
f.Text =  'Script Demo';
f.Show();


g = Graphics.FromHwnd(f.Handle);
for (i = 0; i<10; i++)
g.DrawRectangle(Pens.Red,
                new Rectangle(10+i*10 , 10+i*10, 
                              290-i*20, 180-i*20 ) );
        




[Send e-mail]

Last updated: 3 October 2007
Created: 16 September 2007

i++) s = s + ',' + a[i]; MessageBox.Show(s);

Example. Fibonnachi and Factorial:
          function fib(n){
           if (n==1) return 1;
           else if (n==2) return 2;
           else return fib(n-1)+fib(n-2);
          }

          function fac(n){
           if (n==1) return 1;
           else return n*fac(n-1);
          }
          MessageBox.Show('Rez=' + (fac(5) + fib(3)));
        

Example. Greatest Common Divisor:
          function GCD(a,b){
           if (a>b) return GCD(a-b,b);
           else
            if (b>a) return GCD(a,b-a);
           else
             return a;
          }

          function GCD_fast(a,b){

           while ( a!=b )
           {
             if (a>b) a = a-b;
             else    
              if (b>a) b = b-a;
           }          
           return a;
          }

          MessageBox.Show('GCD='+GCD_fast(12,4));
        

Example. Quick Sort:
         function swap(array, a, b)
         {
           tmp=array[a];
           array[a]=array[b];
           array[b]=tmp;
         }
 
         function partition(array, begin, end, pivot)
         {
           piv=array[pivot];
           swap(array, pivot, end-1);	
           store=begin;
           for(ix=begin; ix < end-1; ix++) {
            if(array[ix]<=piv) {
              swap(array, store, ix);
              store++;
             }
           }
           swap(array, end-1, store);
           return store;
         }

         function qsort(array, begin, end)
         {
           if(end-1>begin) {
             pivot=begin+(end-begin) / 2;
             pivot=partition(array, begin, end, pivot);
             qsort(array, begin, pivot);
             qsort(array, pivot+1, end);
           }
         }

         a = [1,2,10,0,12,34,5,3,3,4,1,23,4];
         qsort(a, 0, a.Length);
         s='';
         for (i=0; i < a.Length; i++)
           s = s+' '+a[i];
         MessageBox.Show(s);
        


Example. RSS Reader:
  a = new XmlDocument();
  a.Load('http://www.codeplex.com/scriptdotnet/Project/ProjectRss.aspx');

  MessageBox.Show('Now:'+DateTime.Now.ToString());
  MessageBox.Show('Tomorrow:'+DateTime.Now.AddDays(1).ToString());
  MessageBox.Show('CodePlex Script.NET RSS::');

  foreach (n in a.SelectNodes('/rss/channel/item/title'))
     MessageBox.Show(n.InnerText);

        


Example. Meta Programming Features:
  
         //the same as simply
         // rez = <[ x=1; MessageBox.Show(x); ]>;
         // <[ program ]> - AST for program
         rez = eval('<[ x=1; MessageBox.Show(x); ]>');

         //Execute program in local context         
         rez.Execute(Context);
        
         //Execute in new Context, desktop version only
         rez.Execute( new ScriptContext() );
        


Example. Drawing on Windows Forms:

f =  new Form();
f.Width = 320;
f.Height = 240;
f.Text =  'Script Demo';
f.Show();


g = Graphics.FromHwnd(f.Handle);
for (i = 0; i<10; i++)
g.DrawRectangle(Pens.Red,
                new Rectangle(10+i*10 , 10+i*10, 
                              290-i*20, 180-i*20 ) );
        




[Send e-mail]

Last updated: 3 October 2007
Created: 16 September 2007