Skip to content

Commit

Permalink
REVIEW: address comments
Browse files Browse the repository at this point in the history
Signed-off-by: imkiva <imkiva@islovely.icu>
  • Loading branch information
imkiva committed Nov 13, 2021
1 parent 1af9964 commit fcd32b8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 38 deletions.
29 changes: 16 additions & 13 deletions base/src/main/java/org/aya/concrete/parse/AyaProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,26 @@ public Generalize visitLevels(AyaParser.LevelsContext ctx) {

public OpDecl.@NotNull BindBlock visitBind(AyaParser.BindBlockContext ctx) {
if (ctx.LOOSER() != null) return new OpDecl.BindBlock(sourcePosOf(ctx), new Ref<>(),
visitQIdsComma(ctx.qIdsComma()), ImmutableSeq.empty(), new Ref<>(), new Ref<>());
visitQIdsComma(ctx.qIdsComma()).collect(ImmutableSeq.factory()), ImmutableSeq.empty(),
new Ref<>(), new Ref<>());
else if (ctx.TIGHTER() != null) return new OpDecl.BindBlock(sourcePosOf(ctx), new Ref<>(),
ImmutableSeq.empty(), visitQIdsComma(ctx.qIdsComma()), new Ref<>(), new Ref<>());
ImmutableSeq.empty(), visitQIdsComma(ctx.qIdsComma()).collect(ImmutableSeq.factory()),
new Ref<>(), new Ref<>());
else return new OpDecl.BindBlock(sourcePosOf(ctx), new Ref<>(),
visitLoosers(ctx.loosers()), visitTighters(ctx.tighters()), new Ref<>(), new Ref<>());
visitLoosers(ctx.loosers()), visitTighters(ctx.tighters()),
new Ref<>(), new Ref<>());
}

public @NotNull ImmutableSeq<QualifiedID> visitLoosers(List<AyaParser.LoosersContext> ctx) {
return ctx.stream().flatMap(c -> visitQIdsComma(c.qIdsComma()).stream()).collect(ImmutableSeq.factory());
return ctx.stream().flatMap(c -> visitQIdsComma(c.qIdsComma())).collect(ImmutableSeq.factory());
}

public @NotNull ImmutableSeq<QualifiedID> visitTighters(List<AyaParser.TightersContext> ctx) {
return ctx.stream().flatMap(c -> visitQIdsComma(c.qIdsComma()).stream()).collect(ImmutableSeq.factory());
return ctx.stream().flatMap(c -> visitQIdsComma(c.qIdsComma())).collect(ImmutableSeq.factory());
}

public @NotNull ImmutableSeq<QualifiedID> visitQIdsComma(AyaParser.QIdsCommaContext ctx) {
return ctx.qualifiedId().stream().map(this::visitQualifiedId).collect(ImmutableSeq.factory());
public @NotNull Stream<QualifiedID> visitQIdsComma(AyaParser.QIdsCommaContext ctx) {
return ctx.qualifiedId().stream().map(this::visitQualifiedId);
}

public @NotNull Sample visitSample(AyaParser.SampleContext ctx) {
Expand Down Expand Up @@ -214,7 +217,7 @@ private <T> T unreachable(ParserRuleContext ctx) {
visitTelescope(ctx.tele()),
type(ctx.type(), sourcePosOf(ctx)),
visitFnBody(ctx.fnBody()),
bind == null ? null : visitBind(bind)
bind == null ? OpDecl.BindBlock.EMPTY : visitBind(bind)
);
}

Expand Down Expand Up @@ -552,7 +555,7 @@ Tuple2<Decl, ImmutableSeq<Stmt>> visitDataDecl(AyaParser.DataDeclContext ctx, St
visitTelescope(ctx.tele()),
type(ctx.type(), sourcePosOf(ctx)),
body,
bind == null ? null : visitBind(bind)
bind == null ? OpDecl.BindBlock.EMPTY : visitBind(bind)
);
return Tuple2.of(data, ctx.OPEN() == null ? ImmutableSeq.empty() : ImmutableSeq.of(
new Command.Open(
Expand Down Expand Up @@ -589,7 +592,7 @@ public Decl.DataCtor visitDataCtor(@NotNull ImmutableSeq<Pattern> patterns, AyaP
visitClauses(ctx.clauses()),
patterns,
ctx.COERCE() != null,
bind == null ? null : visitBind(bind)
bind == null ? OpDecl.BindBlock.EMPTY : visitBind(bind)
);
}

Expand Down Expand Up @@ -696,7 +699,7 @@ private void checkRedefinition(@NotNull RedefinitionError.Kind kind,
type(ctx.type(), sourcePosOf(ctx)),
// ctx.ids(),
fields,
bind == null ? null : visitBind(bind)
bind == null ? OpDecl.BindBlock.EMPTY : visitBind(bind)
);
}

Expand All @@ -722,7 +725,7 @@ public Decl.StructField visitFieldImpl(AyaParser.FieldImplContext ctx) {
Option.of(ctx.expr()).map(this::visitExpr),
ImmutableSeq.empty(),
false,
bind == null ? null : visitBind(bind)
bind == null ? OpDecl.BindBlock.EMPTY : visitBind(bind)
);
}

Expand All @@ -740,7 +743,7 @@ public Decl.StructField visitFieldDecl(AyaParser.FieldDeclContext ctx) {
Option.none(),
visitClauses(ctx.clauses()),
ctx.COERCE() != null,
bind == null ? null : visitBind(bind)
bind == null ? OpDecl.BindBlock.EMPTY : visitBind(bind)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import kala.tuple.Unit;
import kala.value.Ref;
import org.aya.api.error.Reporter;
import org.aya.api.error.SourcePos;
import org.aya.api.ref.DefVar;
import org.aya.concrete.desugar.BinOpSet;
import org.aya.concrete.remark.Remark;
Expand All @@ -18,7 +17,6 @@
import org.aya.concrete.stmt.*;
import org.aya.util.MutableGraph;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Resolves expressions inside stmts, after {@link StmtShallowResolver}
Expand Down Expand Up @@ -46,18 +44,19 @@ private StmtResolver() {
return Unit.unit();
}

public void visitBind(@NotNull OpDecl self, OpDecl.@Nullable BindBlock bind, ResolveInfo info) {
if (bind == null) return;
public void visitBind(@NotNull OpDecl self, OpDecl.@NotNull BindBlock bind, ResolveInfo info) {
if (bind == OpDecl.BindBlock.EMPTY) return;
var ctx = bind.context().value;
assert ctx != null : "no shallow resolver?";
bind.resolvedLoosers().value = bind.loosers().map(looser -> bind(looser.sourcePos(), self, info.opSet(), ctx, OpDecl.BindPred.Looser, looser));
bind.resolvedTighters().value = bind.tighters().map(tighter -> bind(tighter.sourcePos(), self, info.opSet(), ctx, OpDecl.BindPred.Tighter, tighter));
var opSet = info.opSet();
bind.resolvedLoosers().value = bind.loosers().map(looser -> bind(self, opSet, ctx, OpDecl.BindPred.Looser, looser));
bind.resolvedTighters().value = bind.tighters().map(tighter -> bind(self, opSet, ctx, OpDecl.BindPred.Tighter, tighter));
}

private @NotNull DefVar<?, ?> bind(@NotNull SourcePos sourcePos, @NotNull OpDecl self, @NotNull BinOpSet opSet, @NotNull Context ctx,
private @NotNull DefVar<?, ?> bind(@NotNull OpDecl self, @NotNull BinOpSet opSet, @NotNull Context ctx,
@NotNull OpDecl.BindPred pred, @NotNull QualifiedID id) {
var target = resolveOp(opSet.reporter(), ctx, id);
opSet.bind(self, pred, target._2, sourcePos);
opSet.bind(self, pred, target._2, id.sourcePos());
return target._1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public record StmtShallowResolver(
return Unit.unit();
}

public Unit visitBind(OpDecl.@Nullable BindBlock bind, @NotNull ModuleContext context) {
if (bind != null) bind.context().value = context;
return Unit.unit();
public void visitBind(OpDecl.@NotNull BindBlock bind, @NotNull ModuleContext context) {
if (bind != OpDecl.BindBlock.EMPTY) bind.context().value = context;
}

@Override public Unit visitRemark(@NotNull Remark remark, @NotNull ModuleContext context) {
Expand Down
20 changes: 10 additions & 10 deletions base/src/main/java/org/aya/concrete/stmt/Decl.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static final class DataCtor extends Signatured implements OpDecl {
public @NotNull ImmutableSeq<Pattern.Clause> clauses;
public @NotNull ImmutableSeq<Pattern> patterns;
public final @Nullable OpDecl.OpInfo opInfo;
public final @Nullable OpDecl.BindBlock bindBlock;
public final @NotNull OpDecl.BindBlock bindBlock;
public final boolean coerce;

public DataCtor(
Expand All @@ -138,7 +138,7 @@ public DataCtor(
@NotNull ImmutableSeq<Pattern.Clause> clauses,
@NotNull ImmutableSeq<Pattern> patterns,
boolean coerce,
@Nullable OpDecl.BindBlock bindBlock
@NotNull OpDecl.BindBlock bindBlock
) {
super(sourcePos, entireSourcePos, telescope);
this.clauses = clauses;
Expand Down Expand Up @@ -169,7 +169,7 @@ public static final class DataDecl extends Decl implements OpDecl {
public @NotNull Expr result;
public final @NotNull ImmutableSeq<DataCtor> body;
public final @Nullable OpDecl.OpInfo opInfo;
public final @Nullable OpDecl.BindBlock bindBlock;
public final @NotNull OpDecl.BindBlock bindBlock;

public DataDecl(
@NotNull SourcePos sourcePos, @NotNull SourcePos entireSourcePos,
Expand All @@ -179,7 +179,7 @@ public DataDecl(
@NotNull ImmutableSeq<Expr.Param> telescope,
@NotNull Expr result,
@NotNull ImmutableSeq<DataCtor> body,
@Nullable OpDecl.BindBlock bindBlock
@NotNull OpDecl.BindBlock bindBlock
) {
super(sourcePos, entireSourcePos, accessibility, telescope);
this.result = result;
Expand Down Expand Up @@ -213,7 +213,7 @@ public static final class StructDecl extends Decl implements OpDecl {
public @NotNull
final ImmutableSeq<StructField> fields;
public final @Nullable OpDecl.OpInfo opInfo;
public final @Nullable OpDecl.BindBlock bindBlock;
public final @NotNull OpDecl.BindBlock bindBlock;
public @NotNull Expr result;

public StructDecl(
Expand All @@ -225,7 +225,7 @@ public StructDecl(
@NotNull Expr result,
// @NotNull ImmutableSeq<String> superClassNames,
@NotNull ImmutableSeq<StructField> fields,
@Nullable OpDecl.BindBlock bindBlock
@NotNull OpDecl.BindBlock bindBlock
) {
super(sourcePos, entireSourcePos, accessibility, telescope);
this.opInfo = opInfo;
Expand Down Expand Up @@ -255,7 +255,7 @@ public static final class StructField extends Signatured implements OpDecl {
public @NotNull ImmutableSeq<Pattern.Clause> clauses;
public @NotNull Expr result;
public final @Nullable OpDecl.OpInfo opInfo;
public final @Nullable OpDecl.BindBlock bindBlock;
public final @NotNull OpDecl.BindBlock bindBlock;
public @NotNull Option<Expr> body;

public final boolean coerce;
Expand All @@ -269,7 +269,7 @@ public StructField(
@NotNull Option<Expr> body,
@NotNull ImmutableSeq<Pattern.Clause> clauses,
boolean coerce,
@Nullable OpDecl.BindBlock bindBlock
@NotNull OpDecl.BindBlock bindBlock
) {
super(sourcePos, entireSourcePos, telescope);
this.coerce = coerce;
Expand Down Expand Up @@ -299,7 +299,7 @@ public StructField(
public static final class FnDecl extends Decl implements OpDecl {
public final @NotNull EnumSet<Modifier> modifiers;
public final @Nullable OpDecl.OpInfo opInfo;
public final @Nullable OpDecl.BindBlock bindBlock;
public final @NotNull OpDecl.BindBlock bindBlock;
public final @NotNull DefVar<FnDef, FnDecl> ref;
public @NotNull Expr result;
public @NotNull Either<Expr, ImmutableSeq<Pattern.Clause>> body;
Expand All @@ -313,7 +313,7 @@ public FnDecl(
@NotNull ImmutableSeq<Expr.Param> telescope,
@NotNull Expr result,
@NotNull Either<Expr, ImmutableSeq<Pattern.Clause>> body,
@Nullable OpDecl.BindBlock bindBlock
@NotNull OpDecl.BindBlock bindBlock
) {
super(sourcePos, entireSourcePos, accessibility, telescope);
this.modifiers = modifiers;
Expand Down
1 change: 1 addition & 0 deletions base/src/main/java/org/aya/concrete/stmt/OpDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ record BindBlock(
@NotNull Ref<ImmutableSeq<DefVar<?, ?>>> resolvedLoosers,
@NotNull Ref<ImmutableSeq<DefVar<?, ?>>> resolvedTighters
) {
public static final BindBlock EMPTY = new BindBlock(SourcePos.NONE, new Ref<>(), ImmutableSeq.empty(), ImmutableSeq.empty(), new Ref<>(), new Ref<>());
}

@Nullable OpInfo opInfo();
Expand Down
4 changes: 2 additions & 2 deletions base/src/main/java/org/aya/distill/ConcreteDistiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ else if (tighters.isEmpty()) return Doc.cat(Doc.line(), Doc.hang(2, Doc.sep(
Doc.styled(KEYWORD, "bind"), Doc.styled(KEYWORD, "looser"),
Doc.commaList(loosers.view().map(BaseDistiller::visitDefVar)))));
return Doc.cat(Doc.line(), Doc.hang(2, Doc.cat(Doc.styled(KEYWORD, "bind"), Doc.braced(Doc.sep(
Doc.styled(KEYWORD, "tighter"), Doc.ONE_WS, Doc.commaList(tighters.view().map(BaseDistiller::visitDefVar)),
Doc.styled(KEYWORD, "looser"), Doc.ONE_WS, Doc.commaList(loosers.view().map(BaseDistiller::visitDefVar))
Doc.styled(KEYWORD, "tighter"), Doc.commaList(tighters.view().map(BaseDistiller::visitDefVar)),
Doc.styled(KEYWORD, "looser"), Doc.commaList(loosers.view().map(BaseDistiller::visitDefVar))
)))));
}

Expand Down
4 changes: 2 additions & 2 deletions lsp/src/main/java/org/aya/lsp/actions/SyntaxHighlight.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ private void visitOperator(@NotNull DynamicSeq<HighlightResult.Symbol> buffer, @
buffer.append(new HighlightResult.Symbol(LspRange.toRange(sourcePos), kindOf(op.concrete)));
}

private void visitBind(@NotNull DynamicSeq<HighlightResult.Symbol> buffer, @Nullable OpDecl.BindBlock bindBlock) {
if (bindBlock == null) return;
private void visitBind(@NotNull DynamicSeq<HighlightResult.Symbol> buffer, @NotNull OpDecl.BindBlock bindBlock) {
if (bindBlock == OpDecl.BindBlock.EMPTY) return;
bindBlock.loosers().view().zip(bindBlock.resolvedLoosers().value.view())
.forEach(tup -> visitOperator(buffer, tup._1.sourcePos(), tup._2));
bindBlock.tighters().view().zip(bindBlock.resolvedTighters().value.view())
Expand Down

0 comments on commit fcd32b8

Please sign in to comment.