-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathGenPySwift.fu
395 lines (358 loc) · 10.1 KB
/
GenPySwift.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// GenPySwift.fu - Python/Swift code generator
//
// Copyright (C) 2020-2023 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 abstract class GenPySwift : GenBase
{
protected override void WriteDocPara!(FuDocPara para, bool many)
{
if (many) {
WriteNewLine();
StartDocLine();
WriteNewLine();
StartDocLine();
}
foreach (FuDocInline inline in para.Children) {
switch (inline) {
case FuDocText text:
Write(text.Text);
break;
case FuDocCode code:
WriteChar('`');
WriteDocCode(code.Text);
WriteChar('`');
break;
case FuDocLine:
WriteNewLine();
StartDocLine();
break;
default:
assert false;
}
}
}
protected abstract string GetDocBullet();
protected override void WriteDocList!(FuDocList list)
{
WriteNewLine();
foreach (FuDocPara item in list.Items) {
Write(GetDocBullet());
WriteDocPara(item, false);
WriteNewLine();
}
StartDocLine();
}
protected override void WriteLocalName!(FuSymbol symbol, FuPriority parent)
{
if (symbol is FuMember member) {
if (member.IsStatic())
WriteName(this.CurrentMethod.Parent);
else
Write("self");
WriteChar('.');
}
WriteName(symbol);
}
internal override void VisitAggregateInitializer!(FuAggregateInitializer expr)
{
Write("[ ");
WriteCoercedLiterals(expr.Type.AsClassType().GetElementType(), expr.Items);
Write(" ]");
}
internal override void VisitPrefixExpr!(FuPrefixExpr expr, FuPriority parent)
{
switch (expr.Op) {
case FuToken.Increment:
case FuToken.Decrement:
expr.Inner.Accept(this, parent);
break;
default:
base.VisitPrefixExpr(expr, parent);
break;
}
}
internal override void VisitPostfixExpr!(FuPostfixExpr expr, FuPriority parent)
{
switch (expr.Op) {
case FuToken.Increment:
case FuToken.Decrement:
expr.Inner.Accept(this, parent);
break;
default:
base.VisitPostfixExpr(expr, parent);
break;
}
}
static bool IsPtr(FuExpr expr) => expr.Type is FuClassType klass && klass.Class.Id != FuId.StringClass && !(klass is FuStorageType);
protected abstract string GetReferenceEqOp(bool not);
protected override void WriteEqual!(FuExpr left, FuExpr right, FuPriority parent, bool not)
{
if (IsPtr(left) || IsPtr(right))
WriteEqualExpr(left, right, parent, GetReferenceEqOp(not));
else
base.WriteEqual(left, right, parent, not);
}
protected virtual void WriteExpr!(FuExpr expr, FuPriority parent)
{
expr.Accept(this, parent);
}
protected void WriteListAppend!(FuExpr obj, List<FuExpr#> args)
{
WritePostfix(obj, ".append(");
FuType elementType = obj.Type.AsClassType().GetElementType();
if (args.Count == 0)
WriteNewStorage(elementType);
else
WriteCoerced(elementType, args[0], FuPriority.Argument);
WriteChar(')');
}
protected virtual bool VisitPreCall!(FuCallExpr call) => false;
protected bool VisitXcrement!(FuExpr expr, bool postfix, bool write)
{
bool seen;
switch (expr) {
case FuVar def:
return def.Value != null && VisitXcrement(def.Value, postfix, write);
case FuAggregateInitializer:
case FuLiteral:
case FuLambdaExpr:
return false;
case FuInterpolatedString interp:
seen = false;
foreach (FuInterpolatedPart part in interp.Parts)
seen |= VisitXcrement(part.Argument, postfix, write);
return seen;
case FuSymbolReference symbol:
return symbol.Left != null && VisitXcrement(symbol.Left, postfix, write);
case FuUnaryExpr unary:
if (unary.Inner == null) // new C()
return false;
seen = VisitXcrement(unary.Inner, postfix, write);
if ((unary.Op == FuToken.Increment || unary.Op == FuToken.Decrement) && postfix == unary is FuPostfixExpr) {
if (write) {
WriteExpr(unary.Inner, FuPriority.Assign);
WriteLine(unary.Op == FuToken.Increment ? " += 1" : " -= 1");
}
seen = true;
}
return seen;
case FuBinaryExpr binary:
seen = VisitXcrement(binary.Left, postfix, write);
if (binary.Op == FuToken.Is)
return seen;
if (binary.Op == FuToken.CondAnd || binary.Op == FuToken.CondOr)
assert !VisitXcrement(binary.Right, postfix, false);
else
seen |= VisitXcrement(binary.Right, postfix, write);
return seen;
case FuSelectExpr select:
seen = VisitXcrement(select.Cond, postfix, write);
assert !VisitXcrement(select.OnTrue, postfix, false);
assert !VisitXcrement(select.OnFalse, postfix, false);
return seen;
case FuCallExpr call:
seen = VisitXcrement(call.Method, postfix, write);
foreach (FuExpr arg in call.Arguments)
seen |= VisitXcrement(arg, postfix, write);
if (!postfix)
seen |= VisitPreCall(call);
return seen;
default:
assert false;
}
}
internal override void VisitExpr!(FuExpr statement)
{
VisitXcrement(statement, false, true);
if (!(statement is FuUnaryExpr unary) || (unary.Op != FuToken.Increment && unary.Op != FuToken.Decrement)) {
WriteExpr(statement, FuPriority.Statement);
WriteNewLine();
if (statement is FuVar def)
WriteInitCode(def);
}
VisitXcrement(statement, true, true);
CleanupTemporaries();
}
protected override void EndStatement!()
{
WriteNewLine();
}
protected abstract void OpenChild!();
protected abstract void CloseChild!();
protected override void WriteChild!(FuStatement! statement)
{
OpenChild();
statement.AcceptStatement(this);
CloseChild();
}
internal override void VisitBlock!(FuBlock statement)
{
WriteStatements(statement.Statements);
}
bool OpenCond!(string statement, FuExpr cond, FuPriority parent)
{
VisitXcrement(cond, false, true);
Write(statement);
WriteExpr(cond, parent);
OpenChild();
return VisitXcrement(cond, true, true);
}
protected virtual void WriteContinueDoWhile!(FuExpr cond)
{
OpenCond("if ", cond, FuPriority.Argument);
WriteLine("continue");
CloseChild();
VisitXcrement(cond, true, true);
WriteLine("break");
}
protected virtual bool NeedCondXcrement!(FuLoop loop) => loop.Cond != null;
void EndBody!(FuLoop loop)
{
if (loop is FuFor forLoop) {
if (forLoop.IsRange)
return;
VisitOptionalStatement(forLoop.Advance);
}
if (NeedCondXcrement(loop))
VisitXcrement(loop.Cond, false, true);
}
internal override void VisitContinue!(FuContinue statement)
{
if (statement.Loop is FuDoWhile doWhile)
WriteContinueDoWhile(doWhile.Cond);
else {
EndBody(statement.Loop);
WriteLine("continue");
}
}
void OpenWhileTrue!()
{
Write("while ");
VisitLiteralTrue();
OpenChild();
}
protected abstract string GetIfNot();
internal override void VisitDoWhile!(FuDoWhile statement)
{
OpenWhileTrue();
statement.Body.AcceptStatement(this);
if (statement.Body.CompletesNormally()) {
OpenCond(GetIfNot(), statement.Cond, FuPriority.Primary);
WriteLine("break");
CloseChild();
VisitXcrement(statement.Cond, true, true);
}
CloseChild();
}
protected virtual void OpenWhile!(FuLoop loop)
{
OpenCond("while ", loop.Cond, FuPriority.Argument);
}
void CloseWhile!(FuLoop loop)
{
loop.Body.AcceptStatement(this);
if (loop.Body.CompletesNormally())
EndBody(loop);
CloseChild();
if (NeedCondXcrement(loop)) {
if (loop.HasBreak && VisitXcrement(loop.Cond, true, false)) {
Write("else");
OpenChild();
VisitXcrement(loop.Cond, true, true);
CloseChild();
}
else
VisitXcrement(loop.Cond, true, true);
}
}
protected abstract void WriteForRange!(FuVar iter, FuBinaryExpr cond, long rangeStep);
internal override void VisitFor!(FuFor statement)
{
if (statement.IsRange) {
assert statement.Init is FuVar indVar;
Write("for ");
if (statement.IsIndVarUsed)
WriteName(indVar);
else
WriteChar('_');
Write(" in ");
assert statement.Cond is FuBinaryExpr cond;
WriteForRange(indVar, cond, statement.RangeStep);
WriteChild(statement.Body);
}
else {
VisitOptionalStatement(statement.Init);
if (statement.Cond != null)
OpenWhile(statement);
else
OpenWhileTrue();
CloseWhile(statement);
}
}
protected abstract void WriteElseIf!();
internal override void VisitIf!(FuIf statement)
{
bool condPostXcrement = OpenCond("if ", statement.Cond, FuPriority.Argument);
statement.OnTrue.AcceptStatement(this);
CloseChild();
if (statement.OnFalse == null && condPostXcrement && !statement.OnTrue.CompletesNormally())
VisitXcrement(statement.Cond, true, true);
else if (statement.OnFalse != null || condPostXcrement) {
if (!condPostXcrement && statement.OnFalse is FuIf! childIf && !VisitXcrement(childIf.Cond, false, false)) {
WriteElseIf();
VisitIf(childIf);
}
else {
Write("else");
OpenChild();
VisitXcrement(statement.Cond, true, true);
VisitOptionalStatement(statement.OnFalse);
CloseChild();
}
}
}
protected abstract void WriteResultVar!();
internal override void VisitReturn!(FuReturn statement)
{
if (statement.Value == null)
WriteLine("return");
else {
VisitXcrement(statement.Value, false, true);
WriteTemporaries(statement.Value);
if (VisitXcrement(statement.Value, true, false)) {
WriteResultVar(); // FIXME: name clash? only matters if return ... result++, unlikely
Write(" = ");
WriteCoercedExpr(this.CurrentMethod.Type, statement.Value);
WriteNewLine();
VisitXcrement(statement.Value, true, true);
WriteLine("return result");
}
else {
Write("return ");
WriteCoercedExpr(this.CurrentMethod.Type, statement.Value);
WriteNewLine();
}
CleanupTemporaries();
}
}
internal override void VisitWhile!(FuWhile statement)
{
OpenWhile(statement);
CloseWhile(statement);
}
}