You cannot do this with eval as has been suggested, because you would not be able to use your custom add
and multiply
functions that way. You could parse out all of the operators with regular expressions, but it would not be easy.
You would have to make sure you got the order of operations correct, and break it into many steps.
The basic logic of it would be:
Define your add and multiply functions.
Define a function that takes a string of numbers and operators (without parentheses) and splits it into binary operations (ie. '1 + 7' or '5 * 3') with regular expressions, that can be passed to your add and multiply functions. This would have to be done recursively, following your desired order of operations for your custom math, in other words, if you get '5 + 3 * 2' you will have parse out '3 * 2' and evaluate that before using its result to add to 5 (assuming your order would be multiply first).
Define a function that looks for parentheses and recursively finds the innermost set and passes it to function 2 above to be evaluated.
Pass your input string to function 3 above and hope that you got all of the recursive logic and regular expressions correct.