Doing simple math from the command line (like PowerShell)

I like the way that one can do simple math from the PowerShell command line, example;
Code:
PS E:\utils> 52*4
208

Using the following alias;
Code:
e:\utils>alias unknown_cmd
echo %@eval[%@filter[0123456789.*+-/\,"%$"]]

...I can now do;
Code:
e:\utils>52*4
208
...from the TCC Command line.

Again, this is only for simple math, not for log(), fact(), MOD, etc.

When I enter an UNKNOWN_CMD, for example;
Code:
e:\utils>dze
TCC: No expression ""

...which is not pretty, but it works.

The next evolution of doing simple math from the TCC command line involved changing the alias to;
Code:
alias unknown_cmd=e:\utils\unknown_cmd.btm

...and creating UNKNOWN_CMD.BTM
Code:
@setlocal
@echo off
unalias unknown_cmd

set UseEval=N

iff %@isalpha[%$] eq 0 then
  iff %@isfloat[%$] eq 0 then
    iff %@isalnum[%$] eq 0 then
      set UseEval=Y
    endiff
  endiff
endiff

iff %UseEval eq Y then
  echo %@eval[%@filter[0123456789.*+-/\,"%$"]]
else
  on error gosub Catch
  %$
endiff

alias unknown_cmd=%_batchname

endlocal
quit

:Catch
echo       ?: %?
echo      _?: %_?
echo _Syserr: %_syserr

echo @isalpha: %@isalpha[%$]
echo @isfloat: %@isfloat[%$]
echo @isalnum: %@isalnum[%$]

Return

...returns;
Code:
e:\utils>dze
      ?: 0
     _?: 0
_Syserr: 536870913
@isalpha: 1
@isfloat: 0
@isalnum: 1

...which gives me more info than;
Code:
TCC: No expression ""

...but it works okay for me, as I use several AutoHotkey scripts for my non-conforming typing, for example;
Code:
e:\utils>type dre.ahk
:*:dre::dir

...which, when I type dre is automatically changed to dir.

Feel free to make modifications to this procedure for TCC command line math, but if you do, please share your modifications.

Joe
 
May 20, 2008
11,183
94
Syracuse, NY, USA
I have for a long time (and still do) just use an EVAL command, a wrapper for "ECHO %@EVAL[%$]". Once upon a time it was an alias; now it's a plugin. It gives the full power of @EVAL.

It's too bad an UNKNOWN_CMD alias or BTM can't invoke the default unknown command behavior. I wonder what would happen if a plugin UNKNOWN_CMD returned 0xFEDCBA98 (which means "I am disabled"). Would the default unknown command mechanism kick in? If so, perhaps one could capitalize on that.
 
May 20, 2008
11,183
94
Syracuse, NY, USA
Well, it does work. As a plugin, UNKNOWN_CMD can be selective and hand the job off to the built-in one. See my first test below. Now I have to think of a clever way to use it.

Code:
v:\> which unknown_cmd
unknown_cmd is a plugin command (4UTILS)

v:\> which foo
foo is an unknown command

v:\> foo
Aha! You issued "foo".

v:\> which bar
bar is an unknown command

v:\> bar
TCC: Unknown command "bar"
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,396
83
Albuquerque, NM
prospero.unm.edu
Well, it does work. As a plugin, UNKNOWN_CMD can be selective and hand the job off to the built-in one. See my first test below. Now I have to think of a clever way to use it.

That turns out to be a whole lot trickier than I thought at first. For one thing, you can't rely on Evaluate()'s return code to tell you whether or not the string is a valid math expression.

For another, things like parentheses and angle brackets are long gone by the time an UNKNOWN_CMD handler sees the command line.
 
May 20, 2008
11,183
94
Syracuse, NY, USA
Yup! And testing the expression (with @EVAL[] or Evaluate()) could easily produce MAPM error messages. You can't even redirect those guys.

I think an UNKNOWN_CMD alias/plugin gets the whole command line. ... yes/no?
 
Nov 2, 2008
227
2
In the end, I use REXX. The basic calculator is fairly simple,
Code:
/* REXX */
numeric digits 15
parse arg chalk
interpret 'cheese=' chalk
say cheese
exit

If you want to add functions, you write these as subroutines. You can write specialised functions, or simply raid the AAT or some OS/2 bbs for the more generalised ones.

With little effort, you can add the likes of "datedif" to your calculator (add or subtract days, or the difference between two days).
 
Thread starter Similar threads Forum Replies Date
Charles Dye T&T - Aliases 3
Similar threads
A simple command counter