math support in busybox shells

Mike Frysinger vapier at gentoo.org
Thu Apr 2 08:39:19 UTC 2009


On Wednesday 01 April 2009 23:36:25 Denys Vlasenko wrote:
> On Wednesday 01 April 2009 17:10, Mike Frysinger wrote:
> > the ash code, while frightening, doesnt pollute its math code.  whoever
> > did the merge kept it nice and clean :).
> >
> > so this patch moves the arith() func (and few related stuff) out of ash.c
> > and into math.c.  that's the bulk of it, so you can ignore the large
> > chunk against ash.c/math.c.
> >
> > other things:
> >  - rename ASH_MATH to SH_MATH in Config.in and such
> >  - add arith_set_hooks() func to top of math.c
> >  - simplify the 64bit #if mess in math.h
> >  - constify the lookupvar() func in ash
> >  - add a set wrapper to hush since the math code (1) doesnt alloc the
> > variable storage and (2) uses two strings rather than one with "var=val"
> >  - implement $((...)) detection/parsing in hush
>
> +static arith_var_lookup_t lookupvar;
> +static arith_var_set_t setvar;
> +static arith_var_endofname_t endofname;
>
> Apart from these statics, it looks ok.
>
> Can you create
>
> struct arith_eval_state {
> 	arith_var_lookup_t lookupvar;
> 	arith_var_set_t setvar;
> 	arith_var_endofname_t endofname;
> };
>
> and pass a pointer to it into arith() calls?
>
> Line editing does something like that.

np

only real issue ive seen so far is hush specific.  atm, the parser consumes 
all of $((...)) without doing any expansion on the resulting string.  so if i 
do something like:
i=0
echo $(($i + 1))
the arith routine expects "0 + 1" but gets "$i + 1" and so dies -- it is the 
job of the shell to do dollar expansion first.  the non-dollar version works 
fine of course:
echo $((i + 1))

the heredoc code i posted earlier suffers from a similar fate

any tips ?  i think the parser is just not complex enough atm to maintain 
recursive state ... or i'm not doing it right.

after all the parser will need to be recursive to handle:
i=3
echo $(( i + 1 + $(( 2 << $i )) * $(( 3 + $(echo 2) )) ))
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20090402/7aa828b4/attachment.pgp>


More information about the busybox mailing list