public static IEnumerable <IInstruction> BuildWhile(IEnumerable <IInstruction> conditionInstructions, IEnumerable <IInstruction> bodyInstructions) { int currentCount = _count++; string labelEnd = $"WhileCodeGeneration_{currentCount}_end"; string labelStart = $"WhileCodeGeneration_{currentCount}_start"; List <IInstruction> instructions = new List <IInstruction>(); instructions.Add(Instructions.InsertLabelInstruction.WithLabel(labelStart)); instructions.AddRange(conditionInstructions); instructions.Add(IfeqInstruction.WithLabel(labelEnd)); instructions.AddRange(bodyInstructions); instructions.Add(GotoInstruction.WithLabel(labelStart)); instructions.Add(Instructions.InsertLabelInstruction.WithLabel(labelEnd)); return(instructions); }
public static IEnumerable <IInstruction> BuildIf(IEnumerable <IInstruction> conditionInstructions, IEnumerable <IInstruction> trueCondtionInstructions, IEnumerable <IInstruction> falseConditionInstructions) { int currentCount = _count++; List <IInstruction> instructions = new List <IInstruction>(); string labelEnd = $"IfCodeGeneration_{currentCount}_end"; string labelFalse = $"IfCodeGeneration_{currentCount}_false"; string labelTrue = $"IfCodeGeneration_{currentCount}_true"; instructions.AddRange(conditionInstructions); instructions.Add(IfeqInstruction.WithLabel(labelFalse)); instructions.AddRange(trueCondtionInstructions); instructions.Add(GotoInstruction.WithLabel(labelEnd)); instructions.Add(Instructions.InsertLabelInstruction.WithLabel(labelFalse)); instructions.AddRange(falseConditionInstructions); instructions.Add(Instructions.InsertLabelInstruction.WithLabel(labelEnd)); return(instructions); }
public static IEnumerable <IInstruction> BuildLogicalOr(IEnumerable <IInstruction> leftOperandInstructions, IEnumerable <IInstruction> rightOperandInstructions) { int currentCount = _count++; List <IInstruction> instructions = new List <IInstruction>(); string labelEnd = $"OrCodeGeneration_{currentCount}_end"; string labelFalse = $"OrCodeGeneration_{currentCount}_false"; string labelTrue = $"OrCodeGeneration_{currentCount}_true"; instructions.AddRange(leftOperandInstructions); instructions.Add(IfneInstruction.WithLabel(labelTrue)); instructions.AddRange(rightOperandInstructions); instructions.Add(IfeqInstruction.WithLabel(labelFalse)); instructions.Add(Instructions.InsertLabelInstruction.WithLabel(labelTrue)); instructions.Add(Iconst_1Instruction); instructions.Add(GotoInstruction.WithLabel(labelEnd)); instructions.Add(Instructions.InsertLabelInstruction.WithLabel(labelFalse)); instructions.Add(Iconst_0Instruction); instructions.Add(Instructions.InsertLabelInstruction.WithLabel(labelEnd)); return(instructions); }