buzz-lang / Buzz

A programming language designed for robot swarms.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug in table library due to nil comparison from #91

samarseneault opened this issue · comments

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.

Fixed with the latest commit