1 using System.Collections.Generic;
4 namespace CodeGen.generators
12 private const string ClassFormat =
"class {0} {1}{{{2}{3}}}{4}";
13 private string Indent {
get; set; } = GeneratorConf.GetIndent(
true, 4);
18 string fields =
"", inherits =
"", methods =
"", classes =
"";
19 if (!
string.IsNullOrWhiteSpace(
@class.Parent))
21 inherits =
"extends " +
@class.Parent +
" ";
24 if (
@class.Fields?.Length > 0)
26 fields =
"\n" + GeneratorConf.ShiftCode(GenerateInit(
@class), 1, Indent) +
"\n";
29 methods =
@class.Methods?.Aggregate(
"\n" + methods,
32 var staticFields =
@class.Fields?.Aggregate(
"",
35 if (!field.Static)
return current;
36 current +=
"\n" +
@class.Name +
"." + field.Name +
" = ";
37 current += (string.IsNullOrEmpty(field.Default) ?
"null" : field.Default) +
";";
41 classes =
@class.Classes?.Aggregate(classes,
42 (current, cls) => current +
"\n" +
@class.Name +
"." + cls.Name +
" = " + GenerateClass(cls));
44 return string.Format(ClassFormat,
@class.Name, inherits, fields, methods, staticFields + classes);
52 result += field.Type +
" ";
56 result +=
" = " + field.Default;
67 return GenerateMethodWithBody(method,
"");
76 private string GenerateMethodWithBody(
Method method,
string body)
84 result += method.Name;
87 for (var i = 0; i < method.Parameters?.Length; i++)
89 var parameter = method.Parameters[i];
91 result += parameter.Name;
92 if (!
string.IsNullOrEmpty(parameter.Default))
94 result +=
"=" + parameter.Default;
107 result +=
"\n" + GeneratorConf.ShiftCode(body, 1, Indent) +
"\n";
110 if (method.
Type !=
"" && method.
Name !=
"constructor")
112 result +=
"\n" + Indent +
"return null;\n";
125 private string GenerateInit(Class
@class)
130 var init =
new Method
135 var previousIsStatic =
false;
137 var paramList =
new List<Parameter>();
139 for (var i = 0; i <
@class.Fields?.Length; i++)
141 var field =
@class.Fields[i];
145 previousIsStatic =
true;
149 paramList.Add(
new Parameter
152 Default = field.Default
155 body +=
"this." + field.Name +
" = " + field.Name +
";";
157 if (i + 1 <
@class.Fields.Length && !previousIsStatic)
162 previousIsStatic =
false;
165 init.Parameters = paramList.ToArray();
167 if (previousIsStatic && body.Length > Indent.Length)
169 body = body.Substring(0, body.Length - Indent.Length + 1);
172 if (!
string.IsNullOrWhiteSpace(
@class.Parent))
174 body =
"super();\n" + body;
177 result += GenerateMethodWithBody(init, body);
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
string Type
Represents return type of the method. Type: string
The structure that describes class. Contains name, array of fields, methods and subclasses, parent class name, access specifier. Overrides ToString() method.
string Name
Represents the name of the method. Type: string
bool Static
Denotes if the mathod is static or not. Type: boolean
Generator for JavaScript ES6
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Holds the configuration of generator
static string ShiftCode(string code, int num, string indent)
Shifts code using given parameters
string Default
Represents default value of the varibale. Type: string
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
Interface of language generator
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...