Skip to content

Commit

Permalink
Java code generation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Christine Zhou committed Dec 13, 2024
1 parent 545dec4 commit ca9ea84
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Src/PCompiler/CompilerCore/Backend/Java/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static IEnumerable<string> ImportStatements()

#region Event source generation

public static readonly string EventNamespaceName = "Events";
public static readonly string EventNamespaceName = "PEvents";
public static readonly string EventDefnFileName = $"{EventNamespaceName}.java";

#endregion
Expand Down Expand Up @@ -227,9 +227,9 @@ internal static string AsFFIComment(string line)
internal static readonly string PValueClass = "prt.values.PValue";

/// <summary>
/// The fully-qualified class name of the Java P runtime's Event class.
/// The fully-qualified class name of the Java P runtime's PEvent class.
/// </summary>
internal static readonly string EventsClass = "prt.events.Event";
internal static readonly string EventsClass = "prt.events.PEvent";

#endregion

Expand Down
27 changes: 27 additions & 0 deletions Src/PCompiler/CompilerCore/Backend/Java/EventGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,34 @@ private void WriteEventDecl(Event e)
{
WriteLine($" return \"{eventName}\"; }}");
}

// Write the copy constructor for cloning.
WriteLine();
WriteLine("@Override");
WriteLine($"public {eventName} deepClone() {{");
Write($"return new {eventName}(");
if (hasPayload)
{
WriteClone(argType, () => Write("payload"));
}
WriteLine(");");
WriteLine("} // deepClone()");

// Deep equality predicate.
WriteLine();
WriteLine("@Override");
WriteLine($"public boolean deepEquals({Constants.EventsClass}<{payloadRefType}> other) {{");
WriteLine("return (true");
if (hasPayload)
{
Write(" && ");
WriteLine(argType.IsPrimitive
? $"this.payload == other.getPayload()"
: $"{Constants.PrtDeepEqualsMethodName}(this.payload, other.getPayload())");
}
WriteLine(");");
WriteLine("} // deepEquals()");

WriteLine($"}} // {eventName}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ internal void WriteClone(TypeManager.JType t, Action writeTermToBeCloned)
case TypeManager.JType.JSet _:
case TypeManager.JType.JForeign _:
case TypeManager.JType.JNamedTuple _:
case TypeManager.JType.JEvent _:
Write($"{Constants.PrtDeepCloneMethodName}(");
writeTermToBeCloned();
Write(")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ private void WriteExpr(IPExpr expr)
// construct a new List from that collection.
Write($"new {mt.ValueCollectionType}(");
WriteExpr(ve.Expr);
Write($".{mt.KeysMethodName}()");
Write($".{mt.ValuesMethodName}()");
Write(")");
break;
}
Expand Down
6 changes: 6 additions & 0 deletions Src/PCompiler/CompilerCore/Backend/Java/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ internal JMap(JType k, JType v)
/// The type of a collection containing the keys of this Map.
/// </summary>
internal string KeyCollectionType => $"ArrayList<{_k.ReferenceTypeName}>";

/// <summary>
/// The name of the () -> Set method that produces the values in the map.
/// </summary>
internal string ValuesMethodName => "values";

/// <summary>
/// The type of a collection containing the keys of this Map.
Expand Down Expand Up @@ -307,6 +312,7 @@ internal JEvent()
_unboxedType = $"{Constants.EventsClass}<?>";
}
internal override bool IsPrimitive => false;
internal override string DefaultValue => "null";
}

internal class JVoid : JType
Expand Down
14 changes: 13 additions & 1 deletion Src/PRuntimes/PJavaRuntime/src/main/java/prt/events/PEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.Objects;

public abstract class PEvent<P> {
public abstract class PEvent<P> implements prt.values.PValue<PEvent<P>> {
public abstract P getPayload();

@Override
Expand All @@ -17,4 +17,16 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(this.getPayload());
}

// Implement default PValue.deepEquals() method so as not to break
// code generated by old P compilers
public boolean deepEquals(PEvent<P> other) {
throw new UnsupportedOperationException("Please update your p compiler");
}

// Implement default PValue.deepClone() method so as not to break
// code generated by old P compilers
public PEvent<P> deepClone() {
throw new UnsupportedOperationException("Please update your p compiler");
}
}

0 comments on commit ca9ea84

Please sign in to comment.