How should I implement a command processing application?

Posted by Nini Michaels on Programmers See other posts from Programmers or by Nini Michaels
Published on 2012-11-07T11:19:48Z Indexed on 2012/11/07 17:17 UTC
Read the original article Hit count: 401

Filed under:
|

I want to make a simple, proof-of-concept application (REPL) that takes a number and then processes commands on that number.

Example: I start with 1. Then I write "add 2", it gives me 3. Then I write "multiply 7", it gives me 21. Then I want to know if it is prime, so I write "is prime" (on the current number - 21), it gives me false. "is odd" would give me true. And so on.

Now, for a simple application with few commands, even a simple switch would do for processing the commands. But if I want extensibility, how would I need to implement the functionality? Do I use the command pattern? Do I build a simple parser/interpreter for the language? What if I want more complex commands, like "multiply 5 until >200" ? What would be an easy way to extend it (add new commands) without recompiling?

Edit: to clarify a few things, my end goal would not be to make something similar to WolframAlpha, but rather a list (of numbers) processor. But I want to start slowly at first (on single numbers).

I'm having in mind something similar to the way one would use Haskell to process lists, but a very simple version. I'm wondering if something like the command pattern (or equivalent) would suffice, or if I have to make a new mini-language and a parser for it to achieve my goals?

© Programmers or respective owner

Related posts about parsing

Related posts about text-processing