Search in sources :

Example 1 with List

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

the class TraitTest method addAddModifierAtRuntimeBackComp.

@Test
public void addAddModifierAtRuntimeBackComp() {
    Model model = assertParse("module M;" + "class C { Unit m(){skip;} }");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    MethodSig sig = AbsASTBuilderUtil.createMethodSig("n", AbsASTBuilderUtil.getUnit());
    MethodImpl impl = new MethodImpl(sig, new Block(new List<>(), new List<>(new SkipStmt())));
    AddMethodModifier opr = new AddMethodModifier(impl);
    assertNotNull(opr.getMethodImpl());
    ModifyClassModifier mcn = new ModifyClassModifier();
    mcn.setName("M.C");
    DeltaAccess acc = new DeltaAccess(cls.getModuleDecl().getName());
    DeltaDecl dd = new DeltaDecl();
    dd.setName("MyDelta");
    dd.setImportedModule(acc);
    dd.addModuleModifier(mcn);
    mcn.addModifier(opr);
    mcn.setParent(dd);
    acc.setParent(dd);
    opr.setParent(mcn);
    sig.setParent(opr);
    CompilationUnit cu = model.getCompilationUnitList().getChild(0);
    cu.addDeltaDecl(dd);
    dd.setParent(cu);
    model.applyDelta(dd);
    assertEquals(2, cls.getMethods().getNumChild());
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) MethodSig(org.abs_models.frontend.ast.MethodSig) ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) DeltaAccess(org.abs_models.frontend.ast.DeltaAccess) AddMethodModifier(org.abs_models.frontend.ast.AddMethodModifier) Model(org.abs_models.frontend.ast.Model) Block(org.abs_models.frontend.ast.Block) List(org.abs_models.frontend.ast.List) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) SkipStmt(org.abs_models.frontend.ast.SkipStmt) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 2 with List

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

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

the class Main method rewriteModel.

/**
 * Perform various rewrites that cannot be done in JastAdd.
 *
 * JastAdd rewrite rules can only rewrite the current node using
 * node-local information.  ("The code in the body of the rewrite may
 * access and rearrange the nodes in the subtree rooted at A, but not any
 * other nodes in the AST. Furthermore, the code may not have any other
 * side effects." --
 * http://jastadd.org/web/documentation/reference-manual.php#Rewrites)
 *
 * We use this method to generate Exception constructors and the
 * information in ABS.Productline.
 *
 * @param m the model.
 * @param productname The name of the product or null.
 * @throws WrongProgramArgumentException
 */
private static void rewriteModel(Model m, String productname) throws WrongProgramArgumentException {
    // Generate reflective constructors for all features
    ProductLine pl = m.getProductLine();
    if (pl != null) {
        // Let's assume the module and datatype names in abslang.abs did
        // not get changed, and just crash otherwise.  If you're here
        // because of a NPE: Hi!  Make the standard library and this code
        // agree about what the feature reflection module is called.
        ModuleDecl modProductline = null;
        DataTypeDecl featureDecl = null;
        FunctionDecl currentFeatureFun = null;
        FunctionDecl productNameFun = null;
        for (ModuleDecl d : m.getModuleDecls()) {
            if (d.getName().equals(Constants.PL_NAME)) {
                modProductline = d;
                break;
            }
        }
        if (modProductline == null) {
            throw new WrongProgramArgumentException("Internal error: did not find module " + Constants.PL_NAME + "(should have been defined in the abslang.abs standard library)");
        }
        for (Decl d : modProductline.getDecls()) {
            if (d instanceof DataTypeDecl && d.getName().equals("Feature")) {
                featureDecl = (DataTypeDecl) d;
            } else if (d instanceof FunctionDecl && d.getName().equals("product_features")) {
                currentFeatureFun = (FunctionDecl) d;
            } else if (d instanceof FunctionDecl && d.getName().equals("product_name")) {
                productNameFun = (FunctionDecl) d;
            }
        }
        // Adjust Feature datatype
        featureDecl.setDataConstructorList(new List<>());
        for (Feature f : pl.getFeatures()) {
            // TODO: when/if we incorporate feature parameters into the
            // productline feature declarations (as we should), we need to
            // adjust the DataConstructor arguments here.
            featureDecl.addDataConstructorNoTransform(new DataConstructor(f.getName(), new List<>()));
        }
        // Adjust product_name() function
        productNameFun.setFunctionDef(new ExpFunctionDef(new StringLiteral(productname)));
        // Adjust product_features() function
        ProductDecl p = null;
        if (productname != null)
            p = m.findProduct(productname);
        if (p != null) {
            DataConstructorExp feature_arglist = new DataConstructorExp("Cons", new List<>());
            DataConstructorExp current = feature_arglist;
            for (Feature f : p.getProduct().getFeatures()) {
                DataConstructorExp next = new DataConstructorExp("Cons", new List<>());
                // TODO: when/if we incorporate feature parameters into
                // the productline feature declarations (as we should), we
                // need to adjust the DataConstructorExp arguments here.
                current.addParamNoTransform(new DataConstructorExp(f.getName(), new List<>()));
                current.addParamNoTransform(next);
                current = next;
            }
            current.setConstructor("Nil");
            currentFeatureFun.setFunctionDef(new ExpFunctionDef(feature_arglist));
        }
    }
    m.flushTreeCache();
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) Decl(org.abs_models.frontend.ast.Decl) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ProductDecl(org.abs_models.frontend.ast.ProductDecl) DataConstructor(org.abs_models.frontend.ast.DataConstructor) Feature(org.abs_models.frontend.ast.Feature) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp) StringLiteral(org.abs_models.frontend.ast.StringLiteral) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) ArrayList(java.util.ArrayList) List(org.abs_models.frontend.ast.List) ProductLine(org.abs_models.frontend.ast.ProductLine) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ExpFunctionDef(org.abs_models.frontend.ast.ExpFunctionDef)

Example 4 with List

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

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

the class ASTBasedABSTestRunnerGenerator method generateImportsAST.

private List<Import> generateImportsAST() {
    List<Import> imports = new List<>();
    Set<String> mn = new HashSet<>();
    Set<String> qn = new HashSet<>();
    for (InterfaceDecl key : tests.keySet()) {
        getImportsFrom(mn, qn, key.getModuleDecl());
        for (ClassDecl clazz : tests.get(key)) {
            getImportsFrom(mn, qn, clazz.getModuleDecl());
        }
    }
    for (String m : mn) {
        imports.add(new StarImport(m));
    }
    if (!qn.isEmpty()) {
        List<Name> names = new List<>();
        for (String q : qn) {
            names.add(new Name(q));
        }
        imports.add(new NamedImport(names));
    }
    return imports;
}
Also used : Import(org.abs_models.frontend.ast.Import) NamedImport(org.abs_models.frontend.ast.NamedImport) StarImport(org.abs_models.frontend.ast.StarImport) FromImport(org.abs_models.frontend.ast.FromImport) ClassDecl(org.abs_models.frontend.ast.ClassDecl) StarImport(org.abs_models.frontend.ast.StarImport) List(org.abs_models.frontend.ast.List) InterfaceDecl(org.abs_models.frontend.ast.InterfaceDecl) HashSet(java.util.HashSet) Name(org.abs_models.frontend.ast.Name) NamedImport(org.abs_models.frontend.ast.NamedImport)

Aggregations

List (org.abs_models.frontend.ast.List)10 ClassDecl (org.abs_models.frontend.ast.ClassDecl)4 CompilationUnit (org.abs_models.frontend.ast.CompilationUnit)4 MethodSig (org.abs_models.frontend.ast.MethodSig)4 Model (org.abs_models.frontend.ast.Model)4 ArrayList (java.util.ArrayList)3 DeltaAccess (org.abs_models.frontend.ast.DeltaAccess)3 DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)3 ModifyClassModifier (org.abs_models.frontend.ast.ModifyClassModifier)3 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)2 Block (org.abs_models.frontend.ast.Block)2 FnApp (org.abs_models.frontend.ast.FnApp)2 MethodImpl (org.abs_models.frontend.ast.MethodImpl)2 PureExp (org.abs_models.frontend.ast.PureExp)2 StringLiteral (org.abs_models.frontend.ast.StringLiteral)2 Test (org.junit.Test)2 File (java.io.File)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)1