public override void VisitIfStatement (IfStatement node) { WriteKeyword ("if"); WriteSpace (); WriteBetweenParenthesis (node.Condition); WriteLine (); Visit (node.Then); if (node.Else == null) return; WriteKeyword ("else"); WriteLine (); Visit (node.Else); }
static bool IsEmptyThenCondition (IfStatement conditional) { return conditional.Then.Statements.Count == 0 && conditional.Else != null && conditional.Else.Statements.Count > 0; }
void SwapConditionBranches (IfStatement conditional) { conditional.Then = conditional.Else; conditional.Else = null; Push (conditional.Condition); Negate (); conditional.Condition = Pop (); }
static bool IsElseUnecessary (IfStatement condition) { var then = condition.Then; if (then.Statements.Count == 0) return false; var last = then.Statements [then.Statements.Count - 1]; return last is BreakStatement || last is ContinueStatement || last is ReturnStatement; }
void ProcessConditionBranches (ConditionData data, IfStatement conditional) { MoveStatementsToBlock (data.Then, conditional.Then); if (data.Else != null && !IsElseUnecessary (conditional)) { conditional.Else = new BlockStatement (); MoveStatementsToBlock (data.Else, conditional.Else); if (IsEmptyThenCondition (conditional)) SwapConditionBranches (conditional); } }
void AddCompleteCondition (Instruction instruction) { Negate (); var condition = new IfStatement (Pop (), new BlockStatement (), null); ProcessConditionBranches (annotations.GetData<ConditionData> (instruction), condition); Add (condition); }
void AddBasicCondition (Instruction instruction, Instruction target) { var then = new BlockStatement (); var condition = new IfStatement (Pop (), then, null); then.Statements.Add (new GotoStatement (annotations.GetData<string> (instruction))); Add (condition); }
public virtual void VisitIfStatement (IfStatement node) { Visit (node.Condition); Visit (node.Then); Visit (node.Else); }