Fix multiple errors in the variable scanner.

  1. Attribute names should not be handled like variables, so `appendname` is back.
  2. Set-related stuff from `joinvars` was moved into a separate function to allow for more granularity.
  3. Variable name sets are now joined in reverse order to ensure that the assertions in the `Code` constructor succeed.