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