Search in sources :

Example 1 with FunctionDef

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

the class TreeUtilsTest method closestParentIgnoresSelf.

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

Example 2 with FunctionDef

use of org.abs_models.frontend.ast.FunctionDef 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 3 with FunctionDef

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

the class TreeUtilsTest method recurseForSome.

@Test
public void recurseForSome() {
    Model model = assertParse("def Int test(Int i) = 1 + test(2);");
    FunctionDecl functionDecl = getLastFunctionDecl(model);
    assertEquals("test", functionDecl.getName());
    FunctionDef def = functionDecl.getFunctionDef();
    Stream<PureExp> children = def.findChildren(cast(PureExp.class), AddExp.class::isInstance);
    assertNotNull(children);
    List<PureExp> result = children.distinct().collect(Collectors.toList());
    // expecting AddExp, IntLiteral(1), FnApp, NOT IntLiteral(2)
    assertEquals(3, result.size());
    for (PureExp exp : result) {
        assertFalse(exp instanceof IntLiteral && ((IntLiteral) exp).getContent().equals("2"));
    }
}
Also used : AddExp(org.abs_models.frontend.ast.AddExp) 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)

Aggregations

FrontendTest (org.abs_models.frontend.FrontendTest)3 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)3 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)3 FunctionDef (org.abs_models.frontend.ast.FunctionDef)3 Model (org.abs_models.frontend.ast.Model)3 Test (org.junit.Test)3 IntLiteral (org.abs_models.frontend.ast.IntLiteral)2 PureExp (org.abs_models.frontend.ast.PureExp)2 ASTNode (org.abs_models.frontend.ast.ASTNode)1 AddExp (org.abs_models.frontend.ast.AddExp)1 FnApp (org.abs_models.frontend.ast.FnApp)1