From 337a959b6fd39df41d9cfa7ce0ddce542929f91d Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 28 Mar 2024 00:30:33 +0500 Subject: [PATCH] Nim: do not wrap string concatenation and casts in parenthesis when not needed --- .../kaitai/struct/translators/NimTranslator.scala | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala b/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala index 9eb78fca3..ea2215fb6 100644 --- a/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala +++ b/shared/src/main/scala/io/kaitai/struct/translators/NimTranslator.scala @@ -51,7 +51,17 @@ class NimTranslator(provider: TypeProvider, importList: ImportList) extends Base override def arraySubscript(container: expr, idx: expr): String = s"${translate(container, METHOD_PRECEDENCE)}[${translate(idx)}]" - override def strConcat(left: expr, right: expr, extPrec: Int) = "($" + s"${translate(left, METHOD_PRECEDENCE)} & " + "$" + s"${translate(right, METHOD_PRECEDENCE)})" + override def strConcat(left: expr, right: expr, extPrec: Int): String = { + val thisPrec = OPERATOR_PRECEDENCE(Ast.operator.Add) + // $ is a special method which converts everything to a string, so use METHOD_PRECEDENCE + val leftStr = "$" + translate(left, METHOD_PRECEDENCE) + val rightStr = "$" + translate(right, METHOD_PRECEDENCE) + if (thisPrec <= extPrec) { + s"($leftStr & $rightStr)" + } else { + s"$leftStr & $rightStr" + } + } // Members declared in io.kaitai.struct.translators.CommonMethods @@ -86,7 +96,7 @@ class NimTranslator(provider: TypeProvider, importList: ImportList) extends Base importList.add("sequtils") s"${translate(value, METHOD_PRECEDENCE)}.mapIt(it.${ksToNim(at.elType)})" } - case _ => s"(${ksToNim(typeName)}(${translate(value)}))" + case _ => s"${ksToNim(typeName)}(${translate(value)})" } override def doIntLiteral(n: BigInt): String = { if (n <= -2147483649L) { // -9223372036854775808..-2147483649