-
Notifications
You must be signed in to change notification settings - Fork 57
/
GenTs.fu
343 lines (321 loc) · 8.39 KB
/
GenTs.fu
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// GenTs.fu - TypeScript code generator
//
// Copyright (C) 2020-2021 Andy Edwards
// Copyright (C) 2020-2024 Piotr Fusik
//
// This file is part of Fusion Transpiler,
// see https://github.com/fusionlanguage/fut
//
// Fusion Transpiler is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Fusion Transpiler is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Fusion Transpiler. If not, see http://www.gnu.org/licenses/
public class GenTs : GenJs
{
FuSystem System;
// GenFullCode = false: only generate TypeScript declarations (.d.ts files)
// GenFullCode = true: generate full TypeScript code
bool GenFullCode = false;
protected override string GetTargetName() => "TypeScript";
public GenTs WithGenFullCode!()
{
GenFullCode = true;
return this;
}
internal override void VisitEnumValue!(FuConst konst, FuConst? previous)
{
WriteEnumValue(konst);
WriteCharLine(',');
}
protected override void WriteEnum!(FuEnum enu)
{
// WARNING: TypeScript enums allow reverse lookup that the Js generator currently
// doesn't implement
// https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
StartContainerType(enu);
Write("enum ");
Write(enu.Name);
WriteChar(' ');
OpenBlock();
enu.AcceptValues(this);
CloseBlock();
}
protected override void WriteTypeAndName!(FuNamedValue value)
{
WriteName(value);
Write(": ");
WriteType(value.Type);
}
void WriteType!(FuType type, bool readOnly = false)
{
switch (type) {
case FuNumericType:
Write(type.Id == FuId.LongType ? "bigint" : "number");
break;
case FuEnum enu:
Write(enu.Id == FuId.BoolType ? "boolean" : enu.Name);
break;
case FuClassType klass:
readOnly |= !(klass is FuReadWriteClassType);
switch (klass.Class.Id) {
case FuId.StringClass:
Write("string");
break;
case FuId.ArrayPtrClass when !(klass.GetElementType() is FuNumericType):
case FuId.ArrayStorageClass when !(klass.GetElementType() is FuNumericType):
case FuId.ListClass:
case FuId.QueueClass:
case FuId.StackClass:
if (readOnly)
Write("readonly ");
if (klass.GetElementType().Nullable)
WriteChar('(');
WriteType(klass.GetElementType());
if (klass.GetElementType().Nullable)
WriteChar(')');
Write("[]");
break;
default:
if (readOnly && klass.Class.TypeParameterCount > 0)
Write("Readonly<");
switch (klass.Class.Id) {
case FuId.ArrayPtrClass:
case FuId.ArrayStorageClass:
WriteArrayElementType(klass.GetElementType());
Write("Array");
break;
case FuId.HashSetClass:
case FuId.SortedSetClass:
Write("Set<");
WriteType(klass.GetElementType(), false);
WriteChar('>');
break;
case FuId.DictionaryClass:
case FuId.SortedDictionaryClass:
if (klass.GetKeyType() is FuEnum)
Write("Partial<");
Write("Record<");
WriteType(klass.GetKeyType());
Write(", ");
WriteType(klass.GetValueType());
WriteChar('>');
if (klass.GetKeyType() is FuEnum)
WriteChar('>');
break;
case FuId.OrderedDictionaryClass:
Write("Map<");
WriteType(klass.GetKeyType());
Write(", ");
WriteType(klass.GetValueType());
WriteChar('>');
break;
case FuId.RegexClass:
Write("RegExp");
break;
case FuId.MatchClass:
Write("RegExpMatchArray");
break;
case FuId.JsonElementClass:
Write("any");
break;
default:
Write(klass.Class.Name);
break;
}
if (readOnly && klass.Class.TypeParameterCount > 0)
WriteChar('>');
break;
}
if (type.Nullable)
Write(" | null");
break;
default:
Write(type.Name);
break;
}
}
protected override void WriteAsType!(FuVar def)
{
Write(" as ");
Write(def.Type.Name);
}
protected override void WriteBinaryOperand!(FuExpr expr, FuPriority parent, FuBinaryExpr binary)
{
FuType type = binary.Type;
if (expr.Type is FuNumericType && binary.IsRel()) {
// work around https://github.com/microsoft/TypeScript/issues/30540
type = this.System.PromoteNumericTypes(binary.Left.Type, binary.Right.Type);
}
WriteCoerced(type, expr, parent);
}
protected override void WriteEqualOperand!(FuExpr expr, FuExpr other)
{
if (expr.Type is FuNumericType)
WriteCoerced(this.System.PromoteNumericTypes(expr.Type, other.Type), expr, FuPriority.Equality);
else
expr.Accept(this, FuPriority.Equality);
}
protected override void WriteBoolAndOr!(FuBinaryExpr expr)
{
Write("[ ");
expr.Left.Accept(this, FuPriority.Argument);
Write(", ");
expr.Right.Accept(this, FuPriority.Argument);
Write(" ].");
Write(expr.Op == FuToken.And ? "every" : "some");
Write("(Boolean)");
}
protected override void DefineIsVar!(FuBinaryExpr binary)
{
if (binary.Right is FuVar def) {
EnsureChildBlock();
Write("let ");
WriteName(def);
Write(": ");
WriteType(binary.Left.Type);
EndStatement();
}
}
void WriteVisibility!(FuVisibility visibility)
{
switch (visibility) {
case FuVisibility.Private:
case FuVisibility.Internal:
break;
case FuVisibility.Protected:
Write("protected ");
break;
case FuVisibility.Public:
Write("public ");
break;
default:
assert false;
}
}
protected override void WriteConst!(FuConst konst)
{
WriteNewLine();
WriteDoc(konst.Documentation);
WriteVisibility(konst.Visibility);
Write("static readonly ");
WriteName(konst);
Write(": ");
WriteType(konst.Type, true);
if (this.GenFullCode) {
Write(" = ");
konst.Value.Accept(this, FuPriority.Argument);
}
WriteCharLine(';');
}
protected override void WriteField!(FuField field)
{
WriteDoc(field.Documentation);
WriteVisibility(field.Visibility);
if (field.Type.IsFinal() && !field.IsAssignableStorage())
Write("readonly ");
WriteTypeAndName(field);
if (this.GenFullCode)
WriteVarInit(field);
WriteCharLine(';');
}
protected override void WriteMethod!(FuMethod method)
{
WriteNewLine();
WriteMethodDoc(method);
WriteVisibility(method.Visibility);
switch (method.CallType) {
case FuCallType.Static:
Write("static ");
break;
case FuCallType.Virtual:
break;
case FuCallType.Abstract:
Write("abstract ");
break;
case FuCallType.Override:
break;
case FuCallType.Normal:
// no final methods in TS
break;
case FuCallType.Sealed:
// no final methods in TS
break;
default:
assert false;
}
WriteName(method);
WriteChar('(');
int i = 0;
for (FuVar? param = method.FirstParameter(); param != null; param = param.NextVar()) {
if (i > 0)
Write(", ");
WriteName(param);
if (param.Value != null && !this.GenFullCode)
WriteChar('?');
Write(": ");
WriteType(param.Type);
if (param.Value != null && this.GenFullCode)
WriteVarInit(param);
i++;
}
Write("): ");
WriteType(method.Type);
if (this.GenFullCode)
WriteBody(method);
else
WriteCharLine(';');
}
protected override void WriteClass!(FuClass klass, FuProgram program)
{
if (!WriteBaseClass(klass, program))
return;
StartContainerType(klass);
switch (klass.CallType) {
case FuCallType.Normal:
break;
case FuCallType.Abstract:
Write("abstract ");
break;
case FuCallType.Static:
case FuCallType.Sealed:
// there's no final/sealed keyword, but we accomplish it by marking the constructor private
break;
default:
assert false;
}
OpenJsClass(klass);
if (NeedsConstructor(klass) || klass.CallType == FuCallType.Static) {
if (klass.Constructor != null) {
WriteDoc(klass.Constructor.Documentation);
WriteVisibility(klass.Constructor.Visibility);
}
else if (klass.CallType == FuCallType.Static)
Write("private ");
if (this.GenFullCode)
WriteConstructor(klass);
else
WriteLine("constructor();");
}
WriteMembers(klass, this.GenFullCode);
CloseBlock();
}
public override void WriteProgram!(FuProgram program)
{
this.System = program.System;
CreateOutputFile();
if (this.GenFullCode)
WriteTopLevelNatives(program);
WriteTypes(program);
if (this.GenFullCode)
WriteLib(program);
CloseFile();
}
}