I wanted to beat gcampton at making obfuscated return statements that evaluate to 0 (
http://cplusplus.com/forum/beginner/19979/ ) and I ended up with a three line Python script; it was basically an infinite loop and raw_input() being fed into eval() and then printed. You can see it in the thread I linked.
Then I decided to extend the script and make it really useful. I've completed a rudimentary Curses interface and a basic command-line interface, and I plan to make a file I/O mode whereby it will read a file of expressions line by line and print the results of each expression in a file somewhere, or on stdout.
Main source file:
http://pastebin.com/m6d5ad736 (pyexpr.py)
Curses interface:
http://pastebin.com/m4db31e6d (pyexpr_curses.py)
Command-line module:
http://pastebin.com/m722cc733 (pyexpr_cli.py)
Scripted module: <not implemented>
Note: for the command-line, in Bash and similar shells you should surround operators like left-shift and right-shift with quotes, e.g.
./pyexpr 16 ">>" 2
or nothing happens.
The point of this program? Well; the standard UNIX program expr has
1. No interactive mode; you have to type a command at a time, e.g. "expr 25 + 65" which makes it unsuitable as a calculator
2. No support for floating point:
1 2
|
$ ./pyexpr.py -c 256.87 - 0.87
256.0
|
1 2
|
$ expr 256.87 - 0.87
expr: non-numeric argumentp
|
3. No support for bitwise operations except | and &:
1 2
|
$ expr 250 ">>" 52
expr: syntax error
|
4. No support for background number-crunching, which I plan to do tomorrow. Essentially the script will run through a bunch of files, evaluation each expression line-by-line. It's unlikely anyone would want that; but it's still a cool feature. It's not likely to be difficult, either. Probably only a few lines of code.
5. Mine works on every platform Python works on:
http://www.python.org/download/ (look to the left, you should see Windows, Macintosh, Linux and "Other")
expr has only two advantages I can think of:
1. It comes with almost every UNIX variant
2. It has support for regular expressions, and I don't plan to.
Sorry for making such a big deal out of a small thing, but it was enjoyable to write (durr, it's Python) and I think it's quite useful as a program.
Edit: the best thing to enter into the interactive prompt is 25945895 + 394589345 << 349534 >> 345934
The result is awesome.