1 using System.Collections.Generic;
4 namespace CodeGen.generators
12 private const string ClassFormat =
"class {0}{1}\n{2}{3}{4}end";
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,
33 classes =
@class.Classes?.Aggregate(classes,
36 if (!
string.IsNullOrWhiteSpace(classes)) classes +=
"\n";
38 return string.Format(ClassFormat,
@class.Name, inherits, fields, methods, classes);
59 return GenerateMethodWithBody(method,
"");
62 private string GenerateMethodWithBody(
Method method,
string body)
66 if (method.
Access ==
"private") result =
"private " + result;
68 if (method.
Static) result +=
"self.";
71 result += method.Name;
76 for (var i = 0; i < method.Parameters?.Length; i++)
78 result += method.Parameters[i].Name;
81 result +=
'=' + method.Parameters[i].Default;
93 if (!
string.IsNullOrWhiteSpace(body))
94 result +=
"\n" + GeneratorConf.ShiftCode(body, 1, Indent);
101 private string GenerateInit(Class
@class)
103 string result =
"", body =
"";
104 var init =
new Method
108 var previousIsStatic =
false;
109 var paramList =
new List<Parameter>();
111 for (var i = 0; i <
@class.Fields.Length; i++)
113 var field =
@class.Fields[i];
117 previousIsStatic =
true;
121 paramList.Add(
new Parameter
124 Default = field.Default
127 body +=
"@" + field.Name +
" = " + field.Name;
129 if (i + 1 <
@class.Fields.Length && !previousIsStatic)
134 previousIsStatic =
false;
137 init.Parameters = paramList.ToArray();
139 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.
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
bool Static
Denotes if the mathod is static or not. Type: boolean
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 field. Contains access, const and static properties. Inherits from Varia...