private static void MakeLoopEnd(TokenizerOutput program, TLine line, Stack <LoopData> loopStack) { if (loopStack.Count < 1) { throw new TokenizerException(line, "Misplaced NEXT"); } LoopData data = loopStack.Pop(); var next = (KwNext)line.Statement; if (data.LoopVariable.Name != next.Variable.Name) { throw new TokenizerException(line, "NEXT Error : Unknown variable " + next.Variable.Name); } // step 1: change the variable var add = new NumBinaryOperator(data.LoopVariable, data.StepVariable, "+"); var assign = new NumBinaryOperator(data.LoopVariable, add, "="); program.Program.Add(line.Clone(assign)); // step 2: goto to the begining var gt = new KwrGoto(data.StartLine, true); program.Program.Add(line.Clone(gt)); // change the top jump condition ((KwrJumpIfTrue)program.Program[data.StartLine].Statement).JumpPos = program.Program.Count; }
private static void MakeBranch(TokenizerOutput program, TLine line, Stack <LoopData> loopStack) { var kwif = (KwIf)line.Statement; var newIf = new KwrJumpIfNotTrue(kwif.Condition, -1); program.Program.Add(line.Clone(newIf)); // placeholder foreach (Statement s in kwif.Statements) { TokenizeStatement(program, line.Clone(s), loopStack); } if (kwif.ElseStatements == null) { newIf.JumpPos = program.Program.Count; } else { var gt = new KwrGoto(-1, true); program.Program.Add(line.Clone(gt)); newIf.JumpPos = program.Program.Count; foreach (Statement s in kwif.ElseStatements) { TokenizeStatement(program, line.Clone(s), loopStack); } gt.JumpPos = program.Program.Count; } }