CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
CSharp.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.Linq;
3 using CodeGen.utils;
4 
5 namespace CodeGen.generators
6 {
11  public class CSharpGenerator : Generator
12  {
13  private const string ClassFormat = "{0}class {1}{2}{{{3}{4}{5}}}";
14  private string Indent { get; set; } = GeneratorConf.GetIndent(UseTabs, 4);
15 
17  protected override string GenerateClass(Class @class)
18  {
19  string fields = "", inherits = " ", methods = "", classes = "";
20  if (@class.Parent?.Length > 0)
21  {
22  inherits = " : " + @class.Parent + ' ';
23  }
24 
25  fields = @class.Fields?.Aggregate('\n' + fields,
26  (current, field) => current + GenerateField(field) + '\n');
27 
28  methods = @class.Methods?.Aggregate(methods,
29  (current, method) => current + '\n' + GeneratorConf.ShiftCode(GenerateMethod(method), 1, Indent) + '\n');
30 
31  if (@class.Fields?.Length > 0)
32  {
33  methods += GeneratorConf.ShiftCode(GenerateGettersSetters(@class.Fields), 1, Indent);
34  }
35 
36  classes = @class.Classes?.Aggregate(classes,
37  (current, cls) => current + '\n' + GeneratorConf.ShiftCode(GenerateClass(cls), 1, Indent) + '\n') + '\n';
38 
39  var access = "";
40  if (@class.Access?.Length > 0)
41  {
42  access = @class.Access + ' ';
43  }
44 
45  return string.Format(ClassFormat, access, @class.Name, inherits, fields, methods, classes);
46  }
47 
49  public override string GenerateField(Field field)
50  {
51  var result = Indent;
52  if (string.IsNullOrWhiteSpace(field.Access) || field.Access == "default")
53  {
54  result += "private ";
55  }
56  else
57  {
58  result += field.Access + ' ';
59  }
60 
61  if (field.Const)
62  {
63  result += "const ";
64  }
65 
66  if (field.Static)
67  {
68  result += "static ";
69  }
70 
71 
72  result += field.Type + ' ';
73 
74  result += field.Name;
75  if (field.Default?.Length > 0)
76  {
77  result += " = " + field.Default;
78  }
79 
80  result += ';';
81  return result;
82  }
83 
85  public override string GenerateMethod(Method method)
86  {
87  var result = "";
88  if (method.Access == "" || method.Access == "default"|| method.Access == null)
89  {
90  result += "private ";
91  }
92  else
93  {
94  result += method.Access + ' ';
95  }
96 
97  if (method.Static)
98  {
99  result += "static ";
100  }
101 
102  if (method.Type == null || method.Type?.Length == 0)
103  result += "void ";
104  else
105  result += method.Type + ' ';
106 
107  result += method.Name + '(';
108 
109  for (var i = 0; i < method.Parameters?.Length; i++)
110  {
111  var parameter = method.Parameters[i];
112 
113  result += parameter.Type + ' ' + parameter.Name;
114  if (i + 1 < method.Parameters.Length)
115  {
116  result += ", ";
117  }
118  }
119 
120  result += ")\n{";
121  if (!string.IsNullOrWhiteSpace(method.Type) && method.Type != "void")
122  {
123  string defaultVal;
124  if (CSharpNormalizer.BuiltInDefaults.ContainsKey(method.Type))
125  {
126  defaultVal = CSharpNormalizer.BuiltInDefaults[method.Type];
127  }
128  else
129  {
130  defaultVal = "new " + method.Type + "()";
131  }
132 
133  result += '\n' + Indent + "return " + defaultVal + ";\n";
134  }
135 
136  result += '}';
137  return result;
138  }
139 
145  public string GenerateGettersSetters(IEnumerable<Field> fields)
146  {
147  if (fields == null) return "";
148  var result = "";
149  foreach (var field in fields)
150  {
151  if (field.Getter)
152  {
153  result += '\n' + GenerateGetter(field) + '\n';
154  }
155 
156  if (field.Setter)
157  {
158  result += '\n' + GenerateSetter(field) + '\n';
159  }
160  }
161 
162  return result;
163  }
164 
170  public string GenerateGetter(Field field)
171  {
172  return $"public {field.Type} get{Utils.Title(field.Name)}()\n{{\n{Indent}return {field.Name};\n}}";
173  }
174 
180  public string GenerateSetter(Variable field)
181  {
182  return $"public void set{Utils.Title(field.Name)}({field.Type} newValue)\n{{\n{Indent}{field.Name} = newValue;\n}}";
183  }
184  }
185 
189  {
190  private static Normalizer _singletonInstance;
191 
192  private CSharpNormalizer()
193  {
194  }
195 
199  public static readonly Dictionary<string, string> BuiltInDefaults = new Dictionary<string, string>
200  {
201  {"int", "0"},
202  {"double", "0.0"},
203  {"float", "0.0f"},
204  {"char", "''"},
205  {"bool", "false"},
206  {"string", "\"\""},
207  };
208 
213  public static Normalizer GetNormalizer()
214  {
215  return _singletonInstance ?? (_singletonInstance = new CSharpNormalizer());
216  }
217 
219  protected override string NormalizeType(string type)
220  {
221  if (string.IsNullOrWhiteSpace(type))
222  {
223  type = "void";
224  }
225 
226  return type;
227  }
228  }
229 }
The structure that describes variable. Contains name, type and default value
Definition: Models.cs:96
bool Const
Denotes if field is constant or not. Type: boolean
Definition: Models.cs:133
string Type
Represents return type of the method. Type: string
Definition: Models.cs:178
string GenerateGetter(Field field)
Generates getter for field
Definition: CSharp.cs:170
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Definition: CSharp.cs:85
The structure that describes class. Contains name, array of fields, methods and subclasses, parent class name, access specifier. Overrides ToString() method.
Definition: Models.cs:48
string GenerateSetter(Variable field)
Generates setter for field
Definition: CSharp.cs:180
bool Static
Denotes if the mathod is static or not. Type: boolean
Definition: Models.cs:196
bool Static
Denotes if field is static or not. Type: boolean
Definition: Models.cs:139
static Normalizer GetNormalizer()
Method for getting a singleton
Definition: CSharp.cs:213
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Definition: Models.cs:202
Holds the configuration of generator
string GenerateGettersSetters(IEnumerable< Field > fields)
Generates getters and setters for fields
Definition: CSharp.cs:145
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Definition: CSharp.cs:17
Interface of language normalizer: normalizes package data according to specified language ...
override string NormalizeType(string type)
Type normalizer: fixes the type to language's built in
Definition: CSharp.cs:219
static string ShiftCode(string code, int num, string indent)
Shifts code using given parameters
string Access
Represents access level of the field. Type: string
Definition: Models.cs:127
string Default
Represents default value of the varibale. Type: string
Definition: Models.cs:114
Interface of language generator
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
Definition: CSharp.cs:49
string Access
Represents access level of the method. Type: string
Definition: Models.cs:184
static readonly Dictionary< string, string > BuiltInDefaults
The dictionary of built in values
Definition: CSharp.cs:199
The structure that describes method. Contains name, return type, access level, const and static prope...
Definition: Models.cs:166
The structure that describes field. Contains access, const and static properties. Inherits from Varia...
Definition: Models.cs:121