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));
}
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);
}
}
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));
}
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));
}
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;
}
}
Aggregations