public static void Run(Module module) { var functions = module.GetFunctionEnumerator(); while (functions.MoveNext()) { if (functions.Current.Code != null) { Block block = functions.Current.Code; var whileLoops = from Statement st in block where st is While select st; var doWhileLoops = from Statement st in block where st is DoWhile select st; foreach (While loop in whileLoops) { Block loopBlock = loop.Block; int nPos = loopBlock.Statements.IndexOf(loop); loopBlock.Statements.Remove(loop); List <Statement> whileReplacement = new List <Statement>(); whileReplacement.Add(new Goto(loop.Label)); Label whileStartLbl = block.CreateTempLabel(); List <Statement> condStatements = new List <Statement>(); whileReplacement.Add(whileStartLbl); whileReplacement.AddRange(loop.Body); whileReplacement.Add(loop.Label); whileReplacement.AddRange(Goto.ParseConditionStatements(loop.Condition.Statements, whileStartLbl)); loopBlock.Statements.InsertRange(nPos, whileReplacement); } foreach (DoWhile loop in doWhileLoops) { Block loopBlock = loop.Block; int nPos = loopBlock.Statements.IndexOf(loop); loopBlock.Statements.Remove(loop); List <Statement> whileReplacement = new List <Statement>(); Label whileStartLbl = block.CreateTempLabel(); whileReplacement.Add(whileStartLbl); whileReplacement.AddRange(loop.Body); whileReplacement.Add(loop.Label); whileReplacement.AddRange(Goto.ParseConditionStatements(loop.Condition.Statements, whileStartLbl)); loopBlock.Statements.InsertRange(nPos, whileReplacement); } } } }
public static void Run(Module module) { var functions = module.GetFunctionEnumerator(); while (functions.MoveNext()) { if (functions.Current.Code != null) { var ifs = from Statement st in functions.Current.Code where st is If select st; foreach (If ifStatement in ifs) { Block block = ifStatement.Block; int nPos = block.Statements.IndexOf(ifStatement); block.Statements.Remove(ifStatement); var ifReplacement = new List <Statement>(); var condStatements = new List <Statement>(); Label trueSkipLabel = block.CreateTempLabel(); Declaration notDecl = functions.Current.Code.CreateTempDeclaration(new BuiltInType("int")); condStatements.AddRange(ifStatement.ConditionBlock); condStatements.Add(new Move(notDecl, ifStatement.Condition)); condStatements.Add(new WabbitC.Model.Statements.Math.Not(notDecl)); ifReplacement.AddRange(Goto.ParseConditionStatements(condStatements, trueSkipLabel)); ifReplacement.AddRange(ifStatement.TrueCase.Statements); if (ifStatement.FalseCase == null) { ifReplacement.Add(trueSkipLabel); } else { Label falseSkipLabel = block.CreateTempLabel(); ifReplacement.Add(new Goto(falseSkipLabel)); ifReplacement.Add(trueSkipLabel); ifReplacement.AddRange(ifStatement.FalseCase.Statements); ifReplacement.Add(falseSkipLabel); } block.Statements.InsertRange(nPos, ifReplacement); } } } }