use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType in project magik-tools by StevenLooman.
the class MagikIndexer method handleDefinition.
private void handleDefinition(final MagikFile magikFile, final IndexedExemplarDefinition definition) {
final AstNode node = definition.getNode();
final GlobalReference globalRef = definition.getGlobalReference();
final MagikType type = this.typeKeeper.getType(globalRef) != UndefinedType.INSTANCE ? (MagikType) this.typeKeeper.getType(globalRef) : new IndexedType(globalRef);
this.typeKeeper.addType(type);
final Map<String, String> slots = Collections.emptyMap();
final List<String> parents = definition.getParents();
final MagikType defaultParentType = (MagikType) this.typeKeeper.getType(GlobalReference.of("sw:indexed_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.IndexedType in project magik-tools by StevenLooman.
the class TypeKeeper method registerRequiredTypes.
private void registerRequiredTypes() {
final Package swPakkage = this.getPackage("sw");
swPakkage.put("object", new ObjectType(GlobalReference.of("sw:object")));
swPakkage.put("unset", new IntrinsicType(GlobalReference.of("sw:unset")));
swPakkage.put("false", new IntrinsicType(GlobalReference.of("sw:false")));
swPakkage.put("maybe", new IntrinsicType(GlobalReference.of("sw:maybe")));
swPakkage.put("integer", new IntrinsicType(GlobalReference.of("sw:integer")));
swPakkage.put("bignum", new IntrinsicType(GlobalReference.of("sw:bignum")));
swPakkage.put("float", new IntrinsicType(GlobalReference.of("sw:float")));
swPakkage.put("symbol", new IndexedType(GlobalReference.of("sw:symbol")));
swPakkage.put("character", new IntrinsicType(GlobalReference.of("sw:character")));
swPakkage.put("sw_regexp", new IntrinsicType(GlobalReference.of("sw:sw_regexp")));
swPakkage.put("char16_vector", new IndexedType(GlobalReference.of("sw:char16_vector")));
swPakkage.put("simple_vector", new IndexedType(GlobalReference.of("sw:simple_vector")));
swPakkage.put("heavy_thread", new IntrinsicType(GlobalReference.of("sw:heavy_thread")));
swPakkage.put("light_thread", new IntrinsicType(GlobalReference.of("sw:light_thread")));
swPakkage.put("condition", new SlottedType(GlobalReference.of("sw:condition")));
swPakkage.put("enumeration_value", new SlottedType(GlobalReference.of("sw:enumeration_value")));
swPakkage.put("indexed_format_mixin", new IntrinsicType(GlobalReference.of("sw:indexed_format_mixin")));
swPakkage.put("slotted_format_mixin", new IntrinsicType(GlobalReference.of("sw:slotted_format_mixin")));
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testAssignmentMethod.
@Test
void testAssignmentMethod() {
final String code = "" + "_method object.test\n" + " _local a << property_list.new()\n" + " a[:1] << 10\n" + " _return a\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final MagikType propertyListType = new IndexedType(GlobalReference.of("sw:property_list"));
propertyListType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(SelfType.INSTANCE));
typeKeeper.addType(propertyListType);
// 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:property_list");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType in project magik-tools by StevenLooman.
the class JsonTypeKeeperReader method handleType.
private void handleType(final JSONObject instruction) {
final String typeFormat = instruction.getString("type_format");
final String name = instruction.getString("type_name");
final GlobalReference globalRef = GlobalReference.of(name);
final MagikType type;
if (this.typeKeeper.getType(globalRef) != UndefinedType.INSTANCE) {
type = (MagikType) this.typeKeeper.getType(globalRef);
} else if ("intrinsic".equals(typeFormat)) {
type = new IntrinsicType(globalRef);
} else if ("slotted".equals(typeFormat)) {
type = new SlottedType(globalRef);
} else if ("indexed".equals(typeFormat)) {
type = new IndexedType(globalRef);
} else {
throw new InvalidParameterException("Unknown type: " + typeFormat);
}
if (instruction.has("doc")) {
final String doc = instruction.getString("doc");
type.setDoc(doc);
}
instruction.getJSONArray("slots").forEach(slotObj -> {
final JSONObject slot = (JSONObject) slotObj;
final String slotName = slot.getString("name");
final Slot typeSlot = type.addSlot(null, slotName);
final String slotTypeName = slot.getString("type_name");
this.slotTypeNames.put(typeSlot, slotTypeName);
});
// Save inheritance for later...
final JSONArray parents = instruction.getJSONArray("parents");
this.typeParents.put(type, parents);
this.typeKeeper.addType(type);
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType in project magik-tools by StevenLooman.
the class MagikIndexer method fillType.
private void fillType(final MagikType magikType, final MagikFile magikFile, final AstNode node, final String packageName, final Map<String, String> slots, final List<String> parents, @Nullable final AbstractType defaultParentType) {
// Set location.
final URI uri = magikFile.getUri();
final Location location = new Location(uri, node);
magikType.setLocation(location);
// Set slots.
magikType.clearSlots();
slots.entrySet().forEach(entry -> {
final String slotName = entry.getKey();
final String slotTypeName = entry.getValue();
final AbstractType slotType = this.typeParser.parseTypeString(slotTypeName, packageName);
final Slot slot = magikType.addSlot(null, slotName);
slot.setType(slotType);
});
// Parents.
magikType.clearParents();
final PackageNodeHelper packageHelper = new PackageNodeHelper(node);
final String pakkageName = packageHelper.getCurrentPackage();
parents.stream().forEach(parent -> {
final GlobalReference parentGlobalRef = parent.indexOf(':') != -1 ? GlobalReference.of(parent) : GlobalReference.of(pakkageName, parent);
final AbstractType parentType = this.typeKeeper.getType(parentGlobalRef);
if (parentType == UndefinedType.INSTANCE) {
LOGGER.warn("Parent type not found: {} from package: {}", parent, pakkageName);
} else {
magikType.addParent(parentType);
}
});
// Default parent types ()
boolean parentNonIntrinsicType = magikType.getParents().stream().anyMatch(parentType -> parentType instanceof SlottedType || parentType instanceof IndexedType);
if (defaultParentType != null && !parentNonIntrinsicType) {
magikType.addParent(defaultParentType);
}
// Type doc.
final String typeDoc = MagikCommentExtractor.extractDocComments(node).map(Token::getValue).map(// Strip '##'
line -> line.substring(2)).collect(Collectors.joining("\n"));
magikType.setDoc(typeDoc);
}
Aggregations