Skip to content

Commit cc5345f

Browse files
author
Killian Perlin
committed
Remove "to-lkt" option from interpreter
The interpreter should not do the job of the refactor tool. The "to-lkt" option was removed and the tests that used it where updated accordingly.
1 parent c338700 commit cc5345f

File tree

7 files changed

+43
-91
lines changed

7 files changed

+43
-91
lines changed

lkql_jit/cli/src/main/java/com/adacore/lkql_jit/cli/BaseLKQLChecker.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ protected LKQLOptions.Builder getBaseOptionsBuilder() {
130130
.projectFile(this.args.project)
131131
.rulesDir(this.args.rulesDirs)
132132
.ruleInstances(getRuleInstances())
133-
.keepGoingOnMissingFile(this.args.keepGoingOnMissingFile)
134-
.autoTranslateUnits(this.args.autoTranslateUnits);
133+
.keepGoingOnMissingFile(this.args.keepGoingOnMissingFile);
135134
}
136135

137136
/** Get the rule instances defined be the user through the LKQL checker command-line. */
@@ -188,12 +187,6 @@ public abstract static class Args implements Callable<Integer> {
188187
@CommandLine.Spec
189188
public picocli.CommandLine.Model.CommandSpec spec;
190189

191-
@CommandLine.Option(
192-
names = { "--to-lkt" },
193-
description = "List of units to auto translate to Lkt"
194-
)
195-
public List<String> autoTranslateUnits = new ArrayList<>();
196-
197190
@CommandLine.Option(names = { "-v", "--verbose" }, description = "Enable the verbose mode")
198191
public boolean verbose;
199192

lkql_jit/cli/src/main/java/com/adacore/lkql_jit/cli/LKQLLauncher.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ public static class LKQLRun implements Callable<Integer> {
4646
)
4747
public String charset = null;
4848

49-
@CommandLine.Option(
50-
names = { "--to-lkt" },
51-
description = "List of units to auto translate to Lkt"
52-
)
53-
public List<String> autoTranslateUnits = new ArrayList<>();
54-
5549
@CommandLine.Option(names = { "-P", "--project" }, description = "Project file to use")
5650
public String project = null;
5751

@@ -170,8 +164,7 @@ protected int executeScript(Context.Builder contextBuilder) {
170164
.runtime(this.args.RTS)
171165
.keepGoingOnMissingFile(this.args.keepGoingOnMissingFile)
172166
.files(this.args.files)
173-
.charset(this.args.charset)
174-
.autoTranslateUnits(this.args.autoTranslateUnits);
167+
.charset(this.args.charset);
175168

176169
// Finally, pass the options to the LKQL engine
177170
contextBuilder.option("lkql.options", optionsBuilder.build().toJson().toString());

lkql_jit/language/src/main/java/com/adacore/lkql_jit/LKQLLanguage.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.adacore.lkql_jit.nodes.TopLevelList;
2020
import com.adacore.lkql_jit.nodes.root_nodes.TopLevelRootNode;
2121
import com.adacore.lkql_jit.options.LKQLOptions;
22-
import com.adacore.lkql_jit.options.Refactorings.LKQLToLkt;
2322
import com.adacore.lkql_jit.runtime.GlobalScope;
2423
import com.adacore.lkql_jit.runtime.values.LKQLNamespace;
2524
import com.adacore.lkql_jit.utils.Constants;
@@ -30,7 +29,6 @@
3029
import com.oracle.truffle.api.source.Source;
3130
import java.io.PrintStream;
3231
import java.nio.charset.StandardCharsets;
33-
import java.util.Arrays;
3432
import java.util.Scanner;
3533
import org.graalvm.options.OptionCategory;
3634
import org.graalvm.options.OptionDescriptors;
@@ -340,39 +338,9 @@ private LKQLNode translate(
340338
public LKQLNode translate(final Source source, String sourceName) {
341339
var firstLine = new Scanner(source.getReader()).nextLine();
342340
LangkitSupport.AnalysisContextInterface langkitCtx = null;
343-
var baseName = source.getName().replaceFirst("[.][^.]+$", "");
344341
Source src;
345342

346-
if (getContext(null).getOptions().autoTranslateUnits().contains(baseName)) {
347-
System.out.println("Translating " + source.getName() + " to lkt");
348-
String newSrc = source.getCharacters().toString();
349-
var lines = newSrc.split("\n");
350-
351-
if (firstLine.startsWith("# lkql version:")) {
352-
if (firstLine.equals("# lkql version: 1")) {
353-
// Translate LKQL to Lkt (LKQL v2)
354-
newSrc = String.join("\n", Arrays.stream(lines).skip(1).toList());
355-
} else {
356-
throw LKQLRuntimeException.fromMessage(
357-
"Invalid lkql version line for autoTranslateUnit unit"
358-
);
359-
}
360-
}
361-
362-
var lktSrc = LKQLToLkt.lkqlToLkt(sourceName, newSrc);
363-
364-
if (getContext(null).isVerbose()) {
365-
System.out.println("Lkt source for " + sourceName);
366-
System.out.println("=============================");
367-
System.out.println(lktSrc);
368-
System.out.println();
369-
}
370-
371-
// Build a new source
372-
src = Source.newBuilder(Constants.LKQL_ID, lktSrc, source.getName()).build();
373-
374-
langkitCtx = lktAnalysisContext;
375-
} else if (firstLine.startsWith("# lkql version:")) {
343+
if (firstLine.startsWith("# lkql version:")) {
376344
if (firstLine.equals("# lkql version: 1")) {
377345
// lkql V1 uses lkql syntax
378346
langkitCtx = lkqlAnalysisContext;

lkql_jit/options/src/main/java/com/adacore/lkql_jit/options/LKQLOptions.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public record LKQLOptions(
3333
boolean keepGoingOnMissingFile,
3434
boolean showInstantiationChain,
3535
DiagnosticOutputMode diagnosticOutputMode,
36-
AutoFixMode autoFixMode,
37-
Set<String> autoTranslateUnits
36+
AutoFixMode autoFixMode
3837
) {
3938
// ----- Constructors -----
4039

@@ -120,13 +119,7 @@ public static LKQLOptions fromJson(JSONObject jsonLKQLOptions) {
120119
jsonLKQLOptions.getBoolean("keepGoingOnMissingFile"),
121120
jsonLKQLOptions.getBoolean("showInstantiationChain"),
122121
DiagnosticOutputMode.valueOf(jsonLKQLOptions.getString("diagnosticOutputMode")),
123-
AutoFixMode.valueOf(jsonLKQLOptions.getString("autoFixMode")),
124-
jsonLKQLOptions
125-
.getJSONArray("autoTranslateUnits")
126-
.toList()
127-
.stream()
128-
.map(e -> (String) e)
129-
.collect(Collectors.toSet())
122+
AutoFixMode.valueOf(jsonLKQLOptions.getString("autoFixMode"))
130123
);
131124
}
132125

@@ -164,8 +157,7 @@ public JSONObject toJson() {
164157
.put("keepGoingOnMissingFile", keepGoingOnMissingFile)
165158
.put("showInstantiationChain", showInstantiationChain)
166159
.put("diagnosticOutputMode", diagnosticOutputMode.toString())
167-
.put("autoFixMode", autoFixMode.toString())
168-
.put("autoTranslateUnits", new JSONArray(autoTranslateUnits));
160+
.put("autoFixMode", autoFixMode.toString());
169161
}
170162

171163
// ----- Inner classes -----
@@ -232,15 +224,9 @@ public static final class Builder {
232224
private boolean showInstantiationChain = false;
233225
private DiagnosticOutputMode diagnosticOutputMode = DiagnosticOutputMode.PRETTY;
234226
private AutoFixMode autoFixMode = AutoFixMode.DISPLAY;
235-
private List<String> autoTranslateUnits = new ArrayList<>();
236227

237228
// ----- Setters -----
238229

239-
public Builder autoTranslateUnits(List<String> autoTranslateUnits) {
240-
this.autoTranslateUnits = autoTranslateUnits;
241-
return this;
242-
}
243-
244230
public Builder engineMode(EngineMode em) {
245231
engineMode = em;
246232
return this;
@@ -363,8 +349,7 @@ public LKQLOptions build() {
363349
keepGoingOnMissingFile,
364350
showInstantiationChain,
365351
diagnosticOutputMode,
366-
autoFixMode,
367-
new HashSet<>(autoTranslateUnits)
352+
autoFixMode
368353
);
369354
}
370355
}

testsuite/drivers/interpreter_driver.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class InterpreterDriver(BaseDriver):
1616
- input_sources: A list of Ada sources to run analyze with LKQL
1717
- lkql_path: A list of directories forwarded to the `LKQL_PATH`
1818
variable when the test is run.
19+
- lkt_refactor: Should the test try refactor to Lkt syntax (default is False).
1920
"""
2021

2122
perf_supported = True
@@ -24,37 +25,51 @@ class InterpreterDriver(BaseDriver):
2425
def base_args(self):
2526
return self.lkql_exe
2627

27-
def run(self) -> None:
28+
def build_args(self, script_path):
2829
# Build the process's arguments list
29-
args = [*self.base_args(), "--script-path", "script.lkql"]
30-
31-
auto_rewrite_sources = self.test_env.get('to_lkt', None)
30+
args = [*self.base_args(), '--script-path', script_path]
3231

3332
input_sources = self.test_env.get('input_sources', None)
3433
project = self.test_env.get('project', None)
35-
lkql_path = [
36-
self.working_dir(d)
37-
for d in self.test_env.get('lkql_path', [])
38-
]
3934

4035
if project:
4136
args += ['-P', project]
4237

4338
if input_sources:
4439
args += input_sources
4540

46-
# If auto_rewrite_sources are present, then we run the interpreter
47-
# twice, once with rewrite, and once without, so that we can compare
48-
# the results.
49-
if auto_rewrite_sources:
50-
self.output += "lkql to lkt\n"
51-
self.output += "===========\n"
52-
new_args = args + ["--to-lkt"] + auto_rewrite_sources
53-
self.check_run(new_args)
41+
return args
42+
43+
44+
def run(self) -> None:
45+
46+
lkql_path = [
47+
self.working_dir(d)
48+
for d in self.test_env.get('lkql_path', [])
49+
]
50+
51+
# If lkt_refactor flag is set, then before we run the interpreter,
52+
# we refactor the "script.lkql" file and run the interpreter on it.
53+
# This way we can compare the results of the rewritten script
54+
# with the original
55+
if self.test_env.get('lkt_refactor', False):
56+
self.output += 'lkql to lkt\n'
57+
self.output += '===========\n'
58+
59+
# Translate "script.lkql" to "refactored.lkt"
60+
refactored_file_path = self.working_dir('refactored.lkql')
61+
with open(refactored_file_path, 'w') as file:
62+
file.write(self.shell(
63+
[*self.command_base, 'refactor', '-r', 'TO_LKQL_V2', 'script.lkql'],
64+
analyze_output=False
65+
).out)
66+
67+
# Run test on refactored script
68+
self.check_run(self.build_args(refactored_file_path), lkql_path=os.pathsep.join(lkql_path))
5469

55-
self.output += "\n"
56-
self.output += "lkql run\n"
57-
self.output += "========\n"
70+
self.output += '\n'
71+
self.output += 'lkql run\n'
72+
self.output += '========\n'
5873

5974
# Run the interpreter
60-
self.check_run(args, lkql_path=os.pathsep.join(lkql_path))
75+
self.check_run(self.build_args('script.lkql'), lkql_path=os.pathsep.join(lkql_path))

testsuite/tests/interpreter/val_expr/test.out

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
lkql to lkt
22
===========
3-
Translating script.lkql to lkt
43
42
54

65
lkql run
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
driver: 'interpreter'
22
project: 'default_project/default.gpr'
3-
to_lkt:
4-
- script
3+
lkt_refactor: True

0 commit comments

Comments
 (0)