Search in sources :

Example 6 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class TreeUtilsTest method findChildrenIncludeSelf.

@Test
public void findChildrenIncludeSelf() {
    Model model = assertParse("def Int test(Int i) = test(1);");
    FunctionDecl functionDecl = getLastFunctionDecl(model);
    assertEquals("test", functionDecl.getName());
    ExpFunctionDef def = (ExpFunctionDef) functionDecl.getFunctionDef();
    PureExp exp = def.getRhs();
    Stream<PureExp> children = def.findChildren(cast(PureExp.class), n -> true);
    assertNotNull(children);
    List<PureExp> result = children.distinct().collect(Collectors.toList());
    assertEquals(2, result.size());
    assertTrue(result.contains(exp));
}
Also used : Model(org.abs_models.frontend.ast.Model) PureExp(org.abs_models.frontend.ast.PureExp) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) ExpFunctionDef(org.abs_models.frontend.ast.ExpFunctionDef) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 7 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class TreeUtilsTest method findChildrenMultipleTypes.

@Test
public void findChildrenMultipleTypes() {
    Model model = assertParse("def Int test(Int i) = test(1);");
    FunctionDecl functionDecl = getLastFunctionDecl(model);
    assertEquals("test", functionDecl.getName());
    FunctionDef def = functionDecl.getFunctionDef();
    Stream<PureExp> children = def.findChildren(cast(ImmutableList.of(FnApp.class, IntLiteral.class)), n -> true);
    assertNotNull(children);
    List<PureExp> result = children.distinct().collect(Collectors.toList());
    assertEquals(2, result.size());
    for (PureExp exp : result) {
        assertTrue(exp instanceof FnApp || exp instanceof IntLiteral);
    }
}
Also used : FnApp(org.abs_models.frontend.ast.FnApp) Model(org.abs_models.frontend.ast.Model) ExpFunctionDef(org.abs_models.frontend.ast.ExpFunctionDef) FunctionDef(org.abs_models.frontend.ast.FunctionDef) IntLiteral(org.abs_models.frontend.ast.IntLiteral) PureExp(org.abs_models.frontend.ast.PureExp) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 8 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class TreeUtilsTest method closestParent.

@Test
public void closestParent() {
    Model model = assertParse("def Int test() = 1;");
    FunctionDecl functionDecl = getLastFunctionDecl(model);
    assertEquals("test", functionDecl.getName());
    PureExp exp = ((ExpFunctionDef) functionDecl.getFunctionDef()).getRhs();
    assertSame(functionDecl, exp.closestParent(Decl.class));
    assertSame(functionDecl, exp.closestParent(FunctionDecl.class));
}
Also used : Model(org.abs_models.frontend.ast.Model) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) Decl(org.abs_models.frontend.ast.Decl) PureExp(org.abs_models.frontend.ast.PureExp) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) ExpFunctionDef(org.abs_models.frontend.ast.ExpFunctionDef) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 9 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class TreeUtilsTest method findChildrenIncludeOnlySelf.

@Test
public void findChildrenIncludeOnlySelf() {
    Model model = assertParse("def Int test(Int i) = test(1);");
    FunctionDecl functionDecl = getLastFunctionDecl(model);
    assertEquals("test", functionDecl.getName());
    ExpFunctionDef def = (ExpFunctionDef) functionDecl.getFunctionDef();
    PureExp exp = def.getRhs();
    Stream<PureExp> children = def.findChildren(cast(PureExp.class), n -> false);
    assertNotNull(children);
    List<PureExp> result = children.distinct().collect(Collectors.toList());
    assertEquals(1, result.size());
    assertTrue(result.contains(exp));
}
Also used : Model(org.abs_models.frontend.ast.Model) PureExp(org.abs_models.frontend.ast.PureExp) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) ExpFunctionDef(org.abs_models.frontend.ast.ExpFunctionDef) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 10 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class AnnotationUtil method getExpansionId.

/**
 * Gets the expansion ID of a function declaration. If the function declaration is not an expansion, -1 is
 * returned.
 *
 * @param decl a function declaration
 * @return an expansion ID, or -1
 * @throws NullPointerException if decl is null
 */
public static int getExpansionId(FunctionDecl decl) {
    Objects.requireNonNull(decl);
    Annotation annotation = getAnnotation(decl.getAnnotationsNoTransform(), expansionType());
    if (annotation == null) {
        return -1;
    }
    PureExp value = annotation.getValue();
    if (value instanceof IntLiteral) {
        IntLiteral intValue = (IntLiteral) value;
        try {
            int result = Integer.parseInt(intValue.getContent());
            return result < 0 ? -1 : result;
        } catch (NumberFormatException e) {
            return -1;
        }
    } else {
        return -1;
    }
}
Also used : IntLiteral(org.abs_models.frontend.ast.IntLiteral) PureExp(org.abs_models.frontend.ast.PureExp) Annotation(org.abs_models.frontend.ast.Annotation) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation)

Aggregations

PureExp (org.abs_models.frontend.ast.PureExp)22 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)8 FrontendTest (org.abs_models.frontend.FrontendTest)7 Model (org.abs_models.frontend.ast.Model)7 Test (org.junit.Test)7 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)5 FnApp (org.abs_models.frontend.ast.FnApp)5 IntLiteral (org.abs_models.frontend.ast.IntLiteral)5 Annotation (org.abs_models.frontend.ast.Annotation)4 Type (org.abs_models.frontend.typechecker.Type)3 ABSBool (org.abs_models.backend.java.lib.types.ABSBool)2 TypeError (org.abs_models.frontend.analyser.TypeError)2 FunctionDef (org.abs_models.frontend.ast.FunctionDef)2 List (org.abs_models.frontend.ast.List)2 MethodSig (org.abs_models.frontend.ast.MethodSig)2 TypedAnnotation (org.abs_models.frontend.ast.TypedAnnotation)2 VarUse (org.abs_models.frontend.ast.VarUse)2 AddExp (org.abs_models.frontend.ast.AddExp)1 AsyncCall (org.abs_models.frontend.ast.AsyncCall)1 ClassDecl (org.abs_models.frontend.ast.ClassDecl)1