PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Scripting.Ast;
  5. namespace Samples {
  6. class CLabel {
  7. //Expression.Label()
  8. static public void Label1() {
  9. //<Snippet1>
  10. // add the following directive to your file
  11. // using Microsoft.Scripting.Ast;
  12. //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
  13. //expressions with the desired Labels or loops.
  14. LabelTarget MyLabelTarget = Expression.Label();
  15. //This goto jumps over a throw expression:
  16. var MyBlock = Expression.Block(
  17. Expression.Goto(MyLabelTarget),
  18. Expression.Throw(Expression.Constant(new Exception())),
  19. Expression.Label(MyLabelTarget)
  20. );
  21. //No exception should be thrown.
  22. Expression.Lambda<Action>(MyBlock).Compile().Invoke();
  23. //</Snippet1>
  24. }
  25. //Expression.Label(LabelTarget)
  26. static public void Label2() {
  27. //<Snippet1>
  28. // add the following directive to your file
  29. // using Microsoft.Scripting.Ast;
  30. //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
  31. //expressions with the desired Labels or loops.
  32. LabelTarget MyLabelTarget = Expression.Label();
  33. //This element defines a Label. A label indentifies an execution point in the program, enabling
  34. //execution to jump to it.
  35. LabelExpression MyLabel = Expression.Label(MyLabelTarget);
  36. //This goto jumps over a throw expression:
  37. var MyBlock = Expression.Block(
  38. Expression.Goto(MyLabelTarget),
  39. Expression.Throw(Expression.Constant(new Exception())),
  40. MyLabel
  41. );
  42. //No exception should be thrown.
  43. Expression.Lambda<Action>(MyBlock).Compile().Invoke();
  44. //</Snippet1>
  45. }
  46. //Expression.Label(LabelTarget, Expression)
  47. static public void Label3() {
  48. //<Snippet1>
  49. // add the following directive to your file
  50. // using Microsoft.Scripting.Ast;
  51. //We need to create a label target to use with the label.
  52. LabelTarget MyLabelTarget = Expression.Label(typeof(int));
  53. //This element defines a Label. A label indentifies an execution point in the program, enabling
  54. //execution to jump to it. It also has a default value.
  55. LabelExpression MyLabel = Expression.Label(
  56. MyLabelTarget,
  57. Expression.Constant(5)
  58. );
  59. //No exception should be thrown, 5 should be printed.
  60. Console.WriteLine(Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke());
  61. //</Snippet1>
  62. //Validate sample
  63. if (Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke() != 5) throw new Exception();
  64. }
  65. //Expression.Label(string)
  66. static public void Label4() {
  67. //<Snippet1>
  68. // add the following directive to your file
  69. // using Microsoft.Scripting.Ast;
  70. //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
  71. //expressions with the desired Labels or loops. It can use a name for debugging purposes.
  72. LabelTarget MyLabelTarget = Expression.Label("My Label Target");
  73. //This goto jumps over a throw expression:
  74. var MyBlock = Expression.Block(
  75. Expression.Goto(MyLabelTarget),
  76. Expression.Throw(Expression.Constant(new Exception())),
  77. Expression.Label(MyLabelTarget)
  78. );
  79. //No exception should be thrown.
  80. Expression.Lambda<Action>(MyBlock).Compile().Invoke();
  81. //</Snippet1>
  82. }
  83. //Expression.Label(Type)
  84. static public void Label5() {
  85. //<Snippet1>
  86. // add the following directive to your file
  87. // using Microsoft.Scripting.Ast;
  88. //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
  89. //expressions with the desired Labels or loops. It can define a type, which labels associated with this
  90. //LabelTarget will have.
  91. LabelTarget MyLabelTarget = Expression.Label(typeof(int));
  92. //This element defines a Label. A label indentifies an execution point in the program, enabling
  93. //execution to jump to it. It also has a default value, which matches the labeltarget's type.
  94. LabelExpression MyLabel = Expression.Label(
  95. MyLabelTarget,
  96. Expression.Constant(5)
  97. );
  98. //No exception should be thrown, 5 should be printed.
  99. Console.WriteLine(Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke());
  100. //</Snippet1>
  101. //Validate sample
  102. if (Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke() != 5) throw new Exception();
  103. }
  104. //Expression.Label(Type, string)
  105. static public void Label6() {
  106. //<Snippet1>
  107. // add the following directive to your file
  108. // using Microsoft.Scripting.Ast;
  109. //This element defines a LabelTarget. The LabelTarget is used to associate Goto, Break and Continue
  110. //expressions with the desired Labels or loops. It can define a type, which labels associated with this
  111. //LabelTarget will have.
  112. //It defines a name for debugging purposes.
  113. LabelTarget MyLabelTarget = Expression.Label(typeof(int), "MyLabelTarget");
  114. //This element defines a Label. A label indentifies an execution point in the program, enabling
  115. //execution to jump to it. It also has a default value, which matches the labeltarget's type.
  116. LabelExpression MyLabel = Expression.Label(
  117. MyLabelTarget,
  118. Expression.Constant(5)
  119. );
  120. //No exception should be thrown, 5 should be printed.
  121. Console.WriteLine(Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke());
  122. //</Snippet1>
  123. //Validate sample
  124. if (Expression.Lambda<Func<int>>(MyLabel).Compile().Invoke() != 5) throw new Exception();
  125. }
  126. }
  127. }