buzz-lang / Buzz

A programming language designed for robot swarms.
MIT License
292 stars 61 forks source link

Bug in table library due to nil comparison from #91 #94

Closed samarseneault closed 3 years ago

samarseneault commented 3 years ago

PR #91 has introduced a difference in comparisons with nil. This seems to be the cause of bugs in the functions table.min and table.minkey, because the accumulator in reduce is set to nil (see code snippet below), which is always smaller than any numeric value in the PR (see lines 152-153 of src/buzz/buzztype.c). This means it does not affect table.max nor table.maxkey.

table.minkey = function(t) { return reduce(t, function(k, v, a) { return math.min(k, a) }, nil) }

This bug can easily be reproduced in any buzz script with the following code, which prints nil for both cases: log(table.min({.0 = 0})) log(table.minkey({.0 = 0}))

The obvious (but unclean) fix for the table library could be to replace the accumulator with a MAX_INT constant or the like, however this does not solve the issue for other instances of comparisons between numbers and nil.

beltrame commented 3 years ago

Fixed with the latest commit