term rewriting system in javascript