This project is read-only.

1. All types of variables or expression in the Tie script is VAL
2. function(delegate), class and instance of class are expression.

Example1:  event handler
   1:  //suppose there are button "button1" and TextBox "textBox1" in WinForm   
   2:  Script script = new Script();   
   3:  script.DS = new Memory();   
   4:  script.DS.AddObject("btn", button1);   
   5:  script.DS.AddObject("txt", textBox1);   
   6:  string code= @"        
   7:     txt.Text = ""No"";        
   8:     btn.Click += delegate(sender,e)        
   9:    { 
  10:         txt.Text = ""Yes"";       
  11:    }; 
  12:   ";  
  13:    script.Execute(code);  
  14:   //textBox1.Text --> "Yes" when button1 is clicked.

Example 2: dynamic windows form

   1:  string code = @"
   2:     MyForm1 = class(text):System.Windows.Forms.Form()
   3:    {
   4:        this.Text = text;
   5:        var button = new System.Windows.Forms.Button(); 
   6:        button.Text = 'OK'; 
   7:        button.Location = new System.Drawing.Point(60, 80);
   8:        this.Controls.Add(button);
   9:   
  10:        button.Click +=delegate(sender, e)  
  11:        {
  12:             this.Text = 'Clicked';  
  13:        };
  14:    };
  15:   
  16:    form = new MyForm1('my title');
  17:    form.Show();
  18:  ";
  19:   
  20:  Script script = new Script("unknown");
  21:  script.DS =new Memory();
  22:  script.Execute(code);

Example 3: JSON

   1:  string json = @"{
   2:  ""Name"": ""Jean"",
   3:  ""Date"": DateTime(2012,3,18),
   4:  ""Color"": ""Blue"",
   5:  ""Price"": 19.99,
   6:  ""Sizes"": [""Small"",""Medium"",""Large""],
   7:  ""Weight"": ""if(a>1) Color = 'Red';""
   8:  }";
   9:  VAL val = Script.Evaluate(json);
  10:  string name = (string)val["Name"];
  11:  DateTime date = (DateTime)val["Date"];
  12:  string[] sizes = (string[])val["Sizes"].HostValue;
  13:  val["Price"] = new VAL(5.99);
  14:  Memory ds = new Memory();
  15:  ds.AddObject("json", val);
  16:  string code = @"
  17:     json.Name=""Jacket"";
  18:     json.Sizes[1]=""M"";
  19:     json.Rank=1;
  20:    if(json.Rank > 1) json.Price = 30;
  21:  ";
  22:  Script.Execute(code, ds);
  23:  string json1=val.ToJson(""); //export JSON

Example 4: JSON 

   1:  string code = @"
   2:    json.Name = ""Jean"";
   3:    json.Date = new System.DateTime(2012,3,18);
   4:    json.Color = ""Blue"";
   5:    json.Price = 19.99;
   6:    json.Sizes =  {""Small"",""Medium"",""Large""};
   7:    json.Weight"": "if(a>1) Color = 'Red';"
   8:    json.Other.Place.Country = ""USA"";
   9:  ";
  10:  Memory ds = new Memory(); 
  11:  Script.Execute(code, ds);
  12:  VAL json = ds["json"];
  13:  string name = (string)json["Name"];
  14:  string size=(string)(json["Sizes"][2]);  //size -->"Large"
  15:  string json1=json.ToJson(""); //export JSON 


Example 5: Class

   1:  string code=@"
   2:   Circle = class(x,y, radius) //class definition is expression
   3:   { 
   4:      this.x = x;                    
   5:      this.y = y;                    
   6:      this.radius = radius;                      
   7:      this.Area = function()
   8:      { 
   9:        return this.radius*this.radius*3.1415;
  10:      };                                    
  11:     
  12:      this.Circumference = function()                    
  13:      { return 2*this.radius*3.1415; };        
  14:        
  15:      this.Distance = function(x1,y1)
  16:      { 
  17:        return System.Math.Sqrt((x1-this.x)*(x1-this.x)+(y1-this.y)*(y1-this.y));
  18:      };        
  19:    };
  20:                             
  21:     var circle1 = new Circle(0,1,2);                //local variable
  22:     A = circle1.Area(); //global variable
  23:     C = circle1.Circumference();                
  24:     D = circle1.Distance(3,5);
  25:  ";
  26:  Memory ds = new Memory();  HostType.Register(typeof(Math)); //using static class System.Math
  27:  Script.Execute(code, ds); // (double)ds["A"] -- > 4*PI

Example 6: script function called by C#

   1:  Memory ds = new Memory();
   2:  ds.AddObject("d",10);
   3:  VAL sum = Script.InvokeFunction(ds, new VAL(), "function(a,b,c) { return a+b+c+d;}", new object[] { 1, 2, 3 }, null);
   4:  //(int)sum --> 16 

Example 7 : Script extension method

   1:  string code=@"
   2:   sum = function(L)
   3:   {  var sum=0;                   
   4:      foreach(l in L)                   
   5:         sum+=l;                   
   6:      return sum;               
   7:   };               
   8:   L = {1,2,3,4,5};               
   9:   a = sum(L);               
  10:   b = L.sum();               
  11:   c = {2,5,6}.sum(); 
  12:  ";
  13:  Memory ds = new Memory();
  14:  Script.Execute(code, ds); 
  15:  // (int)ds["a"] -- > 15, 
  16:  // (int)ds["b"] --> 15 
  17:  // (int)ds["c"] --> 13 

Example 8: Tie Function Implemented by C#

   1:  private static VAL functions(string func, VAL parameters, Memory DS)
   2:  {            
   3:    int size = parameters.Size;            
   4:    VAL L0 = size > 0 ? parameters[0] : null;            
   5:    VAL L1 = size > 1 ? parameters[1] : null;            
   6:    switch (func)           
   7:      {
   8:   
   9:          case "char":                    
  10:            if (size == 1)
  11:            {                        
  12:              L0.ty = VALTYPE.stringcon;                        
  13:              L0.value = System.Convert.ToChar(L0.value);                        
  14:              return L0;                    
  15:     }
  16:                  break;
  17:        case "float":                    
  18:            if (size == 1)                    
  19:            {                        
  20:              L0.ty = VALTYPE.doublecon;                        
  21:              L0.value = System.Convert.ToSingle(L0.value);                        
  22:              return L0;                    
  23:            }
  27:     return null; 
  24:           break; 
  25:  //case "xxxx":
  26:         }                        
  28:  }
  29:   
  30:  public static void Main()
  31:  {
  32:     Script.FunctionChain.Add(functions);
  33:     string code=@"
  34:        ch = char(""A"");
  35:        f = float(3.14);
  36:  ";
  37:    Memeory ds = new Memory();
  38:    Script.Execute(code,ds);
  39:    // ds["ch"].value --> 'A'
  40:  } 

Last edited Mar 2, 2012 at 1:29 AM by fjiang2, version 55

Comments

No comments yet.