Skip to content

Commit

Permalink
STRUCT: ideas about inheritance desugar
Browse files Browse the repository at this point in the history
  • Loading branch information
mio-19 committed Jul 2, 2022
1 parent 7ee52aa commit 48926ea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions base/src/main/java/org/aya/concrete/stmt/ClassDecl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// 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.ImmutableSeq;
import kala.collection.mutable.MutableHashMap;
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.binop.OpDecl;
Expand Down Expand Up @@ -78,8 +81,31 @@ public static final class StructDecl extends ClassDecl {
// `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 Map<DefVar<FieldDef, StructField>, StructField> fieldMap = null;
public int ulift;

public void calculateFieldMap(@NotNull ImmutableSeq<StructCall> parents) {
if(fieldMap != null) {
return;
}
MutableHashMap<DefVar<FieldDef, StructField>, StructField> fieldMap = MutableHashMap.create();
this.fieldMap = fieldMap;
for(var parent : parents) {
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
}
});
}
for(var parent : parents) {
var implicitOverrides = parent.params();
// TODO
}
}

public StructDecl(
@NotNull SourcePos sourcePos, @NotNull SourcePos entireSourcePos,
@NotNull Accessibility accessibility,
Expand All @@ -103,6 +129,8 @@ public StructDecl(
}

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;
Expand Down Expand Up @@ -131,6 +159,8 @@ public StructField(
this.clauses = clauses;
this.body = body;
this.ref = DefVar.concrete(this, name);
this.rootRef = this.ref;
this.parentRef = Option.none();
this.telescope = telescope;
}

Expand Down
6 changes: 6 additions & 0 deletions base/src/main/java/org/aya/tyck/StmtTycker.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.aya.core.repr.AyaShape;
import org.aya.core.term.CallTerm;
import org.aya.core.term.FormTerm;
import org.aya.core.term.StructCall;
import org.aya.core.term.Term;
import org.aya.generic.Modifier;
import org.aya.tyck.error.NobodyError;
Expand Down Expand Up @@ -107,6 +108,11 @@ private void tracing(@NotNull Consumer<Trace.@NotNull Builder> consumer) {
}
case TeleDecl.PrimDecl decl -> decl.ref.core;
case ClassDecl.StructDecl decl -> {
var parents = decl.parents.map(parent -> {
var struct = tycker.synthesize(parent).wellTyped();
if (!(struct instanceof StructCall call)) throw new UnsupportedOperationException("TODO");
return call;
});
var body = decl.fields.map(field -> (FieldDef) traced(field, tycker, this::tyck));
yield new StructDef(decl.ref, decl.ulift, body);
}
Expand Down

0 comments on commit 48926ea

Please sign in to comment.