Search in sources :

Example 1 with IntLiteral

use of abs.frontend.ast.IntLiteral 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 IntLiteral

use of abs.frontend.ast.IntLiteral 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 IntLiteral

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

the class AnnotationUtil method addToAnnotations.

private static void addToAnnotations(List<Annotation> annotations, Access annotationType, int expansionId) {
    IntLiteral indexLiteral = new IntLiteral(Integer.toString(expansionId));
    Annotation toAdd = getAnnotation(annotations, annotationType);
    if (toAdd == null) {
        toAdd = new TypedAnnotation(new ListLiteral(new List<>()), annotationType);
        annotations.add(toAdd);
    }
    PureExp value = toAdd.getValue();
    if (value instanceof ListLiteral) {
        ListLiteral list = (ListLiteral) value;
        for (PureExp exp : list.getPureExps()) {
            if (exp instanceof IntLiteral) {
                IntLiteral intLiteral = (IntLiteral) exp;
                if (intLiteral.getContent().equals(indexLiteral.getContent())) {
                    return;
                }
            }
        }
        list.addPureExp(indexLiteral);
    } else {
        throw new IllegalArgumentException("Annotation list contains invalid expansion annotation");
    }
}
Also used : ListLiteral(abs.frontend.ast.ListLiteral) IntLiteral(abs.frontend.ast.IntLiteral) TypedAnnotation(abs.frontend.ast.TypedAnnotation) PureExp(abs.frontend.ast.PureExp) Annotation(abs.frontend.ast.Annotation) TypedAnnotation(abs.frontend.ast.TypedAnnotation)

Example 4 with IntLiteral

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

the class AnnotationUtil method annotateExpansion.

public static void annotateExpansion(FunctionDecl expansion, int expansionId) {
    IntLiteral indexLiteral = new IntLiteral(Integer.toString(expansionId));
    Annotation annotation = new TypedAnnotation(indexLiteral, expansionType());
    expansion.addAnnotation(annotation);
}
Also used : IntLiteral(abs.frontend.ast.IntLiteral) TypedAnnotation(abs.frontend.ast.TypedAnnotation) Annotation(abs.frontend.ast.Annotation) TypedAnnotation(abs.frontend.ast.TypedAnnotation)

Example 5 with IntLiteral

use of abs.frontend.ast.IntLiteral 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(abs.frontend.ast.IntLiteral) PureExp(abs.frontend.ast.PureExp) Annotation(abs.frontend.ast.Annotation) TypedAnnotation(abs.frontend.ast.TypedAnnotation)

Aggregations

IntLiteral (abs.frontend.ast.IntLiteral)5 PureExp (abs.frontend.ast.PureExp)4 Annotation (abs.frontend.ast.Annotation)3 TypedAnnotation (abs.frontend.ast.TypedAnnotation)3 FrontendTest (abs.frontend.FrontendTest)2 ExpFunctionDef (abs.frontend.ast.ExpFunctionDef)2 FunctionDecl (abs.frontend.ast.FunctionDecl)2 FunctionDef (abs.frontend.ast.FunctionDef)2 Model (abs.frontend.ast.Model)2 Test (org.junit.Test)2 AddExp (abs.frontend.ast.AddExp)1 FnApp (abs.frontend.ast.FnApp)1 ListLiteral (abs.frontend.ast.ListLiteral)1