How should I implement a command processing application?

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?

Edit2: Thanks for all the responses, all have been very helpful to me, but Emmad Kareem’s helped me the most, so I’ll chose it as the answer. Thanks again!

8

This sounds like an interpreter. It looks like you are worried about the implementation more than the detailed functionality (I am only guessing here). This project if extended is not a trivial task. Make sure you study the scope clearly as this requires engineering approach and not an ad-hoc development approach to get a reliable product rather than a product with 1000 patches that works sometimes only.

Decide on a syntax and be ready to parse it and perform the necessary syntax checks. This link may help you with this:Create your own parser.

Take a look at: this topic as it touches on different aspects of the work, also there are good links that may help you (specially the answer by RMK).:Creating a Language interpreter. You may want to see an example of a nice looking project that is somewhat similar at: Ultimate Programmable Scientific Calculator. You can find source code and working program for a command line C# interpreter here Command-Line-Implementation-of-C#-Made-for-Teaching. Using the compiler to do the complex tasks for you like parsing and variable typing, etc. may be a clever way to escape the complexities of writing all this yourself. Also, there is the Mono option which provides a charp shell feature that you may want to take a look at: CsharpRepl.

2

Unless you’re specifically interested in writing the actual parser for yourself, I’d suggest having a look at one of the parser generator frameworks. For C you have YACC or Bison, but there should be other alternatives for other languages if you prefer.

These takes away the complexities of parsing complex grammars and lets you concentrate on the task you want to do. Of course this may be overkill for the grammar you suggest in the question, but since you mention having the option of expanding to a more complex grammar later, it’s at least worth getting some inspiration from these frameworks.

1

What you are describing is very close to a stack language.

For instance in Factor what you describe would be done like

1
2 +
7 *
even? not

Or you could define your own words and then use them, like

: add ( x y -- sum ) + ;
: multiply ( x y -- product ) * ;
: odd? ( n -- ? ) even? not ;

With these definitions the above example becomes

1
2 add
7 multiply
odd?

Usually stack languages are trivial to parse because they use single words that are space separated. I suggest you have a look at Factor – it may be eaxctly what you want. It should be easy to define the words that do the processing you need.

EDIT: If you actually want to design a similar language, I suggest that you play with one of them anyway. Parsing a stack language is trivial – you split on blank spaces, and a naive implementation of processing is easy: you just need to take care of what happens on a stack.

1

But if I want extensibility, how would I need to implement the functionality?

You shouldn’t. Extensibility creates a whole lot of complexity for very little gain. That said, you’ll need to provide a hook into the existing state. A way to see the state, modify the state, and provide a mechanism to return other results (print to screen). You’ll need a way for the core code to discover modules, load them, and dispatch commands to them.

Do I use the command pattern?

You can, but it’s likely not appropriate.

You’re not going to take the entire input and send it off for processing, but instead parse the input, dispatch to the correct handler and let it do its work. The command doesn’t vary in that communication; so no command pattern.

Do I build a simple parser/interpreter for the language?

You’ll need something to handle breaking the input into tokens. For an extensible solution, you probably won’t do much more. For a well-defined solution, having a full parse tree will provide better performance, error handling, and debug-ability.

but rather a list (of numbers) processor

Then perhaps you should look into the LISt Processing language. The juxtaposition of code and data should fit in well with what you describe.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật