Search in sources :

Example 26 with ExpressionResult

use of nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult 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)

Example 27 with ExpressionResult

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

the class LocalTypeReasonerTest method testMergeReturnTypes.

@Test
void testMergeReturnTypes() {
    final String code = "" + "_method e1.m1\n" + "  _return 1\n" + "  _return x()\n" + "_endmethod";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    // Do analysis.
    final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
    final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
    final AstNode topNode = magikFile.getTopNode();
    final AstNode methodDefNode = topNode.getFirstDescendant(MagikGrammar.METHOD_DEFINITION);
    final ExpressionResult result = reasoner.getNodeType(methodDefNode);
    assertThat(result).isNotNull();
    // First is integer + undefined.
    final AbstractType resultType0 = result.get(0, null);
    assertThat(resultType0).isNotNull().isInstanceOf(CombinedType.class);
    assertThat(resultType0.getFullName()).isEqualTo("_undefined|sw:integer");
    // The rest is unset + undefined, as merging the two yields unset for the rest.
    result.getTypes().stream().skip(1).forEach(type -> {
        assertThat(type).isNotNull().isInstanceOf(CombinedType.class);
        assertThat(type.getFullName()).isEqualTo("_undefined|sw:unset");
    });
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Example 28 with ExpressionResult

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

the class LocalTypeReasonerTest method testBinaryOperatorChained.

@Test
void testBinaryOperatorChained() {
    final String code = "" + "_method object.test\n" + "    _return 1 + :a + 'a'\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final AbstractType char16VectorType = typeKeeper.getType(GlobalReference.of("sw:char16_vector"));
    final AbstractType symbolType = typeKeeper.getType(GlobalReference.of("sw:symbol"));
    final AbstractType integerType = typeKeeper.getType(GlobalReference.of("sw:integer"));
    final BinaryOperator binOp1 = new BinaryOperator(BinaryOperator.Operator.valueFor("+"), integerType, symbolType, symbolType);
    typeKeeper.addBinaryOperator(binOp1);
    final BinaryOperator binOp2 = new BinaryOperator(BinaryOperator.Operator.valueFor("+"), symbolType, char16VectorType, char16VectorType);
    typeKeeper.addBinaryOperator(binOp2);
    // 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:char16_vector");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) 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 29 with ExpressionResult

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

the class LocalTypeReasonerTest method testReasonMethodMultiReturnDifferent.

@Test
void testReasonMethodMultiReturnDifferent() {
    final String code = "" + "_package sw\n" + "_method object.test\n" + "    _block\n" + "        _return 1\n" + "    _endblock\n" + "    _return :a\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 methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
    final ExpressionResult result = reasoner.getNodeType(methodNode);
    assertThat(result.size()).isEqualTo(1);
    final AbstractType resultType = (AbstractType) result.get(0, null);
    assertThat(resultType).isNotNull();
    assertThat(resultType.getFullName()).isEqualTo("sw:integer|sw:symbol");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Example 30 with ExpressionResult

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

the class LocalTypeReasonerTest method testTryWith.

@Test
void testTryWith() {
    final String code = "" + "_method object.test\n" + "    _try _with cond\n" + "    _when warning\n" + "    _when error\n" + "    _endtry\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();
    final AstNode topNode = magikFile.getTopNode();
    final AstNode tryVarNode = topNode.getFirstDescendant(MagikGrammar.TRY_VARIABLE);
    final ExpressionResult result = reasoner.getNodeType(tryVarNode);
    assertThat(result).isNotNull();
    final MagikType resultType = (MagikType) result.get(0, null);
    assertThat(resultType).isNotNull();
    assertThat(resultType.getFullName()).isEqualTo("sw:condition");
}
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)

Aggregations

ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)78 AstNode (com.sonar.sslr.api.AstNode)68 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)41 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)37 Test (org.junit.jupiter.api.Test)34 MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)29 GlobalScope (nl.ramsolutions.sw.magik.analysis.scope.GlobalScope)13 Scope (nl.ramsolutions.sw.magik.analysis.scope.Scope)13 Method (nl.ramsolutions.sw.magik.analysis.typing.types.Method)13 ScopeEntry (nl.ramsolutions.sw.magik.analysis.scope.ScopeEntry)12 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)12 MethodInvocationNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodInvocationNodeHelper)9 LocalTypeReasoner (nl.ramsolutions.sw.magik.analysis.typing.LocalTypeReasoner)9 MethodDefinitionNodeHelper (nl.ramsolutions.sw.magik.analysis.helpers.MethodDefinitionNodeHelper)8 SelfType (nl.ramsolutions.sw.magik.analysis.typing.types.SelfType)8 SlottedType (nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType)8 List (java.util.List)7 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)7 Parameter (nl.ramsolutions.sw.magik.analysis.typing.types.Parameter)7 UndefinedType (nl.ramsolutions.sw.magik.analysis.typing.types.UndefinedType)7