zsh-users
 help / color / mirror / code / Atom feed
* Determining whether a function is used in an arithmetic context
@ 2017-07-25  3:47 dana
  2017-07-25  7:03 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: dana @ 2017-07-25  3:47 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1963 bytes --]

Hey there,

I was playing with arithmetic functions recently and i wanted to have one
of them behave differently depending on whether it's used in an arithmetic
context or not. I can't seem to figure out a *reliable* way to detect that,
though.

The %_ prompt expansion seemed like it might be the way to go — it produces
math when used in an arithmetic command... but not an arithmetic
*substitution*, strangely. (Is that expected?)

The best luck i had was giving the function a different name when used in
arithmetic context, and then checking to see if %N matches $0. However,
this only works when function_argzero is enabled, and unfortunately i think
i need that turned off, since ZSH_ARGZERO is not available in zsh <5.3.

See below for the result of my experiments. Have i missed something?

Cheers
dana

% zsh --version
zsh 5.3.1 (x86_64-apple-darwin16.6.0)

% cat /tmp/foo
myfunc() {
  (( $1 ))
  printf '  $zec %s\n'  ${zsh_eval_context:-\?}
  printf '  $ft  %s\n'  ${functrace:-\?}
  printf '  $fs  %s\n'  ${funcstack:-\?}
  printf '  $0   %s\n'  ${0:-\?}
  printf '  $_   %s\n'  ${_:-\?}
  printf '  $ZAZ %s\n'  ${ZSH_ARGZERO:-\?}
  printf '  %%N   %s\n' ${${(%):-%N}:-\?}
  printf '  %%_   %s\n' ${${(%):-%_}:-\?}
}

functions -M func 1 1 myfunc

print 'Normal context:'
myfunc 1

print 'Arithmetic substitution context:'
: $(( func(1) ))

print 'Arithmetic command context:'
(( func(1) ))

% zsh /tmp/foo
Normal context:
  $zec toplevel
  $zec shfunc
  $ft  /tmp/foo:16
  $fs  myfunc
  $0   myfunc
  $_   myfunc
  $ZAZ /tmp/foo
  %N   myfunc
  %_   ?
Arithmetic substitution context:
  $zec toplevel
  $zec shfunc
  $ft  /tmp/foo:19
  $fs  myfunc
  $0   func
  $_   func
  $ZAZ /tmp/foo
  %N   myfunc
  %_   ?
Arithmetic command context:
  $zec toplevel
  $zec shfunc
  $ft  /tmp/foo:22
  $fs  myfunc
  $0   func
  $_   func
  $ZAZ /tmp/foo
  %N   myfunc
  %_   math

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Determining whether a function is used in an arithmetic context
  2017-07-25  3:47 Determining whether a function is used in an arithmetic context dana
@ 2017-07-25  7:03 ` Bart Schaefer
  2017-07-25 21:20   ` dana
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2017-07-25  7:03 UTC (permalink / raw)
  To: Zsh Users; +Cc: dana

On Mon, Jul 24, 2017 at 8:47 PM, dana <dana@dana.is> wrote:
>
> I was playing with arithmetic functions recently and i wanted to have one
> of them behave differently depending on whether it's used in an arithmetic
> context or not. I can't seem to figure out a *reliable* way to detect that

I guess I'd do it like this:

myfunc() {
  if [[ ${funcstack[2]} == myfunc_math ]]
  then print In math context
  else print Not in math context
  fi
}
myfunc_math() {
  myfunc "$@"
}

functions -M myfunc 1 1 myfunc_math

> The %_ prompt expansion seemed like it might be the way to go — it produces
> math when used in an arithmetic command... but not an arithmetic
> *substitution*, strangely. (Is that expected?)

Yes, it's expected, because it's the parser state -- by the time the function is
actually executing, the parser is done.  %_ is only meaningful during program
input (typically interactively) or in an execution trace, not during
execution itself.
I think it's accidental that it has a value during arithmetic commands.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Determining whether a function is used in an arithmetic context
  2017-07-25  7:03 ` Bart Schaefer
@ 2017-07-25 21:20   ` dana
  0 siblings, 0 replies; 3+ messages in thread
From: dana @ 2017-07-25 21:20 UTC (permalink / raw)
  To: Zsh Users; +Cc: Bart Schaefer

[-- Attachment #1: Type: text/plain, Size: 1243 bytes --]

I suppose that'll do. Thanks!


On 25 July 2017 at 02:03, Bart Schaefer <schaefer@brasslantern.com> wrote:

> On Mon, Jul 24, 2017 at 8:47 PM, dana <dana@dana.is> wrote:
> >
> > I was playing with arithmetic functions recently and i wanted to have one
> > of them behave differently depending on whether it's used in an
> arithmetic
> > context or not. I can't seem to figure out a *reliable* way to detect
> that
>
> I guess I'd do it like this:
>
> myfunc() {
>   if [[ ${funcstack[2]} == myfunc_math ]]
>   then print In math context
>   else print Not in math context
>   fi
> }
> myfunc_math() {
>   myfunc "$@"
> }
>
> functions -M myfunc 1 1 myfunc_math
>
> > The %_ prompt expansion seemed like it might be the way to go — it
> produces
> > math when used in an arithmetic command... but not an arithmetic
> > *substitution*, strangely. (Is that expected?)
>
> Yes, it's expected, because it's the parser state -- by the time the
> function is
> actually executing, the parser is done.  %_ is only meaningful during
> program
> input (typically interactively) or in an execution trace, not during
> execution itself.
> I think it's accidental that it has a value during arithmetic commands.
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-07-25 21:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-25  3:47 Determining whether a function is used in an arithmetic context dana
2017-07-25  7:03 ` Bart Schaefer
2017-07-25 21:20   ` dana

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).