Search in sources :

Example 6 with MagikType

use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType 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");
}
Also used : SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Example 7 with MagikType

use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType 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");
}
Also used : SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Example 8 with MagikType

use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.

the class LocalTypeReasonerTest method testIterableExpressionIterTypeAnnotation.

@Test
void testIterableExpressionIterTypeAnnotation() throws IOException {
    final String code = "" + "_method object.test\n" + "    _for a, b _over fn()  # iter-type: sw:integer, sw:float\n" + "    _loop\n" + "      _return a, b" + "    _endloop\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    // 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 forVariablesNode = topNode.getFirstDescendant(MagikGrammar.FOR_VARIABLES);
    final AstNode aNode = forVariablesNode.getDescendants(MagikGrammar.IDENTIFIER).get(0);
    final ExpressionResult aResult = reasoner.getNodeType(aNode);
    assertThat(aResult.size()).isEqualTo(1);
    final MagikType aResultType = (MagikType) aResult.get(0, null);
    assertThat(aResultType).isNotNull();
    assertThat(aResultType.getFullName()).isEqualTo("sw:integer");
    final AstNode bNode = forVariablesNode.getDescendants(MagikGrammar.IDENTIFIER).get(1);
    final ExpressionResult bResult = reasoner.getNodeType(bNode);
    assertThat(bResult.size()).isEqualTo(1);
    final MagikType bResultType = (MagikType) bResult.get(0, null);
    assertThat(bResultType).isNotNull();
    assertThat(bResultType.getFullName()).isEqualTo("sw:float");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Example 9 with MagikType

use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType 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);
}
Also used : SlottedType(nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType) ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Example 10 with MagikType

use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.

the class LocalTypeReasonerTest method testLoopStatement.

@Test
void testLoopStatement() {
    final String code = "" + "_method object.test\n" + "    _return _for i _over 1.upto(10)\n" + "            _loop\n" + "                _leave _with 1\n" + "            _endloop\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final MagikType integerType = (MagikType) typeKeeper.getType(GlobalReference.of("sw:integer"));
    integerType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "upto()", Collections.emptyList(), null, new ExpressionResult(), new ExpressionResult(integerType));
    // 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:integer");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Aggregations

MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)64 Test (org.junit.jupiter.api.Test)47 AstNode (com.sonar.sslr.api.AstNode)33 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)32 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)28 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)22 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)20 TypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.TypeKeeper)18 SlottedType (nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType)17 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)16 Method (nl.ramsolutions.sw.magik.analysis.typing.types.Method)14 Location (nl.ramsolutions.sw.magik.analysis.Location)10 IntrinsicType (nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType)10 Parameter (nl.ramsolutions.sw.magik.analysis.typing.types.Parameter)9 Position (org.eclipse.lsp4j.Position)9 IndexedType (nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType)8 Path (java.nio.file.Path)7 URI (java.net.URI)6 Collections (java.util.Collections)5 EnumSet (java.util.EnumSet)5