Skip to content

Commit 178ffed

Browse files
committed
Updates behavior of LBound and UBound
1 parent b367183 commit 178ffed

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

zxb/zxbparser.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,41 +3219,46 @@ def p_expr_lbound(p):
32193219

32203220
entry.accessed = True
32213221

3222-
if p[1] == 'LBOUND':
3223-
p[0] = make_number(entry.bounds[OPTIONS.array_base.value].lower,
3224-
p.lineno(3), TYPE.uinteger)
3222+
if entry.scope == SCOPE.parameter:
3223+
num = make_number(0, p.lineno(3), TYPE.uinteger)
3224+
p[0] = make_builtin(p.lineno(1), p[1], [entry, num], type_=TYPE.uinteger)
32253225
else:
3226-
p[0] = make_number(entry.bounds[OPTIONS.array_base.value].upper,
3227-
p.lineno(3), TYPE.uinteger)
3226+
p[0] = make_number(len(entry.bounds), p.lineno(3), TYPE.uinteger)
32283227

32293228

32303229
def p_expr_lbound_expr(p):
32313230
""" bexpr : LBOUND LP ARRAY_ID COMMA expr RP
32323231
| UBOUND LP ARRAY_ID COMMA expr RP
32333232
"""
3233+
expr = p[5]
3234+
if expr is None:
3235+
p[0] = None
3236+
return
3237+
32343238
entry = SYMBOL_TABLE.access_array(p[3], p.lineno(3))
32353239
if entry is None:
32363240
p[0] = None
32373241
return
32383242

32393243
entry.accessed = True
3240-
num = make_typecast(TYPE.uinteger, p[5], p.lineno(6))
3241-
3242-
if is_number(num):
3243-
if num.value == 0: # 0 => Number of dims
3244-
p[0] = make_number(len(entry.bounds), p.lineno(3), TYPE.uinteger)
3245-
return
3244+
num = make_typecast(TYPE.uinteger, expr, p.lineno(6))
3245+
if num is None:
3246+
p[0] = None
3247+
return
32463248

3247-
val = num.value - 1
3248-
if val < 0 or val >= len(entry.bounds):
3249+
if is_number(num) and entry.scope: # Try constant propagation
3250+
val = num.value
3251+
if val < 0 or val > len(entry.bounds):
32493252
syntax_error(p.lineno(6), "Dimension out of range")
32503253
p[0] = None
32513254
return
32523255

3253-
if p[1] == 'LBOUND':
3254-
p[0] = make_number(entry.bounds[val].lower, p.lineno(3), TYPE.uinteger)
3256+
if not val: # 0 => number of dims
3257+
p[0] = make_number(len(entry.bounds), p.lineno(3), TYPE.uinteger)
3258+
elif p[1] == 'LBOUND':
3259+
p[0] = make_number(entry.bounds[val - 1].lower, p.lineno(3), TYPE.uinteger)
32553260
else:
3256-
p[0] = make_number(entry.bounds[val].upper, p.lineno(3), TYPE.uinteger)
3261+
p[0] = make_number(entry.bounds[val - 1].upper, p.lineno(3), TYPE.uinteger)
32573262
return
32583263

32593264
if p[1] == 'LBOUND':

0 commit comments

Comments
 (0)