1 using System.Collections.Generic;
4 namespace CodeGen.generators
12 private const string ClassFormat =
"class {0}{1}:\n{2}{3}{4}";
13 private string Indent {
get; set; } = GeneratorConf.GetIndent(
true, 4);
18 string fields =
"", inherits =
"", methods =
"", classes =
"";
20 if (!
string.IsNullOrWhiteSpace(
@class.Parent))
22 inherits = $
"({@class.Parent})";
25 if (
@class.Fields?.Length > 0)
27 fields = GeneratorConf.ShiftCode(GenerateInit(
@class), 1, Indent);
30 methods =
@class.Methods?.Aggregate(
'\n' + methods,
31 (current, method) => $
"{current}{GeneratorConf.ShiftCode(GenerateMethod(method), 1, Indent)}\n");
33 classes =
@class.Classes?.Aggregate(
'\n' + classes,
36 var result = string.Format(ClassFormat,
@class.Name, inherits, fields, methods, classes);
38 if (result[result.Length - 2] ==
':' && result[result.Length - 1] ==
'\n')
40 result += Indent +
"pass";
49 return "self." + field.Name;
55 return GenerateMethodWithBody(method,
"pass");
58 private string GenerateMethodWithBody(
Method method,
string body)
65 method.Name =
"__" + method.Name;
68 method.Name =
'_' + method.Name;
72 result += method.Name +
'(';
76 result =
"@staticmethod\n" + result;
87 for (var i = 0; i < method.Parameters?.Length; i++)
89 result += method.Parameters[i].Name;
92 result +=
'=' + method.Parameters[i].Default;
105 result +=
"):\n" + GeneratorConf.ShiftCode(body, 1, Indent);
111 string result =
"", body =
"";
116 var previousIsStatic =
false;
117 var paramList =
new List<Parameter>();
119 for (var i = 0; i <
@class.Fields?.Length; i++)
121 var field =
@class.Fields[i];
125 previousIsStatic =
true;
130 switch (field.Access)
146 Default = field.Default
148 var fieldName = field.Name;
149 field.Name = access + field.Name;
150 body += $
"{GenerateField(field)} = {fieldName}";
152 if (i + 1 <
@class.Fields.Length && !previousIsStatic)
157 previousIsStatic =
false;
166 init.Parameters = paramList.ToArray();
168 result += GenerateMethodWithBody(init, body);
The structure that describes class. Contains name, array of fields, methods and subclasses, parent class name, access specifier. Overrides ToString() method.
bool Static
Denotes if the mathod is static or not. Type: boolean
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Holds the configuration of generator
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
static string ShiftCode(string code, int num, string indent)
Shifts code using given parameters
string Default
Represents default value of the varibale. Type: string
Interface of language generator
string Access
Represents access level of the method. Type: string
The structure that describes method. Contains name, return type, access level, const and static prope...
The structure that describes parameter. Inherits from Variable
The structure that describes field. Contains access, const and static properties. Inherits from Varia...
string GenerateInit(Class @class)