about summary refs log tree commit diff
path: root/Functions/Misc/zcalc
blob: 4803733451248abd024e1dfe9f170efaa13ec82e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/bin/zsh -i
#
# Zsh calculator.  Understands most ordinary arithmetic expressions.
# Line editing and history are available. A blank line or `q' quits.
#
# Runs as a script or a function.  If used as a function, the history
# is remembered for reuse in a later call (and also currently in the
# shell's own history).  There are various problems using this as a
# script, so a function is recommended.
#
# The prompt shows a number for the current line.  The corresponding
# result can be referred to with $<line-no>, e.g.
#   1> 32 + 10
#   42
#   2> $1 ** 2
#   1764
# The set of remembered numbers is primed with anything given on the
# command line.  For example,
#   zcalc '2 * 16'
#   1> 32                     # printed by function
#   2> $1 + 2                 # typed by user
#   34
#   3> 
# Here, 32 is stored as $1.  This works in the obvious way for any
# number of arguments.
#
# If the mathfunc library is available, probably understands most system
# mathematical functions.  The left parenthesis must be adjacent to the
# end of the function name, to distinguish from shell parameters
# (translation: to prevent the maintainers from having to write proper
# lookahead parsing).  For example,
#   1> sqrt(2)
#   1.4142135623730951
# is right, but `sqrt (2)' will give you an error.
#
# You can do things with parameters like
#   1> pi = 4.0 * atan(1)
# too.  These go into global parameters, so be careful.  You can declare
# local variables, however:
#   1> local pi
# but note this can't appear on the same line as a calculation.  Don't
# use the variables listed in the `local' and `integer' lines below
# (translation: I can't be bothered to provide a sandbox).
#
# You can declare or delete math functions (implemented via zmathfuncdef):
#   1> function cube $1 * $1 * $1
# This has a single compulsory argument.  Note the function takes care of
# the punctuation.  To delete the function, put nothing (at all) after
# the function name:
#   1> function cube
#
# Some constants are already available: (case sensitive as always):
#   PI     pi, i.e. 3.1415926545897931
#   E      e, i.e. 2.7182818284590455
#
# You can also change the output base.
#   1> [#16]
#   1>
# Changes the default output to hexadecimal with numbers preceded by `16#'.
# Note the line isn't remembered.
#   2> [##16]
#   2>
# Change the default output base to hexadecimal with no prefix.
#   3> [#]
# Reset the default output base.
#
# This is based on the builtin feature that you can change the output base
# of a given expression.  For example,
#   1> [##16]  32 + 20 / 2
#   2A
#   2> 
# prints the result of the calculation in hexadecimal.
#
# You can't change the default input base, but the shell allows any small
# integer as a base:
#   1> 2#1111
#   15
#   2> [##13] 13#6 * 13#9
#   42
# and the standard C-like notation with a leading 0x for hexadecimal is
# also understood.  However, leading 0 for octal is not understood --- it's
# too confusing in a calculator.  Use 8#777 etc.
#
# Options: -#<base> is the same as a line containing just `[#<base>],
# similarly -##<base>; they set the default output base, with and without
# a base discriminator in front, respectively.
#
# With the option -e, the arguments are evaluated as if entered
# interactively.  So, for example:
#   zcalc -e -\#16 -e 1055
# prints
#   0x41f
# Any number of expressions may be given and they are evaluated
# sequentially just as if read automatically.

emulate -L zsh
setopt extendedglob typesetsilent

zcalc_show_value() {
  if [[ -n $_base ]]; then
    print -- $(( $_base $1 ))
  elif [[ $1 = *.* ]] || (( _outdigits )); then
    # With normal output, ensure trailing "." doesn't get lost.
    if [[ -z $_forms[_outform] || ($_outform -eq 1 && $1 = *.) ]]; then
      print -- $(( $1 ))
    else
      printf "$_forms[_outform]\n" $_outdigits $1
    fi
  else
    printf "%d\n" $1
  fi
}

# For testing in ZLE functions.
local ZCALC_ACTIVE=1

# TODO: make local variables that shouldn't be visible in expressions
# begin with _.
local _line ans _base _defbase _forms match mbegin mend
local psvar _optlist _opt _arg _tmp
local compcontext="-zcalc-line-"
integer _num _outdigits _outform=1 _expression_mode
integer _rpn_mode _matched _show_stack _i _n
integer _max_stack _push
local -a _expressions stack

# We use our own history file with an automatic pop on exit.
history -ap "${ZDOTDIR:-$HOME}/.zcalc_history"

_forms=( '%2$g' '%.*g' '%.*f' '%.*E' '')

local _mathfuncs
if zmodload -i zsh/mathfunc 2>/dev/null; then
  zmodload -P _mathfuncs -FL zsh/mathfunc
  _mathfuncs="("${(j.|.)${_mathfuncs##f:}}")"
fi
local -A _userfuncs
for _line in ${(f)"$(functions -M)"}; do
  match=(${=_line})
  # get minimum number of arguments
  _userfuncs[${match[3]}]=${match[4]}
done
_line=
autoload -Uz zmathfuncdef

if (( ! ${+ZCALCPROMPT} )); then
  typeset -g ZCALCPROMPT="%1v> "
fi

# Supply some constants.
float PI E
(( PI = 4 * atan(1), E = exp(1) ))

if [[ -f "${ZDOTDIR:-$HOME}/.zcalcrc" ]]; then
  . "${ZDOTDIR:-$HOME}/.zcalcrc" || return 1
fi

# Process command line
while [[ -n $1 && $1 = -(|[#-]*|f|e|r(<->|)) ]]; do
  _optlist=${1[2,-1]}
  shift
  [[ $_optlist = (|-) ]] && break
  while [[ -n $_optlist ]]; do
    _opt=${_optlist[1]}
    _optlist=${_optlist[2,-1]}
    case $_opt in
      ('#') # Default base
            if [[ -n $_optlist ]]; then
	       _arg=$_optlist
	       _optlist=
	    elif [[ -n $1 ]]; then
	       _arg=$1
	       shift
	    else
	       print -- "-# requires an argument" >&2
	       return 1
	    fi
	    if [[ $_arg != (|\#)[[:digit:]]## ]]; then
	      print -- "-# requires a decimal number as an argument" >&2
	      return 1
	    fi
            _defbase="[#${_arg}]"
	    ;;
	(f) # Force floating point operation
	    setopt forcefloat
	    ;;
        (e) # Arguments are expressions
	    (( _expression_mode = 1 ));
	    ;;
        (r) # RPN mode.
	    (( _rpn_mode = 1 ))
	    ZCALC_ACTIVE=rpn
	    if [[ $_optlist = (#b)(<->)* ]]; then
	       (( _show_stack = ${match[1]} ))
               _optlist=${_optlist[${#match[1]}+1,-2]}
	    fi
	    ;;
    esac
  done
done

if (( _expression_mode )); then
  _expressions=("$@")
  argv=()
fi

for (( _num = 1; _num <= $#; _num++ )); do
  # Make sure all arguments have been evaluated.
  # The `$' before the second argv forces string rather than numeric
  # substitution.
  (( argv[$_num] = $argv[$_num] ))
  print "$_num> $argv[$_num]"
done

psvar[1]=$_num
local _prev_line _cont_prompt
while (( _expression_mode )) ||
  vared -cehp "${_cont_prompt}${ZCALCPROMPT}" _line; do
  if (( _expression_mode )); then
    (( ${#_expressions} )) || break
    _line=$_expressions[1]
    shift _expressions
  fi
  if [[ $_line = (|*[^\\])('\\')#'\' ]]; then
    _prev_line+=$_line[1,-2]
    _cont_prompt="..."
    _line=
    continue
  fi
  _line="$_prev_line$_line"
  _prev_line=
  _cont_prompt=
  # Test whether there are as many open as close
  # parentheses in the _line so far.
  if [[ ${#_line//[^\(]} -gt ${#_line//[^\)]} ]]; then
      _prev_line+=$_line
      _cont_prompt="..."
      _line=
      continue
  fi
  [[ -z $_line ]] && break
  # special cases
  # Set default base if `[#16]' or `[##16]' etc. on its own.
  # Unset it if `[#]' or `[##]'.
  if [[ $_line = (#b)[[:blank:]]#('[#'(\#|)((<->|)(|_|_<->))']')[[:blank:]]#(*) ]]; then
    if [[ -z $match[6] ]]; then
      if [[ -z $match[3] ]]; then
	_defbase=
      else
	_defbase=$match[1]
      fi
      print -s -- $_line
      print -- $(( ${_defbase} ans ))
      _line=
      continue
    else
      _base=$match[1]
    fi
  else
    _base=$_defbase
  fi

  print -s -- $_line

  _line="${${_line##[[:blank:]]#}%%[[:blank:]]#}"
  case "$_line" in
    # Escapes begin with a colon
    (:(\\|)\!*)
    # shell escape: handle completion's habit of quoting the !
    eval ${_line##:(\\|)\![[:blank:]]#}
    _line=
    continue
    ;;

    ((:|)q)
    # Exit
    return 0
    ;;

    ((:|)norm) # restore output format to default
      _outform=1
    ;;

    ((:|)sci[[:blank:]]#(#b)(<->)(#B))
      _outdigits=$match[1]
      _outform=2
    ;;

    ((:|)fix[[:blank:]]#(#b)(<->)(#B))
      _outdigits=$match[1]
      _outform=3
    ;;

    ((:|)eng[[:blank:]]#(#b)(<->)(#B))
      _outdigits=$match[1]
      _outform=4
    ;;

    (:raw)
    _outform=5
    ;;

    ((:|)local([[:blank:]]##*|))
      eval ${_line##:}
      _line=
      continue
    ;;

    ((function|:f(unc(tion|)|))[[:blank:]]##(#b)([^[:blank:]]##)(|[[:blank:]]##([^[:blank:]]*)))
      zmathfuncdef $match[1] $match[3]
      _userfuncs[$match[1]]=${$(functions -Mm $match[1])[4]}
      _line=
      continue
    ;;

    (:*)
    print "Unrecognised escape"
    _line=
    continue
    ;;

    (\$[[:IDENT:]]##)
    # Display only, no calculation
    _line=${_line##\$}
    print -r -- ${(P)_line}
    _line=
    continue
    ;;

    (*)
      _line=${${_line##[[:blank:]]##}%%[[:blank:]]##}
      if [[ _rpn_mode -ne 0 && $_line != '' ]]; then
	_push=1
	_matched=1
	case $_line in
	  (\<[[:IDENT:]]##)
	  ans=${(P)${_line##\<}}
	  ;;

	  (\=|pop|\>[[:IDENT:]]#)
	  if (( ${#stack} < 1 )); then
	    print -r -- "${_line}: not enough values on stack" >&2
	    _line=
	    continue
	  fi
	  case $_line in
	    (=)
	    ans=${stack[1]}
	    ;;
	    (pop|\>)
	    _push=0
	    shift stack
	    ;;
	    (\>[[:IDENT:]]##)
	    if [[ ${_line##\>} = (_*|stack|ans|PI|E) ]]; then
	      print "${_line##\>}: reserved variable" >&2
	      _line=
	      continue
	    fi
	    local ${_line##\>}
	    (( ${_line##\>} = ${stack[1]} ))
	    _push=0
	    shift stack
	    ;;
	    (*)
	    print "BUG in special RPN functions" >&2
	    _line=
	    continue
	    ;;
	  esac
	  ;;

	  (+|-|\^|\||\&|\*|/|\*\*|\>\>|\<\</)
	  # Operators with two arguments
	  if (( ${#stack} < 2 )); then
	    print -r -- "${_line}: not enough values on stack" >&2
	    _line=
	    continue
	  fi
	  eval "(( ans = \${stack[2]} $_line \${stack[1]} ))"
	  shift 2 stack
	  ;;

	  (ldexp|jn|yn|scalb|xy|\<\>)
	  # Functions with two arguments
	  if (( ${#stack} < 2 )); then
	    print -r -- "${_line}: not enough values on stack" >&2
	    _line=
	    continue
	  fi
	  if [[ $_line = (xy|\<\>) ]]; then
	    _tmp=${stack[1]}
	    stack[1]=${stack[2]}
	    stack[2]=$_tmp
	    _push=0
	  else
	    eval "(( ans = ${_line}(\${stack[2]},\${stack[1]}) ))"
	    shift 2 stack
	  fi
	  ;;

	  (${~_mathfuncs})
	  # Functions with a single argument.
	  # This is actually a superset, but we should have matched
	  # any that shouldn't be in it in previous cases.
	  if (( ${#stack} < 1 )); then
	    print -r -- "${_line}: not enough values on stack" >&2
	    _line=
	    continue
	  fi
	  eval "(( ans = ${_line}(\${stack[1]}) ))"
	  shift stack
	  ;;

	  (${(kj.|.)~_userfuncs})
	  # Get minimum number of arguments to user function
	  _n=${_userfuncs[$_line]}
	  if (( ${#stack} < n_ )); then
	    print -r -- "${_line}: not enough values ($_n) on stack" >&2
	    _line=
	    continue
	  fi
	  _line+="("
	  # least recent elements on stack are earlier arguments
	  for (( _i = _n; _i > 0; _i-- )); do
	    _line+=${stack[_i]}
	    (( _i > 1 )) && _line+=","
	  done
	  _line+=")"
	  shift $_n stack
	  eval "(( ans = $_line ))"
	  ;;

	  (*)
	  # Treat as expression evaluating to new value to go on stack.
	  _matched=0
	  ;;
	esac
      else
	_matched=0
      fi
      if (( ! _matched )); then
	# Latest value is stored` as a string, because it might be floating
	# point or integer --- we don't know till after the evaluation, and
	# arrays always store scalars anyway.
	#
	# Since it's a string, we'd better make sure we know which
	# base it's in, so don't change that until we actually print it.
	if ! eval "ans=\$(( $_line ))"; then
	  _line=
	  continue
	fi
	# on error $ans is not set; let user re-edit _line
	[[ -n $ans ]] || continue
      fi
      argv[_num++]=$ans
      psvar[1]=$_num
      (( _push )) && stack=($ans $stack)
    ;;
  esac
  if (( _show_stack )); then
    (( _max_stack = (_show_stack > ${#stack}) ? ${#stack} : _show_stack ))
    for (( _i = _max_stack; _i > 0; _i-- )); do
      printf "%3d: " $_i
      zcalc_show_value ${stack[_i]}
    done
  else
    zcalc_show_value $ans
  fi
  _line=
done

return 0