1 using System.Collections.Generic;
5 namespace CodeGen.generators
13 private const string ClassFormat =
"{0}class {1}{2}{{{3}{4}{5}}}";
14 private string Indent {
get; set; } = GeneratorConf.GetIndent(UseTabs, 4);
19 string fields =
"", inherits =
" ", methods =
"", classes =
"";
20 if (
@class.Parent?.Length > 0)
22 inherits =
" : " +
@class.Parent +
' ';
25 fields =
@class.Fields?.Aggregate(
'\n' + fields,
26 (current, field) => current + GenerateField(field) +
'\n');
28 methods =
@class.Methods?.Aggregate(methods,
31 if (
@class.Fields?.Length > 0)
33 methods += GeneratorConf.ShiftCode(GenerateGettersSetters(
@class.Fields), 1, Indent);
36 classes =
@class.Classes?.Aggregate(classes,
40 if (
@class.Access?.Length > 0)
42 access =
@class.Access +
' ';
45 return string.Format(ClassFormat, access,
@class.Name, inherits, fields, methods, classes);
52 if (
string.IsNullOrWhiteSpace(field.
Access) || field.
Access ==
"default")
58 result += field.Access +
' ';
72 result += field.Type +
' ';
77 result +=
" = " + field.Default;
94 result += method.Access +
' ';
102 if (method.
Type == null || method.
Type?.Length == 0)
105 result += method.Type +
' ';
107 result += method.Name +
'(';
109 for (var i = 0; i < method.Parameters?.Length; i++)
111 var parameter = method.Parameters[i];
113 result += parameter.Type +
' ' + parameter.Name;
121 if (!
string.IsNullOrWhiteSpace(method.
Type) && method.
Type !=
"void")
126 defaultVal = CSharpNormalizer.BuiltInDefaults[method.Type];
130 defaultVal =
"new " + method.Type +
"()";
133 result +=
'\n' + Indent +
"return " + defaultVal +
";\n";
147 if (fields == null)
return "";
149 foreach (var field
in fields)
153 result +=
'\n' + GenerateGetter(field) +
'\n';
158 result +=
'\n' + GenerateSetter(field) +
'\n';
172 return $
"public {field.Type} get{Utils.Title(field.Name)}()\n{{\n{Indent}return {field.Name};\n}}";
182 return $
"public void set{Utils.Title(field.Name)}({field.Type} newValue)\n{{\n{Indent}{field.Name} = newValue;\n}}";
199 public static readonly Dictionary<string, string> BuiltInDefaults =
new Dictionary<string, string>
221 if (
string.IsNullOrWhiteSpace(type))
The structure that describes variable. Contains name, type and default value
bool Const
Denotes if field is constant or not. Type: boolean
string Type
Represents return type of the method. Type: string
string GenerateGetter(Field field)
Generates getter for field
override string GenerateMethod(Method method)
Method generator: generates method from given method object Method objectString of generated code of ...
The structure that describes class. Contains name, array of fields, methods and subclasses, parent class name, access specifier. Overrides ToString() method.
string GenerateSetter(Variable field)
Generates setter for field
bool Static
Denotes if the mathod is static or not. Type: boolean
bool Static
Denotes if field is static or not. Type: boolean
static Normalizer GetNormalizer()
Method for getting a singleton
Parameter[] Parameters
Represents parameters of the method. Type: array of type Parameter
Holds the configuration of generator
string GenerateGettersSetters(IEnumerable< Field > fields)
Generates getters and setters for fields
override string GenerateClass(Class @class)
Class generator: generates class with fields, methods and subclasses from given class object Class ob...
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
static string ShiftCode(string code, int num, string indent)
Shifts code using given parameters
string Access
Represents access level of the field. Type: string
string Default
Represents default value of the varibale. Type: string
Interface of language generator
override string GenerateField(Field field)
Field generator: generates field from given field object Field objectString of generated code of fiel...
string Access
Represents access level of the method. Type: string
static readonly Dictionary< string, string > BuiltInDefaults
The dictionary of built in values
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...