Skip to content

Commit

Permalink
[refactor] Use bash_impl to print arrays (#2219)
Browse files Browse the repository at this point in the history
* [refactor j8] Use `bash_impl` to print arrays
* [refactor pp_value] Use `bash_impl` to pretty-print arrays
  • Loading branch information
akinomyoga authored Jan 4, 2025
1 parent 459d58d commit b069fe6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
19 changes: 12 additions & 7 deletions data_lang/j8.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import math

from _devbuild.gen.id_kind_asdl import Id, Id_t, Id_str
from _devbuild.gen.value_asdl import (value, value_e, value_t, value_str, Obj)
from _devbuild.gen.nil8_asdl import (nvalue, nvalue_t)
from _devbuild.gen.runtime_asdl import error_code_e
from _devbuild.gen.value_asdl import (value, value_e, value_t, value_str, Obj)

from core import bash_impl
from core import error
from data_lang import pyj8
# dependency issue: consts.py pulls in frontend/option_def.py
Expand Down Expand Up @@ -360,14 +362,15 @@ def _PrintSparseArray(self, val, level):

self._PrintBashPrefix('"SparseArray",', level)

if len(val.d) == 0: # Special case like Python/JS
if bash_impl.SparseArray_Count(
val) == 0: # Special case like Python/JS
self.buf.write('{}')
else:
self.buf.write('{')
self._MaybeNewline()

i = 0
for k, v in iteritems(val.d):
for k in bash_impl.SparseArray_GetKeys(val):
if i != 0:
self.buf.write(',')
self._MaybeNewline()
Expand All @@ -378,6 +381,8 @@ def _PrintSparseArray(self, val, level):
self.buf.write(':')
self._MaybeSpace()

v, error_code = bash_impl.SparseArray_GetElement(val, k)
assert error_code == error_code_e.OK, error_code
pyj8.WriteString(v, self.options, self.buf)

i += 1
Expand All @@ -394,14 +399,14 @@ def _PrintBashArray(self, val, level):

self._PrintBashPrefix('"BashArray",', level)

if len(val.strs) == 0: # Special case like Python/JS
if bash_impl.BashArray_Count(val) == 0: # Special case like Python/JS
self.buf.write('{}')
else:
self.buf.write('{')
self._MaybeNewline()

first = True
for i, s in enumerate(val.strs):
for i, s in enumerate(bash_impl.BashArray_GetValues(val)):
if s is None:
continue

Expand Down Expand Up @@ -431,14 +436,14 @@ def _PrintBashAssoc(self, val, level):

self._PrintBashPrefix('"BashAssoc",', level)

if len(val.d) == 0: # Special case like Python/JS
if bash_impl.BashAssoc_Count(val) == 0: # Special case like Python/JS
self.buf.write('{}')
else:
self.buf.write('{')
self._MaybeNewline()

i = 0
for k2, v2 in iteritems(val.d):
for k2, v2 in iteritems(bash_impl.BashAssoc_GetDict(val)):
if i != 0:
self.buf.write(',')
self._MaybeNewline()
Expand Down
16 changes: 10 additions & 6 deletions display/pp_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import math

from _devbuild.gen.pretty_asdl import (doc, Measure, MeasuredDoc)
from _devbuild.gen.runtime_asdl import error_code_e
from _devbuild.gen.value_asdl import Obj, value, value_e, value_t, value_str
from core import bash_impl
from data_lang import j8
from data_lang import j8_lite
from display import ansi
Expand Down Expand Up @@ -175,10 +177,10 @@ def _YshDict(self, vdict):
def _BashArray(self, varray):
# type: (value.BashArray) -> MeasuredDoc
type_name = self._Styled(self.type_style, AsciiText('BashArray'))
if len(varray.strs) == 0:
if bash_impl.BashArray_Count(varray) == 0:
return _Concat([AsciiText('('), type_name, AsciiText(')')])
mdocs = [] # type: List[MeasuredDoc]
for s in varray.strs:
for s in bash_impl.BashArray_GetValues(varray):
if s is None:
mdocs.append(AsciiText('null'))
else:
Expand All @@ -189,10 +191,10 @@ def _BashArray(self, varray):
def _BashAssoc(self, vassoc):
# type: (value.BashAssoc) -> MeasuredDoc
type_name = self._Styled(self.type_style, AsciiText('BashAssoc'))
if len(vassoc.d) == 0:
if bash_impl.BashAssoc_Count(vassoc) == 0:
return _Concat([AsciiText('('), type_name, AsciiText(')')])
mdocs = [] # type: List[MeasuredDoc]
for k2, v2 in iteritems(vassoc.d):
for k2, v2 in iteritems(bash_impl.BashAssoc_GetDict(vassoc)):
mdocs.append(
_Concat([
AsciiText('['),
Expand All @@ -206,10 +208,12 @@ def _BashAssoc(self, vassoc):
def _SparseArray(self, val):
# type: (value.SparseArray) -> MeasuredDoc
type_name = self._Styled(self.type_style, AsciiText('SparseArray'))
if len(val.d) == 0:
if bash_impl.SparseArray_Count(val) == 0:
return _Concat([AsciiText('('), type_name, AsciiText(')')])
mdocs = [] # type: List[MeasuredDoc]
for k2, v2 in iteritems(val.d):
for k2 in bash_impl.SparseArray_GetKeys(val):
v2, error_code = bash_impl.SparseArray_GetElement(val, k2)
assert error_code == error_code_e.OK, error_code
mdocs.append(
_Concat([
AsciiText('['),
Expand Down

0 comments on commit b069fe6

Please sign in to comment.