Do not doubt it. When it comes to the control structure, we will first think of if. The implementation should be as follows:
public class IfThen : ControlFlow { public Expression.ExpressionNode Condition; public Block ThenClause { get { return Children.Count > 0 ? Children.First() as Block: null; } } public Block ElseClause { get { return Children.Count > 1 ? Children.Last() as Block: null; } }
The runtime processing should be as follows:
public override void Run(Context ctx) { Debug.WriteLine("if(" + Condition.ToString() + ")"); Expression.Operand.Operand condVal = Condition.Evaluate(this); Debug.WriteLine("Condition=" + condVal.GetValue(this).ToString()); if (condVal.GetValue(this).AsInt != 0) { if (ThenClause != null) { Debug.WriteLine("Then"); ThenClause.Run(this); } } else { if (ElseClause != null) { Debug.WriteLine("Else"); ElseClause.Run(this); } } }
It looks quite simple.