Skip to content

Commit

Permalink
[cleanup] Leftovers for array refactoring (#2223)
Browse files Browse the repository at this point in the history
* [spec] Fix spec test ble-idioms#24
* [spec] Remove var-op-bash#17 FAIL which has already been fixed
* [bash_impl] Fix typos in type names in code comments
* [bash_impl] Remove blank lines after type annotation for functions
* [osh/sh_exper_eval] Apply yapf-changed
* [doc j8] Update code comment
* [builtin/assign_osh] Fix oversight of BashAssoc({})
  • Loading branch information
akinomyoga authored Jan 4, 2025
1 parent f4f49ac commit 7b4f24c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 48 deletions.
4 changes: 3 additions & 1 deletion builtin/assign_osh.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ def Run(self, cmd_val):
if arg.a:
rval = value.BashArray([]) # type: value_t
elif arg.A:
rval = value.BashAssoc({})
# mycpp limitation: NewDict() needs to be typed
tmp = NewDict() # type: Dict[str, str]
rval = value.BashAssoc(tmp)
else:
rval = None
else:
Expand Down
43 changes: 6 additions & 37 deletions core/bash_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,31 @@

def BigInt_Greater(a, b):
# type: (mops.BigInt, mops.BigInt) -> bool

return mops.Greater(a, b)


def BigInt_Less(a, b):
# type: (mops.BigInt, mops.BigInt) -> bool

return mops.Greater(b, a)


def BigInt_GreaterEq(a, b):
# type: (mops.BigInt, mops.BigInt) -> bool

return not mops.Greater(b, a)


def BigInt_LessEq(a, b):
# type: (mops.BigInt, mops.BigInt) -> bool

return not mops.Greater(a, b)


#------------------------------------------------------------------------------
# All BashArray operations depending on the internal
# representation of SparseArray come here.
# All BashArray operations depending on the internal representation of
# BashArray come here.


def BashArray_IsEmpty(array_val):
# type: (value.BashArray) -> bool

return len(array_val.strs) == 0


Expand All @@ -58,13 +53,11 @@ def BashArray_Count(array_val):

def BashArray_Length(array_val):
# type: (value.BashArray) -> int

return len(array_val.strs)


def BashArray_GetKeys(array_val):
# type: (value.BashArray) -> List[int]

indices = [] # type: List[int]
for i, s in enumerate(array_val.strs):
if s is not None:
Expand All @@ -75,13 +68,11 @@ def BashArray_GetKeys(array_val):

def BashArray_GetValues(array_val):
# type: (value.BashArray) -> List[str]

return array_val.strs


def BashArray_AppendValues(array_val, strs):
# type: (value.BashArray, List[str]) -> None

array_val.strs.extend(strs)


Expand All @@ -104,7 +95,6 @@ def _BashArray_CanonicalizeIndex(array_val, index):

def BashArray_HasElement(array_val, index):
# type: (value.BashArray, int) -> Tuple[bool, error_code_t]

index, n, error_code = _BashArray_CanonicalizeIndex(array_val, index)
if error_code != error_code_e.OK:
return False, error_code
Expand Down Expand Up @@ -140,7 +130,6 @@ def BashArray_GetElement(array_val, index):

def BashArray_SetElement(array_val, index, s):
# type: (value.BashArray, int, str) -> error_code_t

strs = array_val.strs

# a[-1]++ computes this twice; could we avoid it?
Expand Down Expand Up @@ -192,7 +181,6 @@ def BashArray_UnsetElement(array_val, index):

def BashArray_Equals(lhs, rhs):
# type: (value.BashArray, value.BashArray) -> bool

len_lhs = len(lhs.strs)
len_rhs = len(rhs.strs)
if len_lhs != len_rhs:
Expand All @@ -217,7 +205,6 @@ def _BashArray_HasHoles(array_val):

def BashArray_ToStrForShellPrint(array_val, name):
# type: (value.BashArray, Optional[str]) -> str

buff = [] # type: List[str]
first = True
if _BashArray_HasHoles(array_val):
Expand Down Expand Up @@ -266,8 +253,8 @@ def BashArray_ToStrForShellPrint(array_val, name):


#------------------------------------------------------------------------------
# All BashAssoc operations depending on the internal
# representation of SparseArray come here.
# All BashAssoc operations depending on the internal representation of
# BashAssoc come here.


def BashAssoc_IsEmpty(assoc_val):
Expand All @@ -282,56 +269,47 @@ def BashAssoc_Count(assoc_val):

def BashAssoc_GetDict(assoc_val):
# type: (value.BashAssoc) -> Dict[str, str]

return assoc_val.d


def BashAssoc_AppendDict(assoc_val, d):
# type: (value.BashAssoc, Dict[str, str]) -> None

for key in d:
assoc_val.d[key] = d[key]


def BashAssoc_GetKeys(assoc_val):
# type: (value.BashAssoc) -> List[str]

return assoc_val.d.keys()


def BashAssoc_GetValues(assoc_val):
# type: (value.BashAssoc) -> List[str]

return assoc_val.d.values()


def BashAssoc_HasElement(assoc_val, s):
# type: (value.BashAssoc, str) -> bool

return s in assoc_val.d


def BashAssoc_GetElement(assoc_val, s):
# type: (value.BashAssoc, str) -> Optional[str]

return assoc_val.d.get(s)


def BashAssoc_SetElement(assoc_val, key, s):
# type: (value.BashAssoc, str, str) -> None

assoc_val.d[key] = s


def BashAssoc_UnsetElement(assoc_val, key):
# type: (value.BashAssoc, str) -> None

mylib.dict_erase(assoc_val.d, key)


def BashAssoc_Equals(lhs, rhs):
# type: (value.BashAssoc, value.BashAssoc) -> bool

if len(lhs.d) != len(rhs.d):
return False

Expand All @@ -344,7 +322,6 @@ def BashAssoc_Equals(lhs, rhs):

def BashAssoc_ToStrForShellPrint(assoc_val):
# type: (value.BashAssoc) -> str

buff = ["("] # type: List[str]
first = True
for key in sorted(assoc_val.d):
Expand All @@ -363,8 +340,8 @@ def BashAssoc_ToStrForShellPrint(assoc_val):


#------------------------------------------------------------------------------
# All SparseArray operations depending on the internal
# representation of SparseArray come here.
# All SparseArray operations depending on the internal representation of
# SparseArray come here.


def SparseArray_IsEmpty(sparse_val):
Expand All @@ -379,13 +356,11 @@ def SparseArray_Count(sparse_val):

def SparseArray_Length(sparse_val):
# type: (value.SparseArray) -> mops.BigInt

return mops.Add(sparse_val.max_index, mops.ONE)


def SparseArray_GetKeys(sparse_val):
# type: (value.SparseArray) -> List[mops.BigInt]

keys = sparse_val.d.keys()
mylib.BigIntSort(keys)
return keys
Expand Down Expand Up @@ -431,7 +406,6 @@ def _SparseArray_CanonicalizeIndex(sparse_val, index):

def SparseArray_HasElement(sparse_val, index):
# type: (value.SparseArray, mops.BigInt) -> Tuple[bool, error_code_t]

index, error_code = _SparseArray_CanonicalizeIndex(sparse_val, index)
if error_code != error_code_e.OK:
return False, error_code
Expand All @@ -440,7 +414,6 @@ def SparseArray_HasElement(sparse_val, index):

def SparseArray_GetElement(sparse_val, index):
# type: (value.SparseArray, mops.BigInt) -> Tuple[Optional[str], error_code_t]

index, error_code = _SparseArray_CanonicalizeIndex(sparse_val, index)
if error_code != error_code_e.OK:
return None, error_code
Expand All @@ -449,7 +422,6 @@ def SparseArray_GetElement(sparse_val, index):

def SparseArray_SetElement(sparse_val, index, s):
# type: (value.SparseArray, mops.BigInt, str) -> error_code_t

index, error_code = _SparseArray_CanonicalizeIndex(sparse_val, index)
if error_code != error_code_e.OK:
return error_code
Expand All @@ -461,7 +433,6 @@ def SparseArray_SetElement(sparse_val, index, s):

def SparseArray_UnsetElement(sparse_val, index):
# type: (value.SparseArray, mops.BigInt) -> error_code_t

index, error_code = _SparseArray_CanonicalizeIndex(sparse_val, index)
if error_code != error_code_e.OK:
return error_code
Expand All @@ -478,7 +449,6 @@ def SparseArray_UnsetElement(sparse_val, index):

def SparseArray_Equals(lhs, rhs):
# type: (value.SparseArray, value.SparseArray) -> bool

len_lhs = len(lhs.d)
len_rhs = len(rhs.d)
if len_lhs != len_rhs:
Expand All @@ -493,7 +463,6 @@ def SparseArray_Equals(lhs, rhs):

def SparseArray_ToStrForShellPrint(sparse_val):
# type: (value.SparseArray) -> str

body = [] # type: List[str]
for index in SparseArray_GetKeys(sparse_val):
if len(body) > 0:
Expand Down
2 changes: 1 addition & 1 deletion data_lang/j8.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def _PrintBashPrefix(self, type_str, level):
self._ItemIndent(level)
self.buf.write('"type":')
self._MaybeSpace()
self.buf.write(type_str) # "BashArray", or "BashAssoc",
self.buf.write(type_str) # "BashArray", "SparseArray", or "BashAssoc",

self._MaybeNewline()

Expand Down
3 changes: 2 additions & 1 deletion osh/cmd_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ def PlusEquals(old_val, val):
str_to_append = cast(value.Str, UP_val)
val = value.Str(old_val.s + str_to_append.s)

elif tag in (value_e.BashArray, value_e.SparseArray, value_e.BashAssoc):
elif tag in (value_e.BashArray, value_e.SparseArray,
value_e.BashAssoc):
e_die("Can't append array to string")

else:
Expand Down
5 changes: 3 additions & 2 deletions spec/ble-idioms.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ unset -v "a[-3]"
case $SH in bash|zsh|mksh|ash) exit ;; esac

a=({1..9})
var a = _a2sp(a)
unset -v 'a[-1]'
a[-1]=x
declare -p a
Expand All @@ -630,8 +631,8 @@ a[-1]=x
declare -p a

## STDOUT:
declare -a a=(1 2 3 4 5 6 7 x)
declare -a a=(1 2 3 4 5 6 x)
declare -a a=([0]=1 [1]=2 [2]=3 [3]=4 [4]=5 [5]=6 [6]=7 [7]=x)
declare -a a=([0]=1 [1]=2 [2]=3 [3]=4 [4]=5 [5]=6 [6]=x)
## END

## N-I bash/zsh/mksh/ash STDOUT:
Expand Down
7 changes: 1 addition & 6 deletions spec/var-op-bash.test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## compare_shells: bash
## oils_failures_allowed: 8
## oils_failures_allowed: 7

#### Lower Case with , and ,,
x='ABC DEF'
Expand Down Expand Up @@ -337,11 +337,6 @@ if test $? -ne 0; then echo fail; fi


# END
## OK osh STDOUT:
fail
'x y'
a
## END

#### ${#var@X} is a parse error
# note: "y z" causes a bug!
Expand Down

0 comments on commit 7b4f24c

Please sign in to comment.