PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Runtime/Samples/ExpressionTree/UESamples/ConsoleApplication1/CLabel.cs

#
C# | 162 lines | 63 code | 35 blank | 64 comment | 6 complexity | 897cfeeb9fdd0f2e63c49076ef524694 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using Microsoft.Scripting.Ast;
  5
  6namespace Samples {
  7    class CLabel {
  8        //Expression.Label()
  9        static public void Label1() {
 10            //<Snippet1>
 11            // add the following directive to your file
 12            // using Microsoft.Scripting.Ast;  
 13
 14            //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
 15            //expressions with the desired Labels or loops.
 16            LabelTarget MyLabelTarget = Expression.Label();
 17
 18            //This goto jumps over a throw expression:
 19            var MyBlock = Expression.Block(
 20                Expression.Goto(MyLabelTarget),
 21                Expression.Throw(Expression.Constant(new Exception())),
 22                Expression.Label(MyLabelTarget)
 23            );
 24
 25            //No exception should be thrown.
 26            Expression.Lambda<Action>(MyBlock).Compile().Invoke();
 27            //</Snippet1>
 28
 29        }
 30
 31        //Expression.Label(LabelTarget)
 32        static public void Label2() {
 33            //<Snippet1>
 34            // add the following directive to your file
 35            // using Microsoft.Scripting.Ast;  
 36
 37            //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
 38            //expressions with the desired Labels or loops.
 39            LabelTarget MyLabelTarget = Expression.Label();
 40
 41            //This element defines a Label. A label indentifies an execution point in the program, enabling 
 42            //execution to jump to it.
 43            LabelExpression MyLabel = Expression.Label(MyLabelTarget);
 44
 45            //This goto jumps over a throw expression:
 46            var MyBlock = Expression.Block(
 47                Expression.Goto(MyLabelTarget),
 48                Expression.Throw(Expression.Constant(new Exception())),
 49                MyLabel
 50            );
 51
 52            //No exception should be thrown.
 53            Expression.Lambda<Action>(MyBlock).Compile().Invoke();
 54            //</Snippet1>
 55
 56        }
 57
 58        //Expression.Label(LabelTarget, Expression)
 59        static public void Label3() {
 60            //<Snippet1>
 61            // add the following directive to your file
 62            // using Microsoft.Scripting.Ast;  
 63
 64            //We need to create a label target to use with the label.
 65            LabelTarget MyLabelTarget = Expression.Label(typeof(int));
 66
 67            //This element defines a Label. A label indentifies an execution point in the program, enabling 
 68            //execution to jump to it. It also has a default value.
 69            LabelExpression MyLabel = Expression.Label(
 70                MyLabelTarget,
 71                Expression.Constant(5)
 72            );
 73
 74            //No exception should be thrown, 5 should be printed.
 75            Console.WriteLine(Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke());
 76            //</Snippet1>
 77
 78            //Validate sample
 79            if (Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke() != 5) throw new Exception();
 80
 81        }
 82
 83        //Expression.Label(string)
 84        static public void Label4() {
 85            //<Snippet1>
 86            // add the following directive to your file
 87            // using Microsoft.Scripting.Ast;  
 88
 89            //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
 90            //expressions with the desired Labels or loops. It can use a name for debugging purposes.
 91            LabelTarget MyLabelTarget = Expression.Label("My Label Target");
 92
 93            //This goto jumps over a throw expression:
 94            var MyBlock = Expression.Block(
 95                Expression.Goto(MyLabelTarget),
 96                Expression.Throw(Expression.Constant(new Exception())),
 97                Expression.Label(MyLabelTarget)
 98            );
 99
100            //No exception should be thrown.
101            Expression.Lambda<Action>(MyBlock).Compile().Invoke();
102            //</Snippet1>
103        }
104
105
106
107        //Expression.Label(Type)
108        static public void Label5() {
109            //<Snippet1>
110            // add the following directive to your file
111            // using Microsoft.Scripting.Ast;
112
113            //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
114            //expressions with the desired Labels or loops. It can define a type, which labels associated with this
115            //LabelTarget will have.
116            LabelTarget MyLabelTarget = Expression.Label(typeof(int));
117
118            //This element defines a Label. A label indentifies an execution point in the program, enabling 
119            //execution to jump to it. It also has a default value, which matches the labeltarget's type.
120            LabelExpression MyLabel = Expression.Label(
121                MyLabelTarget,
122                Expression.Constant(5)
123            );
124
125            //No exception should be thrown, 5 should be printed.
126            Console.WriteLine(Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke());
127            //</Snippet1>
128
129            //Validate sample
130            if (Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke() != 5) throw new Exception();
131
132        }
133
134        //Expression.Label(Type, string)
135        static public void Label6() {
136            //<Snippet1>
137            // add the following directive to your file
138            // using Microsoft.Scripting.Ast;
139
140            //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
141            //expressions with the desired Labels or loops. It can define a type, which labels associated with this
142            //LabelTarget will have.
143            //It defines a name for debugging purposes.
144            LabelTarget MyLabelTarget = Expression.Label(typeof(int), "MyLabelTarget");
145
146            //This element defines a Label. A label indentifies an execution point in the program, enabling 
147            //execution to jump to it. It also has a default value, which matches the labeltarget's type.
148            LabelExpression MyLabel = Expression.Label(
149                MyLabelTarget,
150                Expression.Constant(5)
151            );
152
153            //No exception should be thrown, 5 should be printed.
154            Console.WriteLine(Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke());
155            //</Snippet1>
156
157            //Validate sample
158            if (Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke() != 5) throw new Exception();
159
160        }
161    }
162}