Skip to content

Commit e3803bb

Browse files
authored
2 parents d9d2d54 + 8444d24 commit e3803bb

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

Diff for: cli-console/src/main/java/org/aya/cli/repl/AyaRepl.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
1+
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
22
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
33
package org.aya.cli.repl;
44

5+
import java.io.Closeable;
6+
import java.io.IOException;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
510
import kala.collection.immutable.ImmutableSeq;
611
import kala.control.Either;
712
import org.aya.cli.console.AnsiReporter;
@@ -19,15 +24,11 @@
1924
import org.aya.syntax.core.def.TopLevelDef;
2025
import org.aya.syntax.literate.CodeOptions;
2126
import org.aya.util.reporter.Problem;
27+
import org.jetbrains.annotations.Contract;
2228
import org.jetbrains.annotations.NotNull;
2329
import org.jetbrains.annotations.Nullable;
2430
import org.jline.builtins.Completers;
2531

26-
import java.io.Closeable;
27-
import java.io.IOException;
28-
import java.nio.file.Path;
29-
import java.nio.file.Paths;
30-
3132
public abstract class AyaRepl implements Closeable, Runnable, Repl {
3233
public static int start(
3334
@NotNull ImmutableSeq<Path> modulePaths,
@@ -81,6 +82,7 @@ public CommandManager makeCommand() {
8182
ReplCommands.CHANGE_NORM_MODE,
8283
ReplCommands.TOGGLE_PRETTY,
8384
ReplCommands.SHOW_TYPE,
85+
ReplCommands.SHOW_MCT,
8486
ReplCommands.SHOW_INFO,
8587
ReplCommands.SHOW_PARSE_TREE,
8688
ReplCommands.CHANGE_PP_WIDTH,
@@ -150,11 +152,11 @@ private boolean singleLoop() {
150152
));
151153
}
152154

153-
public @NotNull Doc render(@NotNull AyaDocile ayaDocile) {
155+
@Contract(pure = true) public @NotNull Doc render(@NotNull AyaDocile ayaDocile) {
154156
return ayaDocile.toDoc(prettierOptions());
155157
}
156158

157-
public @NotNull AyaPrettierOptions prettierOptions() {
159+
@Contract(pure = true) public @NotNull AyaPrettierOptions prettierOptions() {
158160
return config.literatePrettier.prettierOptions;
159161
}
160162

Diff for: cli-console/src/main/java/org/aya/cli/repl/ReplCommands.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
import org.aya.repl.CommandArg;
1919
import org.aya.repl.ReplUtil;
2020
import org.aya.syntax.compile.JitDef;
21-
import org.aya.syntax.core.def.AnyDef;
22-
import org.aya.syntax.core.def.ConDefLike;
23-
import org.aya.syntax.core.def.MemberDefLike;
24-
import org.aya.syntax.core.def.TyckAnyDef;
21+
import org.aya.syntax.core.def.*;
2522
import org.aya.syntax.literate.CodeOptions;
2623
import org.aya.syntax.ref.AnyDefVar;
24+
import org.aya.syntax.ref.DefVar;
2725
import org.jetbrains.annotations.NotNull;
2826
import org.jetbrains.annotations.Nullable;
2927

@@ -54,6 +52,21 @@ record StyleParam(@NotNull Either<RenderOptions.StyleFamilyName, Path> value)
5452
}
5553
};
5654

55+
@NotNull Command SHOW_MCT = new Command(ImmutableSeq.of("case-tree"), "Show the case tree of a pattern-matching function") {
56+
@Entry public @NotNull Command.Result execute(@NotNull AyaRepl repl, @NotNull Code code) {
57+
var resolved = repl.replCompiler.parseToAnyVar(code.code);
58+
if (!(resolved instanceof AnyDefVar anyDefVar)) return Result.err("Not a valid reference", true);
59+
if (!(anyDefVar instanceof DefVar<?, ?> defVar)) return Result.err("JIT-compiled defs are unsupported", true);
60+
if (!(defVar.core instanceof FnDef fn)) return Result.err("Not a function", true);
61+
if (!(fn.body() instanceof Either.Right(var body))) return Result.err("Not a pattern-matching function", true);
62+
var classes = body.classes.map(cl ->
63+
Doc.sep(Doc.commaList(cl.term().view().map(t -> t.toDoc(repl.prettierOptions()))),
64+
Doc.symbol("=>"),
65+
Doc.commaList(cl.cls().mapToObj(Doc::ordinal))));
66+
return new Result(Output.stdout(Doc.vcat(classes)), true);
67+
}
68+
};
69+
5770
@NotNull Command SHOW_INFO = new Command(ImmutableSeq.of("info"), "Show the information of the given definition") {
5871
@Entry public @NotNull Command.Result execute(@NotNull AyaRepl repl, @NotNull Code code) {
5972
var resolved = repl.replCompiler.parseToAnyVar(code.code);

Diff for: syntax/src/main/java/org/aya/prettier/BasePrettier.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ protected static <T extends AyaDocile> Doc arg(@NotNull Fmt<T> fmt, @NotNull Bin
222222
for (int i = 1; i < telescope.size(); i++) {
223223
var param = telescope.get(i);
224224
if (!Objects.equals(param.type(), last.type())) {
225-
if (body != null && names.sizeEquals(1)) {
225+
if (names.sizeEquals(1)) {
226226
var ref = names.getFirst();
227-
var used = telescope.sliceView(i, telescope.size())
228-
.map(ParamLike::type).appended(body)
227+
var prefix = telescope.sliceView(i, telescope.size()).map(ParamLike::type);
228+
var used = (body == null ? prefix : prefix.appended(body))
229229
.anyMatch(p -> altF7.applyAsInt(p, ref.ref()) > 0);
230230
// We omit the name if there is no usage.
231231
if (!used) buf.append(justType(last, Outer.ProjHead));

Diff for: syntax/src/main/java/org/aya/prettier/CorePrettier.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ case MatchCall(JitMatchy _, var discriminant, _) -> {
220220
private @NotNull Doc visitDT(@NotNull Outer outer, DepTypeTerm.UnpiNamed pair, Doc kw, Doc operator) {
221221
var params = pair.names().zip(pair.params(), RichParam::ofExplicit);
222222
var body = pair.body().instTeleVar(params.view().map(ParamLike::ref));
223-
var teleDoc = visitTele(params, body, FindUsage::free);
223+
var teleDoc = visitTele(params, body);
224224
var cod = term(Outer.Codomain, body);
225225
var doc = Doc.sep(kw, teleDoc, operator, cod);
226226
pair.names().forEach(nameGen::unbindName);
@@ -296,8 +296,8 @@ yield visitFn(defVar(def.ref()), def.modifiers(), absTele,
296296
};
297297
}
298298

299-
@Override public @NotNull Doc visitTele(@NotNull Seq<? extends ParamLike<Term>> telescope) {
300-
return visitTele(telescope, null, FindUsage::free);
299+
public @NotNull Doc visitTele(@NotNull Seq<? extends ParamLike<Term>> telescope, @Nullable Term body) {
300+
return visitTele(telescope, body, FindUsage::free);
301301
}
302302

303303
public @NotNull Doc def(@NotNull JitDef unit) {
@@ -337,19 +337,20 @@ yield visitFn(defVar(def.ref()), def.modifiers(), absTele,
337337
line1.append(name);
338338

339339
var tele = enrich(telescope);
340+
var subst = tele.<Term>map(x -> new FreeTerm(x.ref()));
341+
var result = telescope.result(subst);
342+
340343
line1.append(visitTele(tele));
341344
line1.append(HAS_TYPE);
342-
343-
var subst = tele.<Term>map(x -> new FreeTerm(x.ref()));
344-
line1.append(term(Outer.Free, telescope.result(subst)));
345+
line1.append(term(Outer.Free, result));
345346

346347
var line1Doc = Doc.sepNonEmpty(line1);
347348
return cont.apply(line1Doc, subst);
348349
}
349350

350351
/// @param selfTele self tele of the constructor, unlike [JitCon], the data args/owner args should be supplied.
351352
private @NotNull Doc visitConRhs(@NotNull Doc name, boolean coerce, @NotNull AbstractTele selfTele) {
352-
return Doc.sepNonEmpty(coe(coerce), name, visitTele(enrich(selfTele)));
353+
return Doc.sepNonEmpty(coe(coerce), name, visitTele(enrich(selfTele), null));
353354
}
354355

355356
private @NotNull Doc visitCon(
@@ -382,7 +383,7 @@ yield visitFn(defVar(def.ref()), def.modifiers(), absTele,
382383
var dataArgs = richDataTele.<Term>map(t -> new FreeTerm(t.ref()));
383384

384385
var line1 = Doc.sepNonEmpty(KW_DATA, name,
385-
visitTele(richDataTele),
386+
visitTele(richDataTele, null),
386387
HAS_TYPE,
387388
term(Outer.Free, telescope.result(dataArgs)));
388389
var consDoc = dataDef.body().view().map(this::def);
@@ -454,7 +455,7 @@ record VisitTele(@NotNull Doc tele, @NotNull LazyValue<Doc> result) { }
454455

455456
private @NotNull VisitTele visitTele(@NotNull AbstractTele tele) {
456457
var richTele = enrich(tele);
457-
var teleDoc = visitTele(richTele);
458+
var teleDoc = visitTele(richTele, null);
458459

459460
return new VisitTele(teleDoc, LazyValue.of(() -> {
460461
var binds = richTele.<Term>map(x -> new FreeTerm(x.ref()));

0 commit comments

Comments
 (0)