Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relaxed CodeLocationsTest and co. #3849

Merged
merged 7 commits into from
Nov 4, 2022
184 changes: 149 additions & 35 deletions engine/runtime/src/main/java/org/enso/compiler/TreeToIr.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,9 @@ Object getMembers(boolean includeInternal) {
MethodNames.Module.GET_ASSOCIATED_TYPE,
MethodNames.Module.EVAL_EXPRESSION);
}

@Override
public String toString() {
return "Module[" + name + ']';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ object IR {
|rename = $rename,
|onlyNames = $onlyNames,
|hiddenNames = $hiddenNames,
|isAll = $isAll,
|location = $location,
|passData = ${this.showPassData},
|diagnostics = $diagnostics,
Expand Down Expand Up @@ -7482,8 +7483,9 @@ object IR {
@annotation.nowarn
override val location: Option[IdentifiedLocation] =
at match {
case ast: AST => ast.location.map(IdentifiedLocation(_, ast.id))
case _ => None
case ast: AST => ast.location.map(IdentifiedLocation(_, ast.id))
case loc: IdentifiedLocation => Some(loc)
case _ => None
}

/** @inheritdoc */
Expand Down
229 changes: 199 additions & 30 deletions engine/runtime/src/test/java/org/enso/compiler/EnsoCompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,57 @@ public void testParseMain7Foo() throws Exception {
""");
}

@Test
public void testLocationsSimpleArithmeticExpression() throws Exception {
parseTest("""
main = 2 + 45 * 20
""", true, false, true);
}

@Test
public void testLocationsApplicationsAndMethodCalls() throws Exception {
parseTest("""
main = (2-2 == 0).if_then_else (Cons 5 6) 0
""", true, false, true);
}

@Test
public void testLocationsCorrectAssignmentOfVariableReads() throws Exception {
parseTest("""
main =
x = 2 + 2 * 2
y = x * x
IO.println y
""", true, false, true);
}

@Test
public void testLocationsDeeplyNestedFunctions() throws Exception {
parseTest("""
foo = a -> b ->
IO.println a
""", true, false, true);
}

@Test
public void testLocationsDeeplyNestedFunctionsNoBlock() throws Exception {
parseTest("""
Nothing.method =
add = a -> b -> a + b

main = Nothing.method
""", true, false, true);
}

@Test
@Ignore
public void testSpacesAtTheEndOfFile() throws Exception {
var fourSpaces = " ";
parseTest("""
main = add_ten 5
""" + fourSpaces);
}

@Test
public void testCase() throws Exception {
parseTest("""
Expand Down Expand Up @@ -86,6 +137,13 @@ public void testImportAll() throws Exception {
""");
}

@Test
public void testImportTrue() throws Exception {
parseTest("""
from Standard.Base import True
""");
}

@Test
public void testMeaningOfWorld() throws Exception {
parseTest("""
Expand Down Expand Up @@ -483,6 +541,27 @@ public void testTestGroup() throws Exception {
""");
}

@Test
public void testEmptyGroup() throws Exception {
parseTest("""
main =
x = Panic.catch_primitive () .convert_to_dataflow_error
x.catch_primitive err->
case err of
Syntax_Error_Data msg -> "Oopsie, it's a syntax error: " + msg
""");
}

@Test
public void testEmptyGroup2AndAtSymbol() throws Exception {
parseTest("""
main =
x = ()
x = 5
y = @
""");
}

@Test
public void testTestGroupSimple() throws Exception {
parseTest("""
Expand All @@ -493,6 +572,15 @@ public void testTestGroupSimple() throws Exception {
""");
}

@Test
public void testNotAnOperator() throws Exception {
parseTest("""
main =
x = Panic.catch_primitive @ caught_panic-> caught_panic.payload
x.to_text
""");
}

@Test
public void testWildcardLeftHandSide() throws Exception {
parseTest("""
Expand Down Expand Up @@ -788,6 +876,21 @@ public void testAutoScope() throws Exception {
""");
}

@Test
public void testAutoScope2() throws Exception {
parseTest("""
fn1 = fn ...
fn2 = fn 1 ...
""");
}

@Test
public void testForcedTerms() throws Exception {
parseTest("""
ifTest = c -> (~ifT) -> ~ifF -> if c == 0 then ifT else ifF
""");
}

@Test
public void testTextArrayType() throws Exception {
parseTest("""
Expand Down Expand Up @@ -894,6 +997,13 @@ public void testVectorVector() throws Exception {
""");
}

@Test
public void testSidesPlus() throws Exception {
parseTest("""
result = reduce (+)
""");
}

@Test
public void testConstructorMultipleNamedArgs1() throws Exception {
parseTest("""
Expand Down Expand Up @@ -925,56 +1035,115 @@ public void testMethodSections() throws Exception {
""");
}

static String simplifyIR(IR i) {
var txt = i.pretty().replaceAll("id = [0-9a-f\\-]*", "id = _");
for (;;) {
final String pref = "IdentifiedLocation(";
int at = txt.indexOf(pref);
if (at == -1) {
break;
}
int to = at + pref.length();
int depth = 1;
while (depth > 0) {
switch (txt.charAt(to)) {
case '(' -> depth++;
case ')' -> depth--;
@Test
public void testGroupArgument() throws Exception {
parseTest("""
foo = x -> (y = bar x) -> x + y
""");
}

@Test
@Ignore
public void testResolveExecutionContext() throws Exception {
parseTest("""
foo : A -> B -> C in Input
""");
}

@Test
public void testSugaredFunctionDefinition() throws Exception {
parseTest("""
main =
f a b = a - b
f 10 20
""");
}

@Test
@Ignore
public void testInThePresenceOfComments() throws Exception {
parseTest("""
# this is a comment
#this too
## But this is a doc.
main = # define main
y = 1 # assign one to `y`
x = 2 # assign two to #x
# perform the addition
x + y # the addition is performed here
""");
}

static String simplifyIR(IR i, boolean noIds, boolean noLocations, boolean lessDocs) {
var txt = i.pretty();
if (noIds) {
txt = txt.replaceAll("id = [0-9a-f\\-]*", "id = _");
}
if (noLocations) {
for (;;) {
final String pref = "IdentifiedLocation(";
int at = txt.indexOf(pref);
if (at == -1) {
break;
}
int to = at + pref.length();
int depth = 1;
while (depth > 0) {
switch (txt.charAt(to)) {
case '(' -> depth++;
case ')' -> depth--;
}
to++;
}
txt = txt.substring(0, at) + "IdentifiedLocation[_]" + txt.substring(to);
}
to++;
}
txt = txt.substring(0, at) + "IdentifiedLocation[_]" + txt.substring(to);
}
for (;;) {
final String pref = "IR.Comment.Documentation(";
int at = txt.indexOf(pref);
if (at == -1) {
break;
}
int to = txt.indexOf("location =", at + pref.length());
txt = txt.substring(0, at) + "IR.Comment.Doc(" + txt.substring(to);
if (lessDocs) {
for (;;) {
final String pref = "IR.Comment.Documentation(";
int at = txt.indexOf(pref);
if (at == -1) {
break;
}
int to = txt.indexOf("location =", at + pref.length());
txt = txt.substring(0, at) + "IR.Comment.Doc(" + txt.substring(to);
}
for (;;) {
final String pref = "IR.Case.Pattern.Doc(";
int at = txt.indexOf(pref);
if (at == -1) {
break;
}
int to = txt.indexOf("location =", at + pref.length());
txt = txt.substring(0, at) + "IR.Comment.CaseDoc(" + txt.substring(to);
}
}
for (;;) {
final String pref = "IR.Case.Pattern.Doc(";
final String pref = "IR.Error.Syntax(";
int at = txt.indexOf(pref);
if (at == -1) {
break;
}
int to = txt.indexOf("location =", at + pref.length());
txt = txt.substring(0, at) + "IR.Comment.CaseDoc(" + txt.substring(to);
int to = txt.indexOf("reason =", at + pref.length());
txt = txt.substring(0, at) + "IR.Error.Syntax (" + txt.substring(to);
}
return txt;
}

private static void parseTest(String code) throws IOException {
parseTest(code, true, true, true);
}

@SuppressWarnings("unchecked")
static void parseTest(String code) throws IOException {
private static void parseTest(String code, boolean noIds, boolean noLocations, boolean lessDocs) throws IOException {
var src = Source.newBuilder("enso", code, "test-" + Integer.toHexString(code.hashCode()) + ".enso").build();
var ir = ensoCompiler.compile(src);
assertNotNull("IR was generated", ir);

var oldAst = new Parser().runWithIds(src.getCharacters().toString());
var oldIr = AstToIr.translate((ASTOf<Shape>)(Object)oldAst);

Function<IR, String> filter = EnsoCompilerTest::simplifyIR;
Function<IR, String> filter = (f) -> simplifyIR(f, noIds, noLocations, lessDocs);

var old = filter.apply(oldIr);
var now = filter.apply(ir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void parseTest(Source src, boolean generate) throws IOException {
var oldAst = new Parser().runWithIds(src.getCharacters().toString());
var oldIr = AstToIr.translate((ASTOf<Shape>) (Object) oldAst);

Function<IR, String> filter = EnsoCompilerTest::simplifyIR;
Function<IR, String> filter = (f) -> EnsoCompilerTest.simplifyIR(f, true, true, true);

var old = filter.apply(oldIr);
var now = filter.apply(ir);
Expand Down
Loading