void TrStmtList(List<Statement/*!*/>/*!*/ stmts) { Contract.Requires(cce.NonNullElements(stmts)); List<LocalVariable> AllDecls = new List<LocalVariable>(); using (WriteArray()) { j.WriteValue(KremlinAst.ESequence); using (WriteArray()) { WriteEUnit(); // in case the statement list is empty foreach (Statement ss in stmts) { // JsonTextWriter is forward-only, but after calling TrStmt() we may need // to go back and inject new ELet statements to introduce temp variables. // So call TrStmt() once to generate code to a throw-away MemoryStream, // but remember what temps need to be introduced. Then introduce them // and call TrStmt() once more, to generate the actual Json. JsonTextWriter oldj = j; VariableTracker oldtracker = VarTracker.Clone(); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); JsonTextWriter newj = new JsonTextWriter(sw); newj.Formatting = oldj.Formatting; newj.Indentation = oldj.Indentation; j = newj; var oldVarDeclsList = varDeclsList; varDeclsList = new List<LocalVariable>(); TrStmt(ss); j = oldj; VarTracker = oldtracker; var decls = varDeclsList; // Grab the set of just-declared variables generated by the first TrStmt() pass varDeclsList = null; // Switch modes for next TrStmt() pass AllDecls.AddRange(decls); // Accumulate the set of all variables this stmt list has generated foreach (var l in decls) { // ELet v in { Stmt j.WriteStartArray(); j.WriteValue(KremlinAst.ELet); j.WriteStartArray(); WriteBinder(l, l.Name, true); // lident WriteDefaultValue(l.Type); // = default VarTracker.Push(l); // "in" is the contents that follow j.WriteStartArray(); j.WriteValue(KremlinAst.ESequence); j.WriteStartArray(); WriteEUnit(); } newj.Close(); string RawJson = sb.ToString(); if (RawJson != "") { j.WriteRaw(","); j.WriteWhitespace("\n"); j.WriteRaw(RawJson); // Now paste the JSON } if (ss.Labels != null) { // bugbug: these appear to be used to support "Break" statements. j.WriteComment("Labels are unsupported: " + ss.Labels.Data.AssignUniqueId("after_", idGenerator)); } varDeclsList = oldVarDeclsList; } // Now that all statements in the list have been generated, close out the nested ELets generated above AllDecls.Reverse(); foreach (var l in AllDecls) { VarTracker.Pop(l); j.WriteEndArray(); // Closing out the expr list in the ESequence j.WriteEndArray(); // Closing out the array aboce ESequence j.WriteEndArray(); // Closing out the list of binder * expr * expr j.WriteEndArray(); // Closing out the array above ELet } } } }
// Make a copy of the VariableTracker() and its list. Variables pointed to by the // list are not copied. public VariableTracker Clone() { VariableTracker vt = new VariableTracker(); var n = l.First; while (n != null) { vt.l.AddLast(n.Value); n = n.Next; } return vt; }
private Tuple<string, string> buildPieceWithParam(VariableTracker vt, D_PieceWithParam pieceWrap, D_Item item) { StringBuilder cls = new StringBuilder(); StringBuilder method = new StringBuilder(); Tuple<string, string> v; v = buildPiece(vt, pieceWrap.Piece1, item); cls.AppendLine(v.Item1); method.AppendLine(v.Item2); v = buildPiece(vt, pieceWrap.Piece2, item); cls.AppendLine(v.Item1); method.AppendLine(v.Item2 + "conitnue; else break;"); return new Tuple<string, string>(cls.ToString(), method.ToString()); }
private Tuple<string, string> buildPieceWrap(VariableTracker vt, D_PieceWrap pieceWrap, D_Item item) { StringBuilder cls = new StringBuilder(); StringBuilder method = new StringBuilder(); Tuple<string, string> v; if (pieceWrap.HasPieceExtras) { switch (pieceWrap.PieceExtras.PieceExtrasClause) { case D_PieceExtras.D_PieceExtrasClause.QuestionMark: break; case D_PieceExtras.D_PieceExtrasClause.DotDot: method.AppendLine("while(true) {"); v = buildPieceInformation(vt, pieceWrap.PieceInformation, item); cls.AppendLine(v.Item1); method.AppendLine(v.Item2); method.AppendLine("}"); cls.AppendLine("public List<D_" + item.Word.Value + "> " + item.Word.Value + "s = new List<D_" + item.Word.Value + ">();"); break; default: throw new ArgumentOutOfRangeException(); } } return new Tuple<string, string>(cls.ToString(), method.ToString()); }
private Tuple<string, string> buildPieceKernalAddition(VariableTracker vt, D_PieceKernalAddition pieceWrap, D_Item item) { StringBuilder cls = new StringBuilder(); StringBuilder method = new StringBuilder(); Tuple<string, string> v; foreach (var contents in pieceWrap.PieceContents) { v = buildPieceContents(vt, contents, item); cls.AppendLine(v.Item1); method.AppendLine(v.Item2); } for (int i = 0; i < pieceWrap.PieceContents.Count; i++) { method.AppendLine("}"); } return new Tuple<string, string>(cls.ToString(), method.ToString()); }
private Tuple<string, string> buildPieceKernalOr(VariableTracker vt, D_PieceKernalOr pieceWrap, D_Item item) { StringBuilder cls = new StringBuilder(); StringBuilder method = new StringBuilder(); Tuple<string, string> v; foreach (var addition in pieceWrap.PieceKernalAdditions) { v = buildPieceKernalAddition(vt, addition, item); cls.AppendLine(v.Item1); method.AppendLine(v.Item2); } return new Tuple<string, string>(cls.ToString(), method.ToString()); }
private Tuple<string, string> buildPieceInformation(VariableTracker vt, D_PieceInformation pieceInformation, D_Item item) { StringBuilder cls = new StringBuilder(); StringBuilder method = new StringBuilder(); Tuple<string, string> v; switch (pieceInformation.PieceInformationClause) { case D_PieceInformation.D_PieceInformationClause.JustPiece: v = buildJustPiece(vt, pieceInformation.JustPiece, item); cls.AppendLine(v.Item1); method.AppendLine(v.Item2); break; case D_PieceInformation.D_PieceInformationClause.PieceWithParam: v = buildPieceWithParam(vt, pieceInformation.PieceWithParam, item); cls.AppendLine(v.Item1); method.AppendLine(v.Item2); break; default: throw new ArgumentOutOfRangeException(); } return new Tuple<string, string>(cls.ToString(), method.ToString()); }
private Tuple<string, string> buildPieceContents(VariableTracker vt, D_PieceContent pieceWrap, D_Item item) { StringBuilder cls = new StringBuilder(); StringBuilder method = new StringBuilder(); Tuple<string, string> v; switch (pieceWrap.PieceContentClause) { case D_PieceContent.D_PieceContentClause.Word: var d = vt.NewVariable(); method.AppendLine("D_" + pieceWrap.Word.Value + " "+d+";\r\n if(("+d+" = make" + pieceWrap.Word.Value + "(f))!=null){"); break; case D_PieceContent.D_PieceContentClause.String: method.AppendLine("if(isToken(f,\""+pieceWrap.String.Value+"\")){"); break; case D_PieceContent.D_PieceContentClause.Piece: v = buildPiece(vt, pieceWrap.Piece, item); method.AppendLine("{"); cls.AppendLine(v.Item1); method.AppendLine(v.Item2); break; default: throw new ArgumentOutOfRangeException(); } return new Tuple<string, string>(cls.ToString(), method.ToString()); }
private Tuple<string, string> buildPiece(VariableTracker vt, D_Piece piece, D_Item item) { StringBuilder cls = new StringBuilder(); StringBuilder method = new StringBuilder(); Tuple<string, string> v; switch (piece.PieceClause) { case D_Piece.D_PieceClause.PieceWrap: v = buildPieceWrap(vt, piece.PieceWrap, item); cls.Append(v.Item1); method.Append(v.Item2); break; case D_Piece.D_PieceClause.PieceKernalOr: v = buildPieceKernalOr(vt, piece.PieceKernalOr, item); cls.Append(v.Item1); method.Append(v.Item2); break; default: throw new ArgumentOutOfRangeException(); } return new Tuple<string, string>(cls.ToString(), method.ToString()); }
private Tuple<string, string> buildJustPiece(VariableTracker vt, D_JustPiece justPiece, D_Item item) { StringBuilder cls = new StringBuilder(); StringBuilder method = new StringBuilder(); Tuple<string, string> v = buildPiece(vt, justPiece.Piece, item); cls.AppendLine(v.Item1); method.AppendLine(v.Item2); return new Tuple<string, string>(cls.ToString(), method.ToString()); }
public void Run() { var toparse = getParsable().Distinct(); var s = getToks().ToArray(); var f = new PieceTokenizer(s); D_Document doc; if ((doc = makeDocument(f)) == null) { throw new Exception("bad document"); } StringBuilder classes = new StringBuilder(); StringBuilder methods = new StringBuilder(); VariableTracker vt=new VariableTracker(); foreach (var dItem in doc.Items) { var v = buildPiece(vt,dItem.Piece, dItem); classes.AppendFormat( @"public class D_{0} {{ {1} }}", dItem.Word.Value, v.Item1); methods.AppendFormat( @" private D_{0} make{0}(PieceTokenizer f) {{ var g = f.reStart(); D_{0} e = new D_{0}(); {1} }}", dItem.Word.Value, v.Item2); } }