In addition to TH.'s comments, I'd point out that the
etex package for LaTeX does include functions such as
\loctoks for doing a local definition. This has two parts:
- Creating the new name only within the current group
- Setting the tracking number for allocations locally
That's not quite the same as your request, but I think it would be quite possible to use it as the basis for what you want. You'd do something like:
So that the global definition was temporarily removed, meaning that the allocator would then create a new local version pointing to a different register.
For the question of freeing up a register, undefining the name is easy enough. The problem is the tracking number. You could just decrease it by 1, but it's quite likely that other allocations will have taken place between setting up the register you are interested in and deleting it again. It would be possible to do this by extending the allocation system to include a list of freed registers, and checking this list before the main tracking number.
In the work on
expl3, we did try out local allocation of registers, but decided in the end it did not really work so well in TeX. In most languages, a local variable is local to some function. So you can nest functions knowing that multiple 'new' variables with the same name can be created. That is not the case in TeX, where local means 'within the current group'. The need to watch groups, rather than functions, seemed overall to make it less useful to have locally-declared variables. Of course, using grouping to have local values is very much day to day TeX programming.