public override void GenerateCode(DeclarationArray declarations, StreamWriter writer) { byte[] data = declarations.GetBytes(); Stream stream = writer.BaseStream; stream.Write(data, 0, data.Length); }
/// <summary> /// 编译一系列声明 /// </summary> /// <param name="array"> /// 需要编译的声明列表, /// <see cref="PackageDeclaration"/>应该为首元素(如果有), /// <see cref="UsingDeclaration"/>和<see cref="UsingAsDeclaration"/>应该在所有结构声明之前 /// </param> /// <returns>编译结果,Key:类型FullName,Value:类型元数据对象</returns> public IReadOnlyDictionary <string, AccelTypeInfo> Compile(DeclarationArray array) { IDeclaration[] declarations = array.Declarations; if (declarations == null) { return(null); } Dictionary <string, AccelTypeInfo> result = new Dictionary <string, AccelTypeInfo>(); m_ImportedPackages.Clear(); m_TypeAlias.Clear(); string packageName = string.Empty; for (int i = 0; i < declarations.Length; i++) { IDeclaration declaration = declarations[i]; switch (declaration) { case PackageDeclaration packageDeclaration: packageName = packageDeclaration.PackageName; m_ImportedPackages.Add(packageDeclaration.PackageName); break; case UsingDeclaration usingDeclaration: m_ImportedPackages.Add(usingDeclaration.PackageName); break; case UsingAsDeclaration usingAsDeclaration: m_TypeAlias.Add(usingAsDeclaration.AliasName, GetType(usingAsDeclaration.TypeName)); break; case StructDeclaration structDeclaration: DefineType(packageName, null, null, structDeclaration, result); break; default: continue; } } foreach (AccelTypeInfo typeInfo in result.Values) { Type type = typeInfo.Info; Type serializerType = SerializerInjector.Inject(type, typeInfo); SerializerBinder.AddBinding(type, serializerType); DefineDynamicSerializeFunction(typeInfo); DefineDynamicDeserializeFunction(typeInfo); } return(result); }
public sealed override void GenerateCode(DeclarationArray array, StreamWriter writer) { IDeclaration[] declarations = array.Declarations; CodeCompileUnit unit = new CodeCompileUnit(); CodeNamespace codeNamespace = new CodeNamespace(); codeNamespace.Comments.Add(new CodeCommentStatement("扩展类型请使用partial关键字在其他文件扩展,否则更改可能会丢失。", false)); List <IDeclaration> declarations1 = new List <IDeclaration>(Predefines); declarations1.AddRange(declarations); foreach (IDeclaration declaration in declarations1) { switch (declaration) { case PackageDeclaration packageDeclaration: codeNamespace.Name = packageDeclaration.PackageName; break; case UsingDeclaration usingDeclaration: CodeNamespaceImport import = new CodeNamespaceImport(usingDeclaration.PackageName); codeNamespace.Imports.Add(import); break; case UsingAsDeclaration usingAsDeclaration: CodeNamespaceImport alias = new CodeNamespaceImport($"{usingAsDeclaration.AliasName} = {usingAsDeclaration.TypeName.RawString}"); codeNamespace.Imports.Add(alias); break; case StructDeclaration structDeclaration: CodeTypeDeclaration type = GetTypeDecalaration(structDeclaration); codeNamespace.Types.Add(type); break; } } unit.Namespaces.Add(codeNamespace); CodeDomProvider provider = CodeDomProvider.CreateProvider(LanguageName); CodeGeneratorOptions options = new CodeGeneratorOptions { BracingStyle = "C", BlankLinesBetweenMembers = true }; provider.GenerateCodeFromCompileUnit(unit, writer, options); }
public override void GenerateCode(DeclarationArray declarations, StreamWriter writer) { throw new System.NotImplementedException(); }
/// <summary> /// 生成目标语言的代码 /// </summary> /// <param name="declarations">定义列表</param> /// <param name="writer">数据写入器</param> public abstract void GenerateCode(DeclarationArray declarations, StreamWriter writer);