Skip to content

Commit

Permalink
Convert annotations provided by the new parser to IR (#3784)
Browse files Browse the repository at this point in the history
Builds on top of #3780 and converts the new `Tree.Annotated` to IR.
[ci no changelog needed]
  • Loading branch information
JaroslavTulach authored Oct 10, 2022
1 parent b3dd778 commit 5386a8b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
37 changes: 36 additions & 1 deletion engine/runtime/src/main/java/org/enso/compiler/TreeToIr.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.enso.compiler.core.IR$Module$Scope$Import$Module;
import org.enso.compiler.core.IR$Module$Scope$Import$Polyglot;
import org.enso.compiler.core.IR$Module$Scope$Import$Polyglot$Java;
import org.enso.compiler.core.IR$Name$Annotation;
import org.enso.compiler.core.IR$Name$Blank;
import org.enso.compiler.core.IR$Name$Literal;
import org.enso.compiler.core.IR$Name$MethodReference;
Expand Down Expand Up @@ -151,6 +152,12 @@ IR.Module translateModule(Tree module) {
var t = translateComment(app);
bindings = cons(t, bindings);
}
case Tree.Annotated anno -> {
var n = new IR$Name$Annotation("@" + anno.getAnnotation().codeRepr(), getIdentifiedLocation(anno), meta(), diag());
bindings = cons(n, bindings);
var t = translateModuleSymbol(anno.getExpression());
bindings = cons(t, bindings);
}
case null -> {
}
default -> {
Expand Down Expand Up @@ -404,7 +411,7 @@ case Left(reason) =>
}
*/
}
default -> new IR$Error$Syntax(inputAst, new IR$Error$Syntax$UnexpectedExpression$(), meta(), diag());
default -> new IR$Error$Syntax(inputAst, new IR$Error$Syntax$UnexpectedExpression$(), meta(), diag());
};
}

Expand Down Expand Up @@ -817,6 +824,10 @@ yield switch (ast) {
case Tree.Wildcard wild -> {
yield new IR$Name$Blank(getIdentifiedLocation(wild), meta(), diag());
}
case Tree.Annotated anno -> {
var ir = new IR$Name$Annotation("@" + anno.getAnnotation().codeRepr(), getIdentifiedLocation(anno), meta(), diag());
yield translateAnnotation(ir, anno.getExpression(), nil());
}
default -> throw new UnhandledEntity(tree, "translateExpression");
};
/*
Expand Down Expand Up @@ -947,6 +958,30 @@ yield switch (ast) {
*/
}

private IR$Application$Prefix translateAnnotation(IR$Name$Annotation ir, Tree expr, List<IR.CallArgument> callArgs) {
boolean insideTypeSignature = false;
return switch (expr) {
case Tree.App fn -> {
var fnAsArg = translateCallArgument(fn.getArg(), insideTypeSignature);
yield translateAnnotation(ir, fn.getFunc(), cons(fnAsArg, callArgs));
}
case Tree.ArgumentBlockApplication fn -> {
var fnAsArg = translateCallArgument(fn.getLhs(), insideTypeSignature);
var arg = translateCallArgument(expr, insideTypeSignature);
callArgs = cons(fnAsArg, cons(arg, callArgs));
yield translateAnnotation(ir, null, callArgs);
}
case null -> {
yield new IR$Application$Prefix(ir, callArgs, false, ir.location(), meta(), diag());
}
default -> {
var arg = translateCallArgument(expr, insideTypeSignature);
callArgs = cons(arg, callArgs);
yield translateAnnotation(ir, null, callArgs);
}
};
}

IR.Expression translateDecimalLiteral(Tree.Number ast) {
return translateDecimalLiteral(ast, ast.getInteger(), ast.getFractionalDigits());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,51 @@ public void testComments() throws Exception {
""");
}

@Test
public void testAnnotation0() throws Exception {
parseTest("""
dont_stop = @Tail_Call dont_stop
""");
}

@Test
public void testAnnotation1() throws Exception {
parseTest("""
go t = @Tail_Call go t-1
""");
}

@Test
public void testAnnotation2() throws Exception {
parseTest("""
go t x = @Tail_Call go t-1 x
""");
}

@Test
public void testAnnotationBlock() throws Exception {
parseTest("""
go a b = @Tail_Call go
a
b
""");
}

@Test
public void testBuiltinTypeAnnotation() throws Exception {
parseTest("""
@Builtin_Type
type Date
""");
}

@Test
public void testBuiltinMethodAnnotation() throws Exception {
parseTest("""
normalize x = @Builtin_Method "File.normalize"
""");
}

@Test
@Ignore
public void testMetadataRaw() throws Exception {
Expand Down

0 comments on commit 5386a8b

Please sign in to comment.