Skip to content

Commit 5386a8b

Browse files
Convert annotations provided by the new parser to IR (#3784)
Builds on top of #3780 and converts the new `Tree.Annotated` to IR. [ci no changelog needed]
1 parent b3dd778 commit 5386a8b

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

engine/runtime/src/main/java/org/enso/compiler/TreeToIr.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.enso.compiler.core.IR$Module$Scope$Import$Module;
3131
import org.enso.compiler.core.IR$Module$Scope$Import$Polyglot;
3232
import org.enso.compiler.core.IR$Module$Scope$Import$Polyglot$Java;
33+
import org.enso.compiler.core.IR$Name$Annotation;
3334
import org.enso.compiler.core.IR$Name$Blank;
3435
import org.enso.compiler.core.IR$Name$Literal;
3536
import org.enso.compiler.core.IR$Name$MethodReference;
@@ -151,6 +152,12 @@ IR.Module translateModule(Tree module) {
151152
var t = translateComment(app);
152153
bindings = cons(t, bindings);
153154
}
155+
case Tree.Annotated anno -> {
156+
var n = new IR$Name$Annotation("@" + anno.getAnnotation().codeRepr(), getIdentifiedLocation(anno), meta(), diag());
157+
bindings = cons(n, bindings);
158+
var t = translateModuleSymbol(anno.getExpression());
159+
bindings = cons(t, bindings);
160+
}
154161
case null -> {
155162
}
156163
default -> {
@@ -404,7 +411,7 @@ case Left(reason) =>
404411
}
405412
*/
406413
}
407-
default -> new IR$Error$Syntax(inputAst, new IR$Error$Syntax$UnexpectedExpression$(), meta(), diag());
414+
default -> new IR$Error$Syntax(inputAst, new IR$Error$Syntax$UnexpectedExpression$(), meta(), diag());
408415
};
409416
}
410417

@@ -817,6 +824,10 @@ yield switch (ast) {
817824
case Tree.Wildcard wild -> {
818825
yield new IR$Name$Blank(getIdentifiedLocation(wild), meta(), diag());
819826
}
827+
case Tree.Annotated anno -> {
828+
var ir = new IR$Name$Annotation("@" + anno.getAnnotation().codeRepr(), getIdentifiedLocation(anno), meta(), diag());
829+
yield translateAnnotation(ir, anno.getExpression(), nil());
830+
}
820831
default -> throw new UnhandledEntity(tree, "translateExpression");
821832
};
822833
/*
@@ -947,6 +958,30 @@ yield switch (ast) {
947958
*/
948959
}
949960

961+
private IR$Application$Prefix translateAnnotation(IR$Name$Annotation ir, Tree expr, List<IR.CallArgument> callArgs) {
962+
boolean insideTypeSignature = false;
963+
return switch (expr) {
964+
case Tree.App fn -> {
965+
var fnAsArg = translateCallArgument(fn.getArg(), insideTypeSignature);
966+
yield translateAnnotation(ir, fn.getFunc(), cons(fnAsArg, callArgs));
967+
}
968+
case Tree.ArgumentBlockApplication fn -> {
969+
var fnAsArg = translateCallArgument(fn.getLhs(), insideTypeSignature);
970+
var arg = translateCallArgument(expr, insideTypeSignature);
971+
callArgs = cons(fnAsArg, cons(arg, callArgs));
972+
yield translateAnnotation(ir, null, callArgs);
973+
}
974+
case null -> {
975+
yield new IR$Application$Prefix(ir, callArgs, false, ir.location(), meta(), diag());
976+
}
977+
default -> {
978+
var arg = translateCallArgument(expr, insideTypeSignature);
979+
callArgs = cons(arg, callArgs);
980+
yield translateAnnotation(ir, null, callArgs);
981+
}
982+
};
983+
}
984+
950985
IR.Expression translateDecimalLiteral(Tree.Number ast) {
951986
return translateDecimalLiteral(ast, ast.getInteger(), ast.getFractionalDigits());
952987
}

engine/runtime/src/test/java/org/enso/compiler/EnsoCompilerTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,51 @@ public void testComments() throws Exception {
103103
""");
104104
}
105105

106+
@Test
107+
public void testAnnotation0() throws Exception {
108+
parseTest("""
109+
dont_stop = @Tail_Call dont_stop
110+
""");
111+
}
112+
113+
@Test
114+
public void testAnnotation1() throws Exception {
115+
parseTest("""
116+
go t = @Tail_Call go t-1
117+
""");
118+
}
119+
120+
@Test
121+
public void testAnnotation2() throws Exception {
122+
parseTest("""
123+
go t x = @Tail_Call go t-1 x
124+
""");
125+
}
126+
127+
@Test
128+
public void testAnnotationBlock() throws Exception {
129+
parseTest("""
130+
go a b = @Tail_Call go
131+
a
132+
b
133+
""");
134+
}
135+
136+
@Test
137+
public void testBuiltinTypeAnnotation() throws Exception {
138+
parseTest("""
139+
@Builtin_Type
140+
type Date
141+
""");
142+
}
143+
144+
@Test
145+
public void testBuiltinMethodAnnotation() throws Exception {
146+
parseTest("""
147+
normalize x = @Builtin_Method "File.normalize"
148+
""");
149+
}
150+
106151
@Test
107152
@Ignore
108153
public void testMetadataRaw() throws Exception {

0 commit comments

Comments
 (0)