Search in sources :

Example 6 with MethodImpl

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();
    }
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ParamDecl(org.abs_models.frontend.ast.ParamDecl)

Example 7 with MethodImpl

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));
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ParamDecl(org.abs_models.frontend.ast.ParamDecl) Model(org.abs_models.frontend.ast.Model) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) KindedName(org.abs_models.frontend.typechecker.KindedName) VarUse(org.abs_models.frontend.ast.VarUse) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) VarOrFieldUse(org.abs_models.frontend.ast.VarOrFieldUse) ABSTest(org.abs_models.ABSTest) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 8 with MethodImpl

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());
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) MethodSig(org.abs_models.frontend.ast.MethodSig) ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ModifyMethodModifier(org.abs_models.frontend.ast.ModifyMethodModifier) DeltaAccess(org.abs_models.frontend.ast.DeltaAccess) Model(org.abs_models.frontend.ast.Model) Block(org.abs_models.frontend.ast.Block) List(org.abs_models.frontend.ast.List) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) SkipStmt(org.abs_models.frontend.ast.SkipStmt) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Aggregations

MethodImpl (org.abs_models.frontend.ast.MethodImpl)8 ClassDecl (org.abs_models.frontend.ast.ClassDecl)6 Test (org.junit.Test)6 MethodSig (org.abs_models.frontend.ast.MethodSig)5 Model (org.abs_models.frontend.ast.Model)5 ParamDecl (org.abs_models.frontend.ast.ParamDecl)5 FrontendTest (org.abs_models.frontend.FrontendTest)4 ReturnStmt (org.abs_models.frontend.ast.ReturnStmt)4 ABSTest (org.abs_models.ABSTest)3 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)3 VarOrFieldUse (org.abs_models.frontend.ast.VarOrFieldUse)3 KindedName (org.abs_models.frontend.typechecker.KindedName)3 Block (org.abs_models.frontend.ast.Block)2 CompilationUnit (org.abs_models.frontend.ast.CompilationUnit)2 DeltaAccess (org.abs_models.frontend.ast.DeltaAccess)2 DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)2 List (org.abs_models.frontend.ast.List)2 ModifyClassModifier (org.abs_models.frontend.ast.ModifyClassModifier)2 SkipStmt (org.abs_models.frontend.ast.SkipStmt)2 VarUse (org.abs_models.frontend.ast.VarUse)2