use of org.abs_models.frontend.ast.MethodImpl 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.MethodImpl in project abstools by abstools.
the class TypeCheckerTest method classParamsMethodShadowsField.
@Test
public void classParamsMethodShadowsField() {
Model m = assertParse("class C(Bool b) { Bool m(Bool b) { return b; } }");
ModuleDecl u = m.lookupModule("UnitTest");
ClassDecl c = (ClassDecl) u.lookup(new KindedName(KindedName.Kind.CLASS, "C"));
MethodImpl me = c.lookupMethod("m");
ReturnStmt r = (ReturnStmt) me.getBlock().getStmt(0);
VarOrFieldUse vu = (VarOrFieldUse) r.getRetExp();
ParamDecl d = (ParamDecl) vu.getDecl();
assertThat(d.getParent().getParent(), instanceOf(MethodSig.class));
assertThat(vu.getClass().getName(), vu, instanceOf(VarUse.class));
}
use of org.abs_models.frontend.ast.MethodImpl in project abstools by abstools.
the class TraitTest method addModifyModifierAtRuntimeBackComp.
@Test
public void addModifyModifierAtRuntimeBackComp() {
Model model = assertParse("module M;" + "class C { Unit m(){skip;} }");
ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
MethodSig sig = AbsASTBuilderUtil.createMethodSig("m", AbsASTBuilderUtil.getUnit());
MethodImpl impl = new MethodImpl(sig, new Block(new List<>(), new List<>(new SkipStmt(), new SkipStmt())));
ModifyMethodModifier opr = new ModifyMethodModifier(impl);
assertNotNull(opr.getMethodImpl());
ModifyClassModifier mcn = new ModifyClassModifier();
mcn.setName("M.C");
DeltaAccess acc = new DeltaAccess(cls.getModuleDecl().getName());
DeltaDecl dd = new DeltaDecl();
dd.setName("MyDelta");
dd.setImportedModule(acc);
dd.addModuleModifier(mcn);
mcn.addModifier(opr);
mcn.setParent(dd);
acc.setParent(dd);
opr.setParent(mcn);
sig.setParent(opr);
CompilationUnit cu = model.getCompilationUnitList().getChild(0);
cu.addDeltaDecl(dd);
dd.setParent(cu);
model.applyDelta(dd);
assertEquals(1, cls.getMethods().getNumChild());
assertEquals(2, cls.getMethod(0).getBlock().getNumChild());
}
Aggregations