Skip to content

Commit

Permalink
Unify _debug[$name]['arr'] array initialization across langs
Browse files Browse the repository at this point in the history
  • Loading branch information
generalmimon committed Oct 15, 2023
1 parent b9bc788 commit ba40241
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ class JavaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}
}

override def attrDebugArrInit(attrName: Identifier, attrType: DataType): Unit = {
// no _debug[$name]['arr'] initialization needed in Java
}

override def attrDebugEnd(attrId: Identifier, attrType: DataType, io: String, rep: RepeatSpec): Unit = {
val name = idToStr(attrId)
rep match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts(s"$debugName = { $ioProps${if (ioProps != "" && enumNameProps != "") ", " else ""}$enumNameProps };")
}

override def attrDebugArrInit(id: Identifier, attrType: DataType): Unit =
out.puts(s"this._debug.${idToStr(id)}.arr = [];")

override def attrDebugEnd(attrId: Identifier, attrType: DataType, io: String, rep: RepeatSpec): Unit = {
val debugName = attrDebugName(attrId, rep, true)

Expand All @@ -298,8 +301,6 @@ class JavaScriptCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
if (needRaw.level >= 2)
out.puts(s"${privateMemberName(RawIdentifier(RawIdentifier(id)))} = [];")
out.puts(s"${privateMemberName(id)} = [];")
if (config.readStoresPos)
out.puts(s"this._debug.${idToStr(id)}.arr = [];")
}

override def condRepeatEosHeader(id: Identifier, io: String, dataType: DataType): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,14 @@ class PythonCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
case NoRepeat =>
out.puts(s"self._debug['$name']['start'] = $io.pos()")
case _: RepeatExpr | RepeatEos | _: RepeatUntil =>
/** TODO: move array initialization to [[condRepeatCommonInit]] - see
* [[JavaScriptCompiler.condRepeatCommonInit]] for inspiration */
out.puts(s"if not 'arr' in self._debug['$name']:")
out.inc
out.puts(s"self._debug['$name']['arr'] = []")
out.dec
out.puts(s"self._debug['$name']['arr'].append({'start': $io.pos()})")
}
}
}

override def attrDebugArrInit(attrId: Identifier, attrType: DataType): Unit =
out.puts(s"self._debug['${idToStr(attrId)}']['arr'] = []")

override def attrDebugEnd(attrId: Identifier, attrType: DataType, io: String, rep: RepeatSpec): Unit = {
val name = idToStr(attrId)
rep match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,16 @@ class RubyCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
case NoRepeat =>
out.puts(s"(@_debug['$name'] ||= {})[:start] = $io.pos")
case _: RepeatExpr =>
out.puts(s"(@_debug['$name'][:arr] ||= [])[i] = {:start => $io.pos}")
out.puts(s"@_debug['$name'][:arr][i] = {:start => $io.pos}")
case RepeatEos | _: RepeatUntil =>
out.puts(s"(@_debug['$name'][:arr] ||= [])[${privateMemberName(attrId)}.size] = {:start => $io.pos}")
out.puts(s"@_debug['$name'][:arr][${privateMemberName(attrId)}.size] = {:start => $io.pos}")
}
}
}

override def attrDebugArrInit(attrId: Identifier, attrType: DataType): Unit =
out.puts(s"@_debug['${idToStr(attrId)}'][:arr] = []")

override def attrDebugEnd(attrId: Identifier, attrType: DataType, io: String, rep: RepeatSpec): Unit = {
val name = idToStr(attrId)
rep match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ trait CommonReads extends LanguageCompiler {
normalIO
}

if (attrDebugNeeded(id))
if (attrDebugNeeded(id)) {
attrDebugStart(id, attr.dataType, Some(io), NoRepeat)
if (attr.cond.repeat != NoRepeat)
attrDebugArrInit(id, attr.dataType)
}

defEndian match {
case Some(_: CalcEndian) | Some(InheritedEndian) =>
Expand Down Expand Up @@ -108,6 +111,7 @@ trait CommonReads extends LanguageCompiler {
}

def attrDebugStart(attrId: Identifier, attrType: DataType, io: Option[String], repeat: RepeatSpec): Unit = {}
def attrDebugArrInit(attrId: Identifier, attrType: DataType): Unit = {}
def attrDebugEnd(attrName: Identifier, attrType: DataType, io: String, repeat: RepeatSpec): Unit = {}

def attrDebugNeeded(attrId: Identifier): Boolean = false
Expand Down

0 comments on commit ba40241

Please sign in to comment.