public CodeBlock GetBlock() { if (block == null) block = BuildBlock(); return block; }
public Statement IF(string exp, CodeBlock sent1, CodeBlock sent2) { AppendLine($"if ({exp})"); Add(sent1); AppendLine("else"); Add(sent2); return this; }
public void Add(CodeBlock block, int indent = 0) { foreach (var line in block.lines) { line.tab += indent; lines.Add(line); } }
public Statement DEFAULT(CodeBlock sent) { AppendLine($"default:"); Indent(); Add(sent); AppendLine($"break;"); Unindent(); return this; }
public Statement CASE(string exp, CodeBlock sent) { AppendLine($"case {exp}:"); Indent(); Add(sent); AppendLine($"break;"); Unindent(); return this; }
public Statement WHILE(string exp, CodeBlock sent) { AppendLine($"while ({exp})"); Add(sent); return this; }
public Statement SWITCH(string exp, CodeBlock sent) { AppendLine($"switch ({exp})"); Begin(); Add(sent); End(); return this; }
public Statement FOREACH(string exp1, string exp2, CodeBlock sent) { AppendLine($"foreach ({exp1} in {exp2})"); Add(sent); return this; }
private void WriteArrayValue(CodeBlock block, Array A, int columnNumber) { Type ty = Type.GetElementType(); if (ty != null && ty.IsPrimitive) { if (A.Length < 30) { Format = ValueOutputFormat.SingleLine; } else if (A.Length < 100) { Format = ValueOutputFormat.Wrap; columnNumber = 10; } else { Format = ValueOutputFormat.Wrap; columnNumber = 20; } } switch (Format) { case ValueOutputFormat.SingleLine: block.Append("{"); A.OfType <object>().ForEach( x => { NewValue(x).BuildBlock(block); }, _ => block.Append(",") ); block.Append("}"); break; case ValueOutputFormat.Wrap: block.Begin(); for (int i = 0; i < A.Length; i++) { if (i % columnNumber == 0) { block.AppendLine(); } if (i != 0 && i % (columnNumber * 10) == 0) //add empty line every 10 lines { block.AppendLine(); } Value item = NewValue(A.GetValue(i)); item.BuildBlock(block); if (i != A.Length - 1) { block.Append(","); } } block.End(); break; default: block.Begin(); for (int i = 0; i < A.Length; i++) { if (i != 0 && i % columnNumber == 0) { block.AppendLine(); } //block.AppendLine(); Value item = NewValue(A.GetValue(i)); item.BuildBlock(block); if (i != A.Length - 1) { block.Append(","); } } block.End(); break; } }
public void AddWithBeginEnd(CodeBlock block) { Begin(); Add(block, curruent); End(); }
protected override void BuildBlock(CodeBlock block) { base.BuildBlock(block); block.Add(code); }
protected override void BuildBlock(CodeBlock clss) { base.BuildBlock(clss); if (Comment != null) { Comment.Alignment = Alignment.Top; clss.AppendFormat($"{Comment}"); } clss.AppendFormat("{0} struct {1}", new ModifierString(Modifier), base.Name); var body = new CodeBlock(); if (Sorted) { var flds = fields.Where(fld => (fld.Modifier & Modifier.Const) != Modifier.Const); foreach (Field field in flds.OrderBy(fld => fld.Modifier)) { body.Add(field); } foreach (Constructor constructor in constructors) { body.Add(constructor); body.AppendLine(); } foreach (Property property in properties) { body.Add(property); if (property.GetBlock().Count > 1) { body.AppendLine(); } } foreach (Method method in methods) { body.Add(method); body.AppendLine(); } flds = fields.Where(fld => (fld.Modifier & Modifier.Const) == Modifier.Const); if (flds.Count() > 0) { body.AppendLine(); foreach (Field field in flds) { body.Add(field); } } } else { list.ForEach( item => body.Add(item), item => { if (item.Count == 1 && (item is Field || item is Property)) { return; } //if (item.Count == 1 && (item is Member)) // return; body.AppendLine(); } ); } clss.AddWithBeginEnd(body); }
private string FillAndCollect() { Method fill = new Method("FillObject") { modifier = Modifier.Public | Modifier.Override, args = new Arguments().Add<DataRow>("row") }; Method collect = new Method("UpdateRow") { modifier = Modifier.Public | Modifier.Override, args = new Arguments().Add<DataRow>("row") }; foreach (IColumn column in metaTable.Columns) { var fieldDef = dict_column_field[column.ColumnName]; string fieldName = fieldDef.PropertyName; fill.statements.AppendFormat("this.{0} = GetField<{1}>(row, _{0});", fieldName, fieldDef.Type); collect.statements.AppendFormat("SetField(row, _{0}, this.{0});", fieldName); } clss.Add(fill); clss.Add(collect); CodeBlock block = new CodeBlock(); block.Add(fill); block.AppendLine(); block.Add(collect); return block.ToString(2); }
/// <summary> /// Generate code, BuildBlock can be invoked only once /// </summary> /// <returns></returns> protected virtual CodeBlock BuildBlock() { CodeBlock block = new CodeBlock(); return block; }