Search in sources :

Example 11 with Position

use of ast.Ast.Position in project L42 by ElvisResearchGroup.

the class ReplState method start.

public static ReplState start(String code) {
    try {
        Expression.ClassReuse code1 = (ClassReuse) Parser.parse("Repl", code);
        auxiliaryGrammar.WellFormedness.checkAll(code1);
        Expression.ClassReuse code2 = (ClassReuse) Desugar.of(code1);
        assert auxiliaryGrammar.WellFormedness.checkAll(code2);
        ExpCore.ClassB code3 = (ExpCore.ClassB) code2.accept(new InjectionOnCore());
        assert coreVisitors.CheckNoVarDeclaredTwice.of(code3);
        // TODO: will die after new reduction Refresh of position identities, it is used to generate correct Java code.
        code3 = (ExpCore.ClassB) code3.accept(new CloneVisitor() {

            @Override
            public ExpCore visit(ExpCore.ClassB cb) {
                Position p = cb.getP();
                cb = cb.withP(new Position(p.getFile(), p.getLine1(), p.getPos1(), p.getLine2(), p.getPos2(), p.get_next()));
                return super.visit(cb);
            }
        });
        ExpCore.ClassB result = ProgramReduction.allSteps(code3);
        return new ReplState(code, code2, result);
    } catch (org.antlr.v4.runtime.misc.ParseCancellationException parser) {
        System.out.println(parser.getMessage());
        return null;
    } catch (ErrorMessage msg) {
        ErrorFormatter.topFormatErrorMessage(msg);
        return null;
    }
}
Also used : ExpCore(ast.ExpCore) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) Position(ast.Ast.Position) CloneVisitor(coreVisitors.CloneVisitor) Expression(ast.Expression) ClassReuse(ast.Expression.ClassReuse) ErrorMessage(ast.ErrorMessage) ClassReuse(ast.Expression.ClassReuse) ClassB(ast.Expression.ClassB) InjectionOnCore(sugarVisitors.InjectionOnCore)

Example 12 with Position

use of ast.Ast.Position in project L42 by ElvisResearchGroup.

the class DesugarW method visit.

public Expression visit(With e) {
    Position pos = e.getP();
    //case a
    e = with_A_applyDefault(e);
    if (e.getIs().isEmpty() && e.getDecs().isEmpty()) {
        //case c
        return with_C_resolveXsBaseCase(pos, e.getXs(), e.getOns(), e.getDefaultE().get()).accept(this);
    }
    //case b
    if (e.getIs().isEmpty()) {
        return with_B_noI_makeBlock(e).accept(this);
    }
    //assert !e.getIs().isEmpty();
    if (e.getXs().isEmpty() && e.getDecs().isEmpty() && e.getOns().isEmpty()) {
        //case e
        Expression b = e.getDefaultE().get();
        //NOTE WELL: needed to avoid xs that can not be replaced in with xs... this is also the reason we need DesugarW in the first place.
        b = b.accept(this);
        return with_E_handleIs(pos, e.getIs(), b).accept(this);
    }
    //case d
    return with_D_replace_XID_with_InestedDwithX(e).accept(this);
}
Also used : Position(ast.Ast.Position) Expression(ast.Expression)

Example 13 with Position

use of ast.Ast.Position in project L42 by ElvisResearchGroup.

the class Desugar method visit.

public Expression visit(If s) {
    if (!s.get_else().isPresent()) {
        return visit(s.with_else(Optional.of(Expression._void.instance)));
    }
    Position p = s.getP();
    if (!(s.getCond() instanceof Ast.Atom)) {
        String x = Functions.freshName("cond", usedVars);
        return visit(getBlock(p, x, s.getCond(), s.withCond(new X(p, x))));
    }
    MCall check = getMCall(p, s.getCond(), "#checkTrue", getPs());
    Expression.Catch k = getK(p, SignalKind.Exception, "", Type.immVoid, s.get_else().get());
    return visit(getBlock(p, check, Collections.singletonList(k), s.getThen()));
}
Also used : Position(ast.Ast.Position) Expression(ast.Expression) MCall(ast.Expression.MCall) X(ast.Expression.X) MethodSelectorX(ast.Ast.MethodSelectorX) Catch(ast.Expression.Catch)

Example 14 with Position

use of ast.Ast.Position in project L42 by ElvisResearchGroup.

the class TranslateExpression method visit.

//not a propagator visitor. 
@Override
public Void visit(ExpCore.EPath s) {
    Path ss = s.getInner();
    if (ss.isPrimitive()) {
        if (ss.equals(Path.Any())) {
            res.append("platformSpecific.javaTranslation.Resources.Any.type");
        }
        if (ss.equals(Path.Library())) {
            res.append("platformSpecific.javaTranslation.Resources.Library.type");
        }
        if (ss.equals(Path.Void())) {
            res.append("platformSpecific.javaTranslation.Resources.Void.type");
        }
        return null;
    }
    ClassB cbs = Resources.getP().extractClassB(ss);
    if (cbs.getPhase() == Phase.Coherent && IsCompiled.of(cbs)) {
        res.append(Resources.nameOf(ss) + ".type ");
    } else {
        Position pos = Resources.getP().get(ss.outerNumber()).getP();
        int hash = System.identityHashCode(pos);
        String cs = s.toString();
        int dotPos = cs.indexOf(".");
        assert dotPos >= 0;
        cs = cs.substring(dotPos);
        res.append("platformSpecific.javaTranslation.Resources.fromHash(" + hash + ",\"" + cs + "\")");
    }
    return null;
}
Also used : Path(ast.Ast.Path) Position(ast.Ast.Position) ClassB(ast.ExpCore.ClassB)

Aggregations

Position (ast.Ast.Position)14 Expression (ast.Expression)9 ErrorMessage (ast.ErrorMessage)5 ExpCore (ast.ExpCore)5 ArrayList (java.util.ArrayList)5 Ast (ast.Ast)4 ClassB (ast.ExpCore.ClassB)3 ClassB (ast.Expression.ClassB)3 ClassReuse (ast.Expression.ClassReuse)3 C (ast.Ast.C)2 Doc (ast.Ast.Doc)2 MethodSelectorX (ast.Ast.MethodSelectorX)2 Path (ast.Ast.Path)2 Type (ast.Ast.Type)2 VarDec (ast.Ast.VarDec)2 VarDecXE (ast.Ast.VarDecXE)2 Catch (ast.Expression.Catch)2 MethodWithType (ast.Expression.ClassB.MethodWithType)2 MCall (ast.Expression.MCall)2 RoundBlock (ast.Expression.RoundBlock)2