Search in sources :

Example 1 with IntLiteral

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

use of org.abs_models.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(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)

Example 3 with IntLiteral

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

Example 4 with IntLiteral

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

the class AnnotationUtil method addToAnnotations.

/**
 * Add an ExpansionCall annotation, or an argument to an existing
 * annotation.  Creates or adds to [ExpansionCall : list[expansionId]]
 * annotation.
 *
 * @param annotations The list to mutate
 * @param annotationType Currently always EXPANSION_CALL
 * @param expansionId An integer to add to the list.
 */
private static void addToAnnotations(List<Annotation> annotations, TypeIdUse annotationType, int expansionId) {
    IntLiteral indexLiteral = new IntLiteral(Integer.toString(expansionId));
    Annotation toAdd = getAnnotation(annotations, annotationType);
    if (toAdd == null) {
        List<PureExp> llist = new List<>(new ListLiteral(new List<PureExp>(indexLiteral)));
        toAdd = new TypedAnnotation(new FnApp("list", llist), annotationType);
        annotations.add(toAdd);
    } else {
        PureExp value = toAdd.getValue();
        if (!(value instanceof FnApp)) {
            throw new IllegalArgumentException("Annotation list contains invalid expansion annotation");
        }
        FnApp fvalue = (FnApp) value;
        if (!fvalue.getName().equals("list")) {
            throw new IllegalArgumentException("Annotation list contains invalid expansion annotation");
        }
        ListLiteral list = (ListLiteral) fvalue.getParam(0);
        for (PureExp exp : list.getPureExps()) {
            if (exp instanceof IntLiteral) {
                IntLiteral intLiteral = (IntLiteral) exp;
                if (intLiteral.getContent().equals(indexLiteral.getContent())) {
                    return;
                }
            }
        }
        list.addPureExp(indexLiteral);
    }
}
Also used : ListLiteral(org.abs_models.frontend.ast.ListLiteral) FnApp(org.abs_models.frontend.ast.FnApp) IntLiteral(org.abs_models.frontend.ast.IntLiteral) List(org.abs_models.frontend.ast.List) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation) PureExp(org.abs_models.frontend.ast.PureExp) Annotation(org.abs_models.frontend.ast.Annotation) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation)

Example 5 with IntLiteral

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

the class DeployInformationClass method addAnn.

public void addAnn(PureExp exp) {
    // A specification is a list (hence a FnApp with first argument being a list)
    DeployInformationClassSpecification info = new DeployInformationClassSpecification(_paramList, _paramType);
    PureExp list = ((FnApp) exp).getParam(0);
    while (((DataConstructorExp) list).hasParam() && (((DataConstructorExp) list).getParam(0) != null)) {
        // means we have a cons
        PureExp el = ((DataConstructorExp) list).getParam(0);
        list = ((DataConstructorExp) list).getParam(1);
        if (((DataConstructorExp) el).getDataConstructor().getName().equals("Cost")) {
            String name = ((StringLiteral) ((DataConstructorExp) el).getParam(0)).getContent();
            int cost = Integer.parseInt(((IntLiteral) ((DataConstructorExp) el).getParam(1)).getContent());
            info.addCost(name, cost);
            System.out.println("  Annotation is Cost(\"" + name + "\", " + cost + ")");
        } else if (((DataConstructorExp) el).getDataConstructor().getName().equals("MaxUse")) {
            int arity = Integer.parseInt(((IntLiteral) ((DataConstructorExp) el).getParam(0)).getContent());
            info.setProvide(arity);
            System.out.println("  Annotation is MaxUse(" + arity + ")");
        } else if (((DataConstructorExp) el).getDataConstructor().getName().equals("Name")) {
            String name = ((StringLiteral) ((DataConstructorExp) el).getParam(0)).getContent();
            info.addScenarioName(name);
            System.out.println("  Annotation is Name(" + name + ")");
        } else if (((DataConstructorExp) el).getDataConstructor().getName().equals("Param")) {
            String param = ((StringLiteral) ((DataConstructorExp) el).getParam(0)).getContent();
            String port = _paramType.get(param);
            PureExp spec = ((DataConstructorExp) el).getParam(1);
            if (((DataConstructorExp) spec).getDataConstructor().getName().equals("Req")) {
                System.out.print("Req");
                info.addRequirement(param);
                System.out.println("  Annotation is Req(\"" + param + "\")");
            } else if (((DataConstructorExp) spec).getDataConstructor().getName().equals("List")) {
                int arity = Integer.parseInt(((IntLiteral) ((DataConstructorExp) spec).getParam(0)).getContent());
                info.addList(param, arity);
                System.out.println("  Annotation is List(\"" + port + "\", " + arity + ")");
            } else if (((DataConstructorExp) spec).getDataConstructor().getName().equals("OptList")) {
                String value = ((StringLiteral) ((DataConstructorExp) spec).getParam(0)).getContent();
                info.addOptList(param, value);
                System.out.println("  Annotation is OptList(\"" + port + "\", " + value + ")");
            } else if (((DataConstructorExp) spec).getDataConstructor().getName().equals("Default")) {
                System.out.print("Default");
                String value = ((StringLiteral) ((DataConstructorExp) spec).getParam(0)).getContent();
                if (port != null)
                    System.out.print("(\"" + port + "\", " + value + ")");
                info.addDefault(param, value);
                System.out.println("  Annotation is Default(\"" + port + "\", " + value + ")");
            } else if (((DataConstructorExp) spec).getDataConstructor().getName().equals("User")) {
                System.out.print("User");
                if (port != null)
                    System.out.print("(\"" + port + "\")");
                info.addUser(param, port);
                System.out.println("  Annotation is User(\"" + port + "\", " + param + ")");
            }
        }
    }
    _spec.add(info);
}
Also used : DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp) FnApp(org.abs_models.frontend.ast.FnApp) StringLiteral(org.abs_models.frontend.ast.StringLiteral) IntLiteral(org.abs_models.frontend.ast.IntLiteral) PureExp(org.abs_models.frontend.ast.PureExp)

Aggregations

IntLiteral (org.abs_models.frontend.ast.IntLiteral)6 PureExp (org.abs_models.frontend.ast.PureExp)5 Annotation (org.abs_models.frontend.ast.Annotation)3 FnApp (org.abs_models.frontend.ast.FnApp)3 TypedAnnotation (org.abs_models.frontend.ast.TypedAnnotation)3 FrontendTest (org.abs_models.frontend.FrontendTest)2 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)2 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)2 FunctionDef (org.abs_models.frontend.ast.FunctionDef)2 Model (org.abs_models.frontend.ast.Model)2 Test (org.junit.Test)2 AddExp (org.abs_models.frontend.ast.AddExp)1 DataConstructorExp (org.abs_models.frontend.ast.DataConstructorExp)1 List (org.abs_models.frontend.ast.List)1 ListLiteral (org.abs_models.frontend.ast.ListLiteral)1 StringLiteral (org.abs_models.frontend.ast.StringLiteral)1