Search in sources :

Example 1 with FunctionDef

use of abs.frontend.ast.FunctionDef in project abstools by abstools.

the class TreeUtilsTest method findChildrenMultipleTypes.

@Test
public void findChildrenMultipleTypes() {
    Model model = assertParseOkStdLib("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(abs.frontend.ast.FnApp) Model(abs.frontend.ast.Model) FunctionDef(abs.frontend.ast.FunctionDef) ExpFunctionDef(abs.frontend.ast.ExpFunctionDef) IntLiteral(abs.frontend.ast.IntLiteral) PureExp(abs.frontend.ast.PureExp) FunctionDecl(abs.frontend.ast.FunctionDecl) Test(org.junit.Test) FrontendTest(abs.frontend.FrontendTest)

Example 2 with FunctionDef

use of abs.frontend.ast.FunctionDef in project abstools by abstools.

the class TreeUtilsTest method recurseForSome.

@Test
public void recurseForSome() {
    Model model = assertParseOkStdLib("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(abs.frontend.ast.AddExp) Model(abs.frontend.ast.Model) FunctionDef(abs.frontend.ast.FunctionDef) ExpFunctionDef(abs.frontend.ast.ExpFunctionDef) IntLiteral(abs.frontend.ast.IntLiteral) PureExp(abs.frontend.ast.PureExp) FunctionDecl(abs.frontend.ast.FunctionDecl) Test(org.junit.Test) FrontendTest(abs.frontend.FrontendTest)

Example 3 with FunctionDef

use of abs.frontend.ast.FunctionDef in project abstools by abstools.

the class TreeUtilsTest method closestParentIgnoresSelf.

@Test
public void closestParentIgnoresSelf() {
    Model model = assertParseOkStdLib("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(abs.frontend.ast.Model) ASTNode(abs.frontend.ast.ASTNode) FunctionDef(abs.frontend.ast.FunctionDef) ExpFunctionDef(abs.frontend.ast.ExpFunctionDef) FunctionDecl(abs.frontend.ast.FunctionDecl) Test(org.junit.Test) FrontendTest(abs.frontend.FrontendTest)

Aggregations

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