Search in sources :

Example 56 with ExpressionResult

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

the class LocalTypeReasonerTest method testReasonMethodMultiReturnSame.

@Test
void testReasonMethodMultiReturnSame() {
    final String code = "" + "_package sw\n" + "_method object.test\n" + "    _return 1\n" + "    _return 2\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");
}
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 57 with ExpressionResult

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

the class LocalTypeReasonerTest method testSelfNewInit.

@Test
void testSelfNewInit() {
    final String code = "" + "_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"));
    typeKeeper.addType(ropeType);
    ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(SelfType.INSTANCE));
    // 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 58 with ExpressionResult

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

the class LocalTypeReasonerTest method testSelf.

// region: self
@Test
void testSelf() {
    final String code = "" + "_package sw\n" + "_method object.test\n" + "    _return _self.test1\n" + "_endmethod\n";
    // Set up TypeKeeper/TypeReasoner.
    final TypeKeeper typeKeeper = new TypeKeeper();
    final MagikType objectType = (MagikType) typeKeeper.getType(GlobalReference.of("sw:object"));
    objectType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "test1", Collections.emptyList(), null, new ExpressionResult(SelfType.INSTANCE));
    // 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 SelfType resultType = (SelfType) result.get(0, null);
    assertThat(resultType).isNotNull();
    assertThat(resultType.getFullName()).isEqualTo(SelfType.SERIALIZED_NAME);
}
Also used : SelfType(nl.ramsolutions.sw.magik.analysis.typing.types.SelfType) 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 59 with ExpressionResult

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

the class LocalTypeReasonerTest method testGatherParameters.

@Test
void testGatherParameters() {
    final String code = "" + "_method object.test(_gather args)\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 parameterNode = topNode.getFirstDescendant(MagikGrammar.PARAMETER);
    final ExpressionResult result = reasoner.getNodeType(parameterNode);
    final MagikType resultType = (MagikType) result.get(0, null);
    assertThat(resultType).isNotNull();
    assertThat(resultType.getFullName()).isEqualTo("sw:simple_vector");
}
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 60 with ExpressionResult

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

the class LocalTypeReasonerTest method testMultipleAssignmentStatement.

@Test
void testMultipleAssignmentStatement() {
    final String code = "" + "_method object.test\n" + "    (a, b) << (2, 3)\n" + "    _return b\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 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) 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