Search in sources :

Example 1 with FnApp

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

the class ParFnAppTest method recursionWithClosure.

@Test
public void recursionWithClosure() {
    Model m = expand(parse("Int x = 0; Int y = 1; rec((Int i) => x, (Int j) => y)();", "def Int rec(f, g)() = rec();"));
    FnApp call = assertHasCall(m, expandedName("rec_%s_Anon\\d+__"));
    assertEquals(2, call.getNumParam());
}
Also used : FnApp(org.abs_models.frontend.ast.FnApp) Model(org.abs_models.frontend.ast.Model) Test(org.junit.Test)

Example 2 with FnApp

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

the class TreeUtilsTest method findChildrenListLazy.

@Test
public void findChildrenListLazy() {
    Model model = assertParse("def Int test(Int i) = test(1);");
    FunctionDecl functionDecl = getLastFunctionDecl(model);
    assertEquals("test", functionDecl.getName());
    List<PureExp> children = functionDecl.getFunctionDef().findChildren(PureExp.class, true);
    assertEquals(1, children.size());
    assertTrue(children.get(0) instanceof FnApp);
}
Also used : FnApp(org.abs_models.frontend.ast.FnApp) Model(org.abs_models.frontend.ast.Model) 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 3 with FnApp

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

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

the class TimeoutThread method genCode.

/**
 * Generates Erlang code in target directory, adding a last statement that
 * prints the value of the `testresult' variable.
 *
 * @return a Module Name containing a Main Block
 * @throws InternalBackendException
 */
public String genCode(Model model, File targetDir, boolean appendResultprinter) throws IOException, InterruptedException, InternalBackendException {
    if (model.hasErrors()) {
        Assert.fail(model.getErrors().getFirstError().getHelpMessage());
    }
    if (model.hasTypeErrors()) {
        Assert.fail(model.getTypeErrors().getFirstError().getHelpMessage());
    }
    MainBlock mb = model.getMainBlock();
    if (mb != null && appendResultprinter) {
        // We search for this output in the `run' method below
        mb.addStmt(new ExpressionStmt(new List<>(), new FnApp("ABS.StdLib.println", new List<>(new AddAddExp(new StringLiteral("RES="), new FnApp("ABS.StdLib.toString", new List<>(new VarUse("testresult"))))))));
    }
    new ErlangBackend().compile(model, targetDir, // use the following argument for silent compiler:
    EnumSet.noneOf(ErlangBackend.CompileOptions.class));
    if (mb == null)
        return null;
    else
        return mb.getModuleDecl().getName();
}
Also used : AddAddExp(org.abs_models.frontend.ast.AddAddExp) FnApp(org.abs_models.frontend.ast.FnApp) StringLiteral(org.abs_models.frontend.ast.StringLiteral) ArrayList(java.util.ArrayList) List(org.abs_models.frontend.ast.List) LinkedList(java.util.LinkedList) MainBlock(org.abs_models.frontend.ast.MainBlock) VarUse(org.abs_models.frontend.ast.VarUse) ExpressionStmt(org.abs_models.frontend.ast.ExpressionStmt)

Example 5 with FnApp

use of org.abs_models.frontend.ast.FnApp 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)

Aggregations

FnApp (org.abs_models.frontend.ast.FnApp)8 PureExp (org.abs_models.frontend.ast.PureExp)5 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)3 IntLiteral (org.abs_models.frontend.ast.IntLiteral)3 Model (org.abs_models.frontend.ast.Model)3 Test (org.junit.Test)3 FrontendTest (org.abs_models.frontend.FrontendTest)2 List (org.abs_models.frontend.ast.List)2 StringLiteral (org.abs_models.frontend.ast.StringLiteral)2 VarUse (org.abs_models.frontend.ast.VarUse)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 TypeError (org.abs_models.frontend.analyser.TypeError)1 AddAddExp (org.abs_models.frontend.ast.AddAddExp)1 Annotation (org.abs_models.frontend.ast.Annotation)1 DataConstructorExp (org.abs_models.frontend.ast.DataConstructorExp)1 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)1 ExpressionStmt (org.abs_models.frontend.ast.ExpressionStmt)1 FieldDecl (org.abs_models.frontend.ast.FieldDecl)1 FunctionDef (org.abs_models.frontend.ast.FunctionDef)1