Repository navigation
Expand file tree
/
Copy pathBasic.cs
More file actions
167 lines (150 loc) · 4.8 KB
/
Copy pathBasic.cs
File metadata and controls
167 lines (150 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using SqlcGenRuby;
namespace RubyCodegen;
public class RequireGem(string gem)
{
public string Build()
{
return $"require '{gem}'";
}
public string Name()
{
return gem;
}
}
public class ModuleDeclaration(string name, IEnumerable<IComposable> members) : IComposable, IRbsType
{
public string BuildCode()
{
var moduleBody = members
.Select(m => m.BuildCode())
.JoinByNewLine(2)
.Indent();
return $"module {name}\n{moduleBody}\nend";
}
public string BuildType()
{
var moduleBody = members
.Where(m => m is IRbsType)
.Select(m => ((IRbsType)m).BuildType())
.JoinByNewLine(2)
.Indent();
return $"module {name}\n{moduleBody}\nend";
}
}
public class ClassDeclaration(string name, IEnumerable<IComposable> members) : IComposable, IRbsType
{
public string BuildCode()
{
var classBody = members
.Select(m => m.BuildCode())
.JoinByNewLine(2)
.Indent();
return $"class {name}\n{classBody}\nend";
}
public string BuildType()
{
var moduleBody = members
.Where(m => m is IRbsType)
.Select(m => ((IRbsType)m).BuildType())
.JoinByNewLine()
.Indent();
return $"class {name}\n{moduleBody}\nend";
}
}
public class MethodDeclaration(string name, string? argInterface, string? args, string? returnInterface,
IEnumerable<IComposable> statements) : IComposable, IRbsType
{
public string BuildCode()
{
var methodParams = args is null ? string.Empty : $"({args})";
var methodBody = statements
.Select(c => c.BuildCode())
.JoinByNewLine()
.Indent();
return $"def {name}{methodParams}\n{methodBody}\nend";
}
public string BuildType()
{
var methodParams = args is null ? string.Empty : $"({argInterface}) ";
var returnType = returnInterface ?? "void";
var propertiesDef = statements
.Where(s => s is PropertyDeclaration)
.Select(s => ((IRbsType)s).BuildType())
.JoinByNewLine();
return $"{propertiesDef}\ndef {name}: {methodParams}-> {returnType}";
}
}
public class PropertyDeclaration(string name, string constType, IComposable value) : IComposableRbsType
{
public string BuildCode()
{
return $"{name.FirstCharToUpper()} = {value.BuildCode()}".TrimTrailingWhitespacesPerLine();
}
public string BuildType()
{
return $"{name}: {constType}";
}
}
public class SimpleStatement(string assignment, IComposable expression) : IComposable
{
public string BuildCode()
{
return $"{assignment} = {expression.BuildCode()}".TrimTrailingWhitespacesPerLine();
}
}
public class SimpleExpression(string expression) : IComposable
{
public string BuildCode()
{
return expression;
}
}
public class DataDefine(string name, Dictionary<string, string> nameToType) : IComposableRbsType
{
public string BuildCode()
{
var attributes = nameToType
.Select(kv => $":{kv.Key}")
.ToList()
.JoinByCommaAndFormat();
return $"class {name} < Data.define({attributes})\nend";
}
public string BuildType()
{
var attributes = nameToType
.Select(column => $"attr_reader {column.Key}: {column.Value}")
.ToList()
.JoinByNewLine()
.Indent();
var initializeArgs = nameToType
.Select(kv => $"{kv.Value} {kv.Key}")
.ToList()
.JoinByCommaAndFormat()
.Indent();
var initializeDef = $"def initialize: ({initializeArgs}) -> void".Indent();
return $"class {name}\n{attributes}\n{initializeDef}\nend";
}
}
public class NewStruct(string name, Dictionary<string, string> nameToType) : IComposableRbsType
{
public string BuildCode()
{
var initializeDef = nameToType.Select(kv => kv.Key).ToList().JoinByCommaAndFormat();
return $"class {name} < Struct.new({initializeDef})\nend";
}
public string BuildType()
{
var attributes = nameToType
.Select(kv => $"attr_accessor {kv.Key}: {kv.Value}")
.ToList()
.JoinByNewLine()
.Indent();
var initializeArgs = nameToType
.Select(kv => $"{kv.Value} {kv.Key}")
.ToList()
.JoinByCommaAndFormat()
.Indent();
var initializeDef = $"def self.new: ({initializeArgs}) -> {name}".Indent();
return $"class {name} < ::Struct\n{attributes}\n{initializeDef}\nend";
}
}