My apologies.. I read too fast and jumped to conclusions. I had thought the latest code you posted, which is full of issues, was an evolution of the code in the OP and that it also had those errors.
BTW, here is a related tutorial for a Jython version (there is also a link in it to a DSL version)…