It would be extremely hard to do, but not impossible in principle.
As Christian Lindig mentions, trees and dictionaries would be needed, and it is not possible to code those very efficiently in TeX. However, a naive implementation of dictionaries can be
A better (more memory efficient) implementation can be found as property lists in the
expl3 bundle. For trees, I guess that the easiest is to have each node represented as one control sequence containing the list of its children, and one containing the material.
It may be easier to run the programs without compiling them, though. I know that
pgf has some support for object-oriented programming.
On a side note, I should mention that I once wrote in TeX a parser for integer expressions (essentially the same syntax as
\numexpr), where the user could define new functions, and it is rather tricky to get right.