Skip to content

Commit

Permalink
misc: more code review
Browse files Browse the repository at this point in the history
Co-authored-by: Hoshino Tented <hoshinotented@qq.com>
  • Loading branch information
ice1000 and HoshinoTented committed Dec 12, 2023
1 parent 41f14d2 commit 6fb5542
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
5 changes: 1 addition & 4 deletions base/src/main/java/org/aya/core/serde/CompiledAya.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ private void ser(@NotNull ImmutableSeq<GenericDef> defs) {
}

private void serDef(@NotNull AyaShape.Factory factory, @NotNull GenericDef def) {
var shapeResultMaybe = factory.find(def)
.map(x -> SerDef.SerShapeResult.serialize(state, x))
.getOrNull();
var serDef = new Serializer(state).serialize(def, shapeResultMaybe);
var serDef = new Serializer(state, factory).serialize(def);
serDefs.append(serDef);
serOp(serDef, def);
switch (serDef) {
Expand Down
2 changes: 1 addition & 1 deletion base/src/main/java/org/aya/core/serde/SerTerm.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ sealed interface SerShapedApplicable extends Serializable permits SerIntegerOps
record ConInfo(
SerDef.SerShapeResult result,
SerTerm.Data data
) {}
) implements Serializable {}

record SerIntegerOps(
@NotNull SerDef.QName ref,
Expand Down
24 changes: 15 additions & 9 deletions base/src/main/java/org/aya/core/serde/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import kala.tuple.Tuple;
import org.aya.core.def.*;
import org.aya.core.pat.Pat;
import org.aya.core.repr.AyaShape;
import org.aya.core.term.*;
import org.aya.generic.Shaped;
import org.aya.guest0x0.cubical.Partial;
Expand All @@ -18,21 +19,25 @@
import org.aya.util.error.InternalException;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @author ice1000
*/
public record Serializer(@NotNull Serializer.State state) {
public @NotNull SerDef serialize(@NotNull GenericDef def, @Nullable SerDef.SerShapeResult shapeResult) {
public record Serializer(
@NotNull Serializer.State state,
@NotNull AyaShape.Factory factory
) {
public @NotNull SerDef serialize(@NotNull GenericDef def) {
return switch (def) {
case FnDef fn -> new SerDef.Fn(
state.def(fn.ref),
serializeParams(fn.telescope),
fn.body.map(this::serialize, matchings -> matchings.map(this::serialize)),
fn.modifiers,
serialize(fn.result),
shapeResult
factory.find(def)
.map(x -> SerDef.SerShapeResult.serialize(state, x))
.getOrNull()
);
case MemberDef field -> new SerDef.Field(
state.def(field.classRef),
Expand All @@ -43,13 +48,13 @@ public record Serializer(@NotNull Serializer.State state) {
);
case ClassDef clazz -> new SerDef.Clazz(
state.def(clazz.ref()),
clazz.members.map(field -> (SerDef.Field) serialize(field, null))
clazz.members.map(field -> (SerDef.Field) serialize(field))
);
case DataDef data -> new SerDef.Data(
state.def(data.ref),
serializeParams(data.telescope),
serialize(data.result),
data.body.map(ctor -> (SerDef.Ctor) serialize(ctor, null))
data.body.map(ctor -> (SerDef.Ctor) serialize(ctor))
);
case PrimDef prim -> {
assert prim.ref.module != null;
Expand Down Expand Up @@ -185,9 +190,10 @@ case PAppTerm(var of, var args, var cube) -> new SerTerm.PathApp(serialize(of),

private @NotNull SerTerm.SerShapedApplicable serializeShapedApplicable(@NotNull Shaped.Applicable<Term, ?, ?> shapedApplicable) {
return switch (shapedApplicable) {
case IntegerOps.ConRule conRule -> new SerTerm.SerIntegerOps(state.def(conRule.ref()), Either.left(new SerTerm.ConInfo(
SerDef.SerShapeResult.serialize(state, conRule.paramRecognition()), (SerTerm.Data) serialize(conRule.paramType())
)));
case IntegerOps.ConRule conRule ->
new SerTerm.SerIntegerOps(state.def(conRule.ref()), Either.left(new SerTerm.ConInfo(
SerDef.SerShapeResult.serialize(state, conRule.paramRecognition()), (SerTerm.Data) serialize(conRule.paramType())
)));
case IntegerOps.FnRule fnRule -> new SerTerm.SerIntegerOps(state.def(fnRule.ref()), Either.right(fnRule.kind()));
default -> throw new IllegalStateException("Unexpected value: " + shapedApplicable);
};
Expand Down
4 changes: 2 additions & 2 deletions base/src/test/java/org/aya/core/SuedeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ prim strcat (str1 str2: String) : String
private void suedeAll(@Language("Aya") @NotNull String code) {
var res = TyckDeclTest.successTyckDecls(code);
var state = new SerTerm.DeState(res.component1());
var serializer = new Serializer(new Serializer.State());
var serializer = new Serializer(new Serializer.State(), res.component3());
res.component2().view()
.map(x -> serializer.serialize(x, null))
.map(serializer::serialize)
.map(ser -> ser.de(state))
.forEach(Assertions::assertNotNull);
}
Expand Down
5 changes: 3 additions & 2 deletions base/src/test/java/org/aya/tyck/TyckDeclTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import kala.collection.immutable.ImmutableSeq;
import kala.tuple.Tuple;
import kala.tuple.Tuple2;
import kala.tuple.Tuple3;
import org.aya.concrete.ParseTest;
import org.aya.concrete.stmt.Stmt;
import org.aya.concrete.stmt.decl.TeleDecl;
Expand Down Expand Up @@ -54,13 +55,13 @@ public static void header(@NotNull PrimDef.Factory factory, @NotNull TeleDecl<?>
return primFactory;
}

public static @NotNull Tuple2<PrimDef.Factory, ImmutableSeq<GenericDef>>
public static @NotNull Tuple3<PrimDef.Factory, ImmutableSeq<GenericDef>, AyaShape.Factory>
successTyckDecls(@Language("Aya") @NonNls @NotNull String text) {
var res = successDesugarDecls(text);
var shapes = new AyaShape.Factory();
return Tuple.of(res.component1(), res.component2().view()
.map(i -> i instanceof TeleDecl<?> s ? tyck(res.component1(), s, null, shapes) : null)
.filter(Objects::nonNull).toImmutableSeq());
.filter(Objects::nonNull).toImmutableSeq(), shapes);
}

public static @NotNull Tuple2<PrimDef.Factory, ImmutableSeq<GenericDef>>
Expand Down

0 comments on commit 6fb5542

Please sign in to comment.