/// <summary>Multi-condition If block</summary> /// <example> /// If.Block(c => c /// .Option(() => A.Equals(1), () => A.Set(2)) /// .Option(() => A.Equals(2), () => A.Set(9)) /// .Default( () => A.Set(5)) /// ); /// </example> /// <param name="options"></param> public static void Block(Func <_IfBlock, _IfBlock> block) { var ifBlock = block.Invoke(new _IfBlock()); var numOptions = ifBlock._options.Count; var optionConditions = ifBlock._options.Where(x => x is _IfBlock._Option).Cast <_IfBlock._Option>().ToList(); var optionDefault = ifBlock._options.Where(x => x is _IfBlock._Default).Cast <_IfBlock._Default>().ToList(); var hasElse = optionDefault.Any(); Label?lblEnd = null; if (numOptions > 1 || hasElse) { lblEnd = Labels.New(); } var lastCondition = optionConditions.Last(); foreach (var oc in optionConditions) { var isLast = oc == lastCondition; Branching._WriteCondition(oc.Condition, oc.Block, hasElse || !isLast ? lblEnd : null, null, oc.Invert); //don't output "GoTo EndIf" if this is the last condition } if (numOptions > 1) { if (hasElse) { optionDefault[0].Block?.Invoke(); } if (lblEnd != null) //always true in this block, suppressing nullable complaint { Context.Write(lblEnd); } } Reset(); }
public static void True(Func <Condition> condition, Action block) => Branching._WriteCondition(condition, block);
public static void False(Func <Condition> condition, Action block) => Branching._WriteCondition(condition, block, null, null, true);
public void Then(Action block) { Block = block; Branching._WriteCondition(AC, block); //return IfBlock; }