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

wurstscript / WurstScript / 221

22 Nov 2023 03:20PM CUT coverage: 62.578% (-0.02%) from 62.602%
221

Pull #1081

circleci

Frotty
update wc3libs
Pull Request #1081: More performance improvements

17311 of 27663 relevant lines covered (62.58%)

0.63 hits per line

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

0.0
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/CompilationProcess.java
1
package de.peeeq.wurstio;
2

3
import config.WurstProjectBuildMapData;
4
import config.WurstProjectConfigData;
5
import de.peeeq.wurstio.languageserver.requests.RunTests;
6
import de.peeeq.wurstio.mpq.MpqEditor;
7
import de.peeeq.wurstio.utils.FileUtils;
8
import de.peeeq.wurstscript.RunArgs;
9
import de.peeeq.wurstscript.WLogger;
10
import de.peeeq.wurstscript.ast.WurstModel;
11
import de.peeeq.wurstscript.attributes.CompileError;
12
import de.peeeq.wurstscript.gui.WurstGui;
13
import de.peeeq.wurstscript.intermediatelang.interpreter.ILStackFrame;
14
import de.peeeq.wurstscript.jassAst.JassProg;
15
import de.peeeq.wurstscript.jassprinter.JassPrinter;
16
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
17
import de.peeeq.wurstscript.utils.Utils;
18
import org.eclipse.jdt.annotation.Nullable;
19

20
import java.io.File;
21
import java.io.IOException;
22
import java.io.PrintStream;
23
import java.util.ArrayList;
24
import java.util.Optional;
25
import java.util.function.Supplier;
26

27
/**
28
 *
29
 */
30
public class CompilationProcess {
31

32
    private final WurstGui gui;
33
    private final RunArgs runArgs;
34
    private final TimeTaker timeTaker;
35

36
    public CompilationProcess(WurstGui gui, RunArgs runArgs) {
×
37
        this.gui = gui;
×
38
        this.runArgs = runArgs;
×
39
        if (runArgs.isMeasureTimes()) {
×
40
            this.timeTaker = new TimeTaker.Recording();
×
41
        } else {
42
            this.timeTaker = new TimeTaker.Default();
×
43
        }
44
    }
×
45

46
    @Nullable CharSequence doCompilation(@Nullable MpqEditor mpqEditor, boolean isProd) throws IOException {
47
        return doCompilation(mpqEditor, null, isProd);
×
48
    }
49

50
    @Nullable CharSequence doCompilation(@Nullable MpqEditor mpqEditor, @Nullable File projectFolder, boolean isProd) throws IOException {
51
        WurstCompilerJassImpl compiler = new WurstCompilerJassImpl(timeTaker, projectFolder, gui, mpqEditor, runArgs);
×
52
        gui.sendProgress("Check input map");
×
53
        if (mpqEditor != null && !mpqEditor.canWrite()) {
×
54
            WLogger.severe("The supplied map is invalid/corrupted/protected and Wurst cannot write to it.\n" +
×
55
                    "Please supply a valid .w3x input map that can be opened in the world editor.");
56
        }
57

58
        for (String file : runArgs.getFiles()) {
×
59
            compiler.loadFiles(file);
×
60
        }
×
61
        WurstModel model = timeTaker.measure("parse files",
×
62
            compiler::parseFiles);
×
63

64
        if (gui.getErrorCount() > 0) {
×
65
            return null;
×
66
        }
67
        if (model == null) {
×
68
            return null;
×
69
        }
70

71
        timeTaker.measure("Typecheck program",
×
72
                () -> compiler.checkProg(model));
×
73

74
        if (gui.getErrorCount() > 0) {
×
75
            return null;
×
76
        }
77

78
        timeTaker.measure("Translate program to Im",
×
79
                () -> compiler.translateProgToIm(model));
×
80

81
        if (gui.getErrorCount() > 0) {
×
82
            return null;
×
83
        }
84

85
        if (runArgs.isRunTests()) {
×
86
            timeTaker.measure("Run tests",
×
87
                    () -> runTests(compiler.getImTranslator(), compiler, runArgs.getTestTimeout()));
×
88
        }
89

90
        timeTaker.measure("Run compiletime functions", () ->compiler.runCompiletime(new WurstProjectConfigData(), isProd, false));
×
91

92
        JassProg jassProg = timeTaker.measure("Transform program to Jass",
×
93
            compiler::transformProgToJass);
×
94

95
        if (jassProg == null || gui.getErrorCount() > 0) {
×
96
            return null;
×
97
        }
98

99
        boolean withSpace;
100
        withSpace = !runArgs.isOptimize();
×
101

102
        gui.sendProgress("Printing Jass");
×
103

104
        JassPrinter printer = new JassPrinter(withSpace, jassProg);
×
105
        CharSequence mapScript = timeTaker.measure("Print Jass",
×
106
            (Supplier<String>) printer::printProg);
×
107

108
        // output to file
109
        File outputMapscript = timeTaker.measure("Print Jass",
×
110
                () -> writeMapscript(mapScript));
×
111

112
        if (!runArgs.isDisablePjass()) {
×
113
            boolean pjassError = timeTaker.measure("Run PJass",
×
114
                    () -> runPjass(outputMapscript));
×
115
            if (pjassError) return null;
×
116
        }
117
        timeTaker.printReport();
×
118
        return mapScript;
×
119
    }
120

121
    private boolean runPjass(File outputMapscript) {
122
        File commonJ = new File(outputMapscript.getParent(), "common.j");
×
123
        File blizzJ = new File(outputMapscript.getParent(), "blizzard.j");
×
124

125
        Pjass.Result pJassResult;
126
        if (commonJ.exists() && blizzJ.exists()) {
×
127
            pJassResult = Pjass.runPjass(outputMapscript, commonJ.getAbsolutePath(), blizzJ.getAbsolutePath());
×
128
        } else {
129
            pJassResult = Pjass.runPjass(outputMapscript);
×
130
        }
131
        WLogger.info(pJassResult.getMessage());
×
132
        if (!pJassResult.isOk()) {
×
133
            for (CompileError err : pJassResult.getErrors()) {
×
134
                gui.sendError(err);
×
135
            }
×
136
            return true;
×
137
        }
138
        return false;
×
139
    }
140

141
    private File writeMapscript(CharSequence mapScript) {
142
        gui.sendProgress("Writing output file");
×
143
        File outputMapscript;
144
        if (runArgs.getOutFile() != null) {
×
145
            outputMapscript = new File(runArgs.getOutFile());
×
146
        } else {
147
            outputMapscript = new File("./temp/output.j");
×
148
        }
149
        outputMapscript.getParentFile().mkdirs();
×
150
        try {
151
            FileUtils.write(mapScript, outputMapscript);
×
152
            return outputMapscript;
×
153
        } catch (IOException e) {
×
154
            throw new RuntimeException(e);
×
155
        }
156
    }
157

158
    private void runTests(ImTranslator translator, WurstCompilerJassImpl compiler, int testTimeout) {
159
        PrintStream out = System.out;
×
160
        // tests
161
        gui.sendProgress("Running tests");
×
162
        System.out.println("Running tests");
×
163
        RunTests runTests = new RunTests(Optional.empty(), 0, 0, Optional.empty(), testTimeout) {
×
164
            @Override
165
            protected void print(String message) {
166
                out.print(message);
×
167
            }
×
168
        };
169
        runTests.runTests(translator, compiler.getImProg(), Optional.empty(), Optional.empty());
×
170

171
        for (RunTests.TestFailure e : runTests.getFailTests()) {
×
172
            gui.sendError(new CompileError(e.getFunction(), e.getMessage()));
×
173
            if (runArgs.isGui()) {
×
174
                // when using graphical user interface, send stack trace to GUI
175
                for (ILStackFrame sf : Utils.iterateReverse(e.getStackTrace().getStackFrames())) {
×
176
                    gui.sendError(sf.makeCompileError());
×
177
                }
×
178
            }
179
        }
×
180

181
        System.out.println("Finished running tests");
×
182
    }
×
183
}
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