Search in sources :

Example 11 with ParamDecl

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

the class ClassGenerator method generateMethods.

private void generateMethods() {
    ecs.println("%% --- Methods\n");
    for (MethodImpl m : classDecl.getMethodList()) {
        ecs.pf(" %%%% %s:%s", m.getFileName(), m.getStartLine());
        MethodSig ms = m.getMethodSig();
        ecs.pf(" %%%% %s:%s", m.getFileName(), m.getStartLine());
        ErlUtil.functionHeader(ecs, "m_" + ms.getName(), generatorClassMatcher(), ms.getParamList());
        ecs.println("C=(get(this))#state.class,");
        ecs.print("put(vars, #{ 'this' => O");
        for (ParamDecl p : ms.getParamList()) {
            // Same name construction as
            // ErlUtil.functionHeader(CodeStream, String, List<String>, Mask)
            ecs.format(",%n '%s' => %s", p.getName(), ErlUtil.absParamDeclToErlVarName(p));
        }
        ecs.println(" }),");
        ecs.println("try");
        ecs.incIndent();
        Vars vars = new Vars();
        m.getBlock().generateErlangCode(ecs, vars);
        if (!m.hasReturnStmt()) {
            ecs.println(",");
            ecs.println("dataUnit");
        }
        ecs.println();
        ecs.decIndent().println("catch");
        ecs.incIndent();
        ecs.println("_:Exception:Stacktrace ->");
        if (classDecl.hasRecoverBranch()) {
            ecs.incIndent();
            ecs.println("Recovered = try 'recover'(O, Exception) catch _:RecoverError -> io:format(standard_error, \"Recovery block for ~s in class " + classDecl.getQualifiedName() + " failed with exception ~s~n\", [builtin:toString(Cog, Exception), builtin:toString(Cog, RecoverError)]), false end,");
            ecs.println("case Recovered of");
            ecs.incIndent().println("true -> exit(Exception);");
            ecs.println("false ->");
            ecs.incIndent();
            ecs.println("io:format(standard_error, \"Uncaught ~s in method " + ms.getName() + " not handled successfully by recovery block, killing object ~s~n\", [builtin:toString(Cog, Exception), builtin:toString(Cog, O)]),");
            ecs.println("io:format(standard_error, \"stacktrace: ~tp~n\", [Stacktrace]),");
            ecs.println("object:die(O, Exception), exit(Exception)");
            ecs.decIndent().println("end");
            ecs.decIndent();
        } else {
            ecs.incIndent();
            ecs.println("io:format(standard_error, \"Uncaught ~s in method " + ms.getName() + " and no recovery block in class definition, killing object ~s~n\", [builtin:toString(Cog, Exception), builtin:toString(Cog, O)]),");
            ecs.println("io:format(standard_error, \"stacktrace: ~tp~n\", [Stacktrace]),");
            ecs.println("object:die(O, Exception), exit(Exception)");
            ecs.decIndent();
        }
        ecs.decIndent().println("end.");
        ecs.decIndent();
    }
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ParamDecl(org.abs_models.frontend.ast.ParamDecl)

Example 12 with ParamDecl

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

the class ClassGenerator method generateConstructor.

private void generateConstructor() {
    ecs.println("%% --- Constructor: field initializers and init block\n");
    ErlUtil.functionHeaderParamsAsList(ecs, "init", generatorClassMatcher(), classDecl.getParamList(), Mask.none);
    ecs.println("C=(get(this))#state.class,");
    ecs.println("put(vars, #{}),");
    Vars vars = Vars.n();
    for (ParamDecl p : classDecl.getParamList()) {
        ecs.pf("put(this, C:set_val_internal(get(this),'%s',%s)),", p.getName(), "P_" + p.getName());
    }
    for (FieldDecl p : classDecl.getFields()) {
        ErlUtil.emitLocationInformation(ecs, p.getModel(), p.getFileName(), p.getStartLine(), p.getEndLine());
        if (p.hasInitExp()) {
            ecs.format("put(this, C:set_val_internal(get(this),'%s',", p.getName());
            p.getInitExp().generateErlangCode(ecs, vars);
            ecs.println(")),");
        }
    }
    if (classDecl.getInitBlock() != null) {
        classDecl.getInitBlock().generateErlangCode(ecs, vars);
        ecs.println(",");
    }
    if (classDecl.isActiveClass()) {
        ecs.println("cog:task_is_blocked_for_gc(Cog, self(), get(task_info), get(this)),");
        ecs.print("cog:add_task(Cog,active_object_task,null,O,[],#task_info{event=#event{type=schedule, local_id=run}, method= <<\"run\"/utf8>>,this=O,destiny=null},");
        ecs.print(vars.toStack());
        ecs.println("),");
        ecs.println("cog:task_is_runnable(Cog,self()),");
        ecs.print("task:wait_for_token(Cog,");
        ecs.print(vars.toStack());
        ecs.println("),");
    }
    ecs.println("gc:register_object(O),");
    ecs.println("O.");
    ecs.decIndent();
}
Also used : TypedVarOrFieldDecl(org.abs_models.frontend.ast.TypedVarOrFieldDecl) FieldDecl(org.abs_models.frontend.ast.FieldDecl) ParamDecl(org.abs_models.frontend.ast.ParamDecl)

Example 13 with ParamDecl

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

the class JavaGeneratorHelper method generateMethodBody.

public static void generateMethodBody(PrintStream stream, final MethodImpl m, boolean isFliMethod) {
    boolean addReturn = false;
    if (m.getMethodSig().getReturnType().getType().isUnitType()) {
        if (m.getBlock().getNumStmt() == 0 || (!(m.getBlock().getStmt(m.getBlock().getNumStmt() - 1) instanceof ReturnStmt))) {
            addReturn = true;
        }
    }
    stream.println(" {");
    stream.println("__ABS_checkSameCOG(); ");
    if (!isFliMethod && m.isForeign()) {
        stream.print("return this.");
        stream.print(FLI_METHOD_PREFIX + JavaBackend.getMethodName(m.getMethodSig().getName()));
        JavaGeneratorHelper.generateParamArgs(stream, m.getMethodSig().getParams());
        stream.println(";");
    } else {
        stream.println("if (__ABS_getRuntime().debuggingEnabled()) {");
        stream.println(Task.class.getName() + "<?> __ABS_currentTask = __ABS_getRuntime().getCurrentTask();");
        stream.println("__ABS_currentTask.newStackFrame(this, \"" + m.getMethodSig().getName() + "\");");
        for (ParamDecl d : m.getMethodSig().getParams()) {
            stream.print("__ABS_currentTask.setLocalVariable(");
            stream.println("\"" + d.getName() + "\"," + d.getName() + ");");
        }
        stream.println("}");
        m.getBlock().generateJava(stream, addReturn);
    }
    stream.println("}");
}
Also used : ParamDecl(org.abs_models.frontend.ast.ParamDecl) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt)

Example 14 with ParamDecl

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

the class JavaGeneratorHelper method isLocalVarUse.

/**
 * checks if astNode is a use of a local variable or parameter
 */
private static boolean isLocalVarUse(ASTNode<?> astNode) {
    if (astNode instanceof VarUse) {
        VarUse v = (VarUse) astNode;
        VarOrFieldDecl decl = v.getDecl();
        if (decl instanceof VarDecl || decl instanceof ParamDecl) {
            return !(decl.getParent() instanceof LetExp);
        }
    }
    return false;
}
Also used : TypedVarOrFieldDecl(org.abs_models.frontend.ast.TypedVarOrFieldDecl) VarOrFieldDecl(org.abs_models.frontend.ast.VarOrFieldDecl) VarDecl(org.abs_models.frontend.ast.VarDecl) ParamDecl(org.abs_models.frontend.ast.ParamDecl) LetExp(org.abs_models.frontend.ast.LetExp) VarUse(org.abs_models.frontend.ast.VarUse)

Example 15 with ParamDecl

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

the class JavaGeneratorHelper method generateParams.

public static void generateParams(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("final ");
        d.generateJava(stream);
        first = false;
    }
    stream.print(")");
}
Also used : ParamDecl(org.abs_models.frontend.ast.ParamDecl)

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