use of nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType in project magik-tools by StevenLooman.
the class MagikIndexer method handleDefinition.
private void handleDefinition(final MagikFile magikFile, final SlottedExemplarDefinition definition) {
final AstNode node = definition.getNode();
final GlobalReference globalRef = definition.getGlobalReference();
final MagikType type = this.typeKeeper.getType(globalRef) instanceof SlottedType ? (MagikType) this.typeKeeper.getType(globalRef) : new SlottedType(globalRef);
this.typeKeeper.addType(type);
final NewDocParser docParser = new NewDocParser(node);
final Map<String, String> slotTypes = docParser.getSlotTypes();
// This needs a default value ("") due to https://bugs.openjdk.java.net/browse/JDK-8148463
final Map<String, String> slots = definition.getSlots().stream().map(SlottedExemplarDefinition.Slot::getName).collect(Collectors.toMap(slotName -> slotName, slotName -> slotTypes.getOrDefault(slotName, "")));
final List<String> parents = definition.getParents();
final MagikType defaultParentType = (MagikType) this.typeKeeper.getType(GlobalReference.of("sw:slotted_format_mixin"));
this.fillType(type, magikFile, node, globalRef.getPakkage(), slots, parents, defaultParentType);
final Path path = Paths.get(magikFile.getUri());
this.indexedTypes.get(path).add(type);
LOGGER.debug("Indexed type: {}", type);
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testReasonVariable2.
@Test
void testReasonVariable2() {
final String code = "" + "_method object.test\n" + " _local (var1, var2) << rope.new(1)\n" + " var1.add(:a)\n" + " _return var1\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final MagikType ropeType = new SlottedType(GlobalReference.of("sw:rope"));
ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(ropeType));
ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "add()", Collections.emptyList(), null, new ExpressionResult());
typeKeeper.addType(ropeType);
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
// Assert user:object.test type determined.
final AstNode topNode = magikFile.getTopNode();
final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
final ExpressionResult result = reasoner.getNodeType(methodNode);
assertThat(result.size()).isEqualTo(1);
final MagikType resultType = (MagikType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:rope");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testReasonVariable.
@Test
void testReasonVariable() {
final String code = "" + "_method object.test\n" + " _local var << rope.new(1)\n" + " var.add(:a)\n" + " _return var\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final MagikType ropeType = new SlottedType(GlobalReference.of("sw:rope"));
ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(ropeType));
ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "add()", Collections.emptyList(), null, new ExpressionResult());
typeKeeper.addType(ropeType);
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
// Assert user:object.test type determined.
final AstNode topNode = magikFile.getTopNode();
final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
final ExpressionResult result = reasoner.getNodeType(methodNode);
assertThat(result.size()).isEqualTo(1);
final MagikType resultType = (MagikType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:rope");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testSingleNamedSuperType.
@Test
void testSingleNamedSuperType() {
final String code = "" + "_method t.m\n" + " _super(r).m\n" + "_endmethod";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final MagikType rType = new SlottedType(GlobalReference.of("sw", "r"));
typeKeeper.addType(rType);
final MagikType sType = new SlottedType(GlobalReference.of("sw", "s"));
typeKeeper.addType(sType);
final MagikType tType = new SlottedType(GlobalReference.of("sw", "t"));
tType.addParent(rType);
tType.addParent(sType);
typeKeeper.addType(tType);
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
final AstNode topNode = magikFile.getTopNode();
final AstNode superNode = topNode.getFirstDescendant(MagikGrammar.SUPER).getParent();
final ExpressionResult result = reasoner.getNodeType(superNode);
assertThat(result).isNotNull();
final AbstractType resultType0 = result.get(0, null);
assertThat(resultType0).isNotNull().isEqualTo(rType);
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testReasonMethodReturn.
@Test
void testReasonMethodReturn() {
final String code = "" + "_package sw\n" + "_method object.test\n" + " _return rope.new()\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final MagikType ropeType = new SlottedType(GlobalReference.of("sw:rope"));
ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(ropeType));
typeKeeper.addType(ropeType);
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
// Assert user:object.test type determined.
final AstNode topNode = magikFile.getTopNode();
final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
final ExpressionResult result = reasoner.getNodeType(methodNode);
assertThat(result.size()).isEqualTo(1);
final MagikType resultType = (MagikType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:rope");
}
Aggregations