Go read about the shunting-yard algorithm. Or if
^
is the only custom operator in there, you might be able to get away with just finding those and replacing them with pow
or whatever.