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();
}
}
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();
}
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("}");
}
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;
}
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(")");
}
Aggregations