BadScript 2
Loading...
Searching...
No Matches
BadWhileExpression.cs
Go to the documentation of this file.
1using System.Text;
2
9
11
16{
20 private readonly List<BadExpression> m_Body;
21
28 public BadWhileExpression(BadExpression condition, List<BadExpression> block, BadSourcePosition position) : base(
29 false,
30 position
31 )
32 {
33 Condition = condition;
34 m_Body = block;
35 }
36
40 public IEnumerable<BadExpression> Body => m_Body;
41
45 public BadExpression Condition { get; private set; }
46
51 public void SetBody(IEnumerable<BadExpression> body)
52 {
53 m_Body.Clear();
54 m_Body.AddRange(body);
55 }
56
57
59 public override void Optimize()
60 {
62
63 for (int i = 0; i < m_Body.Count; i++)
64 {
66 }
67 }
68
70 public override IEnumerable<BadExpression> GetDescendants()
71 {
72 foreach (BadExpression expression in Condition.GetDescendantsAndSelf())
73 {
74 yield return expression;
75 }
76
77 foreach (BadExpression descendant in m_Body.SelectMany(expression => expression.GetDescendantsAndSelf()))
78 {
79 yield return descendant;
80 }
81 }
82
87 public override string ToString()
88 {
89 StringBuilder sb = new StringBuilder($"while ({Condition})");
90 sb.AppendLine();
91 sb.AppendLine("{");
92
93 foreach (BadExpression expression in m_Body)
94 {
95 sb.AppendLine($"\t{expression}");
96 }
97
98 sb.AppendLine("}");
99
100 return sb.ToString();
101 }
102
104 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
105 {
106 BadObject cond = BadObject.Null;
107
108 foreach (BadObject o in Condition.Execute(context))
109 {
110 cond = o;
111
112 yield return o;
113 }
114
115 IBadBoolean bRet = cond.Dereference() as IBadBoolean ??
116 throw new BadRuntimeException("While Condition is not a boolean", Position);
117
118 while (bRet.Value)
119 {
120 using BadExecutionContext loopContext = new BadExecutionContext(
121 context.Scope.CreateChild(
122 "WhileLoop",
123 context.Scope,
124 null,
125 BadScopeFlags.Breakable | BadScopeFlags.Continuable
126 )
127 );
128
129 if (m_Body.Count != 0)
130 {
131 foreach (BadObject o in loopContext.Execute(m_Body))
132 {
133 yield return o;
134 }
135
136 if (loopContext.Scope.IsBreak || loopContext.Scope.ReturnValue != null)
137 {
138 break;
139 }
140 }
141
142 foreach (BadObject o in Condition.Execute(context))
143 {
144 cond = o;
145
146 yield return o;
147 }
148
149 bRet = cond.Dereference() as IBadBoolean ??
150 throw new BadRuntimeException("While Condition is not a boolean", Position);
151 }
152
153 yield return BadObject.Null;
154 }
155}
Describes a specific position inside a source file.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
Base Implementation for all Expressions used inside the Script.
IEnumerable< BadExpression > GetDescendantsAndSelf()
Returns all Descendants of the Expression and the Expression itself.
BadSourcePosition Position
The source Position of the Expression.
IEnumerable< BadObject > Execute(BadExecutionContext context)
Evaluates the Expression within the current Execution Context.
void SetBody(IEnumerable< BadExpression > body)
Sets the Body of the Loop.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
BadWhileExpression(BadExpression condition, List< BadExpression > block, BadSourcePosition position)
Constructor of the While Expression.
override string ToString()
Returns a human readable representation of the Expression.
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
readonly List< BadExpression > m_Body
The Loop Body.
The Execution Context. Every execution of a script needs a context the script is running in....
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements the Interface for Native Boolean.
Definition IBadBoolean.cs:7
Contains Shared Data Structures and Functionality.
Contains the BadScript2 Constant Folding Optimizations.
Contains the Loop Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.