Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct WIP #429

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
284e28a
REFACTOR: move TeleDecl.StructDecl to ClassDecl.StructDecl
mio-19 Jun 30, 2022
d4b4225
STRUCT: add RefTerm.Self
mio-19 Jun 1, 2022
2c99a71
REFACTOR: make StructDef and StructDecl classable
mio-19 Jun 30, 2022
ddc52d9
REFACTOR: move CallTerm.Struct to term.StructCall
mio-19 Jun 30, 2022
fd36c5b
STRUCT: rewrite StructCall
mio-19 Jun 30, 2022
503d9de
REFACTOR: move StructField to ClassDecl.StructDecl.StructField
mio-19 Jul 1, 2022
8adb04e
MISC: fix idea indent
mio-19 Jul 1, 2022
9616625
STRUCT: add parents in ClassDecl
mio-19 Jul 1, 2022
7a3d1c6
STRUCT: ideas about inheritance desugar
mio-19 Jul 2, 2022
fc8f77f
STRUCT: remove selfTele from FieldDef
mio-19 Jul 3, 2022
200ea0e
STRUCT: implement Field inheritance check
mio-19 Jul 3, 2022
983a8a1
STRUCT: rempve structArgs from Access
mio-19 Jul 3, 2022
7fa15b3
STRUCT: add fieldMap for StructDef
mio-19 Jul 5, 2022
80f5b94
STRUCT: add lookupField in StructDef
mio-19 Jul 5, 2022
c9bc2bf
STRUCT: implement IntroTerm.New
mio-19 Jul 5, 2022
1384d9d
STRUCT: remove RefTerm.Self from core - it will be a syntactic sugar
mio-19 Jul 5, 2022
fb00b8b
STRUCT: partially implement implicit override
mio-19 Jul 8, 2022
621a226
STRUCT: declare StmtResolver.resolveStructDecl()
mio-19 Jul 8, 2022
7d206e6
CLASSABLE: implement LittleTyper.defCall
mio-19 Jul 14, 2022
364c6ad
CLASSABLE: implement part of StmtResolver.resolveStructDecl
mio-19 Jul 14, 2022
45ab617
CLASSABLE: implement a part of DefEq.doCompareTyped for StructCall
mio-19 Jul 14, 2022
60776b3
MISC: fix fmt
mio-19 Jul 16, 2022
66e9611
STRUCT: implement CoreDistiller for StructCall
mio-19 Jul 16, 2022
6d05e93
STRUCT: fill ExprTycker.inferRef()
mio-19 Jul 16, 2022
f6a5071
STRUCT: implement LittleTyper.term(CallTerm.Access)
mio-19 Jul 16, 2022
d7701a8
STRUCT: implement StructCall compare
mio-19 Jul 20, 2022
284a0e4
STRUCT: roughly implement ExprTycker synth for Expr.NewExpr
mio-19 Jul 26, 2022
6e3e577
save kiwa changes
mio-19 Jul 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions base/src/main/java/org/aya/concrete/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ public RefExpr(@NotNull SourcePos sourcePos, @NotNull Var resolvedVar) {

}

record SelfExpr(
@NotNull SourcePos sourcePos,
@Nullable Var resolvedStruct
) implements Expr {
}

record LiftExpr(@NotNull SourcePos sourcePos, @NotNull Expr expr, int lift) implements Expr {}

/**
Expand Down
162 changes: 161 additions & 1 deletion base/src/main/java/org/aya/concrete/stmt/ClassDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.concrete.stmt;

import kala.collection.Map;
import kala.collection.immutable.ImmutableMap;
import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableHashMap;
import kala.collection.mutable.MutableMap;
import kala.control.Option;
import org.aya.concrete.Expr;
import org.aya.concrete.Pattern;
import org.aya.core.def.ClassDef;
import org.aya.core.def.Def;
import org.aya.core.def.FieldDef;
import org.aya.core.def.StructDef;
import org.aya.core.term.StructCall;
import org.aya.ref.DefVar;
import org.aya.resolve.context.Context;
import org.aya.util.MutableGraph;
import org.aya.util.binop.OpDecl;
import org.aya.util.error.SourcePos;
import org.jetbrains.annotations.NotNull;
Expand All @@ -16,7 +29,7 @@
* @author zaoqi
* @see Decl
*/
public non-sealed/*sealed*/ abstract class ClassDecl extends CommonDecl implements Decl.Resulted, Decl.TopLevel {
public sealed abstract class ClassDecl extends CommonDecl implements Decl.Resulted, Decl.TopLevel {
private final @NotNull Decl.Personality personality;
public @Nullable Context ctx = null;
public @NotNull Expr result;
Expand Down Expand Up @@ -58,4 +71,151 @@ protected ClassDecl(
@Override public String toString() {
return getClass().getSimpleName() + "[" + ref().name() + "]";
}

/**
* Concrete structure definition
*
* @author vont
*/
public static final class StructDecl extends ClassDecl {
public final @NotNull DefVar<StructDef, StructDecl> ref;
public @NotNull
final ImmutableSeq<StructField> fields;
// `StructCall`s
// This will be desugared so StructDef doesn't need to store this.
public final @NotNull ImmutableSeq<Expr> parents;
// set in tyck
// rootRef -> StructField
public @Nullable ImmutableMap<DefVar<FieldDef, StructField>, StructField> fieldMap = null;
public int ulift;

public @NotNull ImmutableMap<DefVar<FieldDef, StructField>, StructField> calculateFieldMap(
@NotNull ImmutableSeq<StructCall> parents
) {
if (fieldMap == null) fieldMap = collectFields(parents).toImmutableMap();
return fieldMap;
}

private @NotNull MutableMap<DefVar<FieldDef, StructField>, StructField> collectFields(@NotNull ImmutableSeq<StructCall> parents) {
var fieldMap = MutableMap.create();
for (var parent : parents) {
// TODO: diamond inheritance
var implicitOverrides = parent.params();
var implicitFields = implicitOverrides.map(t -> new StructField(t._1.concrete, (Expr) (Object) t._2.term())); // TODO: implement this
parent.ref().concrete.fieldMap.forEach((field, structField) -> {
var x = fieldMap.put(field.concrete.rootRef, structField);
if (x.isDefined()) {
throw new IllegalStateException("Duplicate field: " + field); // TODO: better error
}
});
implicitFields.forEach(field -> {
var x = fieldMap.put(field.rootRef, field);
if (x.isDefined()) {
throw new IllegalStateException("Duplicate field: " + field); // TODO: better error
}
});
}
}

public StructDecl(
@NotNull SourcePos sourcePos, @NotNull SourcePos entireSourcePos,
@NotNull Accessibility accessibility,
@Nullable OpInfo opInfo,
@NotNull String name,
@NotNull Expr result,
@NotNull ImmutableSeq<Expr> parents,
@NotNull ImmutableSeq<StructField> fields,
@NotNull BindBlock bindBlock,
@NotNull Decl.Personality personality
) {
super(sourcePos, entireSourcePos, opInfo, bindBlock, result, personality, accessibility);
this.parents = parents;
this.fields = fields;
this.ref = DefVar.concrete(this, name);
fields.forEach(field -> field.structRef = ref);
}

@Override public @NotNull DefVar<StructDef, StructDecl> ref() {
return ref;
}

public static final class StructField extends CommonDecl implements Decl.Telescopic, Decl.Resulted {
public final @NotNull DefVar<FieldDef, StructField> rootRef;
public final @NotNull Option<DefVar<FieldDef, StructField>> parentRef;
public final @NotNull DefVar<FieldDef, StructField> ref;
public DefVar<StructDef, StructDecl> structRef;
public @NotNull ImmutableSeq<Pattern.Clause> clauses;
public @NotNull Expr result;
public @NotNull Option<Expr> body;
public final boolean coerce;

// will change after resolve
public @NotNull ImmutableSeq<Expr.Param> telescope;
public @Nullable Def.Signature signature;

public StructField(
@NotNull SourcePos sourcePos, @NotNull SourcePos entireSourcePos,
@Nullable OpInfo opInfo,
@NotNull String name,
@NotNull ImmutableSeq<Expr.Param> telescope,
@NotNull Expr result,
@NotNull Option<Expr> body,
@NotNull ImmutableSeq<Pattern.Clause> clauses,
boolean coerce,
@NotNull BindBlock bindBlock
) {
super(sourcePos, entireSourcePos, Accessibility.Public, opInfo, bindBlock);
this.coerce = coerce;
this.result = result;
this.clauses = clauses;
this.body = body;
this.ref = DefVar.concrete(this, name);
this.rootRef = this.ref;
this.parentRef = Option.none();
this.telescope = telescope;
}

public StructField(@NotNull StructField parent, @NotNull Expr result) {
super(parent.sourcePos, parent.entireSourcePos, Accessibility.Public, parent.opInfo, parent.bindBlock);
if (parent.telescope.isNotEmpty() || parent.body.isDefined() || parent.clauses.isNotEmpty() || parent.coerce)
throw new UnsupportedOperationException("TODO");
this.coerce = parent.coerce;
this.result = result;
this.clauses = parent.clauses;
this.body = parent.body;
this.ref = DefVar.concrete(this, parent.ref.name());
this.rootRef = parent.rootRef;
this.parentRef = Option.some(parent.ref);
this.telescope = parent.telescope;
}

@Override public @NotNull DefVar<FieldDef, StructField> ref() {
return ref;
}

@Override public @NotNull ImmutableSeq<Expr.Param> telescope() {
return telescope;
}

@Override public void setTelescope(@NotNull ImmutableSeq<Expr.Param> telescope) {
this.telescope = telescope;
}

@Override public @Nullable Def.Signature signature() {
return signature;
}

@Override public void setSignature(Def.@Nullable Signature signature) {
this.signature = signature;
}

@Override public @NotNull Expr result() {
return result;
}

@Override public void setResult(@NotNull Expr result) {
this.result = result;
}
}
}
}
2 changes: 1 addition & 1 deletion base/src/main/java/org/aya/concrete/stmt/CommonDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @apiNote This class should only be used in extends and permits clause. Use {@link Decl} elsewhere instead.
* @see Decl
*/
public sealed abstract class CommonDecl implements Decl permits ClassDecl, TeleDecl, TeleDecl.DataCtor, TeleDecl.StructField {
public sealed abstract class CommonDecl implements Decl permits ClassDecl, TeleDecl, TeleDecl.DataCtor, ClassDecl.StructDecl.StructField {
public final @NotNull Accessibility accessibility;
public final @NotNull SourcePos sourcePos;
public final @NotNull SourcePos entireSourcePos;
Expand Down
4 changes: 2 additions & 2 deletions base/src/main/java/org/aya/concrete/stmt/Decl.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ enum Personality {
*
* @author kiva
*/
sealed interface Telescopic permits TeleDecl, TeleDecl.DataCtor, TeleDecl.StructField {
sealed interface Telescopic permits TeleDecl, TeleDecl.DataCtor, ClassDecl.StructDecl.StructField {
@NotNull ImmutableSeq<Expr.Param> telescope();
void setTelescope(@NotNull ImmutableSeq<Expr.Param> telescope);
@Nullable Def.Signature signature();
Expand All @@ -94,7 +94,7 @@ sealed interface TopLevel permits ClassDecl, TeleDecl {
*
* @author kiva
*/
sealed interface Resulted permits ClassDecl, TeleDecl, TeleDecl.StructField {
sealed interface Resulted permits ClassDecl, TeleDecl, ClassDecl.StructDecl.StructField {
@NotNull Expr result();
void setResult(@NotNull Expr result);
}
Expand Down
96 changes: 0 additions & 96 deletions base/src/main/java/org/aya/concrete/stmt/TeleDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableList;
import kala.control.Either;
import kala.control.Option;
import org.aya.concrete.Expr;
import org.aya.concrete.Pattern;
import org.aya.core.def.*;
Expand Down Expand Up @@ -210,101 +209,6 @@ public DataDecl(
}
}

/**
* Concrete structure definition
*
* @author vont
*/
public static final class StructDecl extends TeleDecl {
public final @NotNull DefVar<StructDef, StructDecl> ref;
public @NotNull
final ImmutableSeq<StructField> fields;
public int ulift;

public StructDecl(
@NotNull SourcePos sourcePos, @NotNull SourcePos entireSourcePos,
@NotNull Accessibility accessibility,
@Nullable OpInfo opInfo,
@NotNull String name,
@NotNull ImmutableSeq<Expr.Param> telescope,
@NotNull Expr result,
// @NotNull ImmutableSeq<String> superClassNames,
@NotNull ImmutableSeq<StructField> fields,
@NotNull BindBlock bindBlock,
@NotNull Decl.Personality personality
) {
super(sourcePos, entireSourcePos, accessibility, opInfo, bindBlock, telescope, result, personality);
this.fields = fields;
this.ref = DefVar.concrete(this, name);
fields.forEach(field -> field.structRef = ref);
}

@Override public @NotNull DefVar<StructDef, StructDecl> ref() {
return ref;
}
}

public static final class StructField extends CommonDecl implements Decl.Telescopic, Decl.Resulted {
public final @NotNull DefVar<FieldDef, TeleDecl.StructField> ref;
public DefVar<StructDef, StructDecl> structRef;
public @NotNull ImmutableSeq<Pattern.Clause> clauses;
public @NotNull Expr result;
public @NotNull Option<Expr> body;
public final boolean coerce;

// will change after resolve
public @NotNull ImmutableSeq<Expr.Param> telescope;
public @Nullable Def.Signature signature;

public StructField(
@NotNull SourcePos sourcePos, @NotNull SourcePos entireSourcePos,
@Nullable OpInfo opInfo,
@NotNull String name,
@NotNull ImmutableSeq<Expr.Param> telescope,
@NotNull Expr result,
@NotNull Option<Expr> body,
@NotNull ImmutableSeq<Pattern.Clause> clauses,
boolean coerce,
@NotNull BindBlock bindBlock
) {
super(sourcePos, entireSourcePos, Accessibility.Public, opInfo, bindBlock);
this.coerce = coerce;
this.result = result;
this.clauses = clauses;
this.body = body;
this.ref = DefVar.concrete(this, name);
this.telescope = telescope;
}

@Override public @NotNull DefVar<FieldDef, StructField> ref() {
return ref;
}

@Override public @NotNull ImmutableSeq<Expr.Param> telescope() {
return telescope;
}

@Override public void setTelescope(@NotNull ImmutableSeq<Expr.Param> telescope) {
this.telescope = telescope;
}

@Override public @Nullable Def.Signature signature() {
return signature;
}

@Override public void setSignature(Def.@Nullable Signature signature) {
this.signature = signature;
}

@Override public @NotNull Expr result() {
return result;
}

@Override public void setResult(@NotNull Expr result) {
this.result = result;
}
}

/**
* Concrete function definition
*
Expand Down
1 change: 1 addition & 0 deletions base/src/main/java/org/aya/concrete/visitor/ExprView.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public interface ExprView {
private @NotNull Expr traverse(@NotNull Expr expr) {
return switch (expr) {
case Expr.RefExpr ref -> ref; // I don't know
case Expr.SelfExpr ref -> ref;
case Expr.UnresolvedExpr unresolved -> unresolved;
case Expr.LamExpr lam -> {
var param = commit(lam.param());
Expand Down
5 changes: 2 additions & 3 deletions base/src/main/java/org/aya/concrete/visitor/StmtOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ default void visitDecl(@NotNull Decl decl, P pp) {
if (decl instanceof Decl.Telescopic teleDecl) visitTelescopic(decl, teleDecl, pp);
if (decl instanceof Decl.Resulted resulted) resulted.setResult(visitExpr(resulted.result(), pp));
switch (decl) {
case ClassDecl classDecl -> {}
case TeleDecl.PrimDecl prim -> {}
case TeleDecl.DataDecl data -> data.body.forEach(ctor -> traced(ctor, pp, this::visitDecl));
case TeleDecl.StructDecl struct -> struct.fields.forEach(field -> traced(field, pp, this::visitDecl));
case ClassDecl.StructDecl struct -> struct.fields.forEach(field -> traced(field, pp, this::visitDecl));
case TeleDecl.FnDecl fn -> fn.body = fn.body.map(
expr -> visitExpr(expr, pp),
clauses -> clauses.map(clause -> visitClause(clause, pp))
Expand All @@ -65,7 +64,7 @@ default void visitDecl(@NotNull Decl decl, P pp) {
ctor.patterns = ctor.patterns.map(pat -> visitPattern(pat, pp));
ctor.clauses = ctor.clauses.map(clause -> visitClause(clause, pp));
}
case TeleDecl.StructField field -> {
case ClassDecl.StructDecl.StructField field -> {
field.clauses = field.clauses.map(clause -> visitClause(clause, pp));
field.body = field.body.map(expr -> visitExpr(expr, pp));
}
Expand Down
17 changes: 16 additions & 1 deletion base/src/main/java/org/aya/core/def/ClassDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.core.def;

import kala.collection.immutable.ImmutableSeq;
import org.aya.concrete.stmt.ClassDecl;
import org.aya.core.term.FormTerm;
import org.aya.core.term.Term;
import org.aya.distill.CoreDistiller;
import org.aya.generic.AyaDocile;
import org.aya.pretty.doc.Doc;
import org.aya.ref.DefVar;
import org.aya.util.distill.DistillerOptions;
import org.jetbrains.annotations.NotNull;

public non-sealed/*sealed*/ interface ClassDef extends AyaDocile, GenericDef {
public sealed interface ClassDef extends AyaDocile, GenericDef {
@Override default @NotNull Doc toDoc(@NotNull DistillerOptions options) {
return new CoreDistiller(options).def(this);
}

@Override @NotNull DefVar<? extends ClassDef, ? extends ClassDecl> ref();

abstract sealed class Type implements ClassDef permits StructDef {
public final int resultLevel;

protected Type(int resultLevel) {
super();
this.resultLevel = resultLevel;
}
@Override public @NotNull Term result() {
return new FormTerm.Univ(resultLevel);
}
}
}
Loading