From 44b9f4fbc88b6040e1fe77c13d3a44bec5a43e3f Mon Sep 17 00:00:00 2001 From: Ankush Desai Date: Tue, 14 Nov 2023 15:49:08 -0800 Subject: [PATCH] Fixed a bug in the generated code for setting a field inside a sequence. (#681) * Fixed a bug in the generated code for setting a field inside a sequence. * Added back the mutator function --- .../CompilerCore/Backend/Java/MachineGenerator.cs | 4 ++-- Src/PCompiler/CompilerCore/Backend/Java/TypeManager.cs | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs b/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs index 91aedd0f6..5eb9c5ecb 100644 --- a/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs +++ b/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs @@ -442,11 +442,11 @@ private void WriteStmt(IPStmt stmt) WriteExpr(insertStmt.Variable); if (PLanguageType.TypeIsOfKind(insertStmt.Variable.Type, TypeKind.Sequence)) { - Write($".{t.MutatorMethodName}((int)("); + Write($".{t.InsertMethodName}((int)("); } else { - Write($".{t.MutatorMethodName}(("); + Write($".{t.InsertMethodName}(("); } WriteExpr(insertStmt.Index); Write("), "); diff --git a/Src/PCompiler/CompilerCore/Backend/Java/TypeManager.cs b/Src/PCompiler/CompilerCore/Backend/Java/TypeManager.cs index 7ffcef442..e7f3842c0 100644 --- a/Src/PCompiler/CompilerCore/Backend/Java/TypeManager.cs +++ b/Src/PCompiler/CompilerCore/Backend/Java/TypeManager.cs @@ -89,7 +89,10 @@ internal string ReferenceTypeName /// internal virtual string MutatorMethodName => throw new Exception($"MutatorMethodName not implemented for {TypeName}"); - + + internal virtual string InsertMethodName => + throw new Exception($"MutatorMethodName not implemented for {TypeName}"); + /// /// The name of the method K -> void that removes key K from the collection. Throws for /// non-collection types! @@ -201,6 +204,7 @@ internal JList(JType t) internal override string AccessorMethodName => "get"; internal override string ContainsMethodName => "contains"; internal override string MutatorMethodName => "set"; + internal override string InsertMethodName => "add"; internal override string RemoveMethodName => "remove"; } internal class JMap : JType @@ -218,6 +222,7 @@ internal JMap(JType k, JType v) internal override string AccessorMethodName => "get"; internal override string ContainsMethodName => "containsKey"; internal override string MutatorMethodName => "put"; + internal override string InsertMethodName => "put"; internal override string RemoveMethodName => "remove"; /// @@ -252,6 +257,7 @@ internal JSet(JType t) // the Java PRT runtime. internal override string ContainsMethodName => "contains"; + internal override string InsertMethodName => "add"; internal override string MutatorMethodName => "add"; internal override string RemoveMethodName => "remove"; }