Search in sources :

Example 1 with CodeStream

use of abs.backend.common.CodeStream in project abstools by abstools.

the class ErlApp method getFunStream.

/**
 * All functions for an ABS module are stored in one Erlang module.
 *
 * This method creates the necessary stream.
 */
public CodeStream getFunStream(String moduleName) throws FileNotFoundException, UnsupportedEncodingException {
    if (!funMod.containsKey(moduleName)) {
        CodeStream ecs = new CodeStream(new File(destCodeDir, ErlUtil.getModuleName(moduleName) + "_funs.erl"));
        funMod.put(moduleName, ecs);
        ecs.pf("-module(%s).", ErlUtil.getModuleName(moduleName) + "_funs");
        ecs.println("-compile(export_all).");
        ecs.println("-include_lib(\"../include/abs_types.hrl\").");
        ecs.println();
    }
    return funMod.get(moduleName);
}
Also used : CodeStream(abs.backend.common.CodeStream) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with CodeStream

use of abs.backend.common.CodeStream in project abstools by abstools.

the class ErlApp method generateModuleDefinitions.

public void generateModuleDefinitions(String absModulename, String erlModulename) throws FileNotFoundException, UnsupportedEncodingException {
    CodeStream hcs = createIncludeFile("absmodulename");
    hcs.println("%%This file is licensed under the terms of the Modified BSD License.");
    hcs.println("-undef(ABSMAINMODULE).");
    hcs.println("-define(ABSMAINMODULE," + erlModulename + ").");
    hcs.println("-undef(ABSCOMPILERVERSION).");
    hcs.println("-define(ABSCOMPILERVERSION,\"" + abs.frontend.parser.Main.getVersion() + "." + abs.frontend.parser.Main.getGitVersion() + "\").");
    hcs.close();
}
Also used : CodeStream(abs.backend.common.CodeStream)

Example 3 with CodeStream

use of abs.backend.common.CodeStream in project abstools by abstools.

the class ErlApp method generateDataConstructorInfo.

public void generateDataConstructorInfo(Model model) throws IOException {
    CodeStream s = createSourceFile("abs_constructor_info");
    s.println("%%This file is licensed under the terms of the Modified BSD License.");
    s.println("-module(abs_constructor_info).");
    s.println("-compile(export_all).");
    s.println("-include_lib(\"../include/abs_types.hrl\").");
    s.println();
    String separator = "";
    for (ModuleDecl m : model.getModuleDecls()) {
        for (Decl d : m.getDecls()) {
            if (d instanceof DataTypeDecl) {
                DataTypeDecl dd = (DataTypeDecl) d;
                for (DataConstructor c : dd.getDataConstructors()) {
                    boolean useToString = true;
                    for (ConstructorArg ca : c.getConstructorArgs()) {
                        List<Annotation> ann = ca.getTypeUse().getAnnotations();
                        PureExp key = CompilerUtils.getAnnotationValueFromName(ann, "ABS.StdLib.HTTPName");
                        if (ca.hasSelectorName() || key != null) {
                            useToString = false;
                        }
                    }
                    if (!useToString) {
                        s.println(separator);
                        separator = ";";
                        s.format("to_json(Abs=[data%s | _]) -> ", c.getName());
                        String mapSeparator = "";
                        s.print("#{");
                        s.incIndent();
                        for (int elem = 0; elem < c.getNumConstructorArg(); elem++) {
                            ConstructorArg ca = c.getConstructorArg(elem);
                            List<Annotation> ann = ca.getTypeUse().getAnnotations();
                            String key = null;
                            PureExp keyann = CompilerUtils.getAnnotationValueFromName(ann, "ABS.StdLib.HTTPName");
                            if (keyann != null && keyann instanceof StringLiteral) {
                                key = ((StringLiteral) keyann).getContent();
                            } else if (ca.hasSelectorName()) {
                                key = ca.getSelectorName().toString();
                            }
                            if (key != null) {
                                s.println(mapSeparator);
                                mapSeparator = ",";
                                s.format("<<\"%s\"/utf8>> => modelapi_v2:abs_to_json(lists:nth(%s, Abs))", key, // element:
                                elem + 2);
                            }
                        }
                        s.println();
                        s.decIndent();
                        s.print("}");
                    }
                }
            }
        }
    }
    s.println(separator);
    s.pf("to_json(Abs) -> builtin:toString(null, list_to_tuple(Abs)).");
    s.close();
}
Also used : CodeStream(abs.backend.common.CodeStream)

Example 4 with CodeStream

use of abs.backend.common.CodeStream in project abstools by abstools.

the class ClassGenerator method generateRecoverHandler.

private void generateRecoverHandler() {
    if (classDecl.hasRecoverBranch()) {
        Vars vars = new Vars();
        Vars safe = vars.pass();
        // Build var scopes and statmemnts for each branch
        java.util.List<Vars> branches_vars = new java.util.LinkedList<>();
        java.util.List<String> branches = new java.util.LinkedList<>();
        for (CaseBranchStmt b : classDecl.getRecoverBranchs()) {
            Vars v = vars.pass();
            StringWriter sw = new StringWriter();
            CodeStream buffer = new CodeStream(new WriterOutputStream(sw, Charset.forName("UTF-8")), "");
            b.getLeft().generateErlangCode(ecs, buffer, v);
            buffer.setIndent(ecs.getIndent());
            buffer.println("->");
            buffer.incIndent();
            b.getRight().generateErlangCode(buffer, v);
            buffer.println(",");
            buffer.print("true");
            buffer.decIndent();
            buffer.close();
            branches_vars.add(v);
            branches.add(sw.toString());
            vars.updateTemp(v);
        }
        ErlUtil.functionHeader(ecs, "recover", ErlUtil.Mask.none, generatorClassMatcher(), "Exception");
        ecs.println("Result=case Exception of ");
        ecs.incIndent();
        // Now print statments and mergelines for each branch.
        java.util.List<String> mergeLines = vars.merge(branches_vars);
        Iterator<String> ib = branches.iterator();
        Iterator<String> im = mergeLines.iterator();
        while (ib.hasNext()) {
            ecs.print(ib.next());
            ecs.incIndent();
            ecs.print(im.next());
            ecs.println(";");
            ecs.decIndent();
        }
        ecs.println("_ -> false");
        ecs.decIndent();
        ecs.print("end");
        ecs.println(".");
        ecs.decIndent();
    }
}
Also used : CodeStream(abs.backend.common.CodeStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream)

Aggregations

CodeStream (abs.backend.common.CodeStream)4 File (java.io.File)1 JarFile (java.util.jar.JarFile)1 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)1