Search in sources :

Example 1 with ParamDecl

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

the class ClassGenerator method generateExports.

private void generateExports() {
    ecs.println("-export([get_val_internal/2,set_val_internal/3,init_internal/0,get_state_for_modelapi/1,implemented_interfaces/0,exported/0]).");
    ecs.println("-compile(export_all).");
    ecs.println();
    HashSet<MethodSig> callable_sigs = new HashSet<>();
    HashSet<InterfaceDecl> visited = new HashSet<>();
    for (InterfaceTypeUse i : classDecl.getImplementedInterfaceUseList()) {
        visited.add((InterfaceDecl) i.getDecl());
    }
    while (!visited.isEmpty()) {
        InterfaceDecl id = visited.iterator().next();
        visited.remove(id);
        for (MethodSig ms : id.getBodyList()) {
            if (ms.isHTTPCallable()) {
                callable_sigs.add(ms);
            }
        }
        for (InterfaceTypeUse i : id.getExtendedInterfaceUseList()) {
            visited.add((InterfaceDecl) i.getDecl());
        }
    }
    ecs.print("implemented_interfaces() -> [ ");
    String separator = "";
    for (InterfaceDecl i : classDecl.getSuperTypes()) {
        ecs.format("%s<<\"%s\">>", separator, i.getName());
        separator = ", ";
    }
    ecs.println(" ].");
    ecs.println();
    ecs.print("exported() -> #{ ");
    boolean first = true;
    for (MethodSig ms : callable_sigs) {
        if (ms.isHTTPCallable()) {
            if (!first)
                ecs.print(", ");
            first = false;
            ecs.print("<<\"" + ms.getName() + "\">> => { ");
            ecs.print("'m_" + ms.getName() + "'");
            ecs.print(", ");
            ecs.print("<<\"" + ms.getReturnType().getType().toString() + "\">>");
            ecs.print(", ");
            ecs.print("[ ");
            boolean innerfirst = true;
            for (ParamDecl p : ms.getParamList()) {
                // ABS type, and ABS type arguments (if present)
                if (!innerfirst)
                    ecs.print(", ");
                innerfirst = false;
                ecs.print("{ ");
                ecs.print("<<\"" + p.getName() + "\">>, ");
                ecs.print("<<\"" + p.getType().toString() + "\">>, ");
                generateParameterDescription(p.getType());
                ecs.print(" }");
            }
            ecs.print("] ");
            ecs.print("}");
        }
    }
    ecs.println(" }.");
    ecs.println();
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) ParamDecl(org.abs_models.frontend.ast.ParamDecl) InterfaceTypeUse(org.abs_models.frontend.ast.InterfaceTypeUse) InterfaceDecl(org.abs_models.frontend.ast.InterfaceDecl) HashSet(java.util.HashSet)

Example 2 with ParamDecl

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

the class JavaGeneratorHelper method generateParamArgs.

public static void generateParamArgs(PrintStream stream, String firstArg, List<ParamDecl> params) {
    stream.print("(");
    boolean first = true;
    if (firstArg != null) {
        stream.print(firstArg);
        first = false;
    }
    for (ParamDecl d : params) {
        if (!first)
            stream.print(", ");
        stream.print(JavaBackend.getVariableName(d.getName()));
        first = false;
    }
    stream.print(")");
}
Also used : ParamDecl(org.abs_models.frontend.ast.ParamDecl)

Example 3 with ParamDecl

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

the class ClassDeclGenerator method generateFields.

private void generateFields() {
    for (ParamDecl p : decl.getParams()) {
        stream.print("private ");
        p.generateJava(stream);
        stream.println(";");
    }
    for (FieldDecl f : decl.getFields()) {
        f.generateJava(stream);
    }
}
Also used : FieldDecl(org.abs_models.frontend.ast.FieldDecl) ParamDecl(org.abs_models.frontend.ast.ParamDecl)

Example 4 with ParamDecl

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

the class ClassDeclGenerator method generateConstructor.

private void generateConstructor() {
    // constructor
    stream.print("public " + className);
    JavaGeneratorHelper.generateParams(stream, decl.getParams());
    stream.println(" {");
    for (ParamDecl p : decl.getParams()) {
        stream.println("this." + JavaBackend.getVariableName(p.getName()) + " = " + JavaBackend.getVariableName(p.getName()) + ";");
    }
    stream.println("getCOG().objectCreated(this);");
    stream.println("}");
    stream.println("protected final void __ABS_init() {");
    for (FieldDecl f : decl.getFields()) {
        if (f.hasInitExp()) {
            stream.print("this.");
            stream.print(JavaBackend.getVariableName(f.getName()));
            stream.print(" = ");
            f.getInitExp().generateJava(stream);
            stream.print(";");
        }
    }
    if (decl.hasInitBlock()) {
        decl.getInitBlock().generateJava(stream);
    }
    stream.println("getCOG().objectInitialized(this);");
    stream.println("}");
}
Also used : FieldDecl(org.abs_models.frontend.ast.FieldDecl) ParamDecl(org.abs_models.frontend.ast.ParamDecl)

Example 5 with ParamDecl

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

the class ClassDeclGenerator method generateClassBody.

private void generateClassBody() {
    // singleton() method instantiates strictly one dynamic class object and does initialisation,
    // i.e. adds fields, constructor and methods.
    stream.println("{");
    stream.println("private static " + ABSDynamicClass.class.getName() + " instance;");
    stream.println("public static void setInstance(" + ABSDynamicClass.class.getName() + " cls) { instance = cls; }");
    stream.println("public static " + ABSDynamicClass.class.getName() + " singleton() {");
    stream.println("if (instance == null) {");
    stream.println("instance = new " + ABSDynamicClass.class.getName() + "();");
    stream.println("instance.setName(\"" + decl.getName() + "\");");
    // add class parameters
    for (ParamDecl p : decl.getParams()) {
        String name = JavaBackend.getVariableName(p.getName());
        stream.println("instance.addField(\"" + name + "\", new " + ABSField.class.getName() + "());");
    }
    // add fields
    for (FieldDecl f : decl.getFields()) {
        String name = JavaBackend.getVariableName(f.getName());
        stream.println("instance.addField(\"" + name + "\", " + className + ".field$" + name + ".singleton());");
    }
    // add constructor
    stream.println("instance.setConstructor(" + className + ".CON$TRUCT.singleton());");
    stream.print("instance.setParams(");
    for (int i = 0; i < decl.getNumParam(); i++) {
        if (i != 0)
            stream.print(", ");
        stream.print("\"" + JavaBackend.getVariableName(decl.getParam(i).getName()) + "\"");
    }
    stream.println(");");
    // add methods
    for (MethodImpl m : decl.getMethods()) {
        String methodName = m.getMethodSig().getName();
        stream.println("instance.addMethod(\"" + methodName + "\", " + className + "." + methodName + ".singleton());");
    }
    stream.println("}");
    stream.println("return instance;");
    stream.println("}");
    // generate field inner classes
    for (FieldDecl field : decl.getFields()) {
        field.generateJavaDynamic(stream);
    }
    // generate CON$TRUCT inner class
    generateConstructor();
    // generate method inner classes
    for (MethodImpl method : decl.getMethods()) {
        method.generateJavaDynamic(stream);
        // FIXME not sure how to handle FLI methods
        if (method.isForeign()) {
            stream.println("/* FLI method: not implemented yet */");
            DynamicJavaGeneratorHelper.generateFLIMethod(stream, method);
        }
    }
    generateCreateNewCOGMethod();
    generateNewObjectMethods();
    stream.println("}");
}
Also used : FieldDecl(org.abs_models.frontend.ast.FieldDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ParamDecl(org.abs_models.frontend.ast.ParamDecl) ABSField(org.abs_models.backend.java.lib.runtime.ABSField) ABSDynamicClass(org.abs_models.backend.java.lib.runtime.ABSDynamicClass)

Aggregations

ParamDecl (org.abs_models.frontend.ast.ParamDecl)19 FieldDecl (org.abs_models.frontend.ast.FieldDecl)6 MethodImpl (org.abs_models.frontend.ast.MethodImpl)5 MethodSig (org.abs_models.frontend.ast.MethodSig)5 VarUse (org.abs_models.frontend.ast.VarUse)5 FrontendTest (org.abs_models.frontend.FrontendTest)4 Model (org.abs_models.frontend.ast.Model)4 ReturnStmt (org.abs_models.frontend.ast.ReturnStmt)4 VarOrFieldUse (org.abs_models.frontend.ast.VarOrFieldUse)4 Test (org.junit.Test)4 ABSTest (org.abs_models.ABSTest)3 ClassDecl (org.abs_models.frontend.ast.ClassDecl)3 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)3 KindedName (org.abs_models.frontend.typechecker.KindedName)3 TypedVarOrFieldDecl (org.abs_models.frontend.ast.TypedVarOrFieldDecl)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ABSDynamicClass (org.abs_models.backend.java.lib.runtime.ABSDynamicClass)1 ABSField (org.abs_models.backend.java.lib.runtime.ABSField)1 ABSValue (org.abs_models.backend.java.lib.types.ABSValue)1