Search in sources :

Example 11 with Member

use of ast.ExpCore.ClassB.Member in project L42 by ElvisResearchGroup.

the class ProgramReduction method step.

static Program step(Program p) {
    //precondition: at top level we have a L not of form LC
    assert !IsCompiled.of(p.top());
    CtxL top = CtxL._split(p.top());
    assert top != null;
    Member m = top.originalCtxM();
    assert !IsCompiled.of(m);
    ExpCore hole = top.originalHole();
    if (hole instanceof ClassB) {
        assert !IsCompiled.of(hole);
        return enter(p, top, m);
    }
    ClassB.NestedClass nc = (NestedClass) m;
    return top(p, nc);
}
Also used : NestedClass(ast.ExpCore.ClassB.NestedClass) ExpCore(ast.ExpCore) NestedClass(ast.ExpCore.ClassB.NestedClass) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB)

Example 12 with Member

use of ast.ExpCore.ClassB.Member in project L42 by ElvisResearchGroup.

the class ReplState method add.

public ReplState add(String code) {
    Expression.ClassB cbEmpty = new ClassB(Doc.empty(), new ast.Ast.InterfaceHeader(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Position.noInfo);
    try {
        //parse
        Expression.ClassB codeTmp = (ClassB) Parser.parse("Repl", "{" + code + "}");
        //new original
        ClassReuse newOriginal = this.originalL;
        List<ast.Expression.ClassB.Member> newOriginalMs = newOriginal.getInner().getMs();
        newOriginalMs.addAll(codeTmp.getMs());
        newOriginal.withInner(newOriginal.getInner().withMs(newOriginalMs));
        //new src to desugar
        List<ClassB.Member> newMs = new ArrayList<>();
        int nestedAdded = 0;
        for (Member m : this.desugaredL.getMs()) {
            if (!(m instanceof NestedClass)) {
                continue;
            }
            NestedClass nc = (NestedClass) m;
            newMs.add(new ClassB.NestedClass(Doc.empty(), nc.getName(), cbEmpty, nc.getP()));
            nestedAdded += 1;
        }
        newMs.addAll(codeTmp.getMs());
        codeTmp = codeTmp.withMs(newMs);
        Expression code2 = Desugar.of(codeTmp);
        ExpCore.ClassB code3 = (ExpCore.ClassB) code2.accept(new InjectionOnCore());
        // 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);
            }
        });
        //integrate new desugared src with old desugared code
        List<Member> resultMs = new ArrayList<>(this.desugaredL.getMs());
        for (int i = nestedAdded; i < code3.getMs().size(); i++) {
            resultMs.add(code3.getMs().get(i));
        }
        code3 = code3.withMs(resultMs);
        //call the repl and return
        ExpCore.ClassB result = ProgramReduction.allSteps(code3);
        return new ReplState(this.originalS + "\n" + code, newOriginal, result);
    } catch (ParseCancellationException parser) {
        System.out.println(parser.getMessage());
        return null;
    } catch (ErrorMessage msg) {
        ErrorFormatter.topFormatErrorMessage(msg);
        return null;
    }
}
Also used : ExpCore(ast.ExpCore) Position(ast.Ast.Position) ClassB(ast.Expression.ClassB) ArrayList(java.util.ArrayList) NestedClass(ast.ExpCore.ClassB.NestedClass) CloneVisitor(coreVisitors.CloneVisitor) NestedClass(ast.ExpCore.ClassB.NestedClass) Expression(ast.Expression) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ClassReuse(ast.Expression.ClassReuse) ErrorMessage(ast.ErrorMessage) Member(ast.ExpCore.ClassB.Member) ClassB(ast.Expression.ClassB) InjectionOnCore(sugarVisitors.InjectionOnCore)

Example 13 with Member

use of ast.ExpCore.ClassB.Member in project L42 by ElvisResearchGroup.

the class InjectionOnCore method visit.

public ExpCore visit(Expression.ClassB s) {
    Doc doc1 = s.getDoc1();
    List<Ast.Type> supertypes = s.getSupertypes();
    boolean isInterface = false;
    if (s.getH() instanceof Ast.ConcreteHeader) {
        throw Assertions.codeNotReachable();
    }
    if (s.getH() instanceof Ast.InterfaceHeader) {
        isInterface = true;
    }
    List<Member> members = new ArrayList<>();
    for (ast.Expression.ClassB.Member mi : s.getMs()) {
        members.add(mi.match(m -> new ClassB.NestedClass(m.getDoc(), m.getName(), lift(m.getInner()), m.getP()), m -> new ClassB.MethodImplemented(m.getDoc(), m.getS(), lift(m.getInner()), m.getP()), m -> {
            Doc mdoc = m.getDoc();
            Ast.MethodSelector ms = m.getMs();
            MethodType mt = m.getMt();
            return new ClassB.MethodWithType(mdoc, ms, mt, lift(m.getInner()), m.getP());
        }));
    }
    ClassB result = new ClassB(doc1, isInterface, supertypes, members, s.getP(), Phase.None, 0);
    return result;
}
Also used : Ast(ast.Ast) Path(ast.Ast.Path) Catch(ast.Expression.Catch) Match(tools.Match) Doc(ast.Ast.Doc) Type(ast.Ast.Type) Phase(ast.ExpCore.ClassB.Phase) Assertions(tools.Assertions) MethodType(ast.Ast.MethodType) ExpCore(ast.ExpCore) Member(ast.ExpCore.ClassB.Member) MethodSelector(ast.Ast.MethodSelector) Block(ast.ExpCore.Block) Expression(ast.Expression) WalkBy(ast.Expression.WalkBy) ArrayList(java.util.ArrayList) List(java.util.List) SignalKind(ast.Ast.SignalKind) ContextId(ast.Expression.ContextId) Position(ast.Ast.Position) ClassReuse(ast.Expression.ClassReuse) Optional(java.util.Optional) Visitor(sugarVisitors.Visitor) Collections(java.util.Collections) MethodType(ast.Ast.MethodType) MethodSelector(ast.Ast.MethodSelector) ArrayList(java.util.ArrayList) Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) Doc(ast.Ast.Doc) Member(ast.ExpCore.ClassB.Member)

Example 14 with Member

use of ast.ExpCore.ClassB.Member in project L42 by ElvisResearchGroup.

the class CtxL method _split.

public static CtxL _split(ExpCore.ClassB l) {
    int pos = firstNotCompiled(l.getMs());
    if (pos == l.getMs().size()) {
        return null;
    }
    Member m = l.getMs().get(pos);
    ExpCore inner = m.getInner();
    boolean metaReady = m instanceof NestedClass && IsCompiled.of(m.getInner());
    CtxC innerSplit;
    if (metaReady) {
        innerSplit = CtxC.hole(inner);
    } else {
        innerSplit = CtxC.split(inner);
    }
    return new CtxL(l, pos, innerSplit);
}
Also used : ExpCore(ast.ExpCore) NestedClass(ast.ExpCore.ClassB.NestedClass) Member(ast.ExpCore.ClassB.Member)

Example 15 with Member

use of ast.ExpCore.ClassB.Member in project L42 by ElvisResearchGroup.

the class TestHelper method _dbgCompact.

/*
  static class LoggedPrintStream extends PrintStream {
    final StringBuilder buf;
    final PrintStream underlying;
    LoggedPrintStream(StringBuilder sb, OutputStream os, PrintStream ul) {
        super(os);
        this.buf = sb;
        this.underlying = ul;
    }
  public static LoggedPrintStream create(PrintStream toLog) {//from http://stackoverflow.com/questions/4334808/how-could-i-read-java-console-output-into-a-string-buffer
    try {
      final StringBuilder sb = new StringBuilder();
      Field f = FilterOutputStream.class.getDeclaredField("out");
      f.setAccessible(true);
      OutputStream psout = (OutputStream) f.get(toLog);
      return new LoggedPrintStream(sb, new FilterOutputStream(psout) {
        public void write(int b) throws IOException {
          super.write(b);
          sb.append((char) b);
          }
        }, toLog);
      }
    catch (NoSuchFieldException|IllegalArgumentException |IllegalAccessException e) {
      throw Assertions.codeNotReachable();
      }}}*/
public static void _dbgCompact(ExpCore e) {
    assert e instanceof ClassB;
    ClassB cb = (ClassB) e;
    ArrayList<Member> ms = new ArrayList<>();
    for (Member m : cb.getMs()) {
        if (!(m instanceof NestedClass)) {
            continue;
        }
        NestedClass nc = (NestedClass) m;
        if ((nc.getInner() instanceof ClassB)) {
            continue;
        }
        ms.add(nc);
        break;
    }
    cb = cb.withMs(ms);
    System.out.println(ToFormattedText.of(cb));
}
Also used : ArrayList(java.util.ArrayList) NestedClass(ast.ExpCore.ClassB.NestedClass) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB)

Aggregations

Member (ast.ExpCore.ClassB.Member)54 ArrayList (java.util.ArrayList)32 ClassB (ast.ExpCore.ClassB)21 MethodWithType (ast.ExpCore.ClassB.MethodWithType)21 NestedClass (ast.ExpCore.ClassB.NestedClass)20 Ast (ast.Ast)14 ExpCore (ast.ExpCore)11 Path (ast.Ast.Path)10 List (java.util.List)7 Doc (ast.Ast.Doc)5 MethodSelector (ast.Ast.MethodSelector)5 MethodType (ast.Ast.MethodType)5 Collections (java.util.Collections)5 C (ast.Ast.C)4 MethodImplemented (ast.ExpCore.ClassB.MethodImplemented)4 Phase (ast.ExpCore.ClassB.Phase)4 HashSet (java.util.HashSet)4 Optional (java.util.Optional)4 Assertions (tools.Assertions)4 Position (ast.Ast.Position)3