Search in sources :

Example 1 with StringLiteral

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

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

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

StringLiteral (org.abs_models.frontend.ast.StringLiteral)3 ArrayList (java.util.ArrayList)2 DataConstructorExp (org.abs_models.frontend.ast.DataConstructorExp)2 FnApp (org.abs_models.frontend.ast.FnApp)2 List (org.abs_models.frontend.ast.List)2 LinkedList (java.util.LinkedList)1 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)1 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)1 AddAddExp (org.abs_models.frontend.ast.AddAddExp)1 DataConstructor (org.abs_models.frontend.ast.DataConstructor)1 DataTypeDecl (org.abs_models.frontend.ast.DataTypeDecl)1 Decl (org.abs_models.frontend.ast.Decl)1 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)1 ExpressionStmt (org.abs_models.frontend.ast.ExpressionStmt)1 Feature (org.abs_models.frontend.ast.Feature)1 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)1 IntLiteral (org.abs_models.frontend.ast.IntLiteral)1 MainBlock (org.abs_models.frontend.ast.MainBlock)1 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)1 ProductDecl (org.abs_models.frontend.ast.ProductDecl)1