Skip to content

Commit

Permalink
merge: Turn some classes into records (#1292)
Browse files Browse the repository at this point in the history
Including `DataDef` and `PrimDef`. Also some comments-related changes.
  • Loading branch information
ice1000 authored Jan 28, 2025
2 parents be37201 + f7e1471 commit ac52873
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 127 deletions.
18 changes: 9 additions & 9 deletions base/src/main/java/org/aya/primitive/PrimFactory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.primitive;

import java.util.EnumMap;
import java.util.function.BiFunction;
import java.util.function.Function;

import static org.aya.syntax.core.def.PrimDef.*;
import static org.aya.syntax.core.term.SortTerm.Type0;

import kala.collection.Map;
import kala.collection.immutable.ImmutableMap;
import kala.collection.immutable.ImmutableSeq;
Expand All @@ -26,13 +33,6 @@
import org.aya.util.ForLSP;
import org.jetbrains.annotations.NotNull;

import java.util.EnumMap;
import java.util.function.BiFunction;
import java.util.function.Function;

import static org.aya.syntax.core.def.PrimDef.*;
import static org.aya.syntax.core.term.SortTerm.Type0;

public class PrimFactory {
private final @NotNull Map<@NotNull ID, @NotNull PrimSeed> seeds;
private final @NotNull EnumMap<@NotNull ID, @NotNull PrimDefLike> defs = new EnumMap<>(ID.class);
Expand Down Expand Up @@ -166,7 +166,7 @@ public record PrimSeed(
ImmutableSeq.empty());

public @NotNull PrimDefLike factory(@NotNull ID name, @NotNull DefVar<PrimDef, PrimDecl> ref) {
var rst = new PrimDef.Delegate(seeds.get(name).supply(ref).ref);
var rst = new PrimDef.Delegate(seeds.get(name).supply(ref).ref());
definePrim(rst);
return rst;
}
Expand Down
4 changes: 2 additions & 2 deletions base/src/main/java/org/aya/primitive/ShapeMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ yield matchMany(MatchMode.OrderedEq, conLike.innerPats(), con.args().view(),

private boolean matchData(@NotNull DataShape shape, @NotNull DataDef data) {
if (!matchTele(shape.tele(), data.telescope())) return false;
return matchInside(() -> captures.put(shape.name(), data.ref),
() -> matchMany(MatchMode.Eq, shape.cons(), data.body,
return matchInside(() -> captures.put(shape.name(), data.ref()),
() -> matchMany(MatchMode.Eq, shape.cons(), data.body(),
(s, c) -> captureIfMatches(s, c, this::matchCon, ConDef::ref)));
}

Expand Down
19 changes: 7 additions & 12 deletions base/src/main/java/org/aya/resolve/context/Context.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.resolve.context;

import java.nio.file.Path;
import java.util.function.Function;
import java.util.function.Predicate;

import kala.collection.SeqLike;
import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableList;
Expand All @@ -22,10 +26,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Path;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* @author re-xyr
*/
Expand Down Expand Up @@ -175,10 +175,7 @@ default MutableList<LocalVar> collect(@NotNull MutableList<LocalVar> container)
return iterate(c -> c.getModuleLocalMaybe(modName));
}

default @NotNull Context bind(
@NotNull LocalVar ref,
@NotNull Predicate<@Nullable AnyVar> toWarn
) {
default @NotNull Context bind(@NotNull LocalVar ref, @NotNull Predicate<@Nullable AnyVar> toWarn) {
return bind(ref.name(), ref, toWarn);
}

Expand Down Expand Up @@ -218,8 +215,6 @@ default MutableList<LocalVar> collect(@NotNull MutableList<LocalVar> container)
}

class ResolvingInterruptedException extends InterruptException {
@Override public InterruptStage stage() {
return InterruptStage.Resolving;
}
@Override public InterruptStage stage() { return InterruptStage.Resolving; }
}
}
2 changes: 1 addition & 1 deletion base/src/main/java/org/aya/tyck/StmtTycker.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private void checkPrim(@NotNull ExprTycker tycker, PrimDecl prim) {
tycker.unifyTermReported(
DepTypeTerm.makePi(tele.params().view().map(Param::type), tele.result()),
// No checks, slightly faster than TeleDef.defType
DepTypeTerm.makePi(core.telescope.view().map(Param::type), core.result),
DepTypeTerm.makePi(core.telescope().view().map(Param::type), core.result()),
null, prim.entireSourcePos(),
msg -> new PrimError.BadSignature(prim, msg, new UnifyInfo(tycker.state)));
var zonker = new Finalizer.Zonk<>(tycker);
Expand Down
23 changes: 11 additions & 12 deletions base/src/test/java/org/aya/tyck/TyckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.tyck;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
import java.util.function.Function;
import java.util.function.IntFunction;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import kala.collection.immutable.ImmutableSeq;
import kala.collection.immutable.primitive.ImmutableIntSeq;
import org.aya.normalize.Normalizer;
Expand All @@ -22,16 +32,6 @@
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
import java.util.function.Function;
import java.util.function.IntFunction;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TyckTest {
@Test public void test0() {
var result = tyck("""
Expand Down Expand Up @@ -159,8 +159,7 @@ class Monoid
""").defs.isNotEmpty());
}

@Test
public void what() {
@Test public void what() {
assertTrue(tyck("""
class Kontainer
| Taipe : Type
Expand Down
12 changes: 6 additions & 6 deletions cli-impl/src/main/java/org/aya/cli/library/LibraryCompiler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.cli.library;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableSet;
import org.aya.cli.library.incremental.CompilerAdvisor;
Expand Down Expand Up @@ -36,10 +40,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* @author kiva
*/
Expand Down Expand Up @@ -337,7 +337,7 @@ static void clear(@NotNull PrimFactory factory, @NotNull ImmutableSeq<Stmt> stmt
static void clear(@NotNull PrimFactory factory, @NotNull Stmt stmt) {
switch (stmt) {
case Command.Module mod -> clear(factory, mod.contents());
case PrimDecl decl when decl.ref.core != null -> factory.clear(decl.ref.core.id);
case PrimDecl decl when decl.ref.core != null -> factory.clear(decl.ref.core.id());
default -> { }
}
}
Expand Down
36 changes: 18 additions & 18 deletions ide/src/main/java/org/aya/ide/Resolver.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.ide;

import java.util.Objects;

import kala.collection.SeqView;
import kala.collection.mutable.MutableList;
import kala.control.Option;
Expand All @@ -12,9 +14,8 @@
import org.aya.ide.util.XY;
import org.aya.syntax.concrete.stmt.ModuleName;
import org.aya.syntax.concrete.stmt.StmtVisitor;
import org.aya.syntax.concrete.stmt.decl.DataCon;
import org.aya.syntax.concrete.stmt.decl.DataDecl;
import org.aya.syntax.concrete.stmt.decl.Decl;
import org.aya.syntax.concrete.stmt.decl.*;
import org.aya.syntax.core.def.ClassDef;
import org.aya.syntax.core.def.DataDef;
import org.aya.syntax.core.def.TyckDef;
import org.aya.syntax.core.term.Term;
Expand All @@ -24,8 +25,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public interface Resolver {
/** resolve a symbol by its qualified name in the whole library */
static @NotNull Option<@NotNull TyckDef> resolveDef(
Expand All @@ -45,9 +44,9 @@ public interface Resolver {
) {
var program = source.program().get();
if (program == null) return SeqView.empty();
var collect = MutableList.<WithPos<AnyVar>>create();
program.view().forEach(new XYResolver(xy, collect));
return collect.view().mapNotNull(pos -> switch (pos.data()) {
var collect = new XYResolver(xy, MutableList.create());
program.view().forEach(collect);
return collect.collect.view().mapNotNull(pos -> switch (pos.data()) {
case DefVar<?, ?> defVar -> new WithPos<>(pos.sourcePos(), defVar);
case LocalVar localVar -> new WithPos<>(pos.sourcePos(), localVar);
case ModuleVar moduleVar -> new WithPos<>(pos.sourcePos(), moduleVar);
Expand All @@ -65,17 +64,18 @@ public interface Resolver {

private static @NotNull SeqView<TyckDef> withChildren(@NotNull TyckDef def) {
return switch (def) {
case DataDef data -> SeqView.<TyckDef>of(data).appendedAll(data.body);
// case ClassDef struct -> SeqView.<Def>of(struct).appendedAll(struct.members);
case DataDef data -> SeqView.<TyckDef>of(data).appendedAll(data.body());
case ClassDef struct -> SeqView.<TyckDef>of(struct).appendedAll(struct.members());
default -> SeqView.of(def);
};
}

static @NotNull SeqView<DefVar<?, ?>> withChildren(@NotNull Decl def) {
return switch (def) {
case DataDecl data -> SeqView.<DefVar<?, ?>>of(data.ref).appendedAll(data.body.clauses.map(DataCon::ref));
// case ClassDecl struct ->
// SeqView.<DefVar<?, ?>>of(struct.ref).appendedAll(struct.members.map(TeleDecl.ClassMember::ref));
case DataDecl data -> SeqView.<DefVar<?, ?>>of(data.ref)
.appendedAll(data.body.clauses.map(DataCon::ref));
case ClassDecl struct -> SeqView.<DefVar<?, ?>>of(struct.ref)
.appendedAll(struct.members.map(ClassMember::ref));
default -> SeqView.of(def.ref());
};
}
Expand Down Expand Up @@ -110,12 +110,12 @@ public interface Resolver {
* @author ice1000, kiva, wsx
*/
record XYResolver(XY xy, MutableList<WithPos<AnyVar>> collect) implements StmtVisitor {
@Override
public void visitVar(@NotNull SourcePos pos, @NotNull AnyVar var, @NotNull LazyValue<@Nullable Term> type) {
@Override public void
visitVar(@NotNull SourcePos pos, @NotNull AnyVar var, @NotNull LazyValue<@Nullable Term> type) {
if (xy.inside(pos)) collect.append(new WithPos<>(pos, var));
}
@Override
public void visitVarDecl(@NotNull SourcePos pos, @NotNull AnyVar var, @NotNull LazyValue<@Nullable Term> type) {
@Override public void
visitVarDecl(@NotNull SourcePos pos, @NotNull AnyVar var, @NotNull LazyValue<@Nullable Term> type) {
if (var instanceof LocalVar v && v.isGenerated()) return;
StmtVisitor.super.visitVarDecl(pos, var, type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.compiler.serializers;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;

import kala.collection.immutable.ImmutableMap;
import kala.collection.immutable.ImmutableSeq;
import kala.tuple.Tuple;
Expand All @@ -16,9 +19,6 @@
import org.aya.syntax.ref.DefVar;
import org.jetbrains.annotations.NotNull;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;

// You should compile this with its constructors
public final class DataSerializer extends JitTeleSerializer<DataDef> {
private final @NotNull ShapeFactory shapeFactory;
Expand Down Expand Up @@ -60,7 +60,7 @@ protected CodeShape.GlobalId[] buildRecognition(DataDef unit) {
// The capture is one-to-one
var flipped = ImmutableMap.from(recog.captures().view()
.map((k, v) -> Tuple.<DefVar<?, ?>, CodeShape.GlobalId>of(((TyckAnyDef<?>) v).ref, k)));
var capture = unit.body.map(x -> flipped.get(x.ref));
var capture = unit.body().map(x -> flipped.get(x.ref));
return capture.toArray(CodeShape.GlobalId.class);
}

Expand All @@ -71,7 +71,7 @@ protected CodeShape.GlobalId[] buildRecognition(DataDef unit) {

@Override
protected @NotNull ImmutableSeq<FreeJavaExpr> superConArgs(@NotNull FreeCodeBuilder builder, DataDef unit) {
return super.superConArgs(builder, unit).appended(builder.iconst(unit.body.size()));
return super.superConArgs(builder, unit).appended(builder.iconst(unit.body().size()));
}

/**
Expand All @@ -81,13 +81,13 @@ private void buildConstructors(@NotNull FreeCodeBuilder builder, DataDef unit) {
var cons = Constants.JITDATA_CONS;
var consRef = builder.refField(cons, builder.thisRef());

if (unit.body.isEmpty()) {
if (unit.body().isEmpty()) {
builder.returnWith(consRef);
return;
}

builder.ifNull(builder.getArray(consRef, 0), cb -> {
unit.body.forEachIndexed((idx, con) -> {
unit.body().forEachIndexed((idx, con) -> {
cb.updateArray(consRef, idx, AbstractExprializer.getInstance(builder, con));
});
}, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.compiler.serializers;

import java.lang.constant.ClassDesc;

import static org.aya.compiler.serializers.NameSerializer.getReference;

import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableList;
import org.aya.compiler.free.FreeClassBuilder;
Expand All @@ -19,10 +23,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;

import java.lang.constant.ClassDesc;

import static org.aya.compiler.serializers.NameSerializer.getReference;

/**
* Serializing a module, note that it may not a file module, so we need not to make importing.
*/
Expand All @@ -49,7 +49,7 @@ public ModuleSerializer(@NotNull ShapeFactory shapeFactory) {

private void serializeCons(@NotNull FreeClassBuilder builder, @NotNull DataDef dataDef) {
var ser = new ConSerializer(recorder);
dataDef.body.forEach(con -> ser.serialize(builder, con));
dataDef.body().forEach(con -> ser.serialize(builder, con));
}

private void serializeMems(@NotNull FreeClassBuilder builder, @NotNull ClassDef classDef) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Copyright (c) 2020-2025 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.compiler.serializers;

import java.lang.constant.ClassDesc;

import kala.collection.immutable.ImmutableSeq;
import org.aya.compiler.free.FreeClassBuilder;
import org.aya.compiler.free.FreeCodeBuilder;
Expand All @@ -12,8 +14,6 @@
import org.aya.syntax.core.term.call.PrimCall;
import org.jetbrains.annotations.NotNull;

import java.lang.constant.ClassDesc;

public final class PrimSerializer extends JitTeleSerializer<PrimDef> {
public PrimSerializer(ModuleSerializer.@NotNull MatchyRecorder recorder) {
super(JitPrim.class, recorder);
Expand All @@ -26,7 +26,7 @@ public PrimSerializer(ModuleSerializer.@NotNull MatchyRecorder recorder) {

@Override
protected @NotNull ImmutableSeq<FreeJavaExpr> superConArgs(@NotNull FreeCodeBuilder builder, PrimDef unit) {
return super.superConArgs(builder, unit).appended(builder.refEnum(unit.id));
return super.superConArgs(builder, unit).appended(builder.refEnum(unit.id()));
}

@Override public @NotNull PrimSerializer serialize(@NotNull FreeClassBuilder builder, PrimDef unit) {
Expand Down
Loading

0 comments on commit ac52873

Please sign in to comment.