/// <summary> /// Génère une fonction de sérialisation pour la classe donnée. /// </summary> /// <param name="decl"></param> /// <returns></returns> public string GenerateSerializer(ClassDeclaration decl) { StringBuilder sb = new StringBuilder(); ClankType type = m_project.Types.Types[decl.Name]; string typename = GenerateTypeName(type); sb.AppendLine("void " + typename + "::serialize(std::ostream& output) {"); foreach (var inst in decl.Instructions) { Variable attr = null; if (inst is VariableDeclarationInstruction) { VariableDeclarationInstruction vardecl = (VariableDeclarationInstruction)inst; attr = vardecl.Var; } else if (inst is VariableDeclarationAndAssignmentInstruction) { VariableDeclarationAndAssignmentInstruction vardecl = (VariableDeclarationAndAssignmentInstruction)inst; attr = (Variable)vardecl.Declaration.Var; } else { continue; } sb.AppendLine("\t// " + attr.Name); sb.AppendLine(Tools.StringUtils.Indent(GenerateSerializationInstruction( new Variable() { Type = attr.Type, Name = "this->" + attr.Name }))); } sb.AppendLine("}"); return(sb.ToString()); }
/// <summary> /// Génère une fonction de désérialisation pour la classe donnée. /// </summary> /// <param name="decl"></param> /// <returns></returns> public string GenerateDeserializer(ClassDeclaration decl) { StringBuilder sb = new StringBuilder(); ClankType type = m_project.Types.Types[decl.Name]; string typename = GenerateTypeName(type); string objName = "_obj"; sb.AppendLine(typename + " " + typename + "::deserialize(std::istream& input) {"); sb.AppendLine("\t" + typename + " " + objName + " = " + typename + "();"); foreach (var inst in decl.Instructions) { Variable attr = null; if (inst is VariableDeclarationInstruction) { VariableDeclarationInstruction vardecl = (VariableDeclarationInstruction)inst; attr = vardecl.Var; } else if (inst is VariableDeclarationAndAssignmentInstruction) { VariableDeclarationAndAssignmentInstruction vardecl = (VariableDeclarationAndAssignmentInstruction)inst; attr = (Variable)vardecl.Declaration.Var; } else { continue; } sb.AppendLine("\t// " + attr.Name); sb.AppendLine(Tools.StringUtils.Indent(GenerateDeserializationInstruction(attr, objName + "_" + attr.Name))); sb.AppendLine("\t" + objName + "." + attr.Name + " = (" + GenerateTypeInstanceNamePrefixed(attr.Type) + ")" + objName + "_" + attr.Name + ";"); } sb.AppendLine("\treturn " + objName + ";"); sb.AppendLine("}"); return(sb.ToString()); }
/// <summary> /// Génère le code de déclaration d'une instruction. /// </summary> /// <returns></returns> string GenerateDeclarationInstruction(VariableDeclarationInstruction instruction) { StringBuilder builder = new StringBuilder(); foreach (string modifier in instruction.Modifiers) { builder.Append(modifier + " "); } return(builder.ToString() + GenerateTypeInstanceName(instruction.Var.Type) + " " + instruction.Var.Name + ";"); }
/// <summary> /// Ajoute tous les types dont dépend l'instruction inst dans le set passé en paramètres. /// </summary> void AggregateDependencies(Instruction inst, HashSet <ClankType> types) { if (inst is ClassDeclaration) { ClassDeclaration decl = (ClassDeclaration)inst; foreach (var instruction in decl.Instructions) { AggregateDependencies(instruction, types); } } else if (inst is Model.Language.Macros.RemoteFunctionWrapper) { var decl = (Model.Language.Macros.RemoteFunctionWrapper)inst; AggregateDependencies(decl.Func, types); } if (inst is VariableDeclarationInstruction) { VariableDeclarationInstruction decl = (VariableDeclarationInstruction)inst; AddToSet(types, decl.Var.Type); } else if (inst is VariableDeclarationAndAssignmentInstruction) { VariableDeclarationAndAssignmentInstruction decl = (VariableDeclarationAndAssignmentInstruction)inst; AddToSet(types, decl.Declaration.Var.Type); } else if (inst is FunctionDeclaration) { FunctionDeclaration decl = (FunctionDeclaration)inst; AddToSet(types, decl.Func.ReturnType); foreach (var arg in decl.Func.Arguments) { AddToSet(types, arg.ArgType); } foreach (var instruction in decl.Code) { AggregateDependencies(instruction, types); } } else if (inst is FunctionCallInstruction) { FunctionCallInstruction decl = (FunctionCallInstruction)inst; AddToSet(types, decl.Call.Func.ReturnType); foreach (var arg in decl.Call.Arguments) { AddToSet(types, arg.Type); } } }
/// <summary> /// Génère le code de déclaration d'une instruction. /// </summary> /// <returns></returns> string GenerateDeclarationInstruction(VariableDeclarationInstruction instruction) { return(GenerateTypeInstanceName(instruction.Var.Type) + " " + instruction.Var.Name + ";"); }
/// <summary> /// Génère le code de déclaration d'une instruction. /// </summary> /// <returns></returns> static string GenerateDeclarationInstruction(VariableDeclarationInstruction instruction) { return(instruction.Var.Name + " = Nil"); }