Skip to content

Commit

Permalink
Remove Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
julianhyde committed Dec 13, 2023
1 parent b0505fe commit 39abcd3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/main/java/net/hydromatic/morel/compile/BuiltIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.hydromatic.morel.type.ForallType;
import net.hydromatic.morel.type.Keys;
import net.hydromatic.morel.type.PrimitiveType;
import net.hydromatic.morel.type.ProgressiveRecordType;
import net.hydromatic.morel.type.RecordType;
import net.hydromatic.morel.type.Type;
import net.hydromatic.morel.type.TypeSystem;
Expand Down Expand Up @@ -1252,7 +1251,7 @@ public enum BuiltIn {

/** Value "Sys.file", aka "file", of type "{...}" (partial record). */
SYS_FILE("Sys", "file", "file", ts ->
ts.progressiveRecordType(ProgressiveRecordType.DefaultHandler.INSTANCE,
ts.progressiveRecordType(
ImmutableSortedMap.<String, Type>orderedBy(RecordType.ORDERING)
.build())),

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/net/hydromatic/morel/eval/Directory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import net.hydromatic.morel.type.Keys;
import net.hydromatic.morel.type.PrimitiveType;
import net.hydromatic.morel.type.ProgressiveRecordType;
import net.hydromatic.morel.type.RecordType;
import net.hydromatic.morel.type.Type;
import net.hydromatic.morel.type.TypeSystem;
Expand Down Expand Up @@ -77,7 +76,6 @@ public Directory(File file) {
@Override public Type.Key typeKey() {
if (entries == null) {
return Keys.progressiveRecord(
ProgressiveRecordType.DefaultHandler.INSTANCE,
ImmutableSortedMap.of());
}
return Keys.record(Maps.transformValues(entries, Codes.TypedValue::typeKey));
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/net/hydromatic/morel/type/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ public static Type.Key tuple(List<? extends Type.Key> args) {

/** Returns a key that identifies a {@link ProgressiveRecordType}. */
public static Type.Key progressiveRecord(
ProgressiveRecordType.Handler handler,
SortedMap<String, ? extends Type.Key> argNameTypes) {
return new ProgressiveRecordKey(handler,
return new ProgressiveRecordKey(
ImmutableSortedMap.copyOfSorted(argNameTypes));
}

Expand Down Expand Up @@ -182,7 +181,7 @@ static StringBuilder describeRecordType(StringBuilder buf, int left,
* leaves other keys unchanged. */
public static Type.Key toProgressive(Type.Key key) {
if (key instanceof RecordKey) {
return progressiveRecord(ProgressiveRecordType.DefaultHandler.INSTANCE,
return progressiveRecord(
((RecordKey) key).argNameTypes);
}
return key;
Expand Down Expand Up @@ -443,18 +442,15 @@ public Type toType(TypeSystem typeSystem) {
}

private static class ProgressiveRecordKey extends Type.Key {
final ProgressiveRecordType.Handler handler;
final ImmutableSortedMap<String, Type.Key> argNameTypes;

ProgressiveRecordKey(ProgressiveRecordType.Handler handler,
ImmutableSortedMap<String, Type.Key> argNameTypes) {
ProgressiveRecordKey(ImmutableSortedMap<String, Type.Key> argNameTypes) {
super(Op.PROGRESSIVE_RECORD_TYPE);
this.handler = requireNonNull(handler, "handler");
this.argNameTypes = requireNonNull(argNameTypes);
}

@Override public Type.Key copy(UnaryOperator<Type.Key> transform) {
return progressiveRecord(handler,
return progressiveRecord(
Maps.transformValues(argNameTypes, transform::apply));
}

Expand All @@ -464,14 +460,13 @@ private static class ProgressiveRecordKey extends Type.Key {
}

@Override public int hashCode() {
return hash(argNameTypes, handler);
return argNameTypes.hashCode();
}

@Override public boolean equals(Object obj) {
return obj == this
|| obj instanceof ProgressiveRecordKey
&& ((ProgressiveRecordKey) obj).argNameTypes.equals(argNameTypes)
&& ((ProgressiveRecordKey) obj).handler.equals(handler);
&& ((ProgressiveRecordKey) obj).argNameTypes.equals(argNameTypes);
}

@Override public Type toType(TypeSystem typeSystem) {
Expand All @@ -484,7 +479,7 @@ private static class ProgressiveRecordKey extends Type.Key {
.put(ProgressiveRecordType.DUMMY, PrimitiveType.UNIT)
.build();
}
return new ProgressiveRecordType(handler, argNameTypes);
return new ProgressiveRecordType(argNameTypes);
}
}

Expand Down
18 changes: 2 additions & 16 deletions src/main/java/net/hydromatic/morel/type/ProgressiveRecordType.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,23 @@

import java.util.SortedMap;

import static java.util.Objects.requireNonNull;

/** Progressive record type. */
public class ProgressiveRecordType extends RecordType {
/** A field of this name indicates that a record type is progressive. */
public static final String DUMMY = "$dummy";

private final Handler handler;

/** Creates a ProgressiveRecordType. */
ProgressiveRecordType(Handler handler,
SortedMap<String, Type> argNameTypes) {
ProgressiveRecordType(SortedMap<String, Type> argNameTypes) {
super(argNameTypes);
assert argNameTypes.containsKey(DUMMY);
this.handler = requireNonNull(handler, "handler");
}

@Override public boolean isProgressive() {
return true;
}

@Override public Key key() {
return Keys.progressiveRecord(handler, Keys.toKeys(argNameTypes));
}

/** Handles mutations. */
public interface Handler {
}

public enum DefaultHandler implements Handler {
INSTANCE
return Keys.progressiveRecord(Keys.toKeys(argNameTypes));
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/hydromatic/morel/type/TypeSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,8 @@ public static boolean areContiguousIntegers(Iterable<String> strings) {

/** Creates a progressive record type. */
public ProgressiveRecordType progressiveRecordType(
ProgressiveRecordType.Handler handler,
SortedMap<String, ? extends Type> argNameTypes) {
Key key = Keys.progressiveRecord(handler, Keys.toKeys(argNameTypes));
Key key = Keys.progressiveRecord(Keys.toKeys(argNameTypes));
return (ProgressiveRecordType) typeFor(key);
}

Expand Down

0 comments on commit 39abcd3

Please sign in to comment.