sjbarag / brs

An interpreter for the BrightScript language that runs on non-Roku platforms.
MIT License
113 stars 43 forks source link

Array index expressions don't allow floating-point indices #398

Open sjbarag opened 4 years ago

sjbarag commented 4 years ago

Surprisingly, the reference brightscript interpreter allows floating-point indexes into arrays by performing "toward zero" rounding for almost all values. See the following test output for details:

a = ["foo", "bar", "baz"]
print a[1 + 1e-10] ' => "bar"
print a[1.4] ' => "bar"
print a[1.5] ' => "bar"
print a[1.99999] ' => "bar"
print a[1.9999999] ' => "bar"
print a[1.99999999] '=> "baz"
print a[-0.5] ' => "foo"
print a[-0.9999999] ' => "foo"
print a[-0.99999999] ' => "invalid
print a[-1] ' => invalid
heftyfunseeker commented 4 years ago
Brightscript Debugger> a = [0,1,2,3]

Brightscript Debugger> b = 1.9999999

Brightscript Debugger> ?b
 2 

Brightscript Debugger> ?a[b]
 1
-------------------------------------------------------
Brightscript Debugger> var
args             roAssociativeArray refcnt=3 count:7
global           Interface:ifGlobal
m                roAssociativeArray refcnt=2 count:6
screen           roSGScreen refcnt=1
appinfo          roAppManager refcnt=1
inputhandler     roInput refcnt=1
syslog           roSystemLog refcnt=1
scene            roSGNode:RootScene refcnt=1
texttospeech     roTextToSpeech refcnt=1
deeplinknode     roSGNode:ContentNode refcnt=1
msg              roSGNodeEvent refcnt=1
msgtype          String (VT_STR_CONST) val:"roSGNodeEvent"
event            <uninitialized>
a                roArray refcnt=1 count:4
b                Float val:2

While investigating this, it appears toStr rounds to the nearest hundred thousandths and could make things pretty tricky when debugging! Should we replicate that behavior here: https://github.com/sjbarag/brs/blob/master/src/stdlib/String.ts#L155

That's assuming we are indeed rounding to the nearest hundred thousandths in toStr