public static Executable[] ResolveBlock(IList<Executable> code) { List<Executable> output = new List<Executable>(); for (int i = 0; i < code.Count; ++i) { Executable line = code[i]; if (line is IfRawComponent && ((IfRawComponent)line).Type == IfRawComponent.ComponentType.IF) { IfRawComponent rawIf = (IfRawComponent)line; IfStatement head = null; IfStatement tail = null; if (rawIf.Type != IfRawComponent.ComponentType.IF) { throw new ParserException(rawIf.FirstToken, "Unexpected token."); } head = new IfStatement(rawIf.FirstToken, rawIf.NullableExpression, rawIf.Body); tail = head; bool keepGoing = true; while (keepGoing) { if (i + 1 < code.Count && code[i + 1] is IfRawComponent) { ++i; IfRawComponent consecutiveIfComponent = (IfRawComponent)code[i]; if (consecutiveIfComponent.Type == IfRawComponent.ComponentType.ELIF) { IfStatement elifPiece = new IfStatement(consecutiveIfComponent.FirstToken, consecutiveIfComponent.NullableExpression, consecutiveIfComponent.Body); tail.SetFalseCode(Listify(elifPiece)); tail = elifPiece; } else if (consecutiveIfComponent.Type == IfRawComponent.ComponentType.ELSE) { tail.SetFalseCode(consecutiveIfComponent.Body); keepGoing = false; break; } else { --i; break; } } else { break; } } line = head; } output.AddRange(line.Resolve()); } return output.ToArray(); }
private void SerializeIfStatement(List<string> output, IfStatement ifStatement, string indention, bool useFirstIndention) { if (useFirstIndention) { output.Add(indention); } output.Add("if ("); SerializeExpression(output, ifStatement.Condition); output.Add(") {\n"); SerializeExecList(output, ifStatement.TrueCode, indention + "\t"); output.Add(indention); output.Add("}"); if (ifStatement.FalseCode.Length == 0) { output.Add("\n"); } else { output.Add(" else "); if (ifStatement.FalseCode.Length == 1 && ifStatement.FalseCode[0] is IfStatement) { SerializeIfStatement(output, (IfStatement)ifStatement.FalseCode[0], indention, false); } else { output.Add("{\n"); SerializeExecList(output, ifStatement.FalseCode, indention + "\t"); output.Add(indention); output.Add("}\n"); } } }
private void SerializeIfStatement(List<string> output, IfStatement ifStatement, string indention, bool skipIndention) { if (!skipIndention) { output.Add(indention); } output.Add("if "); SerializeExpression(output, ifStatement.Condition); output.Add(":\n"); SerializeBlock(output, ifStatement.TrueCode, indention + "\t"); if (ifStatement.FalseCode.Length > 0) { if (ifStatement.FalseCode.Length == 1 && ifStatement.FalseCode[0] is IfStatement) { output.Add(indention); output.Add("el"); SerializeIfStatement(output, (IfStatement)ifStatement.FalseCode[0], indention, true); } else { output.Add(indention); output.Add("else:\n"); SerializeBlock(output, ifStatement.FalseCode, indention + "\t"); } } }