Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

wurstscript / WurstScript / 1005

21 Mar 2019 - 22:31 coverage decreased (-0.09%) to 61.245%
1005

Pull #822

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
more new bug fixes
Pull Request #822: WIP: support for persisting objects in compiletime expressions

14931 of 24379 relevant lines covered (61.25%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.47
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/ImPrinter.java
1
package de.peeeq.wurstscript.translation.imtranslation;
2

3
import de.peeeq.wurstscript.jassIm.*;
4

5
import java.io.IOException;
6
import java.util.List;
7
import java.util.stream.Collectors;
8

9
public class ImPrinter {
!
10

11

12
    public static void print(ImProg p, Appendable sb, int indent) {
13
        for (ImVar g : p.getGlobals()) {
1×
14
            g.print(sb, indent);
1×
15
            append(sb, "\n");
1×
16
        }
1×
17
        append(sb, "\n\n");
1×
18
        p.getGlobalInits().forEach((v,es) -> {
1×
19
            append(sb, v.getName());
1×
20
            append(sb, " = ");
1×
21
            boolean first = true;
1×
22
            for (ImExpr e : es) {
1×
23
                if (!first) {
1×
24
                    append(sb, ", ");
1×
25
                }
26
                e.print(sb, indent);
1×
27
                first = false;
1×
28
            }
1×
29
            append(sb, "\n");
1×
30
        });
1×
31
        append(sb, "\n\n");
1×
32
        for (ImFunction f : p.getFunctions()) {
1×
33
            f.print(sb, indent);
1×
34
            append(sb, "\n");
1×
35
        }
1×
36
        for (ImMethod m : p.getMethods()) {
1×
37
            printMethod(sb, m, indent);
1×
38
        }
1×
39
        append(sb, "\n\n");
1×
40
        for (ImClass c : p.getClasses()) {
1×
41
            print(c, sb, indent);
1×
42
        }
1×
43

44
    }
1×
45

46
    public static void print(ImClass c, Appendable sb, int indent) {
47
        append(sb, "class ");
1×
48
        append(sb, c.getName());
1×
49
        append(sb, smallHash(c));
1×
50
        printTypeVariables(c.getTypeVariables(), sb, indent);
1×
51
        append(sb, " extends ");
1×
52
        for (ImClassType sc : c.getSuperClasses()) {
1×
53
            sc.print(sb, indent);
1×
54
            append(sb, " ");
1×
55
        }
1×
56
        append(sb, "{\n");
1×
57
        for (ImVar f : c.getFields()) {
1×
58
            indent(sb, indent + 1);
1×
59
            f.print(sb, indent + 1);
1×
60
            append(sb, "\n");
1×
61
        }
1×
62
        append(sb, "\n\n");
1×
63
        for (ImMethod m : c.getMethods()) {
1×
64
            indent(sb, indent + 1);
1×
65
            printMethod(sb, m, indent + 1);
1×
66
        }
1×
67

68
        for (ImFunction func : c.getFunctions()) {
1×
69
            func.print(sb, indent + 1);
1×
70
        }
1×
71

72
        append(sb, "}\n\n");
1×
73
    }
1×
74

75
    private static void printMethod(Appendable sb, ImMethod m, int indent) {
76
        if (m.getIsAbstract()) {
1×
77
            append(sb, "abstract ");
1×
78
        }
79
        append(sb, "method ");
1×
80
        m.getMethodClass().print(sb, indent);
1×
81
        append(sb, ".");
1×
82
        append(sb, m.getName());
1×
83
        append(sb, smallHash(m));
1×
84
        append(sb, " implemented by ");
1×
85
        append(sb, m.getImplementation().getName());
1×
86
        append(sb, smallHash(m.getImplementation()));
1×
87
        append(sb, "\n");
1×
88
        for (ImMethod sm : m.getSubMethods()) {
1×
89
            append(sb, "        sub: ");
1×
90
            sm.getMethodClass().print(sb, indent);
1×
91
            append(sb, ".");
1×
92
            append(sb, sm.getName());
1×
93
            append(sb, smallHash(sm));
1×
94
            append(sb, "\n");
1×
95
        }
1×
96
        append(sb, "\n");
1×
97
    }
1×
98

99

100
    private static void append(Appendable sb, Object s) {
101
        append(sb, s.toString());
1×
102
    }
1×
103

104
    private static void append(Appendable sb, String s) {
105
        try {
106
            sb.append(s);
1×
UNCOV
107
        } catch (IOException e) {
!
UNCOV
108
            throw new RuntimeException(e);
!
109
        }
1×
110
    }
1×
111

112

113
    public static void print(ImSimpleType p, Appendable sb, int indent) {
114
        append(sb, p.getTypename());
1×
115
    }
1×
116

117
    public static void print(ImArrayType t, Appendable sb, int indent) {
118
        append(sb, "array<");
1×
119
        t.getEntryType().print(sb, indent);
1×
120
        append(sb, ">");
1×
121
    }
1×
122

123
    public static void print(ImTupleType p, Appendable sb, int indent) {
124
        append(sb, "⦅");
1×
125
        boolean first = true;
1×
126
        for (ImType t : p.getTypes()) {
1×
127
            if (!first) append(sb, ", ");
1×
128
            t.print(sb, indent);
1×
129
            first = false;
1×
130
        }
1×
131
        append(sb, "⦆");
1×
132
    }
1×
133

134
    public static void print(ImVoid p, Appendable sb, int indent) {
UNCOV
135
        append(sb, "void");
!
UNCOV
136
    }
!
137

138
    public static void print(ImFunction p, Appendable sb, int indent) {
139
        indent(sb, indent);
1×
140
        for (FunctionFlag flag : p.getFlags()) {
1×
141
            append(sb, flag);
1×
142
            append(sb, " ");
1×
143
        }
1×
144
        append(sb, "function ");
1×
145
        append(sb, p.getName());
1×
146
        append(sb, smallHash(p));
1×
147
        printTypeVariables(p.getTypeVariables(), sb, indent);
1×
148
        append(sb, "(");
1×
149
        boolean first = true;
1×
150
        for (ImVar p1 : p.getParameters()) {
1×
151
            if (!first) append(sb, ", ");
1×
152
            p1.print(sb, indent);
1×
153
            first = false;
1×
154
        }
1×
155
        append(sb, ")");
1×
156
        if (!(p.getReturnType() instanceof ImVoid)) {
1×
157
            append(sb, " returns ");
1×
158
            p.getReturnType().print(sb, indent);
1×
159
        }
160
        append(sb, " { \n");
1×
161
        for (ImVar v : p.getLocals()) {
1×
162
            indent(sb, indent + 1);
1×
163
            append(sb, "local ");
1×
164
            v.print(sb, indent + 1);
1×
165
            append(sb, "\n");
1×
166
        }
1×
167
        p.getBody().print(sb, indent + 1);
1×
168
        indent(sb, indent);
1×
169
        append(sb, "}\n\n");
1×
170
    }
1×
171

172
    private static void printTypeVariables(ImTypeVars tvs, Appendable sb, int indent) {
173
        if (tvs.isEmpty()) {
1×
174
            return;
1×
175
        }
UNCOV
176
        append(sb, "<");
!
UNCOV
177
        for (int i = 0; i < tvs.size(); i++) {
!
UNCOV
178
            if (i > 0) {
!
UNCOV
179
                append(sb, ", ");
!
180
            }
UNCOV
181
            tvs.get(i).print(sb, indent);
!
182
        }
UNCOV
183
        append(sb, ">");
!
UNCOV
184
    }
!
185

186
    public static void print(ImIf p, Appendable sb, int indent) {
187
        append(sb, "if ");
1×
188
        p.getCondition().print(sb, indent);
1×
189
        append(sb, "{\n");
1×
190
        p.getThenBlock().print(sb, indent + 1);
1×
191
        indent(sb, indent);
1×
192
        append(sb, "} else {\n");
1×
193
        p.getElseBlock().print(sb, indent + 1);
1×
194
        indent(sb, indent);
1×
195
        append(sb, "}");
1×
196

197
    }
1×
198

199
    private static void indent(Appendable sb, int indent) {
200
        for (int i = 0; i < indent; i++) {
1×
201
            append(sb, "    ");
1×
202
        }
203
    }
1×
204

205

206
    public static void print(ImLoop p, Appendable sb, int indent) {
207
        append(sb, "loop {\n");
1×
208
        p.getBody().print(sb, indent + 1);
1×
209
        indent(sb, indent);
1×
210
        append(sb, "}");
1×
211
    }
1×
212

213
    public static void print(ImExitwhen p, Appendable sb, int indent) {
214
        append(sb, "exitwhen ");
1×
215
        p.getCondition().print(sb, indent);
1×
216
    }
1×
217

218
    public static void print(ImReturn p, Appendable sb, int indent) {
219
        append(sb, "return ");
1×
220
        p.getReturnValue().print(sb, indent);
1×
221
    }
1×
222

223
    public static void print(ImSet p, Appendable sb, int indent) {
224
        p.getLeft().print(sb, indent);
1×
225
        append(sb, " = ");
1×
226
        p.getRight().print(sb, indent);
1×
227
    }
1×
228

229
    public static void print(ImNoExpr p, Appendable sb, int indent) {
230
        append(sb, "%nothing%");
1×
231
    }
1×
232

233
    public static void print(ImStatementExpr p, Appendable sb, int indent) {
234
        append(sb, "{\n");
1×
235
        p.getStatements().print(sb, indent + 1);
1×
236
        indent(sb, indent + 1);
1×
237
        append(sb, ">>>  ");
1×
238
        p.getExpr().print(sb, indent + 1);
1×
239
        append(sb, "}");
1×
240
    }
1×
241

242

243
    public static void print(ImFunctionCall p, Appendable sb, int indent) {
244
        append(sb, p.getFunc().getName());
1×
245
        append(sb, smallHash(p.getFunc()));
1×
246
        printTypeArguments(p.getTypeArguments(), indent, sb);
1×
247
        printArgumentList(sb, indent, p.getArguments());
1×
248
    }
1×
249

250

251
    public static void printArgumentList(Appendable sb, int indent,
252
                                         List<ImExpr> args) {
253
        append(sb, "(");
1×
254
        boolean first = true;
1×
255
        for (ImExpr a : args) {
1×
256
            if (!first) {
1×
257
                append(sb, ", ");
1×
258
            }
259
            a.print(sb, indent);
1×
260
            first = false;
1×
261
        }
1×
262
        append(sb, ")");
1×
263
    }
1×
264

265

266
    public static void print(ImVarAccess p, Appendable sb, int indent) {
267
        append(sb, p.getVar().getName());
1×
268
        append(sb, "_");
1×
269
        append(sb, smallHash(p.getVar()));
1×
270

271
    }
1×
272

273
    public static String smallHash(Object g) {
274
        String c = "" + g.hashCode();
1×
275
        return c.substring(0, Math.min(3, c.length() - 1));
1×
276
    }
277

278
    public static void print(ImVarArrayAccess p, Appendable sb, int indent) {
279
        append(sb, p.getVar().getName());
1×
280
        append(sb, "_");
1×
281
        append(sb, smallHash(p.getVar()));
1×
282
        for (ImExpr ie : p.getIndexes()) {
1×
283
            append(sb, "[");
1×
284
            ie.print(sb, indent + 1);
1×
285
            append(sb, "]");
1×
286
        }
1×
287
    }
1×
288

289

290
    public static void print(ImTupleExpr p, Appendable sb, int indent) {
291
        append(sb, "<");
1×
292
        boolean first = true;
1×
293
        for (ImExpr e : p.getExprs()) {
1×
294
            if (!first) append(sb, ", ");
1×
295
            e.print(sb, indent);
1×
296
            first = false;
1×
297
        }
1×
298
        append(sb, ">");
1×
299
    }
1×
300

301

302
    public static void print(ImTupleSelection p, Appendable sb, int indent) {
303
        p.getTupleExpr().print(sb, indent);
1×
304
        append(sb, "#");
1×
305
        append(sb, p.getTupleIndex());
1×
306
    }
1×
307

308

309
    public static void print(ImIntVal p, Appendable sb, int indent) {
310
        append(sb, p.getValI());
1×
311
    }
1×
312

313

314
    public static void print(ImRealVal p, Appendable sb, int indent) {
315
        append(sb, p.getValR());
1×
316
    }
1×
317

318

319
    public static void print(ImStringVal p, Appendable sb, int indent) {
320
        append(sb, '"');
1×
321
        append(sb, p.getValS());
1×
322
        append(sb, '"');
1×
323
    }
1×
324

325

326
    public static void print(ImBoolVal p, Appendable sb, int indent) {
327
        append(sb, p.getValB() ? "true" : "false");
1×
328
    }
1×
329

330

331
    public static void print(ImFuncRef p, Appendable sb, int indent) {
332
        append(sb, "function ");
1×
333
        append(sb, p.getFunc().getName());
1×
334
    }
1×
335

336

337
    public static void print(ImNull p, Appendable sb, int indent) {
338
        append(sb, "null");
1×
339
        if (!(p.getType() instanceof ImVoid)) {
1×
340
            append(sb, "<");
1×
341
            p.getType().print(sb, indent);
1×
342
            append(sb, ">");
1×
343
        }
344
    }
1×
345

346

347
    public static void print(ImStmts ss, Appendable sb, int indent) {
348
        for (ImStmt s : ss) {
1×
349
            indent(sb, indent);
1×
350
            s.print(sb, indent);
1×
351
            append(sb, ";\n");
1×
352
        }
1×
353
    }
1×
354

355

356
    public static void print(ImVar v, Appendable sb, int indent) {
357
        v.getType().print(sb, indent);
1×
358
        append(sb, " ");
1×
359
        append(sb, v.getName());
1×
360
        append(sb, smallHash(v));
1×
361
    }
1×
362

363

364
    public static void print(ImOperatorCall e, Appendable sb, int indent) {
365
        append(sb, "(");
1×
366
        if (e.getArguments().size() == 2) {
1×
367
            // binary operator
368
            e.getArguments().get(0).print(sb, indent);
1×
369
            append(sb, " ");
1×
370
            append(sb, e.getOp());
1×
371
            append(sb, " ");
1×
372
            e.getArguments().get(1).print(sb, indent);
1×
373
        } else {
374
            append(sb, e.getOp());
1×
375
            for (ImExpr a : e.getArguments()) {
1×
376
                append(sb, " ");
1×
377
                a.print(sb, indent);
1×
378
            }
1×
379
        }
380
        append(sb, ")");
1×
381
    }
1×
382

383

384
    public static String printToString(ImPrintable p) {
UNCOV
385
        return asString(p);
!
386
    }
387

388
    public static String asString(ImPrintable p) {
389
        Appendable sb = new StringBuilder();
1×
390
        try {
391
            p.print(sb, 0);
1×
UNCOV
392
        } catch (Throwable t) {
!
UNCOV
393
            t.printStackTrace();
!
UNCOV
394
            append(sb, "## error {");
!
UNCOV
395
            append(sb, t);
!
UNCOV
396
            append(sb, "}");
!
397
        }
1×
398
        return sb.toString();
1×
399
    }
400

401

402
    public static void print(ImMethodCall mc, Appendable sb,
403
                             int indent) {
404
        mc.getReceiver().print(sb, 0);
1×
405
        append(sb, ".");
1×
406
        append(sb, mc.getMethod().getName());
1×
407
        append(sb, smallHash(mc.getMethod()));
1×
408
        printTypeArguments(mc.getTypeArguments(), indent, sb);
1×
409
        printArgumentList(sb, 0, mc.getArguments());
1×
410
    }
1×
411

412
    public static String asString(ImFunction f) {
413
        return f.getName() + smallHash(f);
1×
414

415
    }
416

417
    public static void print(ImMemberAccess e, Appendable sb,
418
                             int indent) {
419
        e.getReceiver().print(sb, 0);
1×
420
        append(sb, ".");
1×
421
        append(sb, e.getVar().getName());
1×
422
        append(sb, smallHash(e.getVar()));
1×
423
        for (ImExpr index : e.getIndexes()) {
1×
424
            append(sb, "[");
1×
425
            index.print(sb, indent);
1×
426
            append(sb, "]");
1×
427
        }
1×
428
        printTypeArguments(e.getTypeArguments(), indent, sb);
1×
429
    }
1×
430

431

432
    public static void print(ImAlloc e, Appendable sb, int indent) {
433
        append(sb, "#alloc(");
1×
434
        e.getClazz().print(sb, indent);
1×
435
        append(sb, ")");
1×
436

437
    }
1×
438

439

440
    public static void print(ImDealloc e, Appendable sb, int indent) {
441
        append(sb, "#dealloc(");
1×
442
        e.getClazz().print(sb, indent);
1×
443
        append(sb, ", ");
1×
444
        e.getObj().print(sb, 0);
1×
445
        append(sb, ")");
1×
446
    }
1×
447

448

449
    public static void print(ImInstanceof e, Appendable sb,
450
                             int indent) {
451
        e.getObj().print(sb, 0);
1×
452
        append(sb, " instanceof ");
1×
453
        e.getClazz().print(sb, indent);
1×
454
    }
1×
455

456

457
    public static void print(ImTypeIdOfClass e, Appendable sb,
458
                             int indent) {
459
        append(sb, e.getClass().getName());
1×
460
        append(sb, ".typeId");
1×
461

462
    }
1×
463

464

465
    public static void print(ImTypeIdOfObj e, Appendable sb,
466
                             int indent) {
467
        e.getObj().print(sb, 0);
1×
468
        append(sb, ".typeId");
1×
469
    }
1×
470

471

472
    public static void print(ImArrayTypeMulti imArrayTypeMulti,
473
                             Appendable sb, int indent) {
474
        append(sb, "array<");
1×
475
        imArrayTypeMulti.getEntryType().print(sb, indent);
1×
476
        append(sb, " size: ");
1×
477
        append(sb, imArrayTypeMulti.getArraySize());
1×
478
        append(sb, ">");
1×
479

480
    }
1×
481

482

483
    public static void print(ImGetStackTrace e, Appendable sb, int indent) {
484
        append(sb, "#getStackTrace()");
1×
485
    }
1×
486

487

488
    public static void print(ImCompiletimeExpr e, Appendable sb, int indent) {
489
        append(sb, "compiletime<<");
1×
490
        e.getExpr().print(sb, indent);
1×
491
        append(sb, ">>");
1×
492
    }
1×
493

494
    public static void print(ImVarargLoop e, Appendable sb, int indent) {
495
        append(sb, "foreach vararg ");
1×
496
        e.getLoopVar().print(sb, indent);
1×
497
        append(sb, " {\n");
1×
498
        e.getBody().print(sb, indent + 1);
1×
499
        indent(sb, indent);
1×
500
        append(sb, "}");
1×
501
    }
1×
502

503

504
    public static void print(ImTypeVarDispatch e, Appendable sb, int indent) {
UNCOV
505
        append(sb, "<");
!
UNCOV
506
        append(sb, e.getTypeVariable().getName());
!
UNCOV
507
        append(sb, ">.");
!
UNCOV
508
        append(sb, e.getTypeClassFunc().getName());
!
UNCOV
509
        printArgumentList(sb, indent, e.getArguments());
!
UNCOV
510
    }
!
511

512
    public static void print(ImTypeVarRef e, Appendable sb, int indent) {
513
        append(sb, e.getTypeVariable().getName());
!
514
        append(sb, smallHash(e.getTypeVariable()));
!
515
    }
!
516

517
    public static void print(ImClassType ct, Appendable sb, int indent) {
518
        append(sb, ct.getClassDef().getName());
1×
519
        append(sb, smallHash(ct.getClassDef()));
1×
520
        ImTypeArguments typeArguments = ct.getTypeArguments();
1×
521
        printTypeArguments(typeArguments, indent, sb);
1×
522
    }
1×
523

524
    private static void printTypeArguments(ImTypeArguments typeArguments, int indent, Appendable sb) {
525
        if (!typeArguments.isEmpty()) {
1×
526
            append(sb, "<");
!
527
            boolean first = true;
!
528
            for (ImTypeArgument ta : typeArguments) {
!
UNCOV
529
                if (!first) {
!
UNCOV
530
                    append(sb, ", ");
!
531
                }
UNCOV
532
                ta.getType().print(sb, indent);
!
UNCOV
533
                first = false;
!
UNCOV
534
            }
!
535
            append(sb, ">");
!
536
        }
537
    }
1×
538

539
    public static void print(ImTypeVar tv, Appendable sb, int indent) {
UNCOV
540
        append(sb, tv.getName());
!
541
        append(sb, smallHash(tv));
!
UNCOV
542
    }
!
543

544
    public static String asString(ImStmts s) {
545
        return asString((ImPrintable) s);
!
546
    }
547

548
    public static String asString(List<?> s) {
549
        return "[" + ((List<?>) s).stream()
!
UNCOV
550
                    .map(Object::toString)
!
UNCOV
551
                    .collect(Collectors.joining(", ")) + "]";
!
552
    }
553

554
    public static String asString(ImTypeClassFunc s) {
UNCOV
555
        return s.getName() + smallHash(s);
!
556
    }
557

558
    public static String asString(ImClass s) {
UNCOV
559
        return s.getName() + smallHash(s);
!
560
    }
561

562
    public static String asString(ImMethod s) {
UNCOV
563
        return s.getName() + smallHash(s);
!
564
    }
565

566
    public static String asString(ImTypeArgument s) {
UNCOV
567
        return s.getType() + "" + s.getTypeClassBinding();
!
568
    }
569

570
    public static void print(ImCast e, Appendable sb, int indent) {
571
        append(sb, "(");
1×
572
        e.getExpr().print(sb, indent);
1×
573
        append(sb, " castTo ");
1×
574
        e.getToType().print(sb, indent);
1×
575
        append(sb, ")");
1×
576
    }
1×
577
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc