private string FillAndCollect() { Method fill = new Method("FillObject") { Modifier = Modifier.Public | Modifier.Override, Params = new Parameters().Add <DataRow>("row") }; Method collect = new Method("UpdateRow") { Modifier = Modifier.Public | Modifier.Override, Params = new Parameters().Add <DataRow>("row") }; foreach (IColumn column in metaTable.Columns) { var fieldDef = dict_column_field[column.ColumnName]; string fieldName = fieldDef.PropertyName; fill.Statement.AppendFormat("this.{0} = GetField<{1}>(row, _{0});", fieldName, fieldDef.Type); collect.Statement.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)); }
private IPrimitive GenerateModules(MetaModel model) { var code = new CodeBlock(); if (model.Modules?.Count > 0) { foreach (var module in model.Modules) { foreach (var line in _moduleProvider.GetCode(module.Name)) { code.Add(line); } if (model.Debug) { code.Add($"{module.Name}.Debug = true;"); } foreach (var param in module.Parameters) { string valueStr = FormatParameterValue(param); code.Add($"{module.Name}.{param.Id} = {valueStr};"); } code.Add($"{module.Name}:RegisterModule(Modules);"); } } return(code); }
public void Generate(Assembly asm, Context ctx) { var code = new CodeBlock(); var beginning = Label.New(LabelType.Local); var body = new If(Predicate, code); code.Add(Code); code.Add(new Jmp(beginning)); ctx.Invalidate(); beginning.Generate(asm, ctx); body.Generate(asm, ctx); }
private static Method EncodeMethod(Record record, string baseName, List <RecordField> members) { Method method = new Method("void", "Encode"); method.Modifiers.Add("public"); if (record.IsCutomized) { method.Name = "encode"; } else { method.Modifiers.Add("override"); } method.MethodBody.Add("MemoryStream stream = new MemoryStream();"); method.MethodBody.Add("BinaryWriter writer = new BinaryWriter(stream);"); foreach (RecordField member in members) { string typeName = member.Type; string format = "writer.Write({0});"; switch (typeName) { case "String": format = "Record.WriteString(writer, {0}, " + member.ExtraInfo + ");"; break; case "Guid": format = "writer.Write({0}.ToByteArray());"; break; case "List<StringOffset>": format = "WriteStringOffset(writer, {0});"; break; } if (typeName.StartsWith("List<") || typeName.StartsWith("FastSearchList<")) { typeName = StringHelper.GetSubStringBetween(typeName, '<', '>'); CodeBlock foreach_block = new CodeBlock(); string loopVar = typeName.ToLower() + "Var"; foreach_block.Leading = String.Format("foreach({0} {1} in {2})", typeName, loopVar, member.Name); foreach_block.Add(String.Format(format, loopVar)); method.MethodBody.Add(foreach_block); } else { method.MethodBody.Add(String.Format(format, member.Name)); } } string sizeType = baseName == "EscherRecord" ? "UInt32" : "UInt16"; method.MethodBody.Add("this.Data = stream.ToArray();"); method.MethodBody.Add("this.Size = (" + sizeType + ")Data.Length;"); method.MethodBody.Add("base.Encode();"); return(method); }
public string Generate(MetaModel model) { var factory = new PrimitiveFactory(); var code = new CodeBlock(); code.Add(GenerateHeader()) .Add(InitMethodGenerator.GenerateCode(model)) .Add(VariablesGenerator.GenerateCode(model)) .Add(PrepareMethodGenerator.GenerateCode(model)) .Add(new EntryFunctionGenerator(model, factory).Generate()) .Add(GenerateRoutines()) .Add(GenerateModules(model)); return(code.ToCode()); }
private static Class BuildBaseClass(Record record, Dictionary <string, Record> AllRecords) { string className = record.Name; Class baseClass = new Class(className); baseClass.Modifiers.Add("public"); baseClass.Modifiers.Add("partial"); if (record.Parent != null) { baseClass.BaseClass = record.Parent; baseClass.Constructors.Add(String.Format("public {0}() {{ }}", className)); baseClass.Constructors.Add(String.Format( "public {0}({1} record) : base(record) {{ }}", className, baseClass.BaseClass )); if (record.Fields.Count > 0) { foreach (RecordField member in record.Fields) { Field field = new Field(member.Type, member.Name); field.Modifiers.Add("public"); if (member.Description != null) { field.Summary = member.Description; } baseClass.Members.Add(field); } } } else { Method method = new Method(className, "Read"); method.Modifiers.Add("public"); method.Modifiers.Add("static"); method.Params.Add(new Parameter("Stream", "stream")); method.MethodBody.Add(String.Format("{0} record = {0}.ReadBase(stream);", className)); CodeBlock switchblock = new CodeBlock("switch (record.Type)"); AddChildCase(record, className, switchblock); CodeLines defaultblock = new CodeLines(); defaultblock.Lines.Add("default:"); defaultblock.Lines.Add("\treturn record;"); switchblock.Add(defaultblock); method.MethodBody.Add(switchblock); baseClass.Members.Add(method); } return(baseClass); }
private static void AddChildCase(Record record, string className, CodeBlock switchblock) { foreach (Record childRecord in record.ChildRecords) { if (!childRecord.IsAbstract) { string name = childRecord.Name; CodeLines caseblock = new CodeLines(); caseblock.Lines.Add(String.Format("case {0}Type.{1}:", className, name)); caseblock.Lines.Add(String.Format("\treturn new {0}(record);", name)); switchblock.Add(caseblock); } else { AddChildCase(childRecord, className, switchblock); } } }
public override CodeElement GetSetter(string BufferName, string VariableName) { CodeBlock res = new CodeBlock(); int position = 0; foreach (var sec in BitSections) { res.Add(SetValueString( string.Format("{0}[{1}]", BufferName, sec.Start.ByteOffset), ApplyShift(VariableName, -position), sec.GetMask())); position += sec.Length; //buff[3] = (byte)((buff[3] & ~ByteMask) | ((val >> sec.Start.ByteOffset*8) & ByteMask)); } return(res); }
private static Method DecodeMethod(Record record, string baseName, List <RecordField> members) { Method method = new Method("void", "Decode"); method.Modifiers.Add("public"); if (record.IsCutomized) { method.Name = "decode"; } else { method.Modifiers.Add("override"); } method.MethodBody.Add("MemoryStream stream = new MemoryStream(Data);"); method.MethodBody.Add("BinaryReader reader = new BinaryReader(stream);"); foreach (RecordField member in members) { string typeName = member.Type; if (typeName.StartsWith("List<") || typeName.StartsWith("FastSearchList<")) { method.MethodBody.Add(String.Format("int count = {0};", member.ExtraInfo)); method.MethodBody.Add(String.Format("this.{0} = new {1}(count);", member.Name, typeName)); typeName = StringHelper.GetSubStringBetween(typeName, '<', '>'); string format = "{0}.Add(" + GetReadingCode(typeName, "16") + ");"; CodeBlock for_loop = new CodeBlock("for (int i = 0; i < count; i++)"); for_loop.Add(String.Format(format, member.Name)); method.MethodBody.Add(for_loop); } else { string format = "this.{0} = " + GetReadingCode(member.Type, member.ExtraInfo) + ";"; method.MethodBody.Add(String.Format(format, member.Name)); } } return(method); }