• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 271

29 Sep 2025 12:12PM UTC coverage: 64.649% (+2.4%) from 62.222%
271

Pull #1096

circleci

Frotty
Merge branch 'perf-improvements' of https://github.com/wurstscript/WurstScript into perf-improvements
Pull Request #1096: Perf improvements

18202 of 28155 relevant lines covered (64.65%)

0.65 hits per line

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

94.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
            v.print(sb, indent);
1✔
20
            append(sb, " = ");
1✔
21
            boolean first = true;
1✔
22
            for (ImSet e : es) {
1✔
23
                if (!first) {
1✔
24
                    append(sb, ", ");
1✔
25
                }
26
                e.getRight().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✔
107
        } catch (IOException e) {
×
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) {
135
        append(sb, "void");
×
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
        }
176
        append(sb, "<");
1✔
177
        for (int i = 0; i < tvs.size(); i++) {
1✔
178
            if (i > 0) {
1✔
179
                append(sb, ", ");
1✔
180
            }
181
            tvs.get(i).print(sb, indent);
1✔
182
        }
183
        append(sb, ">");
1✔
184
    }
1✔
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
        if (p.getCallType() == CallType.EXECUTE) {
1✔
245
            append(sb, "EXECUTE ");
1✔
246
        }
247
        append(sb, p.getFunc().getName());
1✔
248
        append(sb, smallHash(p.getFunc()));
1✔
249
        printTypeArguments(p.getTypeArguments(), indent, sb);
1✔
250
        printArgumentList(sb, indent, p.getArguments());
1✔
251
    }
1✔
252

253

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

268

269
    public static void print(ImVarAccess p, Appendable sb, int indent) {
270
        append(sb, p.getVar().getName());
1✔
271
        append(sb, "_");
1✔
272
        append(sb, smallHash(p.getVar()));
1✔
273

274
    }
1✔
275

276
    public static String smallHash(Object g) {
277
        int h = g.hashCode();
1✔
278
        // avoid negative hashes
279
        h = h & Integer.MAX_VALUE;
1✔
280
        // take only the last 3 digits
281
        int v = h % 1000;
1✔
282
        return Integer.toString(v);
1✔
283
    }
284

285

286
    public static void print(ImVarArrayAccess p, Appendable sb, int indent) {
287
        append(sb, p.getVar().getName());
1✔
288
        append(sb, "_");
1✔
289
        append(sb, smallHash(p.getVar()));
1✔
290
        for (ImExpr ie : p.getIndexes()) {
1✔
291
            append(sb, "[");
1✔
292
            ie.print(sb, indent + 1);
1✔
293
            append(sb, "]");
1✔
294
        }
1✔
295
    }
1✔
296

297

298
    public static void print(ImTupleExpr p, Appendable sb, int indent) {
299
        append(sb, "<");
1✔
300
        boolean first = true;
1✔
301
        for (ImExpr e : p.getExprs()) {
1✔
302
            if (!first) append(sb, ", ");
1✔
303
            e.print(sb, indent);
1✔
304
            first = false;
1✔
305
        }
1✔
306
        append(sb, ">");
1✔
307
    }
1✔
308

309

310
    public static void print(ImTupleSelection p, Appendable sb, int indent) {
311
        p.getTupleExpr().print(sb, indent);
1✔
312
        append(sb, "#");
1✔
313
        append(sb, p.getTupleIndex());
1✔
314
    }
1✔
315

316

317
    public static void print(ImIntVal p, Appendable sb, int indent) {
318
        append(sb, p.getValI());
1✔
319
    }
1✔
320

321

322
    public static void print(ImRealVal p, Appendable sb, int indent) {
323
        append(sb, p.getValR());
1✔
324
    }
1✔
325

326

327
    public static void print(ImStringVal p, Appendable sb, int indent) {
328
        append(sb, '"');
1✔
329
        append(sb, p.getValS());
1✔
330
        append(sb, '"');
1✔
331
    }
1✔
332

333

334
    public static void print(ImBoolVal p, Appendable sb, int indent) {
335
        append(sb, p.getValB() ? "true" : "false");
1✔
336
    }
1✔
337

338

339
    public static void print(ImFuncRef p, Appendable sb, int indent) {
340
        append(sb, "function ");
1✔
341
        append(sb, p.getFunc().getName());
1✔
342
    }
1✔
343

344

345
    public static void print(ImNull p, Appendable sb, int indent) {
346
        append(sb, "null");
1✔
347
        if (!(p.getType() instanceof ImVoid)) {
1✔
348
            append(sb, "<");
1✔
349
            p.getType().print(sb, indent);
1✔
350
            append(sb, ">");
1✔
351
        }
352
    }
1✔
353

354

355
    public static void print(ImStmts ss, Appendable sb, int indent) {
356
        for (ImStmt s : ss) {
1✔
357
            indent(sb, indent);
1✔
358
            s.print(sb, indent);
1✔
359
            append(sb, ";\n");
1✔
360
        }
1✔
361
    }
1✔
362

363

364
    public static void print(ImVar v, Appendable sb, int indent) {
365
        v.getType().print(sb, indent);
1✔
366
        append(sb, " ");
1✔
367
        append(sb, v.getName());
1✔
368
        append(sb, smallHash(v));
1✔
369
    }
1✔
370

371

372
    public static void print(ImOperatorCall e, Appendable sb, int indent) {
373
        append(sb, "(");
1✔
374
        if (e.getArguments().size() == 2) {
1✔
375
            // binary operator
376
            e.getArguments().get(0).print(sb, indent);
1✔
377
            append(sb, " ");
1✔
378
            append(sb, e.getOp());
1✔
379
            append(sb, " ");
1✔
380
            e.getArguments().get(1).print(sb, indent);
1✔
381
        } else {
382
            append(sb, e.getOp());
1✔
383
            for (ImExpr a : e.getArguments()) {
1✔
384
                append(sb, " ");
1✔
385
                a.print(sb, indent);
1✔
386
            }
1✔
387
        }
388
        append(sb, ")");
1✔
389
    }
1✔
390

391

392
    public static String printToString(ImPrintable p) {
393
        return asString(p);
×
394
    }
395

396
    public static String asString(ImPrintable p) {
397
        Appendable sb = new StringBuilder();
1✔
398
        try {
399
            p.print(sb, 0);
1✔
400
        } catch (Throwable t) {
×
401
            t.printStackTrace();
×
402
            append(sb, "## error {");
×
403
            append(sb, t);
×
404
            append(sb, "}");
×
405
        }
1✔
406
        return sb.toString();
1✔
407
    }
408

409

410
    public static void print(ImMethodCall mc, Appendable sb,
411
                             int indent) {
412
        mc.getReceiver().print(sb, 0);
1✔
413
        append(sb, ".");
1✔
414
        append(sb, mc.getMethod().getName());
1✔
415
        append(sb, smallHash(mc.getMethod()));
1✔
416
        printTypeArguments(mc.getTypeArguments(), indent, sb);
1✔
417
        printArgumentList(sb, 0, mc.getArguments());
1✔
418
    }
1✔
419

420
    public static String asString(ImFunction f) {
421
        return f.getName() + smallHash(f);
1✔
422

423
    }
424

425
    public static void print(ImMemberAccess e, Appendable sb,
426
                             int indent) {
427
        e.getReceiver().print(sb, 0);
1✔
428
        append(sb, ".");
1✔
429
        append(sb, e.getVar().getName());
1✔
430
        append(sb, smallHash(e.getVar()));
1✔
431
        for (ImExpr index : e.getIndexes()) {
1✔
432
            append(sb, "[");
1✔
433
            index.print(sb, indent);
1✔
434
            append(sb, "]");
1✔
435
        }
1✔
436
        printTypeArguments(e.getTypeArguments(), indent, sb);
1✔
437
    }
1✔
438

439

440
    public static void print(ImAlloc e, Appendable sb, int indent) {
441
        append(sb, "#alloc(");
1✔
442
        e.getClazz().print(sb, indent);
1✔
443
        append(sb, ")");
1✔
444

445
    }
1✔
446

447

448
    public static void print(ImDealloc e, Appendable sb, int indent) {
449
        append(sb, "#dealloc(");
1✔
450
        e.getClazz().print(sb, indent);
1✔
451
        append(sb, ", ");
1✔
452
        e.getObj().print(sb, 0);
1✔
453
        append(sb, ")");
1✔
454
    }
1✔
455

456

457
    public static void print(ImInstanceof e, Appendable sb,
458
                             int indent) {
459
        e.getObj().print(sb, 0);
1✔
460
        append(sb, " instanceof ");
1✔
461
        e.getClazz().print(sb, indent);
1✔
462
    }
1✔
463

464

465
    public static void print(ImTypeIdOfClass e, Appendable sb,
466
                             int indent) {
467
        append(sb, e.getClass().getName());
1✔
468
        append(sb, ".typeId");
1✔
469

470
    }
1✔
471

472

473
    public static void print(ImTypeIdOfObj e, Appendable sb,
474
                             int indent) {
475
        e.getObj().print(sb, 0);
1✔
476
        append(sb, ".typeId");
1✔
477
    }
1✔
478

479

480
    public static void print(ImArrayTypeMulti imArrayTypeMulti,
481
                             Appendable sb, int indent) {
482
        append(sb, "array<");
1✔
483
        imArrayTypeMulti.getEntryType().print(sb, indent);
1✔
484
        append(sb, " size: ");
1✔
485
        append(sb, imArrayTypeMulti.getArraySize());
1✔
486
        append(sb, ">");
1✔
487

488
    }
1✔
489

490

491
    public static void print(ImGetStackTrace e, Appendable sb, int indent) {
492
        append(sb, "#getStackTrace()");
1✔
493
    }
1✔
494

495

496
    public static void print(ImCompiletimeExpr e, Appendable sb, int indent) {
497
        append(sb, "compiletime<<");
1✔
498
        e.getExpr().print(sb, indent);
1✔
499
        append(sb, ">>");
1✔
500
    }
1✔
501

502
    public static void print(ImVarargLoop e, Appendable sb, int indent) {
503
        append(sb, "foreach vararg ");
1✔
504
        e.getLoopVar().print(sb, indent);
1✔
505
        append(sb, " {\n");
1✔
506
        e.getBody().print(sb, indent + 1);
1✔
507
        indent(sb, indent);
1✔
508
        append(sb, "}");
1✔
509
    }
1✔
510

511

512
    public static void print(ImTypeVarDispatch e, Appendable sb, int indent) {
513
        append(sb, "<");
×
514
        append(sb, e.getTypeVariable().getName());
×
515
        append(sb, ">.");
×
516
        append(sb, e.getTypeClassFunc().getName());
×
517
        printArgumentList(sb, indent, e.getArguments());
×
518
    }
×
519

520
    public static void print(ImTypeVarRef e, Appendable sb, int indent) {
521
        append(sb, e.getTypeVariable().getName());
1✔
522
        append(sb, smallHash(e.getTypeVariable()));
1✔
523
    }
1✔
524

525
    public static void print(ImClassType ct, Appendable sb, int indent) {
526
        append(sb, ct.getClassDef().getName());
1✔
527
        //append(sb, smallHash(ct.getClassDef()));
528
        ImTypeArguments typeArguments = ct.getTypeArguments();
1✔
529
        printTypeArguments(typeArguments, indent, sb);
1✔
530
    }
1✔
531

532
    private static void printTypeArguments(ImTypeArguments typeArguments, int indent, Appendable sb) {
533
        if (!typeArguments.isEmpty()) {
1✔
534
            append(sb, "<");
1✔
535
            boolean first = true;
1✔
536
            for (ImTypeArgument ta : typeArguments) {
1✔
537
                if (!first) {
1✔
538
                    append(sb, ", ");
1✔
539
                }
540
                ta.getType().print(sb, indent);
1✔
541
                first = false;
1✔
542
            }
1✔
543
            append(sb, ">");
1✔
544
        }
545
    }
1✔
546

547
    public static void print(ImTypeVar tv, Appendable sb, int indent) {
548
        append(sb, tv.getName());
1✔
549
        append(sb, smallHash(tv));
1✔
550
    }
1✔
551

552
    public static String asString(ImStmts s) {
553
        return asString((ImPrintable) s);
×
554
    }
555

556
    public static String asString(List<?> s) {
557
        return "[" + s.stream()
1✔
558
                    .map(Object::toString)
1✔
559
                    .collect(Collectors.joining(", ")) + "]";
1✔
560
    }
561

562
    public static String asString(ImTypeClassFunc s) {
563
        return s.getName() + smallHash(s);
×
564
    }
565

566
    public static String asString(ImClass s) {
567
        return s.getName() + smallHash(s);
×
568
    }
569

570
    public static String asString(ImMethod s) {
571
        return s.getName() + smallHash(s);
×
572
    }
573

574
    public static String asString(ImTypeArgument s) {
575
        return s.getType() + "" + s.getTypeClassBinding();
1✔
576
    }
577

578
    public static void print(ImCast e, Appendable sb, int indent) {
579
        append(sb, "(");
1✔
580
        e.getExpr().print(sb, indent);
1✔
581
        append(sb, " castTo ");
1✔
582
        e.getToType().print(sb, indent);
1✔
583
        append(sb, ")");
1✔
584
    }
1✔
585

586
    public static void print(ImAnyType at, Appendable sb, int indent) {
587
        append(sb, "any");
1✔
588
    }
1✔
589
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc